See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310]
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 /** 18 * Cohort library tests. 19 * 20 * @package core_cohort 21 * @category phpunit 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once("$CFG->dirroot/cohort/lib.php"); 30 31 32 /** 33 * Cohort library tests. 34 * 35 * @package core_cohort 36 * @category phpunit 37 * @copyright 2012 Petr Skoda {@link http://skodak.org} 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class core_cohort_cohortlib_testcase extends advanced_testcase { 41 42 public function test_cohort_add_cohort() { 43 global $DB; 44 45 $this->resetAfterTest(); 46 47 $cohort = new stdClass(); 48 $cohort->contextid = context_system::instance()->id; 49 $cohort->name = 'test cohort'; 50 $cohort->idnumber = 'testid'; 51 $cohort->description = 'test cohort desc'; 52 $cohort->descriptionformat = FORMAT_HTML; 53 54 $id = cohort_add_cohort($cohort); 55 $this->assertNotEmpty($id); 56 57 $newcohort = $DB->get_record('cohort', array('id'=>$id)); 58 $this->assertEquals($cohort->contextid, $newcohort->contextid); 59 $this->assertSame($cohort->name, $newcohort->name); 60 $this->assertSame($cohort->description, $newcohort->description); 61 $this->assertEquals($cohort->descriptionformat, $newcohort->descriptionformat); 62 $this->assertNotEmpty($newcohort->timecreated); 63 $this->assertSame($newcohort->component, ''); 64 $this->assertSame($newcohort->theme, ''); 65 $this->assertSame($newcohort->timecreated, $newcohort->timemodified); 66 } 67 68 /** 69 * @expectedException coding_exception 70 * @expectedExceptionMessage Missing cohort name in cohort_add_cohort(). 71 */ 72 public function test_cohort_add_cohort_missing_name() { 73 $cohort = new stdClass(); 74 $cohort->contextid = context_system::instance()->id; 75 $cohort->name = null; 76 $cohort->idnumber = 'testid'; 77 $cohort->description = 'test cohort desc'; 78 $cohort->descriptionformat = FORMAT_HTML; 79 80 cohort_add_cohort($cohort); 81 } 82 83 public function test_cohort_add_cohort_event() { 84 $this->resetAfterTest(); 85 86 // Setup cohort data structure. 87 $cohort = new stdClass(); 88 $cohort->contextid = context_system::instance()->id; 89 $cohort->name = 'test cohort'; 90 $cohort->idnumber = 'testid'; 91 $cohort->description = 'test cohort desc'; 92 $cohort->descriptionformat = FORMAT_HTML; 93 94 // Catch Events. 95 $sink = $this->redirectEvents(); 96 97 // Perform the add operation. 98 $id = cohort_add_cohort($cohort); 99 100 // Capture the event. 101 $events = $sink->get_events(); 102 $sink->close(); 103 104 // Validate the event. 105 $this->assertCount(1, $events); 106 $event = $events[0]; 107 $this->assertInstanceOf('\core\event\cohort_created', $event); 108 $this->assertEquals('cohort', $event->objecttable); 109 $this->assertEquals($id, $event->objectid); 110 $this->assertEquals($cohort->contextid, $event->contextid); 111 $url = new moodle_url('/cohort/index.php', array('contextid' => $event->contextid)); 112 $this->assertEquals($url, $event->get_url()); 113 $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id)); 114 $this->assertEventLegacyData($cohort, $event); 115 $this->assertEventContextNotUsed($event); 116 } 117 118 public function test_cohort_update_cohort() { 119 global $DB; 120 121 $this->resetAfterTest(); 122 123 $cohort = new stdClass(); 124 $cohort->contextid = context_system::instance()->id; 125 $cohort->name = 'test cohort'; 126 $cohort->idnumber = 'testid'; 127 $cohort->description = 'test cohort desc'; 128 $cohort->descriptionformat = FORMAT_HTML; 129 $id = cohort_add_cohort($cohort); 130 $this->assertNotEmpty($id); 131 $DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id'=>$id)); 132 $DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id'=>$id)); 133 $cohort = $DB->get_record('cohort', array('id'=>$id)); 134 135 $cohort->name = 'test cohort 2'; 136 cohort_update_cohort($cohort); 137 138 $newcohort = $DB->get_record('cohort', array('id'=>$id)); 139 140 $this->assertSame($cohort->contextid, $newcohort->contextid); 141 $this->assertSame($cohort->name, $newcohort->name); 142 $this->assertSame($cohort->description, $newcohort->description); 143 $this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat); 144 $this->assertSame($cohort->timecreated, $newcohort->timecreated); 145 $this->assertSame($cohort->component, $newcohort->component); 146 $this->assertSame($newcohort->theme, ''); 147 $this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified); 148 $this->assertLessThanOrEqual(time(), $newcohort->timemodified); 149 } 150 151 public function test_cohort_update_cohort_event() { 152 global $DB; 153 154 $this->resetAfterTest(); 155 156 // Setup the cohort data structure. 157 $cohort = new stdClass(); 158 $cohort->contextid = context_system::instance()->id; 159 $cohort->name = 'test cohort'; 160 $cohort->idnumber = 'testid'; 161 $cohort->description = 'test cohort desc'; 162 $cohort->descriptionformat = FORMAT_HTML; 163 $cohort->theme = ''; 164 $id = cohort_add_cohort($cohort); 165 $this->assertNotEmpty($id); 166 167 $cohort->name = 'test cohort 2'; 168 169 // Catch Events. 170 $sink = $this->redirectEvents(); 171 172 // Peform the update. 173 cohort_update_cohort($cohort); 174 // Add again theme property to the cohort object for comparing it to the event snapshop. 175 $cohort->theme = ''; 176 177 $events = $sink->get_events(); 178 $sink->close(); 179 180 // Validate the event. 181 $this->assertCount(1, $events); 182 $event = $events[0]; 183 $updatedcohort = $DB->get_record('cohort', array('id'=>$id)); 184 $this->assertInstanceOf('\core\event\cohort_updated', $event); 185 $this->assertEquals('cohort', $event->objecttable); 186 $this->assertEquals($updatedcohort->id, $event->objectid); 187 $this->assertEquals($updatedcohort->contextid, $event->contextid); 188 $url = new moodle_url('/cohort/edit.php', array('id' => $event->objectid)); 189 $this->assertEquals($url, $event->get_url()); 190 $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id)); 191 $this->assertEventLegacyData($cohort, $event); 192 $this->assertEventContextNotUsed($event); 193 } 194 195 public function test_cohort_delete_cohort() { 196 global $DB; 197 198 $this->resetAfterTest(); 199 200 $cohort = $this->getDataGenerator()->create_cohort(); 201 202 cohort_delete_cohort($cohort); 203 204 $this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id))); 205 } 206 207 public function test_cohort_delete_cohort_event() { 208 209 $this->resetAfterTest(); 210 211 $cohort = $this->getDataGenerator()->create_cohort(); 212 213 // Capture the events. 214 $sink = $this->redirectEvents(); 215 216 // Perform the delete. 217 cohort_delete_cohort($cohort); 218 219 $events = $sink->get_events(); 220 $sink->close(); 221 222 // Validate the event structure. 223 $this->assertCount(1, $events); 224 $event = $events[0]; 225 $this->assertInstanceOf('\core\event\cohort_deleted', $event); 226 $this->assertEquals('cohort', $event->objecttable); 227 $this->assertEquals($cohort->id, $event->objectid); 228 $url = new moodle_url('/cohort/index.php', array('contextid' => $event->contextid)); 229 $this->assertEquals($url, $event->get_url()); 230 $this->assertEquals($cohort, $event->get_record_snapshot('cohort', $cohort->id)); 231 $this->assertEventLegacyData($cohort, $event); 232 $this->assertEventContextNotUsed($event); 233 } 234 235 public function test_cohort_delete_category() { 236 global $DB; 237 238 $this->resetAfterTest(); 239 240 $category = $this->getDataGenerator()->create_category(); 241 242 $cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category->id)->id)); 243 244 cohort_delete_category($category); 245 246 $this->assertTrue($DB->record_exists('cohort', array('id'=>$cohort->id))); 247 $newcohort = $DB->get_record('cohort', array('id'=>$cohort->id)); 248 $this->assertEquals(context_system::instance()->id, $newcohort->contextid); 249 } 250 251 public function test_cohort_add_member() { 252 global $DB; 253 254 $this->resetAfterTest(); 255 256 $cohort = $this->getDataGenerator()->create_cohort(); 257 $user = $this->getDataGenerator()->create_user(); 258 259 $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 260 cohort_add_member($cohort->id, $user->id); 261 $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 262 } 263 264 public function test_cohort_add_member_event() { 265 global $USER; 266 $this->resetAfterTest(); 267 268 // Setup the data. 269 $cohort = $this->getDataGenerator()->create_cohort(); 270 $user = $this->getDataGenerator()->create_user(); 271 272 // Capture the events. 273 $sink = $this->redirectEvents(); 274 275 // Peform the add member operation. 276 cohort_add_member($cohort->id, $user->id); 277 278 $events = $sink->get_events(); 279 $sink->close(); 280 281 // Validate the event. 282 $this->assertCount(1, $events); 283 $event = $events[0]; 284 $this->assertInstanceOf('\core\event\cohort_member_added', $event); 285 $this->assertEquals('cohort', $event->objecttable); 286 $this->assertEquals($cohort->id, $event->objectid); 287 $this->assertEquals($user->id, $event->relateduserid); 288 $this->assertEquals($USER->id, $event->userid); 289 $url = new moodle_url('/cohort/assign.php', array('id' => $event->objectid)); 290 $this->assertEquals($url, $event->get_url()); 291 $this->assertEventLegacyData((object) array('cohortid' => $cohort->id, 'userid' => $user->id), $event); 292 $this->assertEventContextNotUsed($event); 293 } 294 295 public function test_cohort_remove_member() { 296 global $DB; 297 298 $this->resetAfterTest(); 299 300 $cohort = $this->getDataGenerator()->create_cohort(); 301 $user = $this->getDataGenerator()->create_user(); 302 303 cohort_add_member($cohort->id, $user->id); 304 $this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 305 306 cohort_remove_member($cohort->id, $user->id); 307 $this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id))); 308 } 309 310 public function test_cohort_remove_member_event() { 311 global $USER; 312 $this->resetAfterTest(); 313 314 // Setup the data. 315 $cohort = $this->getDataGenerator()->create_cohort(); 316 $user = $this->getDataGenerator()->create_user(); 317 cohort_add_member($cohort->id, $user->id); 318 319 // Capture the events. 320 $sink = $this->redirectEvents(); 321 322 // Peform the remove operation. 323 cohort_remove_member($cohort->id, $user->id); 324 $events = $sink->get_events(); 325 $sink->close(); 326 327 // Validate the event. 328 $this->assertCount(1, $events); 329 $event = $events[0]; 330 $this->assertInstanceOf('\core\event\cohort_member_removed', $event); 331 $this->assertEquals('cohort', $event->objecttable); 332 $this->assertEquals($cohort->id, $event->objectid); 333 $this->assertEquals($user->id, $event->relateduserid); 334 $this->assertEquals($USER->id, $event->userid); 335 $url = new moodle_url('/cohort/assign.php', array('id' => $event->objectid)); 336 $this->assertEquals($url, $event->get_url()); 337 $this->assertEventLegacyData((object) array('cohortid' => $cohort->id, 'userid' => $user->id), $event); 338 $this->assertEventContextNotUsed($event); 339 } 340 341 public function test_cohort_is_member() { 342 global $DB; 343 344 $this->resetAfterTest(); 345 346 $cohort = $this->getDataGenerator()->create_cohort(); 347 $user = $this->getDataGenerator()->create_user(); 348 349 $this->assertFalse(cohort_is_member($cohort->id, $user->id)); 350 cohort_add_member($cohort->id, $user->id); 351 $this->assertTrue(cohort_is_member($cohort->id, $user->id)); 352 } 353 354 public function test_cohort_get_cohorts() { 355 global $DB; 356 357 $this->resetAfterTest(); 358 359 $category1 = $this->getDataGenerator()->create_category(); 360 $category2 = $this->getDataGenerator()->create_category(); 361 362 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>'')); 363 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr')); 364 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us')); 365 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id)); 366 367 $result = cohort_get_cohorts(context_coursecat::instance($category2->id)->id); 368 $this->assertEquals(0, $result['totalcohorts']); 369 $this->assertEquals(0, count($result['cohorts'])); 370 $this->assertEquals(0, $result['allcohorts']); 371 372 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id); 373 $this->assertEquals(3, $result['totalcohorts']); 374 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3), $result['cohorts']); 375 $this->assertEquals(3, $result['allcohorts']); 376 377 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'arrrgh'); 378 $this->assertEquals(1, $result['totalcohorts']); 379 $this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']); 380 $this->assertEquals(3, $result['allcohorts']); 381 382 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'brrr'); 383 $this->assertEquals(1, $result['totalcohorts']); 384 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']); 385 $this->assertEquals(3, $result['allcohorts']); 386 387 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'grrr'); 388 $this->assertEquals(1, $result['totalcohorts']); 389 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 390 $this->assertEquals(3, $result['allcohorts']); 391 392 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 1, 1, 'yyy'); 393 $this->assertEquals(3, $result['totalcohorts']); 394 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']); 395 $this->assertEquals(3, $result['allcohorts']); 396 397 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'po_us'); 398 $this->assertEquals(1, $result['totalcohorts']); 399 $this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']); 400 $this->assertEquals(3, $result['allcohorts']); 401 402 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 0, 100, 'pokus'); 403 $this->assertEquals(0, $result['totalcohorts']); 404 $this->assertEquals(array(), $result['cohorts']); 405 $this->assertEquals(3, $result['allcohorts']); 406 407 $result = cohort_get_cohorts(context_system::instance()->id); 408 $this->assertEquals(1, $result['totalcohorts']); 409 $this->assertEquals(array($cohort4->id=>$cohort4), $result['cohorts']); 410 $this->assertEquals(1, $result['allcohorts']); 411 } 412 413 public function test_cohort_get_all_cohorts() { 414 global $DB; 415 416 $this->resetAfterTest(); 417 418 $category1 = $this->getDataGenerator()->create_category(); 419 $category2 = $this->getDataGenerator()->create_category(); 420 421 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>'')); 422 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr')); 423 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($category2->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us')); 424 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_system::instance()->id)); 425 426 // Get list of all cohorts as admin. 427 $this->setAdminUser(); 428 429 $result = cohort_get_all_cohorts(0, 100, ''); 430 $this->assertEquals(4, $result['totalcohorts']); 431 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3, $cohort4->id=>$cohort4), $result['cohorts']); 432 $this->assertEquals(4, $result['allcohorts']); 433 434 $result = cohort_get_all_cohorts(0, 100, 'grrr'); 435 $this->assertEquals(1, $result['totalcohorts']); 436 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 437 $this->assertEquals(4, $result['allcohorts']); 438 439 // Get list of all cohorts as manager who has capability everywhere. 440 $user = $this->getDataGenerator()->create_user(); 441 $managerrole = $DB->get_record('role', array('shortname' => 'manager')); 442 role_assign($managerrole->id, $user->id, context_system::instance()->id); 443 $this->setUser($user); 444 445 $result = cohort_get_all_cohorts(0, 100, ''); 446 $this->assertEquals(4, $result['totalcohorts']); 447 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3, $cohort4->id=>$cohort4), $result['cohorts']); 448 $this->assertEquals(4, $result['allcohorts']); 449 450 $result = cohort_get_all_cohorts(0, 100, 'grrr'); 451 $this->assertEquals(1, $result['totalcohorts']); 452 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 453 $this->assertEquals(4, $result['allcohorts']); 454 455 // Get list of all cohorts as manager who has capability everywhere except category2. 456 $context2 = context_coursecat::instance($category2->id); 457 role_change_permission($managerrole->id, $context2, 'moodle/cohort:view', CAP_PROHIBIT); 458 role_change_permission($managerrole->id, $context2, 'moodle/cohort:manage', CAP_PROHIBIT); 459 $this->assertFalse(has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $context2)); 460 461 $result = cohort_get_all_cohorts(0, 100, ''); 462 $this->assertEquals(3, $result['totalcohorts']); 463 $this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort4->id=>$cohort4), $result['cohorts']); 464 $this->assertEquals(3, $result['allcohorts']); 465 466 $result = cohort_get_all_cohorts(0, 100, 'grrr'); 467 $this->assertEquals(1, $result['totalcohorts']); 468 $this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']); 469 $this->assertEquals(3, $result['allcohorts']); 470 471 $result = cohort_get_cohorts(context_coursecat::instance($category1->id)->id, 1, 1, 'yyy'); 472 $this->assertEquals(2, $result['totalcohorts']); 473 $this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']); 474 $this->assertEquals(2, $result['allcohorts']); 475 } 476 477 public function test_cohort_get_available_cohorts() { 478 global $DB; 479 480 $this->resetAfterTest(); 481 482 $category1 = $this->getDataGenerator()->create_category(); 483 $category2 = $this->getDataGenerator()->create_category(); 484 485 $course1 = $this->getDataGenerator()->create_course(array('category' => $category1->id)); 486 $course2 = $this->getDataGenerator()->create_course(array('category' => $category2->id)); 487 488 $category1ctx = context_coursecat::instance($category1->id); 489 $category2ctx = context_coursecat::instance($category2->id); 490 $course1ctx = context_course::instance(($course1->id)); 491 $course2ctx = context_course::instance(($course2->id)); 492 $systemctx = context_system::instance(); 493 494 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category1ctx->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>'')); 495 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category1ctx->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr', 'visible'=>0)); 496 $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category2ctx->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us')); 497 $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>$systemctx->id, 'name' => 'ddd')); 498 $cohort5 = $this->getDataGenerator()->create_cohort(array('contextid'=>$systemctx->id, 'visible'=>0, 'name' => 'eee')); 499 500 /* 501 Structure of generated course categories, courses and cohort: 502 503 system 504 -cohort4 (visible, has 3 members) 505 -cohort5 (not visible, no members) 506 category1 507 -cohort1 (visible, no members) 508 -cohort2 (not visible, has 1 member) 509 course1 510 category2 511 -cohort3 (visible, has 2 member) 512 course2 513 514 In this test we call cohort_get_available_cohorts() for users with different roles 515 and with different paramteres ($withmembers, $search, $offset, $limit) to make sure we go 516 through all possible options of SQL query. 517 */ 518 519 // Admin can see visible and invisible cohorts defined in above contexts. 520 $this->setAdminUser(); 521 522 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); 523 $this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id, $cohort5->id), array_keys($result)); 524 525 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 2, ''); 526 $this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result)); 527 528 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 2, ''); 529 $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); 530 531 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy'); 532 $this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result)); 533 534 $result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, ''); 535 $this->assertEquals(array($cohort3->id, $cohort4->id, $cohort5->id), array_keys($result)); 536 537 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY); 538 $this->assertEmpty($result); 539 540 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY); 541 $this->assertEmpty($result); 542 543 // Get list of available cohorts as a teacher in the course. 544 $user1 = $this->getDataGenerator()->create_user(); 545 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 546 role_assign($teacherrole->id, $user1->id, $course1ctx->id); 547 role_assign($teacherrole->id, $user1->id, $course2ctx->id); 548 $this->setUser($user1); 549 550 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); 551 $this->assertEquals(array($cohort1->id, $cohort4->id), array_keys($result)); 552 553 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 1, ''); 554 $this->assertEquals(array($cohort1->id), array_keys($result)); 555 556 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 1, ''); 557 $this->assertEquals(array($cohort4->id), array_keys($result)); 558 559 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy'); 560 $this->assertEquals(array($cohort1->id), array_keys($result)); 561 562 $result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, ''); 563 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 564 565 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY); 566 $this->assertEmpty($result); 567 568 // Now add members to cohorts. 569 $user2 = $this->getDataGenerator()->create_user(); 570 $user3 = $this->getDataGenerator()->create_user(); 571 $user4 = $this->getDataGenerator()->create_user(); 572 $user5 = $this->getDataGenerator()->create_user(); 573 $user6 = $this->getDataGenerator()->create_user(); 574 cohort_add_member($cohort2->id, $user3->id); 575 cohort_add_member($cohort3->id, $user2->id); 576 cohort_add_member($cohort3->id, $user3->id); 577 cohort_add_member($cohort4->id, $user4->id); 578 cohort_add_member($cohort4->id, $user5->id); 579 cohort_add_member($cohort4->id, $user6->id); 580 581 // Check filtering non-empty cohorts as admin. 582 $this->setAdminUser(); 583 584 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 585 $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); 586 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 587 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 588 589 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 590 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 591 $this->assertEquals(2, $result[$cohort3->id]->memberscnt); 592 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 593 594 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy'); 595 $this->assertEquals(array($cohort2->id), array_keys($result)); 596 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 597 598 // Check filtering non-empty cohorts as teacher. 599 $this->setUser($user1); 600 601 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 602 $this->assertEquals(array($cohort4->id), array_keys($result)); 603 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 604 605 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, ''); 606 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 607 $this->assertEquals(2, $result[$cohort3->id]->memberscnt); 608 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 609 610 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy'); 611 $this->assertEmpty($result); 612 613 // Enrol users. 614 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 615 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $studentrole->id); 616 $this->getDataGenerator()->enrol_user($user3->id, $course1->id, $studentrole->id); 617 $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $studentrole->id); 618 $this->getDataGenerator()->enrol_user($user6->id, $course1->id, $studentrole->id); 619 $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id); 620 $this->getDataGenerator()->enrol_user($user4->id, $course2->id, $studentrole->id); 621 $this->getDataGenerator()->enrol_user($user5->id, $course2->id, $studentrole->id); 622 $this->getDataGenerator()->enrol_user($user6->id, $course2->id, $studentrole->id); 623 624 // Check cohorts with enrolments as admin. 625 $this->setAdminUser(); 626 627 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, ''); 628 $this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result)); 629 $this->assertEquals(1, $result[$cohort2->id]->enrolledcnt); 630 $this->assertEquals(2, $result[$cohort4->id]->enrolledcnt); 631 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 632 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 633 634 $result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, ''); 635 $this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result)); 636 $this->assertEquals(1, $result[$cohort3->id]->enrolledcnt); 637 $this->assertEquals(3, $result[$cohort4->id]->enrolledcnt); 638 $this->assertEquals(2, $result[$cohort3->id]->memberscnt); 639 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 640 641 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, 'yyy'); 642 $this->assertEquals(array($cohort2->id), array_keys($result)); 643 $this->assertEquals(1, $result[$cohort2->id]->enrolledcnt); 644 $this->assertEquals(1, $result[$cohort2->id]->memberscnt); 645 646 $result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY, 0, 0, ''); 647 $this->assertEquals(array($cohort4->id), array_keys($result)); 648 $this->assertEquals(2, $result[$cohort4->id]->enrolledcnt); 649 $this->assertEquals(3, $result[$cohort4->id]->memberscnt); 650 651 // Assign user1 additional 'manager' role in the category context. He can now see hidden cohort in category1 652 // but still can not see hidden category in system. 653 $managerrole = $DB->get_record('role', array('shortname' => 'manager')); 654 role_assign($managerrole->id, $user1->id, context_coursecat::instance($category1->id)); 655 $this->setUser($user1); 656 $result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, ''); 657 $this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id), array_keys($result)); 658 } 659 660 /** 661 * Create a cohort with allowcohortthemes enabled/disabled. 662 */ 663 public function test_cohort_add_theme_cohort() { 664 global $DB; 665 666 $this->resetAfterTest(); 667 668 // Theme is added when allowcohortthemes is enabled. 669 set_config('allowcohortthemes', 1); 670 set_config('theme', 'boost'); 671 672 $systemctx = context_system::instance(); 673 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 1', 674 'idnumber' => 'testid1', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic')); 675 676 $id = cohort_add_cohort($cohort1); 677 $this->assertNotEmpty($id); 678 $newcohort = $DB->get_record('cohort', array('id' => $id)); 679 $this->assertEquals($cohort1->contextid, $newcohort->contextid); 680 $this->assertSame($cohort1->name, $newcohort->name); 681 $this->assertSame($cohort1->description, $newcohort->description); 682 $this->assertEquals($cohort1->descriptionformat, $newcohort->descriptionformat); 683 $this->assertNotEmpty($newcohort->theme); 684 $this->assertSame($cohort1->theme, $newcohort->theme); 685 $this->assertNotEmpty($newcohort->timecreated); 686 $this->assertSame($newcohort->component, ''); 687 $this->assertSame($newcohort->timecreated, $newcohort->timemodified); 688 689 // Theme is not added when allowcohortthemes is disabled. 690 set_config('allowcohortthemes', 0); 691 692 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 2', 693 'idnumber' => 'testid2', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic')); 694 695 $id = cohort_add_cohort($cohort2); 696 $this->assertNotEmpty($id); 697 $newcohort = $DB->get_record('cohort', array('id' => $id)); 698 $this->assertSame($cohort2->name, $newcohort->name); 699 $this->assertEmpty($newcohort->theme); 700 } 701 702 /** 703 * Update a cohort with allowcohortthemes enabled/disabled. 704 */ 705 public function test_cohort_update_theme_cohort() { 706 global $DB; 707 708 $this->resetAfterTest(); 709 710 // Enable cohort themes. 711 set_config('allowcohortthemes', 1); 712 set_config('theme', 'boost'); 713 714 $systemctx = context_system::instance(); 715 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 1', 716 'idnumber' => 'testid1', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic')); 717 $id = cohort_add_cohort($cohort1); 718 $this->assertNotEmpty($id); 719 720 // Theme is updated when allowcohortthemes is enabled. 721 $cohort1 = $DB->get_record('cohort', array('id' => $id)); 722 $cohort1->name = 'test cohort 1 updated'; 723 $cohort1->theme = 'classic'; 724 cohort_update_cohort($cohort1); 725 $updatedcohort = $DB->get_record('cohort', array('id' => $id)); 726 $this->assertEquals($cohort1->contextid, $updatedcohort->contextid); 727 $this->assertSame($cohort1->name, $updatedcohort->name); 728 $this->assertSame($cohort1->description, $updatedcohort->description); 729 $this->assertNotEmpty($updatedcohort->theme); 730 $this->assertSame($cohort1->theme, $updatedcohort->theme); 731 732 // Theme is not updated neither overwritten when allowcohortthemes is disabled. 733 set_config('allowcohortthemes', 0); 734 $cohort2 = $DB->get_record('cohort', array('id' => $id)); 735 $cohort2->theme = 'classic'; 736 cohort_update_cohort($cohort2); 737 $updatedcohort = $DB->get_record('cohort', array('id' => $id)); 738 $this->assertEquals($cohort2->contextid, $updatedcohort->contextid); 739 $this->assertNotEmpty($updatedcohort->theme); 740 $this->assertSame($cohort1->theme, $updatedcohort->theme); 741 } 742 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body