Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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