See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 declare(strict_types=1); 18 19 namespace core_reportbuilder; 20 21 use context_system; 22 use core_reportbuilder_generator; 23 use core_reportbuilder_testcase; 24 use core_user\reportbuilder\datasource\users; 25 use stdClass; 26 use core_reportbuilder\local\models\report; 27 use core_reportbuilder\local\report\base; 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 global $CFG; 32 require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php"); 33 34 /** 35 * Unit tests for the report manager class 36 * 37 * @package core_reportbuilder 38 * @covers \core_reportbuilder\manager 39 * @copyright 2020 Paul Holden <paulh@moodle.com> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class manager_test extends core_reportbuilder_testcase { 43 44 /** 45 * Test creating a report instance from persistent 46 */ 47 public function test_get_report_from_persistent(): void { 48 global $CFG; 49 require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php"); 50 51 $this->resetAfterTest(); 52 53 $report = manager::create_report_persistent((object) [ 54 'type' => base::TYPE_SYSTEM_REPORT, 55 'source' => system_report_available::class, 56 ]); 57 58 $systemreport = manager::get_report_from_persistent($report); 59 $this->assertInstanceOf(system_report::class, $systemreport); 60 } 61 62 /** 63 * Test creating a report instance from persistent differs per-user, using a report source whose own initialization is 64 * dependent on the current user (the users report source, loading available user profile fields) 65 * 66 * Note: internally the {@see get_custom_report_content} test helper calls {@see manager::get_report_from_persistent} 67 */ 68 public function test_get_report_from_persistent_per_user(): void { 69 $this->resetAfterTest(); 70 $this->setAdminUser(); 71 72 // Custom profile field, visible only to the admin. 73 $this->getDataGenerator()->create_custom_profile_field([ 74 'shortname' => 'text', 'name' => 'Text field', 'datatype' => 'text', 'visible' => 0]); 75 $user = $this->getDataGenerator()->create_user(['username' => 'usertwo', 'profile_field_text' => 'Hello']); 76 77 /** @var core_reportbuilder_generator $generator */ 78 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 79 80 $report = $generator->create_report(['name' => 'Hidden profile field', 'source' => users::class, 'default' => 0]); 81 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username', 'sortenabled' => 1]); 82 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_text']); 83 84 $content = $this->get_custom_report_content($report->get('id')); 85 $this->assertEquals([ 86 ['admin', ''], 87 ['usertwo', 'Hello'], 88 ], array_map('array_values', $content)); 89 90 // Now switch to second, non-admin, user. 91 $this->setUser($user); 92 93 $content = $this->get_custom_report_content($report->get('id')); 94 $this->assertEquals([ 95 ['admin'], 96 ['usertwo'], 97 ], array_map('array_values', $content)); 98 } 99 100 /** 101 * Test creating a report instance from persistent with an invalid source 102 */ 103 public function test_get_report_from_persistent_invalid(): void { 104 $this->resetAfterTest(); 105 106 $report = manager::create_report_persistent((object) [ 107 'type' => base::TYPE_SYSTEM_REPORT, 108 'source' => stdClass::class, 109 ]); 110 111 $this->expectException(source_invalid_exception::class); 112 manager::get_report_from_persistent($report); 113 } 114 115 /** 116 * Test creating a report instance from persistent with an unavailable source 117 */ 118 public function test_get_report_from_persistent_unavailable(): void { 119 global $CFG; 120 require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_unavailable.php"); 121 122 $this->resetAfterTest(); 123 124 $report = manager::create_report_persistent((object) [ 125 'type' => base::TYPE_SYSTEM_REPORT, 126 'source' => system_report_unavailable::class, 127 ]); 128 129 $this->expectException(source_unavailable_exception::class); 130 manager::get_report_from_persistent($report); 131 } 132 133 /** 134 * Test report source exists 135 */ 136 public function test_report_source_exists(): void { 137 global $CFG; 138 139 require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php"); 140 $this->assertTrue(manager::report_source_exists(system_report_available::class)); 141 142 $this->assertFalse(manager::report_source_exists(stdClass::class)); 143 } 144 145 /** 146 * Test report source available 147 */ 148 public function test_report_source_available(): void { 149 global $CFG; 150 151 require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php"); 152 $this->assertTrue(manager::report_source_available(system_report_available::class)); 153 154 require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_unavailable.php"); 155 $this->assertFalse(manager::report_source_available(system_report_unavailable::class)); 156 } 157 158 /** 159 * Test creating a report persistent model 160 */ 161 public function test_create_report_persistent(): void { 162 global $CFG; 163 require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php"); 164 165 $this->resetAfterTest(); 166 167 $report = manager::create_report_persistent((object) [ 168 'type' => base::TYPE_SYSTEM_REPORT, 169 'source' => \core_reportbuilder\system_report_available::class, 170 ]); 171 172 $this->assertInstanceOf(report::class, $report); 173 $this->assertEquals(base::TYPE_SYSTEM_REPORT, $report->get('type')); 174 $this->assertEquals(system_report_available::class, $report->get('source')); 175 $this->assertInstanceOf(context_system::class, $report->get_context()); 176 } 177 178 /** 179 * Data provider for {@see test_report_limit_reached} 180 * 181 * @return array 182 */ 183 public function report_limit_reached_provider(): array { 184 return [ 185 [0, 1, false], 186 [1, 1, true], 187 [2, 1, false], 188 [1, 2, true], 189 ]; 190 } 191 192 /** 193 * Test test_report_limit_reached method to check site custom reports limit 194 * 195 * @param int $customreportslimit 196 * @param int $existingreports 197 * @param bool $expected 198 * @dataProvider report_limit_reached_provider 199 */ 200 public function test_report_limit_reached(int $customreportslimit, int $existingreports, bool $expected): void { 201 global $CFG; 202 203 $this->resetAfterTest(); 204 205 /** @var core_reportbuilder_generator $generator */ 206 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 207 for ($i = 1; $i <= $existingreports; $i++) { 208 $generator->create_report(['name' => 'Limited report '.$i, 'source' => users::class]); 209 } 210 211 // Set current custom report limit, and check whether the limit has been reached. 212 $CFG->customreportslimit = $customreportslimit; 213 $this->assertEquals($expected, manager::report_limit_reached()); 214 } 215 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body