[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/cohort/tests/ -> cohortlib_test.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1