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, tags, 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 98 $badgesite = $generator->create_badge([ 99 'name' => 'Badge 1', 100 'language' => 'de', 101 'expireperiod' => HOURSECS, 102 'tags' => ['cool'], 103 ]); 104 $badgecourse = $generator->create_badge([ 105 'name' => 'Badge 2', 106 'type' => BADGE_TYPE_COURSE, 107 'courseid' => $course->id, 108 ]); 109 110 $badgesite->issue($user->id, true); 111 $badgecourse->issue($user->id, true); 112 113 // Create criteria for manually awarding by role. 114 $managerrole = $DB->get_field('role', 'id', ['shortname' => 'manager']); 115 $generator->create_criteria(['badgeid' => $badgesite->id, 'roleid' => $managerrole]); 116 117 /** @var core_reportbuilder_generator $generator */ 118 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 119 $report = $generator->create_report(['name' => 'Badges', 'source' => users::class, 'default' => 0]); 120 121 // These two columns have been asserted previously, we're only adding them for consistent sorting. 122 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname', 'sortenabled' => 1]); 123 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]); 124 125 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:namewithlink']); 126 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:criteria']); 127 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:image']); 128 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:language']); 129 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:version']); 130 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:status']); 131 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:expiry']); 132 133 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'tag:name']); 134 135 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:expire']); 136 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge_issued:visible']); 137 138 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'course:fullname']); 139 140 $content = $this->get_custom_report_content($report->get('id')); 141 $this->assertCount(3, $content); 142 143 // Admin user, no badge issued. 144 [$fullname, $badgename, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires, $visible, $coursename] 145 = array_values($content[0]); 146 $this->assertEquals('Admin User', $fullname); 147 $this->assertEmpty($badgename); 148 $this->assertEmpty($criteria); 149 $this->assertEmpty($image); 150 $this->assertEmpty($language); 151 $this->assertEmpty($version); 152 $this->assertEmpty($status); 153 $this->assertEmpty($expiry); 154 $this->assertEmpty($tag); 155 $this->assertEmpty($expires); 156 $this->assertEmpty($visible); 157 $this->assertEmpty($coursename); 158 159 $expectedbadgesitelink = \html_writer::link(new \moodle_url('/badges/overview.php', 160 ['id' => $badgesite->id]), ($badgesite->name)); 161 162 // User issued site badge. 163 [$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires, 164 $visible, $coursename] = array_values($content[1]); 165 $this->assertEquals(fullname($user), $fullname); 166 $this->assertEquals($badgesite->name, $badgename); 167 $this->assertEquals($expectedbadgesitelink, $namewithlink); 168 $this->assertStringContainsString('Awarded by: Manager', $criteria); 169 $this->assertStringContainsString('Image caption', $image); 170 $this->assertEquals('German', $language); 171 $this->assertEquals(2, $version); 172 $this->assertEquals('Available (criteria locked)', $status); 173 $this->assertEquals('1 hour', $expiry); 174 $this->assertEquals('cool', $tag); 175 $this->assertNotEmpty($expires); 176 $this->assertEquals('Yes', $visible); 177 $this->assertEquals('PHPUnit test site', $coursename); 178 179 $expectedbadgecourselink = \html_writer::link(new \moodle_url('/badges/overview.php', 180 ['id' => $badgecourse->id]), ($badgecourse->name)); 181 182 // User issued course badge. 183 [$fullname, $badgename, $namewithlink, $criteria, $image, $language, $version, $status, $expiry, $tag, $expires, 184 $visible, $coursename] = array_values($content[2]); 185 $this->assertEquals(fullname($user), $fullname); 186 $this->assertEquals($badgecourse->name, $badgename); 187 $this->assertEquals($expectedbadgecourselink, $namewithlink); 188 $this->assertEquals('Criteria for this badge have not been set up yet.', $criteria); 189 $this->assertStringContainsString('Image caption', $image); 190 $this->assertEquals('English', $language); 191 $this->assertEquals(2, $version); 192 $this->assertEquals('Available (criteria locked)', $status); 193 $this->assertEquals('Never', $expiry); 194 $this->assertEmpty($tag); 195 $this->assertEmpty($expires); 196 $this->assertEquals('Yes', $visible); 197 $this->assertEquals($course->fullname, $coursename); 198 } 199 200 /** 201 * Data provider for {@see test_datasource_filters} 202 * 203 * @return array[] 204 */ 205 public function datasource_filters_provider(): array { 206 return [ 207 // User. 208 'Filter user fullname' => ['user:fullname', [ 209 'user:fullname_operator' => text::IS_EQUAL_TO, 210 'user:fullname_value' => 'Zoe Zebra', 211 ], true], 212 'Filter user fullname (no match)' => ['user:fullname', [ 213 'user:fullname_operator' => text::IS_EQUAL_TO, 214 'user:fullname_value' => 'Alice Aardvark', 215 ], false], 216 217 // Badge. 218 'Filter badge name' => ['badge:name', [ 219 'badge:name_operator' => text::IS_EQUAL_TO, 220 'badge:name_value' => 'Course badge', 221 ], true], 222 'Filter badge name (no match)' => ['badge:name', [ 223 'badge:name_operator' => text::IS_EQUAL_TO, 224 'badge:name_value' => 'Other badge', 225 ], false], 226 'Filter badge status' => ['badge:status', [ 227 'badge:status_operator' => select::EQUAL_TO, 228 'badge:status_value' => BADGE_STATUS_ACTIVE_LOCKED, 229 ], true], 230 'Filter badge status (no match)' => ['badge:status', [ 231 'badge:status_operator' => select::EQUAL_TO, 232 'badge:status_value' => BADGE_STATUS_ACTIVE, 233 ], false], 234 'Filter badge type' => ['badge:type', [ 235 'badge:type_operator' => select::EQUAL_TO, 236 'badge:type_value' => BADGE_TYPE_COURSE, 237 ], true], 238 'Filter badge type (no match)' => ['badge:type', [ 239 'badge:type_operator' => select::EQUAL_TO, 240 'badge:type_value' => BADGE_TYPE_SITE, 241 ], false], 242 243 // Badge tag. 244 'Filter tag name' => ['tag:name', [ 245 'tag:name_operator' => tags::NOT_EMPTY, 246 ], true], 247 'Filter tag name (no match)' => ['tag:name', [ 248 'tag:name_operator' => tags::EQUAL_TO, 249 'tag:name_value' => [-1], 250 ], false], 251 252 // Badge issued. 253 'Filter badge issued date' => ['badge_issued:issued', [ 254 'badge_issued:issued_operator' => date::DATE_RANGE, 255 'badge_issued:issued_from' => 1622502000, 256 ], true], 257 'Filter badge issued date (no match)' => ['badge_issued:issued', [ 258 'badge_issued:issued_operator' => date::DATE_RANGE, 259 'badge_issued:issued_to' => 1622502000, 260 ], false], 261 'Filter badge issued expires' => ['badge_issued:expires', [ 262 'badge_issued:expires_operator' => date::DATE_RANGE, 263 'badge_issued:expires_from' => 1622502000, 264 ], true], 265 'Filter badge issued expires (no match)' => ['badge_issued:expires', [ 266 'badge_issued:expires_operator' => date::DATE_RANGE, 267 'badge_issued:expires_to' => 1622502000, 268 ], false], 269 'Filter badge issued visible' => ['badge_issued:visible', [ 270 'badge_issued:visible_operator' => boolean_select::CHECKED, 271 ], true], 272 'Filter badge issued visible (no match)' => ['badge_issued:visible', [ 273 'badge_issued:visible_operator' => boolean_select::NOT_CHECKED, 274 ], false], 275 276 // Course. 277 'Filter course fullname' => ['course:fullname', [ 278 'course:fullname_operator' => text::IS_EQUAL_TO, 279 'course:fullname_value' => 'Course 1', 280 ], true], 281 'Filter course fullname (no match)' => ['course:fullname', [ 282 'course:fullname_operator' => text::IS_EQUAL_TO, 283 'course:fullname_value' => 'Course 2', 284 ], false], 285 ]; 286 } 287 288 /** 289 * Test datasource filters 290 * 291 * @param string $filtername 292 * @param array $filtervalues 293 * @param bool $expectmatch 294 * 295 * @dataProvider datasource_filters_provider 296 */ 297 public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void { 298 $this->resetAfterTest(); 299 300 $course = $this->getDataGenerator()->create_course(['fullname' => 'Course 1']); 301 $user = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe', 'lastname' => 'Zebra']); 302 303 /** @var core_badges_generator $generator */ 304 $generator = $this->getDataGenerator()->get_plugin_generator('core_badges'); 305 $badge = $generator->create_badge([ 306 'name' => 'Course badge', 307 'type' => BADGE_TYPE_COURSE, 308 'courseid' => $course->id, 309 'expireperiod' => HOURSECS, 310 'tags' => ['cool'], 311 ]); 312 $badge->issue($user->id, true); 313 314 /** @var core_reportbuilder_generator $generator */ 315 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder'); 316 317 // Create report containing single username column, and given filter. 318 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]); 319 $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username']); 320 321 // Add filter, set it's values. 322 $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]); 323 $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues); 324 325 if ($expectmatch) { 326 $this->assertCount(1, $content); 327 $this->assertEquals($user->username, reset($content[0])); 328 } else { 329 $this->assertEmpty($content); 330 } 331 } 332 333 /** 334 * Stress test datasource 335 * 336 * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php 337 */ 338 public function test_stress_datasource(): void { 339 if (!PHPUNIT_LONGTEST) { 340 $this->markTestSkipped('PHPUNIT_LONGTEST is not defined'); 341 } 342 343 $this->resetAfterTest(); 344 345 $course = $this->getDataGenerator()->create_course(); 346 $user = $this->getDataGenerator()->create_and_enrol($course); 347 348 /** @var core_badges_generator $generator */ 349 $generator = $this->getDataGenerator()->get_plugin_generator('core_badges'); 350 $badge = $generator->create_badge(['name' => 'Course badge', 'type' => BADGE_TYPE_COURSE, 'courseid' => $course->id]); 351 $badge->issue($user->id, true); 352 353 $this->datasource_stress_test_columns(users::class); 354 $this->datasource_stress_test_columns_aggregation(users::class); 355 $this->datasource_stress_test_conditions(users::class, 'user:username'); 356 } 357 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body