Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 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 namespace core_cohort; 18 19 use core_cohort\customfield\cohort_handler; 20 use core_customfield\data_controller; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 require_once("$CFG->dirroot/cohort/lib.php"); 26 27 28 /** 29 * Cohort library tests. 30 * 31 * @package core_cohort 32 * @category phpunit 33 * @copyright 2012 Petr Skoda {@link http://skodak.org} 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class lib_test extends \advanced_testcase { 37 38 /** 39 * Create Cohort custom field for testing. 40 * 41 * @return \core_customfield\field_controller 42 */ 43 protected function create_cohort_custom_field(): \core_customfield\field_controller { 44 $fieldcategory = self::getDataGenerator()->create_custom_field_category([ 45 'component' => 'core_cohort', 46 'area' => 'cohort', 47 'name' => 'Other fields', 48 ]); 49 50 return self::getDataGenerator()->create_custom_field([ 51 'shortname' => 'testfield1', 52 'name' => 'Custom field', 53 'type' => 'text', 54 'categoryid' => $fieldcategory->get('id'), 55 ]); 56 } 57 58 public function test_cohort_add_cohort() { 59 global $DB; 60 61 $this->resetAfterTest(); 62 $this->setAdminUser(); 63 64 $this->create_cohort_custom_field(); 65 66 $cohort = new \stdClass(); 67 $cohort->contextid = \context_system::instance()->id; 68 $cohort->name = 'test cohort'; 69 $cohort->idnumber = 'testid'; 70 $cohort->description = 'test cohort desc'; 71 $cohort->descriptionformat = FORMAT_HTML; 72 $cohort->customfield_testfield1 = 'Test value 1'; 73 74 $id = cohort_add_cohort($cohort); 75 $this->assertNotEmpty($id); 76 77 $newcohort = $DB->get_record('cohort', array('id'=>$id)); 78 $this->assertEquals($cohort->contextid, $newcohort->contextid); 79 $this->assertSame($cohort->name, $newcohort->name); 80 $this->assertSame($cohort->description, $newcohort->description); 81 $this->assertEquals($cohort->descriptionformat, $newcohort->descriptionformat); 82 $this->assertNotEmpty($newcohort->timecreated); 83 $this->assertSame($newcohort->component, ''); 84 $this->assertSame($newcohort->theme, ''); 85 $this->assertSame($newcohort->timecreated, $newcohort->timemodified); 86 87 $handler = cohort_handler::create(); 88 $customfieldsdata = $handler->export_instance_data_object($id); 89 $this->assertEquals('Test value 1', $customfieldsdata->testfield1); 90 } 91 92 public function test_cohort_add_cohort_missing_name() { 93 $cohort = new \stdClass(); 94 $cohort->contextid = \context_system::instance()->id; 95 $cohort->name = null; 96 $cohort->idnumber = 'testid'; 97 $cohort->description = 'test cohort desc'; 98 $cohort->descriptionformat = FORMAT_HTML; 99 100 $this->expectException(\coding_exception::class); 101 $this->expectExceptionMessage('Missing cohort name in cohort_add_cohort()'); 102 cohort_add_cohort($cohort); 103 } 104 105 public function test_cohort_add_cohort_event() { 106 $this->resetAfterTest(); 107 108 // Setup cohort data structure. 109 $cohort = new \stdClass(); 110 $cohort->contextid = \context_system::instance()->id; 111 $cohort->name = 'test cohort'; 112 $cohort->idnumber = 'testid'; 113 $cohort->description = 'test cohort desc'; 114 $cohort->descriptionformat = FORMAT_HTML; 115 116 // Catch Events. 117 $sink = $this->redirectEvents(); 118 119 // Perform the add operation. 120 $id = cohort_add_cohort($cohort); 121 122 // Capture the event. 123 $events = $sink->get_events(); 124 $sink->close(); 125 126 // Validate the event. 127 $this->assertCount(1, $events); 128 $event = $events[0]; 129 $this->assertInstanceOf('\core\event\cohort_created', $event); 130 $this->assertEquals('cohort', $event->objecttable); 131 $this->assertEquals($id, $event->objectid); 132 $this->assertEquals($cohort->contextid, $event->contextid); 133 $url = new \moodle_url('/cohort/index.php', array('contextid' => $event->contextid)); 134 $this->assertEquals($url, $event->get_url()); 135 $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id)); 136 $this->assertEventContextNotUsed($event); 137 } 138 139 public function test_cohort_update_cohort() { 140 global $DB; 141 142 $this->resetAfterTest(); 143 $this->setAdminUser(); 144 145 $this->create_cohort_custom_field(); 146 147 $cohort = new \stdClass(); 148 $cohort->contextid = \context_system::instance()->id; 149 $cohort->name = 'test cohort'; 150 $cohort->idnumber = 'testid'; 151 $cohort->description = 'test cohort desc'; 152 $cohort->descriptionformat = FORMAT_HTML; 153 $cohort->customfield_testfield1 = 'Test value 1'; 154 155 $id = cohort_add_cohort($cohort); 156 $this->assertNotEmpty($id); 157 $DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id'=>$id)); 158 $DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id'=>$id)); 159 $cohort = $DB->get_record('cohort', array('id'=>$id)); 160 161 $cohort->name = 'test cohort 2'; 162 $cohort->customfield_testfield1 = 'Test value updated'; 163 164 cohort_update_cohort($cohort); 165 166 $newcohort = $DB->get_record('cohort', array('id'=>$id)); 167 168 $this->assertSame($cohort->contextid, $newcohort->contextid); 169 $this->assertSame($cohort->name, $newcohort->name); 170 $this->assertSame($cohort->description, $newcohort->description); 171 $this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat); 172 $this->assertSame($cohort->timecreated, $newcohort->timecreated); 173 $this->assertSame($cohort->component, $newcohort->component); 174 $this->assertSame($newcohort->theme, ''); 175 $this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified); 176 $this->assertLessThanOrEqual(time(), $newcohort->timemodified); 177 178 $handler = cohort_handler::create(); 179 $customfieldsdata = $handler->export_instance_data_object($id); 180 $this->assertEquals('Test value updated', $customfieldsdata->testfield1); 181 } 182 183 public function test_cohort_update_cohort_event() { 184 global $DB; 185 186 $this->resetAfterTest(); 187 188 // Setup the cohort data structure. 189 $cohort = new \stdClass(); 190 $cohort->contextid = \context_system::instance()->id; 191 $cohort->name = 'test cohort'; 192 $cohort->idnumber = 'testid'; 193 $cohort->description = 'test cohort desc'; 194 $cohort->descriptionformat = FORMAT_HTML; 195 $cohort->theme = ''; 196 $id = cohort_add_cohort($cohort); 197 $this->assertNotEmpty($id); 198 199 $cohort->name = 'test cohort 2'; 200 201 // Catch Events. 202 $sink = $this->redirectEvents(); 203 204 // Peform the update. 205 cohort_update_cohort($cohort); 206 // Add again theme property to the cohort object for comparing it to the event snapshop. 207 $cohort->theme = ''; 208 209 $events = $sink->get_events(); 210 $sink->close(); 211 212 // Validate the event. 213 $this->assertCount(1, $events); 214 $event = $events[0]; 215 $updatedcohort = $DB->get_record('cohort', array('id'=>$id)); 216 $this->assertInstanceOf('\core\event\cohort_updated', $event); 217 $this->assertEquals('cohort', $event->objecttable); 218 $this->assertEquals($updatedcohort->id, $event->objectid); 219 $this->assertEquals($updatedcohort->contextid, $event->contextid); 220 $url = new \moodle_url('/cohort/edit.php', array('id' => $event->objectid)); 221 $this->assertEquals($url, $event->get_url()); 222 $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id)); 223 $this->assertEventContextNotUsed($event); 224 } 225 226 public function test_cohort_delete_cohort() { 227 global $DB; 228 229 $this->resetAfterTest(); 230 $this->setAdminUser(); 231 232 $field = $this->create_cohort_custom_field(); 233 234 $cohort = $this->getDataGenerator()->create_cohort(['customfield_testfield1' => 'Test value 1']); 235 $this->assertTrue($DB->record_exists('customfield_data', ['instanceid' => $cohort->id, 'fieldid' => $field->get('id')])); 236 237 cohort_delete_cohort($cohort); 238 239 $this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id))); 240 $this->assertFalse($DB->record_exists('customfield_data', ['instanceid' => $cohort->id, 'fieldid' => $field->get('id')])); 241 } 242 243 public function test_cohort_delete_cohort_event() { 244 245 $this->resetAfterTest(); 246 247 $cohort = $this->getDataGenerator()->create_cohort(); 248 249 // Capture the events. 250 $sink = $this->redirectEvents(); 251 252 // Perform the delete. 253 cohort_delete_cohort($cohort); 254 255 $events = $sink->get_events(); 256 $sink->close(); 257 258 // Validate the event structure. 259 $this->assertCount(1, $events); 260 $event = $events[0]; 261 $this->assertInstanceOf('\core\event\cohort_deleted', $event); 262 $this->assertEquals('cohort', $event->objecttable); 263 $this->assertEquals($cohort->id, $event->objectid); 264 $url = new \moodle_url('/cohort/index.php', array('contextid' => $event->contextid)); 265 $this->assertEquals($url, $event->get_url()); 266 $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $cohort->id)); 267 $this->assertEventContextNotUsed($event); 268 } 269 270 public function test_cohort_delete_category() { 271 global $DB; 272 273 $this->resetAfterTest(); 274 275 $category = $this->getDataGenerator()->create_category(); 276 277 $cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category->id)->id)); 278 279 cohort_delete_category($category); 280 281 $this->assertTrue($DB->record_exists('cohort', array('id'=>$cohort->id))); 282 $newcohort = $DB->get_record('cohort', array('id'=>$cohort->id)); 283 $this->assertEquals(\context_system::instance()->id, $newcohort->contextid); 284 } 285 286 public function test_cohort_add_member() { 287 global $DB; 288 289 $this->resetAfterTest(); 290 291 $cohort = $this->getDataGenerator()->create_cohort(); 292 $user = $this->getDataGenerator()->create_user(); 293 294 $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 295 cohort_add_member($cohort->id, $user->id); 296 $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 297 } 298 299 public function test_cohort_add_member_event() { 300 global $USER; 301 $this->resetAfterTest(); 302 303 // Setup the data. 304 $cohort = $this->getDataGenerator()->create_cohort(); 305 $user = $this->getDataGenerator()->create_user(); 306 307 // Capture the events. 308 $sink = $this->redirectEvents(); 309 310 // Peform the add member operation. 311 cohort_add_member($cohort->id, $user->id); 312 313 $events = $sink->get_events(); 314 $sink->close(); 315 316 // Validate the event. 317 $this->assertCount(1, $events); 318 $event = $events[0]; 319 $this->assertInstanceOf('\core\event\cohort_member_added', $event); 320 $this->assertEquals('cohort', $event->objecttable); 321 $this->assertEquals($cohort->id, $event->objectid); 322 $this->assertEquals($user->id, $event->relateduserid); 323 $this->assertEquals($USER->id, $event->userid); 324 $url = new \moodle_url('/cohort/assign.php', array('id' => $event->objectid)); 325 $this->assertEquals($url, $event->get_url()); 326 $this->assertEventContextNotUsed($event); 327 } 328 329 public function test_cohort_remove_member() { 330 global $DB; 331 332 $this->resetAfterTest(); 333 334 $cohort = $this->getDataGenerator()->create_cohort(); 335 $user = $this->getDataGenerator()->create_user(); 336 337 cohort_add_member($cohort->id, $user->id); 338 $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 339 340 cohort_remove_member($cohort->id, $user->id); 341 $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 342 } 343 344 public function test_cohort_remove_member_event() { 345 global $USER; 346 $this->resetAfterTest(); 347 348 // Setup the data. 349 $cohort = $this->getDataGenerator()->create_cohort(); 350 $user = $this->getDataGenerator()->create_user(); 351 cohort_add_member($cohort->id, $user->id); 352 353 // Capture the events. 354 $sink = $this->redirectEvents(); 355 356 // Peform the remove operation. 357 cohort_remove_member($cohort->id, $user->id); 358 $events = $sink->get_events(); 359 $sink->close(); 360 361 // Validate the event. 362 $this->assertCount(1, $events); 363 $event = $events[0]; 364 $this->assertInstanceOf('\core\event\cohort_member_removed', $event); 365 $this->assertEquals('cohort', $event->objecttable); 366 $this->assertEquals($cohort->id, $event->objectid); 367 $this->assertEquals($user->id, $event->relateduserid); 368 $this->assertEquals($USER->id, $event->userid); 369 $url = new \moodle_url('/cohort/assign.php', array('id' => $event->objectid)); 370 $this->assertEquals($url, $event->get_url()); 371 $this->assertEventContextNotUsed($event); 372 } 373 374 public function test_cohort_is_member() { 375 global $DB; 376 377 $this->resetAfterTest(); 378 379 $cohort = $this->getDataGenerator()->create_cohort(); 380 $user = $this->getDataGenerator()->create_user(); 381 382 $this->assertFalse(cohort_is_member($cohort->id, $user->id)); 383 cohort_add_member($cohort->id, $user->id); 384 $this->assertTrue(cohort_is_member($cohort->id, $user->id)); 385 } 386 387 public function test_cohort_get_cohorts() { 388 global $DB; 389 390 $this->resetAfterTest(); 391 392 $category1 = $this->getDataGenerator()->create_category(); 393 $category2 = $this->getDataGenerator()->create_category(); 394 395 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>'')); 396 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr')); 397 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us')); 398 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_system::instance()->id)); 399 400 $result = cohort_get_cohorts(\context_coursecat::instance($category2->id)->id); 401 $this->assertEquals(0, $result['totalcohorts']); 402 $this->assertEquals(0, count($result['cohorts'])); 403 $this->assertEquals(0, $result['allcohorts']); 404 405 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id); 406 $this->assertEquals(3, $result['totalcohorts']); 407 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3), $result['cohorts']); 408 $this->assertEquals(3, $result['allcohorts']); 409 410 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'arrrgh'); 411 $this->assertEquals(1, $result['totalcohorts']); 412 $this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']); 413 $this->assertEquals(3, $result['allcohorts']); 414 415 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'brrr'); 416 $this->assertEquals(1, $result['totalcohorts']); 417 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']); 418 $this->assertEquals(3, $result['allcohorts']); 419 420 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'grrr'); 421 $this->assertEquals(1, $result['totalcohorts']); 422 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 423 $this->assertEquals(3, $result['allcohorts']); 424 425 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 1, 1, 'yyy'); 426 $this->assertEquals(3, $result['totalcohorts']); 427 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']); 428 $this->assertEquals(3, $result['allcohorts']); 429 430 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'po_us'); 431 $this->assertEquals(1, $result['totalcohorts']); 432 $this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']); 433 $this->assertEquals(3, $result['allcohorts']); 434 435 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'pokus'); 436 $this->assertEquals(0, $result['totalcohorts']); 437 $this->assertEquals(array(), $result['cohorts']); 438 $this->assertEquals(3, $result['allcohorts']); 439 440 $result = cohort_get_cohorts(\context_system::instance()->id); 441 $this->assertEquals(1, $result['totalcohorts']); 442 $this->assertEquals(array($cohort4->id=>$cohort4), $result['cohorts']); 443 $this->assertEquals(1, $result['allcohorts']); 444 } 445 446 public function test_cohort_get_all_cohorts() { 447 global $DB; 448 449 $this->resetAfterTest(); 450 451 $category1 = $this->getDataGenerator()->create_category(); 452 $category2 = $this->getDataGenerator()->create_category(); 453 454 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>'')); 455 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr')); 456 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category2->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us')); 457 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_system::instance()->id)); 458 459 // Get list of all cohorts as admin. 460 $this->setAdminUser(); 461 462 $result = cohort_get_all_cohorts(0, 100, ''); 463 $this->assertEquals(4, $result['totalcohorts']); 464 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3, $cohort4->id=>$cohort4), $result['cohorts']); 465 $this->assertEquals(4, $result['allcohorts']); 466 467 $result = cohort_get_all_cohorts(0, 100, 'grrr'); 468 $this->assertEquals(1, $result['totalcohorts']); 469 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 470 $this->assertEquals(4, $result['allcohorts']); 471 472 // Get list of all cohorts as manager who has capability everywhere. 473 $user = $this->getDataGenerator()->create_user(); 474 $managerrole = $DB->get_record('role', array('shortname' => 'manager')); 475 role_assign($managerrole->id, $user->id, \context_system::instance()->id); 476 $this->setUser($user); 477 478 $result = cohort_get_all_cohorts(0, 100, ''); 479 $this->assertEquals(4, $result['totalcohorts']); 480 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3, $cohort4->id=>$cohort4), $result['cohorts']); 481 $this->assertEquals(4, $result['allcohorts']); 482 483 $result = cohort_get_all_cohorts(0, 100, 'grrr'); 484 $this->assertEquals(1, $result['totalcohorts']); 485 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 486 $this->assertEquals(4, $result['allcohorts']); 487 488 // Get list of all cohorts as manager who has capability everywhere except category2. 489 $context2 = \context_coursecat::instance($category2->id); 490 role_change_permission($managerrole->id, $context2, 'moodle/cohort:view', CAP_PROHIBIT); 491 role_change_permission($managerrole->id, $context2, 'moodle/cohort:manage', CAP_PROHIBIT); 492 $this->assertFalse(has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $context2)); 493 494 $result = cohort_get_all_cohorts(0, 100, ''); 495 $this->assertEquals(3, $result['totalcohorts']); 496 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort4->id=>$cohort4), $result['cohorts']); 497 $this->assertEquals(3, $result['allcohorts']); 498 499 $result = cohort_get_all_cohorts(0, 100, 'grrr'); 500 $this->assertEquals(1, $result['totalcohorts']); 501 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 502 $this->assertEquals(3, $result['allcohorts']); 503 504 $result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 1, 1, 'yyy'); 505 $this->assertEquals(2, $result['totalcohorts']); 506 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']); 507 $this->assertEquals(2, $result['allcohorts']); 508 } 509 510 public function test_cohort_get_available_cohorts() { 511 global $DB; 512 513 $this->resetAfterTest(); 514 515 $category1 = $this->getDataGenerator()->create_category(); 516 $category2 = $this->getDataGenerator()->create_category(); 517 518 $course1 = $this->getDataGenerator()->create_course(array('category' => $category1->id)); 519 $course2 = $this->getDataGenerator()->create_course(array('category' => $category2->id)); 520 521 $category1ctx = \context_coursecat::instance($category1->id); 522 $category2ctx = \context_coursecat::instance($category2->id); 523 $course1ctx = \context_course::instance(($course1->id)); 524 $course2ctx = \context_course::instance(($course2->id)); 525 $systemctx = \context_system::instance(); 526 527 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category1ctx->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>'')); 528 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category1ctx->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr', 'visible'=>0)); 529 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category2ctx->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us')); 530 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>$systemctx->id, 'name' => 'ddd')); 531 $cohort5 = $this->getDataGenerator()->create_cohort(array('contextid'=>$systemctx->id, 'visible'=>0, 'name' => 'eee')); 532 533 /* 534 Structure of generated course categories, courses and cohort: 535 536 system 537 -cohort4 (visible, has 3 members) 538 -cohort5 (not visible, no members) 539 category1 540 -cohort1 (visible, no members) 541 -cohort2 (not visible, has 1 member) 542 course1 543 category2 544 -cohort3 (visible, has 2 member) 545 course2 546 547 In this test we call cohort_get_available_cohorts() for users with different roles 548 and with different paramteres ($withmembers, $search, $offset, $limit) to make sure we go 549 through all possible options of SQL query. 550 */ 551 552 // Admin can see visible and invisible cohorts defined in above contexts. 553 $this->setAdminUser(); 554 555 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); 556 $this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id, $cohort5->id), array_keys($result)); 557 558 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 2, ''); 559 $this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result)); 560 561 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 2, ''); 562 $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); 563 564 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy'); 565 $this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result)); 566 567 $result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, ''); 568 $this->assertEquals(array($cohort3->id, $cohort4->id, $cohort5->id), array_keys($result)); 569 570 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY); 571 $this->assertEmpty($result); 572 573 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY); 574 $this->assertEmpty($result); 575 576 // Get list of available cohorts as a teacher in the course. 577 $user1 = $this->getDataGenerator()->create_user(); 578 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 579 role_assign($teacherrole->id, $user1->id, $course1ctx->id); 580 role_assign($teacherrole->id, $user1->id, $course2ctx->id); 581 $this->setUser($user1); 582 583 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); 584 $this->assertEquals(array($cohort1->id, $cohort4->id), array_keys($result)); 585 586 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 1, ''); 587 $this->assertEquals(array($cohort1->id), array_keys($result)); 588 589 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 1, ''); 590 $this->assertEquals(array($cohort4->id), array_keys($result)); 591 592 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy'); 593 $this->assertEquals(array($cohort1->id), array_keys($result)); 594 595 $result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, ''); 596 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 597 598 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY); 599 $this->assertEmpty($result); 600 601 // Now add members to cohorts. 602 $user2 = $this->getDataGenerator()->create_user(); 603 $user3 = $this->getDataGenerator()->create_user(); 604 $user4 = $this->getDataGenerator()->create_user(); 605 $user5 = $this->getDataGenerator()->create_user(); 606 $user6 = $this->getDataGenerator()->create_user(); 607 cohort_add_member($cohort2->id, $user3->id); 608 cohort_add_member($cohort3->id, $user2->id); 609 cohort_add_member($cohort3->id, $user3->id); 610 cohort_add_member($cohort4->id, $user4->id); 611 cohort_add_member($cohort4->id, $user5->id); 612 cohort_add_member($cohort4->id, $user6->id); 613 614 // Check filtering non-empty cohorts as admin. 615 $this->setAdminUser(); 616 617 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 618 $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); 619 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 620 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 621 622 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 623 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 624 $this->assertEquals(2, $result[$cohort3->id]->memberscnt); 625 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 626 627 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy'); 628 $this->assertEquals(array($cohort2->id), array_keys($result)); 629 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 630 631 // Check filtering non-empty cohorts as teacher. 632 $this->setUser($user1); 633 634 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 635 $this->assertEquals(array($cohort4->id), array_keys($result)); 636 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 637 638 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 639 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 640 $this->assertEquals(2, $result[$cohort3->id]->memberscnt); 641 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 642 643 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy'); 644 $this->assertEmpty($result); 645 646 // Enrol users. 647 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 648 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $studentrole->id); 649 $this->getDataGenerator()->enrol_user($user3->id, $course1->id, $studentrole->id); 650 $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $studentrole->id); 651 $this->getDataGenerator()->enrol_user($user6->id, $course1->id, $studentrole->id); 652 $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id); 653 $this->getDataGenerator()->enrol_user($user4->id, $course2->id, $studentrole->id); 654 $this->getDataGenerator()->enrol_user($user5->id, $course2->id, $studentrole->id); 655 $this->getDataGenerator()->enrol_user($user6->id, $course2->id, $studentrole->id); 656 657 // Check cohorts with enrolments as admin. 658 $this->setAdminUser(); 659 660 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, ''); 661 $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); 662 $this->assertEquals(1, $result[$cohort2->id]->enrolledcnt); 663 $this->assertEquals(2, $result[$cohort4->id]->enrolledcnt); 664 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 665 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 666 667 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, ''); 668 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 669 $this->assertEquals(1, $result[$cohort3->id]->enrolledcnt); 670 $this->assertEquals(3, $result[$cohort4->id]->enrolledcnt); 671 $this->assertEquals(2, $result[$cohort3->id]->memberscnt); 672 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 673 674 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, 'yyy'); 675 $this->assertEquals(array($cohort2->id), array_keys($result)); 676 $this->assertEquals(1, $result[$cohort2->id]->enrolledcnt); 677 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 678 679 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY, 0, 0, ''); 680 $this->assertEquals(array($cohort4->id), array_keys($result)); 681 $this->assertEquals(2, $result[$cohort4->id]->enrolledcnt); 682 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 683 684 // Assign user1 additional 'manager' role in the category context. He can now see hidden cohort in category1 685 // but still can not see hidden category in system. 686 $managerrole = $DB->get_record('role', array('shortname' => 'manager')); 687 role_assign($managerrole->id, $user1->id, \context_coursecat::instance($category1->id)); 688 $this->setUser($user1); 689 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); 690 $this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id), array_keys($result)); 691 } 692 693 /** 694 * Test that all get functions return custom fields data. 695 * 696 * @covers \cohort_get_cohort, \cohort_get_cohorts, \cohort_get_all_cohorts 697 * @covers \cohort_get_available_cohorts, \cohort_get_user_cohorts 698 */ 699 public function test_get_functions_return_custom_fields() { 700 $this->resetAfterTest(); 701 $this->setAdminUser(); 702 703 $user = self::getDataGenerator()->create_user(); 704 $course = self::getDataGenerator()->create_course(); 705 $coursectx = \context_course::instance(($course->id)); 706 707 $this->create_cohort_custom_field(); 708 709 $cohort1 = $this->getDataGenerator()->create_cohort(['customfield_testfield1' => 'Test value 1']); 710 $cohort2 = $this->getDataGenerator()->create_cohort(); 711 712 // Test cohort_get_cohort. 713 $result = cohort_get_cohort($cohort1->id, $coursectx, true); 714 $this->assertObjectHasAttribute('customfields', $result); 715 $this->assertCount(1, $result->customfields); 716 $field = reset($result->customfields); 717 $this->assertInstanceOf(data_controller::class, $field); 718 $this->assertEquals('testfield1', $field->get_field()->get('shortname')); 719 $this->assertEquals('Test value 1', $field->get_value()); 720 721 // Test custom fields are not returned if not needed. 722 $result = cohort_get_cohort($cohort1->id, $coursectx); 723 $this->assertObjectNotHasAttribute('customfields', $result); 724 725 // Test cohort_get_cohorts. 726 $result = cohort_get_cohorts(\context_system::instance()->id, 0, 25, '', true); 727 $this->assertEquals(2, $result['totalcohorts']); 728 $this->assertEquals(2, $result['allcohorts']); 729 foreach ($result['cohorts'] as $cohort) { 730 $this->assertObjectHasAttribute('customfields', $cohort); 731 $this->assertCount(1, $cohort->customfields); 732 $field = reset($cohort->customfields); 733 $this->assertInstanceOf(data_controller::class, $field); 734 $this->assertEquals('testfield1', $field->get_field()->get('shortname')); 735 736 if ($cohort->id == $cohort1->id ) { 737 $this->assertEquals('Test value 1', $field->get_value()); 738 } else { 739 $this->assertEquals('', $field->get_value()); 740 } 741 } 742 743 // Test custom fields are not returned if not needed. 744 $result = cohort_get_cohorts(\context_system::instance()->id, 0, 25, ''); 745 $this->assertEquals(2, $result['totalcohorts']); 746 $this->assertEquals(2, $result['allcohorts']); 747 foreach ($result['cohorts'] as $cohort) { 748 $this->assertObjectNotHasAttribute('customfields', $cohort); 749 } 750 751 // Test test_cohort_get_all_cohorts. 752 $result = cohort_get_all_cohorts(0, 100, '', true); 753 $this->assertEquals(2, $result['totalcohorts']); 754 $this->assertEquals(2, $result['allcohorts']); 755 foreach ($result['cohorts'] as $cohort) { 756 $this->assertObjectHasAttribute('customfields', $cohort); 757 $this->assertCount(1, $cohort->customfields); 758 $field = reset($cohort->customfields); 759 $this->assertInstanceOf(data_controller::class, $field); 760 $this->assertEquals('testfield1', $field->get_field()->get('shortname')); 761 762 if ($cohort->id == $cohort1->id ) { 763 $this->assertEquals('Test value 1', $field->get_value()); 764 } else { 765 $this->assertEquals('', $field->get_value()); 766 } 767 } 768 769 // Test custom fields are not returned if not needed. 770 $result = cohort_get_all_cohorts(0, 100, ''); 771 $this->assertEquals(2, $result['totalcohorts']); 772 $this->assertEquals(2, $result['allcohorts']); 773 foreach ($result['cohorts'] as $cohort) { 774 $this->assertObjectNotHasAttribute('customfields', $cohort); 775 } 776 777 // Test cohort_get_available_cohorts. 778 $result = cohort_get_available_cohorts($coursectx, COHORT_ALL, 0, 25, '', true); 779 $this->assertCount(2, $result); 780 foreach ($result as $cohort) { 781 $this->assertObjectHasAttribute('customfields', $cohort); 782 $this->assertCount(1, $cohort->customfields); 783 $field = reset($cohort->customfields); 784 $this->assertInstanceOf(data_controller::class, $field); 785 $this->assertEquals('testfield1', $field->get_field()->get('shortname')); 786 787 if ($cohort->id == $cohort1->id ) { 788 $this->assertEquals('Test value 1', $field->get_value()); 789 } else { 790 $this->assertEquals('', $field->get_value()); 791 } 792 } 793 794 // Test custom fields are not returned if not needed. 795 $result = cohort_get_available_cohorts($coursectx, COHORT_ALL, 0, 25, ''); 796 $this->assertCount(2, $result); 797 foreach ($result as $cohort) { 798 $this->assertObjectNotHasAttribute('customfields', $cohort); 799 } 800 801 // Test cohort_get_user_cohorts. 802 cohort_add_member($cohort1->id, $user->id); 803 cohort_add_member($cohort2->id, $user->id); 804 805 $result = cohort_get_user_cohorts($user->id, true); 806 $this->assertCount(2, $result); 807 foreach ($result as $cohort) { 808 $this->assertObjectHasAttribute('customfields', $cohort); 809 $this->assertCount(1, $cohort->customfields); 810 $field = reset($cohort->customfields); 811 $this->assertInstanceOf(data_controller::class, $field); 812 $this->assertEquals('testfield1', $field->get_field()->get('shortname')); 813 814 if ($cohort->id == $cohort1->id ) { 815 $this->assertEquals('Test value 1', $field->get_value()); 816 } else { 817 $this->assertEquals('', $field->get_value()); 818 } 819 } 820 821 // Test that there is no custom fields returned if not required. 822 $result = cohort_get_user_cohorts($user->id); 823 $this->assertCount(2, $result); 824 foreach ($result as $cohort) { 825 $this->assertObjectNotHasAttribute('customfields', $cohort); 826 } 827 } 828 829 /** 830 * Create a cohort with allowcohortthemes enabled/disabled. 831 */ 832 public function test_cohort_add_theme_cohort() { 833 global $DB; 834 835 $this->resetAfterTest(); 836 837 // Theme is added when allowcohortthemes is enabled. 838 set_config('allowcohortthemes', 1); 839 set_config('theme', 'boost'); 840 841 $systemctx = \context_system::instance(); 842 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 1', 843 'idnumber' => 'testid1', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic')); 844 845 $id = cohort_add_cohort($cohort1); 846 $this->assertNotEmpty($id); 847 $newcohort = $DB->get_record('cohort', array('id' => $id)); 848 $this->assertEquals($cohort1->contextid, $newcohort->contextid); 849 $this->assertSame($cohort1->name, $newcohort->name); 850 $this->assertSame($cohort1->description, $newcohort->description); 851 $this->assertEquals($cohort1->descriptionformat, $newcohort->descriptionformat); 852 $this->assertNotEmpty($newcohort->theme); 853 $this->assertSame($cohort1->theme, $newcohort->theme); 854 $this->assertNotEmpty($newcohort->timecreated); 855 $this->assertSame($newcohort->component, ''); 856 $this->assertSame($newcohort->timecreated, $newcohort->timemodified); 857 858 // Theme is not added when allowcohortthemes is disabled. 859 set_config('allowcohortthemes', 0); 860 861 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 2', 862 'idnumber' => 'testid2', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic')); 863 864 $id = cohort_add_cohort($cohort2); 865 $this->assertNotEmpty($id); 866 $newcohort = $DB->get_record('cohort', array('id' => $id)); 867 $this->assertSame($cohort2->name, $newcohort->name); 868 $this->assertEmpty($newcohort->theme); 869 } 870 871 /** 872 * Update a cohort with allowcohortthemes enabled/disabled. 873 */ 874 public function test_cohort_update_theme_cohort() { 875 global $DB; 876 877 $this->resetAfterTest(); 878 879 // Enable cohort themes. 880 set_config('allowcohortthemes', 1); 881 set_config('theme', 'boost'); 882 883 $systemctx = \context_system::instance(); 884 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 1', 885 'idnumber' => 'testid1', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic')); 886 $id = cohort_add_cohort($cohort1); 887 $this->assertNotEmpty($id); 888 889 // Theme is updated when allowcohortthemes is enabled. 890 $cohort1 = $DB->get_record('cohort', array('id' => $id)); 891 $cohort1->name = 'test cohort 1 updated'; 892 $cohort1->theme = 'classic'; 893 cohort_update_cohort($cohort1); 894 $updatedcohort = $DB->get_record('cohort', array('id' => $id)); 895 $this->assertEquals($cohort1->contextid, $updatedcohort->contextid); 896 $this->assertSame($cohort1->name, $updatedcohort->name); 897 $this->assertSame($cohort1->description, $updatedcohort->description); 898 $this->assertNotEmpty($updatedcohort->theme); 899 $this->assertSame($cohort1->theme, $updatedcohort->theme); 900 901 // Theme is not updated neither overwritten when allowcohortthemes is disabled. 902 set_config('allowcohortthemes', 0); 903 $cohort2 = $DB->get_record('cohort', array('id' => $id)); 904 $cohort2->theme = 'classic'; 905 cohort_update_cohort($cohort2); 906 $updatedcohort = $DB->get_record('cohort', array('id' => $id)); 907 $this->assertEquals($cohort2->contextid, $updatedcohort->contextid); 908 $this->assertNotEmpty($updatedcohort->theme); 909 $this->assertSame($cohort1->theme, $updatedcohort->theme); 910 } 911 912 /** 913 * Test that lib function returns custom field data for a cohorts. 914 * 915 * @covers \cohort_get_custom_fields_data 916 */ 917 public function test_cohort_get_custom_fields_data() { 918 $this->resetAfterTest(); 919 $this->setAdminUser(); 920 921 $this->create_cohort_custom_field(); 922 923 $cohort1 = $this->getDataGenerator()->create_cohort(['customfield_testfield1' => 'Test value 1']); 924 $cohort2 = $this->getDataGenerator()->create_cohort(); 925 926 $result = cohort_get_custom_fields_data([$cohort1->id, $cohort2->id, 777]); 927 $this->assertArrayHasKey($cohort1->id, $result); 928 $this->assertArrayHasKey($cohort2->id, $result); 929 $this->assertArrayHasKey(777, $result); 930 931 foreach ($result as $cohortid => $fieldcontrollers) { 932 foreach ($fieldcontrollers as $fieldcontroller) { 933 $this->assertInstanceOf(data_controller::class, $fieldcontroller); 934 if ($cohortid == $cohort1->id) { 935 $this->assertSame('Test value 1', $fieldcontroller->export_value()); 936 } else { 937 $this->assertNull($fieldcontroller->export_value()); 938 } 939 } 940 } 941 } 942 943 /** 944 * Test the behaviour of cohort_get_cohort(). 945 * 946 * @covers ::cohort_get_cohort 947 */ 948 public function test_cohort_get_cohort() { 949 $this->resetAfterTest(); 950 951 $cat = $this->getDataGenerator()->create_category(); 952 $cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]); 953 $cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]); 954 955 $course1 = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON1']); 956 $course2 = $this->getDataGenerator()->create_course(['category' => $cat2->id, 'shortname' => 'ANON2']); 957 958 $cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]); 959 960 $result = cohort_get_cohort($cohort1->id, \context_course::instance($course2->id)); 961 $this->assertFalse($result); 962 963 $result = cohort_get_cohort($cohort1->id, \context_course::instance($course2->id), true); 964 $this->assertFalse($result); 965 966 $result = cohort_get_cohort($cohort1->id, \context_course::instance($course1->id)); 967 $this->assertEquals($cohort1->id, $result->id); 968 969 $result = cohort_get_cohort($cohort1->id, \context_course::instance($course1->id), true); 970 $this->assertEquals($cohort1->id, $result->id); 971 } 972 973 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body