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