[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/tests/ -> grouplib_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   * Tests groups subsystems.
  19   *
  20   * @package    core_group
  21   * @category   phpunit
  22   * @copyright  2007 onwards Martin Dougiamas (http://dougiamas.com)
  23   * @author     Andrew Nicols
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Unit tests for lib/grouplib.php
  31   * @group core_group
  32   */
  33  class core_grouplib_testcase extends advanced_testcase {
  34  
  35      public function test_groups_get_group_by_idnumber() {
  36          $this->resetAfterTest(true);
  37  
  38          $generator = $this->getDataGenerator();
  39  
  40          // Create a course category and course.
  41          $cat = $generator->create_category(array('parent' => 0));
  42          $course = $generator->create_course(array('category' => $cat->id));
  43  
  44          $idnumber1 = 'idnumber1';
  45          $idnumber2 = 'idnumber2';
  46  
  47          /*
  48           * Test with an empty and a null idnumber.
  49           */
  50          // An empty idnumber should always return a false value.
  51          $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  52          $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  53  
  54          // Even when a group exists which also has an empty idnumber.
  55          $generator->create_group(array('courseid' => $course->id));
  56          $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  57          $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  58  
  59          /*
  60           * Test with a valid idnumber.
  61           */
  62          // There is no matching idnumber at present.
  63          $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1));
  64  
  65          // We should now have a valid group returned by the idnumber search.
  66          $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1));
  67          $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber1));
  68  
  69          // An empty idnumber should still return false.
  70          $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  71          $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  72  
  73          /*
  74           * Test with another idnumber.
  75           */
  76          // There is no matching idnumber at present.
  77          $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2));
  78  
  79          // We should now have a valid group returned by the idnumber search.
  80          $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
  81          $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber2));
  82  
  83          /*
  84           * Group idnumbers are unique within a course so test that we don't
  85           * retrieve groups for the first course.
  86           */
  87  
  88          // Create a second course.
  89          $course = $generator->create_course(array('category' => $cat->id));
  90  
  91          // An empty idnumber should always return a false value.
  92          $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  93          $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  94  
  95          // Our existing idnumbers shouldn't be returned here as we're in a different course.
  96          $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1));
  97          $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2));
  98  
  99          // We should be able to reuse the idnumbers again since this is a different course.
 100          $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1));
 101          $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber1));
 102  
 103          $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
 104          $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber2));
 105      }
 106  
 107      public function test_groups_get_grouping_by_idnumber() {
 108          $this->resetAfterTest(true);
 109  
 110          $generator = $this->getDataGenerator();
 111  
 112          // Create a course category and course.
 113          $cat = $generator->create_category(array('parent' => 0));
 114          $course = $generator->create_course(array('category' => $cat->id));
 115  
 116          $idnumber1 = 'idnumber1';
 117          $idnumber2 = 'idnumber2';
 118  
 119          /*
 120           * Test with an empty and a null idnumber.
 121           */
 122          // An empty idnumber should always return a false value.
 123          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
 124          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
 125  
 126          // Even when a grouping exists which also has an empty idnumber.
 127          $generator->create_grouping(array('courseid' => $course->id));
 128          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
 129          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
 130  
 131          /*
 132           * Test with a valid idnumber
 133           */
 134          // There is no matching idnumber at present.
 135          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
 136  
 137          // We should now have a valid group returned by the idnumber search.
 138          $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
 139          $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1));
 140  
 141          // An empty idnumber should still return false.
 142          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
 143          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
 144  
 145          /*
 146           * Test with another idnumber.
 147           */
 148          // There is no matching idnumber at present.
 149          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
 150  
 151          // We should now have a valid grouping returned by the idnumber search.
 152          $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
 153          $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
 154  
 155          /*
 156           * Grouping idnumbers are unique within a course so test that we don't
 157           * retrieve groupings for the first course.
 158           */
 159  
 160          // Create a second course.
 161          $course = $generator->create_course(array('category' => $cat->id));
 162  
 163          // An empty idnumber should always return a false value.
 164          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
 165          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
 166  
 167          // Our existing idnumbers shouldn't be returned here as we're in a different course.
 168          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
 169          $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
 170  
 171          // We should be able to reuse the idnumbers again since this is a different course.
 172          $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
 173          $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1));
 174  
 175          $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
 176          $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
 177      }
 178  
 179      public function test_groups_get_group_by_name() {
 180          $this->resetAfterTest(true);
 181  
 182          $generator = $this->getDataGenerator();
 183  
 184          // Create a course category and course.
 185          $cat = $generator->create_category(array('parent' => 0));
 186          $course = $generator->create_course(array('category' => $cat->id));
 187  
 188          $name1 = 'Name 1';
 189          $name2 = 'Name 2';
 190  
 191          // Test with an empty and a null idnumber.
 192          $this->assertFalse(groups_get_group_by_name($course->id, ''));
 193          $this->assertFalse(groups_get_group_by_name($course->id, null));
 194  
 195          // Even when a group exists.
 196          $generator->create_group(array('courseid' => $course->id));
 197          $this->assertFalse(groups_get_group_by_name($course->id, ''));
 198          $this->assertFalse(groups_get_group_by_name($course->id, null));
 199  
 200          // Test with a valid name, but one that doesn't exist yet.
 201          $this->assertFalse(groups_get_group_by_name($course->id, $name1));
 202          $this->assertFalse(groups_get_group_by_name($course->id, $name2));
 203  
 204          // We should now have a valid group returned by the name search.
 205          $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
 206          $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1));
 207          $this->assertFalse(groups_get_group_by_name($course->id, $name2));
 208  
 209          // We should now have a two valid groups returned by the name search.
 210          $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
 211          $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1));
 212          $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2));
 213  
 214          // Delete a group.
 215          $this->assertTrue(groups_delete_group($group1));
 216          $this->assertFalse(groups_get_group_by_name($course->id, $name1));
 217          $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2));
 218  
 219          /*
 220           * Group idnumbers are unique within a course so test that we don't
 221           * retrieve groups for the first course.
 222           */
 223  
 224          // Create a second course.
 225          $course = $generator->create_course(array('category' => $cat->id));
 226  
 227          // An empty name should always return a false value.
 228          $this->assertFalse(groups_get_group_by_name($course->id, ''));
 229          $this->assertFalse(groups_get_group_by_name($course->id, null));
 230  
 231          // Our existing names shouldn't be returned here as we're in a different course.
 232          $this->assertFalse(groups_get_group_by_name($course->id, $name1));
 233          $this->assertFalse(groups_get_group_by_name($course->id, $name2));
 234  
 235          // We should be able to reuse the idnumbers again since this is a different course.
 236          $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
 237          $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1));
 238  
 239          $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
 240          $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2));
 241      }
 242  
 243      public function test_groups_get_grouping() {
 244          $this->resetAfterTest(true);
 245  
 246          $generator = $this->getDataGenerator();
 247  
 248          // Create a course category and course.
 249          $cat = $generator->create_category(array('parent' => 0));
 250          $course = $generator->create_course(array('category' => $cat->id));
 251  
 252          $name1 = 'Grouping 1';
 253          $name2 = 'Grouping 2';
 254  
 255          // Test with an empty and a null idnumber.
 256          $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
 257          $this->assertFalse(groups_get_grouping_by_name($course->id, null));
 258  
 259          // Even when a group exists.
 260          $generator->create_group(array('courseid' => $course->id));
 261          $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
 262          $this->assertFalse(groups_get_grouping_by_name($course->id, null));
 263  
 264          // Test with a valid name, but one that doesn't exist yet.
 265          $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
 266          $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
 267  
 268          // We should now have a valid group returned by the name search.
 269          $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
 270          $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
 271          $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
 272  
 273          // We should now have a two valid groups returned by the name search.
 274          $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
 275          $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
 276          $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
 277  
 278          // Delete a group.
 279          $this->assertTrue(groups_delete_grouping($group1));
 280          $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
 281          $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
 282  
 283          /*
 284           * Group idnumbers are unique within a course so test that we don't
 285           * retrieve groups for the first course.
 286           */
 287  
 288          // Create a second course.
 289          $course = $generator->create_course(array('category' => $cat->id));
 290  
 291          // An empty name should always return a false value.
 292          $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
 293          $this->assertFalse(groups_get_grouping_by_name($course->id, null));
 294  
 295          // Our existing names shouldn't be returned here as we're in a different course.
 296          $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
 297          $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
 298  
 299          // We should be able to reuse the idnumbers again since this is a different course.
 300          $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
 301          $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
 302  
 303          $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
 304          $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
 305      }
 306  
 307      public function test_groups_get_course_data() {
 308          $this->resetAfterTest(true);
 309  
 310          $generator = $this->getDataGenerator();
 311  
 312          // Create a course category and course.
 313          $cat = $generator->create_category(array('parent' => 0));
 314          $course = $generator->create_course(array('category' => $cat->id));
 315          $grouping1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1'));
 316          $grouping2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 2'));
 317          $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
 318          $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
 319          $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
 320          $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
 321  
 322          // Assign the groups to groupings.
 323          $this->assertTrue(groups_assign_grouping($grouping1->id, $group1->id));
 324          $this->assertTrue(groups_assign_grouping($grouping1->id, $group2->id));
 325          $this->assertTrue(groups_assign_grouping($grouping2->id, $group3->id));
 326          $this->assertTrue(groups_assign_grouping($grouping2->id, $group4->id));
 327  
 328          // Get the data.
 329          $data = groups_get_course_data($course->id);
 330          $this->assertInstanceOf('stdClass', $data);
 331          $this->assertObjectHasAttribute('groups', $data);
 332          $this->assertObjectHasAttribute('groupings', $data);
 333          $this->assertObjectHasAttribute('mappings', $data);
 334  
 335          // Test we have the expected items returns.
 336          $this->assertCount(4, $data->groups);
 337          $this->assertCount(2, $data->groupings);
 338          $this->assertCount(4, $data->mappings);
 339  
 340          // Check we have the expected groups.
 341          $this->assertArrayHasKey($group1->id, $data->groups);
 342          $this->assertArrayHasKey($group2->id, $data->groups);
 343          $this->assertArrayHasKey($group3->id, $data->groups);
 344          $this->assertArrayHasKey($group4->id, $data->groups);
 345  
 346          // Test a group-id is mapped correctly.
 347          $this->assertSame($group3->name, $data->groups[$group3->id]->name);
 348  
 349          // Check we have the expected number of groupings.
 350          $this->assertContains($grouping1->id, array_keys($data->groupings));
 351          $this->assertContains($grouping2->id, array_keys($data->groupings));
 352  
 353          // Test a grouping-id is mapped correctly.
 354          $this->assertEquals($grouping2->name, $data->groupings[$grouping2->id]->name);
 355  
 356          // Test that all of the mappings are correct.
 357          $grouping1maps = 0;
 358          $grouping2maps = 0;
 359          $group1maps = 0;
 360          $group2maps = 0;
 361          $group3maps = 0;
 362          $group4maps = 0;
 363          foreach ($data->mappings as $mapping) {
 364              if ($mapping->groupingid === $grouping1->id) {
 365                  $grouping1maps++;
 366                  $this->assertContains($mapping->groupid, array($group1->id, $group2->id));
 367              } else if ($mapping->groupingid === $grouping2->id) {
 368                  $grouping2maps++;
 369                  $this->assertContains($mapping->groupid, array($group3->id, $group4->id));
 370              } else {
 371                  $this->fail('Unexpected groupingid');
 372              }
 373              switch ($mapping->groupid) {
 374                  case $group1->id : $group1maps++; break;
 375                  case $group2->id : $group2maps++; break;
 376                  case $group3->id : $group3maps++; break;
 377                  case $group4->id : $group4maps++; break;
 378              }
 379          }
 380          $this->assertEquals(2, $grouping1maps);
 381          $this->assertEquals(2, $grouping2maps);
 382          $this->assertEquals(1, $group1maps);
 383          $this->assertEquals(1, $group2maps);
 384          $this->assertEquals(1, $group3maps);
 385          $this->assertEquals(1, $group4maps);
 386  
 387          // Test the groups_get_all_groups which uses this functionality.
 388          $groups  = groups_get_all_groups($course->id);
 389          $groupkeys = array_keys($groups);
 390          $this->assertCount(4, $groups);
 391          $this->assertContains($group1->id, $groupkeys);
 392          $this->assertContains($group2->id, $groupkeys);
 393          $this->assertContains($group3->id, $groupkeys);
 394          $this->assertContains($group4->id, $groupkeys);
 395  
 396          $groups  = groups_get_all_groups($course->id, null, $grouping1->id);
 397          $groupkeys = array_keys($groups);
 398          $this->assertCount(2, $groups);
 399          $this->assertContains($group1->id, $groupkeys);
 400          $this->assertContains($group2->id, $groupkeys);
 401          $this->assertNotContains($group3->id, $groupkeys);
 402          $this->assertNotContains($group4->id, $groupkeys);
 403  
 404          $groups  = groups_get_all_groups($course->id, null, $grouping2->id);
 405          $groupkeys = array_keys($groups);
 406          $this->assertCount(2, $groups);
 407          $this->assertNotContains($group1->id, $groupkeys);
 408          $this->assertNotContains($group2->id, $groupkeys);
 409          $this->assertContains($group3->id, $groupkeys);
 410          $this->assertContains($group4->id, $groupkeys);
 411  
 412          // Test this function using an alternate column for the result index
 413          $groups  = groups_get_all_groups($course->id, null, $grouping2->id, 'g.name, g.id');
 414          $groupkeys = array_keys($groups);
 415          $this->assertCount(2, $groups);
 416          $this->assertNotContains($group3->id, $groupkeys);
 417          $this->assertContains($group3->name, $groupkeys);
 418          $this->assertEquals($group3->id, $groups[$group3->name]->id);
 419      }
 420  
 421      /**
 422       * Tests for groups_group_visible.
 423       */
 424      public function test_groups_group_visible() {
 425          global $CFG, $DB;
 426  
 427          $generator = $this->getDataGenerator();
 428          $this->resetAfterTest();
 429          $this->setAdminUser();
 430  
 431          // Create a course category, course and groups.
 432          $cat = $generator->create_category(array('parent' => 0));
 433          $course = $generator->create_course(array('category' => $cat->id));
 434          $coursecontext = context_course::instance($course->id);
 435          $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
 436          $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
 437          $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
 438          $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
 439  
 440          // Create cm.
 441          $assign = $generator->create_module("assign", array('course' => $course->id));
 442          $cm = get_coursemodule_from_instance("assign", $assign->id);
 443  
 444          // Create users.
 445          $user1 = $generator->create_user();
 446          $user2 = $generator->create_user();
 447          $user3 = $generator->create_user();
 448  
 449          // Enrol users into the course.
 450          $generator->enrol_user($user1->id, $course->id);
 451          $generator->enrol_user($user2->id, $course->id);
 452  
 453          // Assign groups.
 454          groups_add_member($group1, $user2);
 455  
 456          // Give capability at course level to the user to access all groups.
 457          $role = $DB->get_field("role", "id", array("shortname" => "manager"));
 458          $generator->enrol_user($user3->id, $course->id, $role);
 459          // Make sure the user has the capability.
 460          assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id);
 461  
 462          // No groups , not forced.
 463          $result = groups_group_visible($group1->id, $course, null, $user1->id);
 464          $this->assertTrue($result);
 465          $result = groups_group_visible(0, $course, null, $user1->id);
 466          $this->assertTrue($result); // Requesting all groups.
 467  
 468          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 469          $this->assertTrue($result); // Cm with no groups.
 470  
 471          $cm->groupmode = SEPARATEGROUPS;
 472          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 473          $this->assertFalse($result); // Cm with separate groups.
 474          $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
 475          $this->assertTrue($result); // Cm with separate groups.
 476  
 477          $cm->groupmode = VISIBLEGROUPS;
 478          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 479          $this->assertTrue($result); // Cm with visible groups.
 480  
 481          // No groups, forced.
 482          $course->groupmode = NOGROUPS;
 483          $course->groupmodeforce = true;
 484          update_course($course);
 485          $result = groups_group_visible($group1->id, $course, null, $user1->id);
 486          $this->assertTrue($result);
 487          $result = groups_group_visible(0, $course, null, $user1->id);
 488          $this->assertTrue($result); // Requesting all groups.
 489  
 490          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 491          $this->assertTrue($result); // Cm with no groups.
 492  
 493          $cm->groupmode = SEPARATEGROUPS;
 494          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 495          $this->assertTrue($result); // Cm with separate groups.
 496          $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
 497          $this->assertTrue($result); // Cm with separate groups.
 498  
 499          $cm->groupmode = SEPARATEGROUPS;
 500          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 501          $this->assertTrue($result); // Cm with visible groups.
 502  
 503          // Visible groups, forced.
 504          $course->groupmode = VISIBLEGROUPS;
 505          $course->groupmodeforce = true;
 506          update_course($course);
 507          $result = groups_group_visible($group1->id, $course, null, $user1->id);
 508          $this->assertTrue($result);
 509          $result = groups_group_visible(0, $course, null, $user1->id);
 510          $this->assertTrue($result); // Requesting all groups.
 511  
 512          $cm->groupmode = NOGROUPS;
 513          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 514          $this->assertTrue($result); // Cm with no groups.
 515  
 516          $cm->groupmode = SEPARATEGROUPS;
 517          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 518          $this->assertTrue($result); // Cm with separate groups.
 519          $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
 520          $this->assertTrue($result); // Cm with separate groups.
 521  
 522          $cm->groupmode = VISIBLEGROUPS;
 523          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 524          $this->assertTrue($result); // Cm with visible groups.
 525  
 526          // Visible groups, not forced.
 527          $course->groupmode = VISIBLEGROUPS;
 528          $course->groupmodeforce = false;
 529          update_course($course);
 530          $result = groups_group_visible($group1->id, $course, null, $user1->id);
 531          $this->assertTrue($result);
 532          $result = groups_group_visible(0, $course, null, $user1->id);
 533          $this->assertTrue($result); // Requesting all groups.
 534  
 535          $cm->groupmode = NOGROUPS;
 536          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 537          $this->assertTrue($result); // Cm with no groups.
 538  
 539          $cm->groupmode = SEPARATEGROUPS;
 540          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 541          $this->assertFalse($result); // Cm with separate groups.
 542          $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
 543          $this->assertTrue($result); // Cm with separate groups.
 544  
 545          $cm->groupmode = VISIBLEGROUPS;
 546          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 547          $this->assertTrue($result); // Cm with visible groups.
 548  
 549          // Separate groups, forced.
 550          $course->groupmode = SEPARATEGROUPS;
 551          $course->groupmodeforce = true;
 552          update_course($course);
 553          $result = groups_group_visible($group1->id, $course, null, $user1->id);
 554          $this->assertFalse($result);
 555          $result = groups_group_visible($group1->id, $course, null, $user2->id);
 556          $this->assertTrue($result);
 557          $result = groups_group_visible(0, $course, null, $user2->id);
 558          $this->assertFalse($result); // Requesting all groups.
 559          $result = groups_group_visible(0, $course, null, $user3->id);
 560          $this->assertTrue($result); // Requesting all groups.
 561          $result = groups_group_visible($group1->id, $course, null, $user3->id);
 562          $this->assertTrue($result); // Make sure user with access to all groups can see any group.
 563  
 564          $cm->groupmode = NOGROUPS;
 565          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 566          $this->assertFalse($result); // Cm with no groups.
 567  
 568          $cm->groupmode = SEPARATEGROUPS;
 569          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 570          $this->assertFalse($result); // Cm with separate groups.
 571          $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
 572          $this->assertTrue($result); // Cm with separate groups.
 573          $result = groups_group_visible($group1->id, $course, $cm, $user3->id);
 574          $this->assertTrue($result); // Make sure user with access to all groups can see any group.
 575  
 576          $cm->groupmode = VISIBLEGROUPS;
 577          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 578          $this->assertFalse($result); // Cm with visible groups.
 579  
 580          // Separate groups, not forced.
 581          $course->groupmode = SEPARATEGROUPS;
 582          $course->groupmodeforce = false;
 583          update_course($course);
 584          $result = groups_group_visible($group1->id, $course, null, $user1->id);
 585          $this->assertFalse($result);
 586          $result = groups_group_visible($group1->id, $course, null, $user2->id);
 587          $this->assertTrue($result);
 588          $result = groups_group_visible(0, $course, null, $user2->id);
 589          $this->assertFalse($result); // Requesting all groups.
 590          $result = groups_group_visible(0, $course, null, $user3->id);
 591          $this->assertTrue($result); // Requesting all groups.
 592  
 593          $cm->groupmode = NOGROUPS;
 594          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 595          $this->assertTrue($result); // Cm with no groups.
 596  
 597          $cm->groupmode = SEPARATEGROUPS;
 598          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 599          $this->assertFalse($result); // Cm with separate groups.
 600          $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
 601          $this->assertTrue($result); // Cm with separate groups.
 602  
 603          $cm->groupmode = VISIBLEGROUPS;
 604          $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
 605          $this->assertTrue($result); // Cm with visible groups.
 606      }
 607  
 608      function test_groups_get_groupmode() {
 609          global $DB;
 610          $generator = $this->getDataGenerator();
 611          $this->resetAfterTest();
 612          $this->setAdminUser();
 613  
 614          // Create a course with no groups forcing.
 615          $course1 = $generator->create_course();
 616  
 617          // Create cm1 with no groups, cm1 with visible groups, cm2 with separate groups and cm3 with visible groups.
 618          $assign1 = $generator->create_module("assign", array('course' => $course1->id));
 619          $assign2 = $generator->create_module("assign", array('course' => $course1->id),
 620                  array('groupmode' => SEPARATEGROUPS));
 621          $assign3 = $generator->create_module("assign", array('course' => $course1->id),
 622                  array('groupmode' => VISIBLEGROUPS));
 623  
 624          // Request data for tests.
 625          $cm1 = get_coursemodule_from_instance("assign", $assign1->id);
 626          $cm2 = get_coursemodule_from_instance("assign", $assign2->id);
 627          $cm3 = get_coursemodule_from_instance("assign", $assign3->id);
 628          $modinfo = get_fast_modinfo($course1->id);
 629  
 630          // Assert that any method of getting activity groupmode returns the correct result.
 631          $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1));
 632          $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1));
 633          $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
 634          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
 635          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
 636          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
 637          $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3));
 638          $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1));
 639          $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
 640  
 641          // Update the course set the groupmode SEPARATEGROUPS but not forced.
 642          update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS));
 643          // Re-request the data from DB.
 644          $course1 = $DB->get_record('course', array('id' => $course1->id));
 645          $modinfo = get_fast_modinfo($course1->id);
 646  
 647          // Existing activities are not changed.
 648          $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1));
 649          $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1));
 650          $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
 651          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
 652          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
 653          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
 654          $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3));
 655          $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1));
 656          $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
 657  
 658          // Update the course set the groupmode SEPARATEGROUPS and forced.
 659          update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => true));
 660          // Re-request the data from DB.
 661          $course1 = $DB->get_record('course', array('id' => $course1->id));
 662          $modinfo = get_fast_modinfo($course1->id);
 663  
 664          // Make sure all activities have separate groups mode now.
 665          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1));
 666          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1, $course1));
 667          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
 668          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
 669          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
 670          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
 671          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3));
 672          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3, $course1));
 673          $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
 674      }
 675  
 676      /**
 677       * Tests for groups_allgroups_course_menu() .
 678       */
 679      public function test_groups_allgroups_course_menu() {
 680          global $SESSION;
 681  
 682          $this->resetAfterTest();
 683  
 684          // Generate data.
 685          $course = $this->getDataGenerator()->create_course();
 686          $record = new stdClass();
 687          $record->courseid = $course->id;
 688          $group1 = $this->getDataGenerator()->create_group($record);
 689          $group2 = $this->getDataGenerator()->create_group($record);
 690          $user = $this->getDataGenerator()->create_user();
 691          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 692          $this->setUser($user);
 693  
 694          $html = groups_allgroups_course_menu($course, 'someurl.php');
 695          // Since user is not a part of this group and doesn't have accessallgroups permission,
 696          // the html should be empty.
 697          $this->assertEmpty($html);
 698  
 699          groups_add_member($group1->id, $user);
 700          // Now user can access one of the group. We can't assert an exact match here because of random ids generated by yui. So do
 701          // partial match to see if all groups are listed or not.
 702          $html = groups_allgroups_course_menu($course, 'someurl.php');
 703          $this->assertContains(format_string($group1->name), $html);
 704          $this->assertNotContains(format_string($group2->name), $html);
 705  
 706          $this->setAdminUser();
 707  
 708          // Now user can access everything.
 709          $html = groups_allgroups_course_menu($course, 'someurl.php');
 710          $this->assertContains(format_string($group1->name), $html);
 711          $this->assertContains(format_string($group2->name), $html);
 712  
 713          // Make sure separate groups mode, doesn't change anything.
 714          $course->groupmode = SEPARATEGROUPS;
 715          update_course($course);
 716          $html = groups_allgroups_course_menu($course, 'someurl.php');
 717          $this->assertContains(format_string($group1->name), $html);
 718          $this->assertContains(format_string($group2->name), $html);
 719  
 720          // Make sure Visible groups mode, doesn't change anything.
 721          $course->groupmode = VISIBLEGROUPS;
 722          update_course($course);
 723          $html = groups_allgroups_course_menu($course, 'someurl.php');
 724          $this->assertContains(format_string($group1->name), $html);
 725          $this->assertContains(format_string($group2->name), $html);
 726  
 727          // Let us test activegroup changes now.
 728          $this->setUser($user);
 729          $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid] = 5;
 730          groups_allgroups_course_menu($course, 'someurl.php', false); // Do not update session.
 731          $this->assertSame(5, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
 732          groups_allgroups_course_menu($course, 'someurl.php', true, $group1->id); // Update session.
 733          $this->assertSame($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
 734          // Try to update session with an invalid groupid. It should not accept the invalid id.
 735          groups_allgroups_course_menu($course, 'someurl.php', true, 256);
 736          $this->assertEquals($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
 737      }
 738  
 739      /**
 740       * This unit test checks that groups_get_all_groups returns groups in
 741       * alphabetical order even if they are in a grouping.
 742       */
 743      public function test_groups_ordering() {
 744          $generator = $this->getDataGenerator();
 745          $this->resetAfterTest();
 746  
 747          // Create a course category and course.
 748          $cat = $generator->create_category(array('parent' => 0));
 749          $course = $generator->create_course(array('category' => $cat->id));
 750          $grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping'));
 751  
 752          // Create groups in reverse order.
 753          $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
 754          $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
 755  
 756          // Assign the groups to the grouping in reverse order.
 757          $this->assertTrue(groups_assign_grouping($grouping->id, $group2->id));
 758          $this->assertTrue(groups_assign_grouping($grouping->id, $group1->id));
 759  
 760          // Get all groups and check they are alphabetical.
 761          $groups = array_values(groups_get_all_groups($course->id, 0));
 762          $this->assertEquals('Group 1', $groups[0]->name);
 763          $this->assertEquals('Group 2', $groups[1]->name);
 764  
 765          // Now check the same is true when accessed by grouping.
 766          $groups = array_values(groups_get_all_groups($course->id, 0, $grouping->id));
 767          $this->assertEquals('Group 1', $groups[0]->name);
 768          $this->assertEquals('Group 2', $groups[1]->name);
 769      }
 770  
 771      /**
 772       * Tests for groups_get_user_groups() method.
 773       */
 774      public function test_groups_get_user_groups() {
 775          $this->resetAfterTest(true);
 776          $generator = $this->getDataGenerator();
 777  
 778          // Create courses.
 779          $course1 = $generator->create_course();
 780          $course2 = $generator->create_course();
 781  
 782          // Create users.
 783          $user1 = $generator->create_user();
 784          $user2 = $generator->create_user();
 785          $user3 = $generator->create_user();
 786  
 787          // Enrol users.
 788          $generator->enrol_user($user1->id, $course1->id);
 789          $generator->enrol_user($user1->id, $course2->id);
 790          $generator->enrol_user($user2->id, $course2->id);
 791          $generator->enrol_user($user3->id, $course2->id);
 792  
 793          // Create groups.
 794          $group1 = $generator->create_group(array('courseid' => $course1->id));
 795          $group2 = $generator->create_group(array('courseid' => $course2->id));
 796          $group3 = $generator->create_group(array('courseid' => $course2->id));
 797  
 798          // Assign users to groups.
 799          $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id)));
 800          $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id)));
 801  
 802          // Get user groups.
 803          $usergroups1 = groups_get_user_groups($course1->id, $user1->id);
 804          $usergroups2 = groups_get_user_groups($course2->id, $user2->id);;
 805  
 806          // Assert return data.
 807          $this->assertEquals($group1->id, $usergroups1[0][0]);
 808          $this->assertEquals($group2->id, $usergroups2[0][0]);
 809  
 810          // Now, test with groupings.
 811          $grouping1 = $generator->create_grouping(array('courseid' => $course1->id));
 812          $grouping2 = $generator->create_grouping(array('courseid' => $course2->id));
 813  
 814          // Assign the groups to grouping.
 815          groups_assign_grouping($grouping1->id, $group1->id);
 816          groups_assign_grouping($grouping2->id, $group2->id);
 817          groups_assign_grouping($grouping2->id, $group3->id);
 818  
 819          // Test with grouping.
 820          $usergroups1 = groups_get_user_groups($course1->id, $user1->id);
 821          $usergroups2 = groups_get_user_groups($course2->id, $user2->id);
 822          $this->assertArrayHasKey($grouping1->id, $usergroups1);
 823          $this->assertArrayHasKey($grouping2->id, $usergroups2);
 824  
 825          // Test user without a group.
 826          $usergroups1 = groups_get_user_groups($course2->id, $user3->id);
 827          $this->assertCount(0, $usergroups1[0]);
 828  
 829          // Test with userid = 0.
 830          $usergroups1 = groups_get_user_groups($course1->id, 0);
 831          $usergroups2 = groups_get_user_groups($course2->id, 0);
 832          $this->assertCount(0, $usergroups1[0]);
 833          $this->assertCount(0, $usergroups2[0]);
 834  
 835          // Test with courseid = 0.
 836          $usergroups1 = groups_get_user_groups(0, $user1->id);
 837          $usergroups2 = groups_get_user_groups(0, $user2->id);
 838          $this->assertCount(0, $usergroups1[0]);
 839          $this->assertCount(0, $usergroups2[0]);
 840      }
 841  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1