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