Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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