Differences Between: [Versions 402 and 403]
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_badges\reportbuilder\datasource; 20 21 use core_badges_generator; 22 use core_reportbuilder_generator; 23 use core_reportbuilder_testcase; 24 use core_reportbuilder\local\filters\{boolean_select, date, select, text}; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php"); 30 require_once("{$CFG->libdir}/badgeslib.php"); 31 32 /** 33 * Unit tests for user badges datasource 34 * 35 * @package core_badges 36 * @covers \core_badges\reportbuilder\datasource\users 37 * @copyright 2023 Paul Holden <paulh@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class users_test extends core_reportbuilder_testcase { 41 42 /** 43 * Test default datasource 44 */ 45 public function test_datasource_default(): void { 46 $this->resetAfterTest(); 47 48 $user = $this->getDataGenerator()->create_user(['firstname' => 'Zoe', 'lastname' => 'Zebra']); 49 50 /** @var core_badges_generator $generator */ 51 $generator = $this->getDataGenerator()->get_plugin_generator('core_badges'); 52 ($badgeone = $generator->create_badge(['name' => 'Badge 1', 'description' => 'My first badge'])) 53 ->issue($user->id, true); 54 ($badgetwo = $generator->create_badge(['name' => 'Badge 2', 'description' => 'My second badge'])) 55 ->issue($user->id, true); 56 57 /** @var core_reportbuilder_generator $generator */ 58 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 59 $report = $generator->create_report(['name' => 'Badges', 'source' => users::class, 'default' => 1]); 60 61 $content = $this->get_custom_report_content($report->get('id')); 62 $this->assertCount(3, $content); 63 64 // Default columns are user, badge, description, issued time. Sorted by user, badge, issued time. 65 [$userfullname, $badgename, $badgedescription, $badgeissued] = array_values($content[0]); 66 $this->assertEquals('Admin User', $userfullname); 67 $this->assertEmpty($badgename); 68 $this->assertEmpty($badgedescription); 69 $this->assertEmpty($badgeissued); 70 71 [$userfullname, $badgename, $badgedescription, $badgeissued] = array_values($content[1]); 72 $this->assertEquals(fullname($user), $userfullname); 73 $this->assertEquals($badgeone->name, $badgename); 74 $this->assertEquals($badgeone->description, $badgedescription); 75 $this->assertNotEmpty($badgeissued); 76 77 [$userfullname, $badgename, $badgedescription, $badgeissued] = array_values($content[2]); 78 $this->assertEquals(fullname($user), $userfullname); 79 $this->assertEquals($badgetwo->name, $badgename); 80 $this->assertEquals($badgetwo->description, $badgedescription); 81 $this->assertNotEmpty($badgeissued); 82 } 83 84 /** 85 * Test datasource columns that aren't added by default 86 */ 87 public function test_datasource_non_default_columns(): void { 88 global $DB; 89 90 $this->resetAfterTest(); 91 92 $course = $this->getDataGenerator()->create_course(); 93 $user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']); 94 95 /** @var core_badges_generator $generator */ 96 $generator = $this->getDataGenerator()->get_plugin_generator('core_badges'); 97 ($badgesite = $generator->create_badge(['name' => 'Badge 1', 'language' => 'de', 'expireperiod' => HOURSECS])) 98 ->issue($user->id, true); 99 ($badgecourse = $generator->create_badge(['name' => 'Badge 2', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id])) 100 ->issue($user->id, true); 101 102 // Create criteria for manually awarding by role. 103 $managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']); 104 $generator->create_criteria(['badgeid' => $badgesite->id, 'roleid' => $managerrole]); 105 106 /** @var core_reportbuilder_generator $generator */ 107 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 108 $report = $generator->create_report(['name' => 'Badges', 'source' => users::class, 'default' => 0]); 109 110 // These two columns have been asserted previously, we're only adding them for consistent sorting. 111 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]); 112 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]); 113 114 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:criteria']); 115 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:image']); 116 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:language']); 117 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:version']); 118 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:status']); 119 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:expiry']); 120 121 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:expire']); 122 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:visible']); 123 124 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']); 125 126 $content = $this->get_custom_report_content($report->get('id')); 127 $this->assertCount(3, $content); 128 129 // Admin user, no badge issued. 130 [, , $criteria, $image, $language, $version, $status, $expiry, $expires, $visible, $coursename] = array_values($content[0]); 131 $this->assertEmpty($criteria); 132 $this->assertEmpty($image); 133 $this->assertEmpty($language); 134 $this->assertEmpty($version); 135 $this->assertEmpty($status); 136 $this->assertEmpty($expiry); 137 $this->assertEmpty($expires); 138 $this->assertEmpty($visible); 139 $this->assertEmpty($coursename); 140 141 // Site badge issued. 142 [, , $criteria, $image, $language, $version, $status, $expiry, $expires, $visible, $coursename] = array_values($content[1]); 143 $this->assertStringContainsString('Awarded by: Manager', $criteria); 144 $this->assertStringContainsString('Image caption', $image); 145 $this->assertEquals('German', $language); 146 $this->assertEquals(2, $version); 147 $this->assertEquals('Available (criteria locked)', $status); 148 $this->assertEquals('1 hour', $expiry); 149 $this->assertNotEmpty($expires); 150 $this->assertEquals('Yes', $visible); 151 $this->assertEquals('PHPUnit test site', $coursename); 152 153 // Course badge issued. 154 [, , $criteria, $image, $language, $version, $status, $expiry, $expires, $visible, $coursename] = array_values($content[2]); 155 $this->assertEquals('Criteria for this badge have not been set up yet.', $criteria); 156 $this->assertStringContainsString('Image caption', $image); 157 $this->assertEquals('English', $language); 158 $this->assertEquals(2, $version); 159 $this->assertEquals('Available (criteria locked)', $status); 160 $this->assertEquals('Never', $expiry); 161 $this->assertEmpty($expires); 162 $this->assertEquals('Yes', $visible); 163 $this->assertEquals($course->fullname, $coursename); 164 } 165 166 /** 167 * Data provider for {@see test_datasource_filters} 168 * 169 * @return array[] 170 */ 171 public function datasource_filters_provider(): array { 172 return [ 173 // Badge. 174 'Filter badge name' => ['badge:name', [ 175 'badge:name_operator' => text::IS_EQUAL_TO, 176 'badge:name_value' => 'Course badge', 177 ], true], 178 'Filter badge name (no match)' => ['badge:name', [ 179 'badge:name_operator' => text::IS_EQUAL_TO, 180 'badge:name_value' => 'Other badge', 181 ], false], 182 'Filter badge status' => ['badge:status', [ 183 'badge:status_operator' => select::EQUAL_TO, 184 'badge:status_value' => BADGE_STATUS_ACTIVE_LOCKED, 185 ], true], 186 'Filter badge status (no match)' => ['badge:status', [ 187 'badge:status_operator' => select::EQUAL_TO, 188 'badge:status_value' => BADGE_STATUS_ACTIVE, 189 ], false], 190 'Filter badge type' => ['badge:type', [ 191 'badge:type_operator' => select::EQUAL_TO, 192 'badge:type_value' => BADGE_TYPE_COURSE, 193 ], true], 194 'Filter badge type (no match)' => ['badge:type', [ 195 'badge:type_operator' => select::EQUAL_TO, 196 'badge:type_value' => BADGE_TYPE_SITE, 197 ], false], 198 199 // Badge issued. 200 'Filter badge issued date' => ['badge_issued:issued', [ 201 'badge_issued:issued_operator' => date::DATE_RANGE, 202 'badge_issued:issued_from' => 1622502000, 203 ], true], 204 'Filter badge issued date (no match)' => ['badge_issued:issued', [ 205 'badge_issued:issued_operator' => date::DATE_RANGE, 206 'badge_issued:issued_to' => 1622502000, 207 ], false], 208 'Filter badge issued expires' => ['badge_issued:expires', [ 209 'badge_issued:expires_operator' => date::DATE_RANGE, 210 'badge_issued:expires_from' => 1622502000, 211 ], true], 212 'Filter badge issued expires (no match)' => ['badge_issued:expires', [ 213 'badge_issued:expires_operator' => date::DATE_RANGE, 214 'badge_issued:expires_to' => 1622502000, 215 ], false], 216 'Filter badge issued visible' => ['badge_issued:visible', [ 217 'badge_issued:visible_operator' => boolean_select::CHECKED, 218 ], true], 219 'Filter badge issued visible (no match)' => ['badge_issued:visible', [ 220 'badge_issued:visible_operator' => boolean_select::NOT_CHECKED, 221 ], false], 222 223 // Course. 224 'Filter course fullname' => ['course:fullname', [ 225 'course:fullname_operator' => text::IS_EQUAL_TO, 226 'course:fullname_value' => 'Course 1', 227 ], true], 228 'Filter course fullname (no match)' => ['course:fullname', [ 229 'course:fullname_operator' => text::IS_EQUAL_TO, 230 'course:fullname_value' => 'Course 2', 231 ], false], 232 ]; 233 } 234 235 /** 236 * Test datasource filters 237 * 238 * @param string $filtername 239 * @param array $filtervalues 240 * @param bool $expectmatch 241 * 242 * @dataProvider datasource_filters_provider 243 */ 244 public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void { 245 $this->resetAfterTest(); 246 247 $course = $this->getDataGenerator()->create_course(['fullname' => 'Course 1']); 248 $user = $this->getDataGenerator()->create_and_enrol($course); 249 250 /** @var core_badges_generator $generator */ 251 $generator = $this->getDataGenerator()->get_plugin_generator('core_badges'); 252 $generator->create_badge([ 253 'name' => 'Course badge', 254 'type' => BADGE_TYPE_COURSE, 255 'courseid' => $course->id, 256 'expireperiod' => HOURSECS, 257 ])->issue($user->id, true); 258 259 /** @var core_reportbuilder_generator $generator */ 260 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 261 262 // Create report containing single username column, and given filter. 263 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]); 264 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']); 265 266 // Add filter, set it's values. 267 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]); 268 $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues); 269 270 if ($expectmatch) { 271 $this->assertCount(1, $content); 272 $this->assertEquals($user->username, reset($content[0])); 273 } else { 274 $this->assertEmpty($content); 275 } 276 } 277 278 /** 279 * Stress test datasource 280 * 281 * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php 282 */ 283 public function test_stress_datasource(): void { 284 if (!PHPUNIT_LONGTEST) { 285 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined'); 286 } 287 288 $this->resetAfterTest(); 289 290 $course = $this->getDataGenerator()->create_course(); 291 $user = $this->getDataGenerator()->create_and_enrol($course); 292 293 /** @var core_badges_generator $generator */ 294 $generator = $this->getDataGenerator()->get_plugin_generator('core_badges'); 295 $generator->create_badge([ 296 'name' => 'Course badge', 297 'type' => BADGE_TYPE_COURSE, 298 'courseid' => $course->id, 299 ])->issue($user->id, true); 300 301 $this->datasource_stress_test_columns(users::class); 302 $this->datasource_stress_test_columns_aggregation(users::class); 303 $this->datasource_stress_test_conditions(users::class, 'user:username'); 304 } 305 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body