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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  // This file is part of Moodle - https://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   * Unit tests for {@link group_non_members_selector} class.
  19   *
  20   * @package     core_user
  21   * @category    test
  22   * @copyright   2019 Huong Nguyen <huongnv13@gmail.com>
  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 . '/user/selector/lib.php');
  30  
  31  /**
  32   * Unit tests for {@link group_non_members_selector} class.
  33   *
  34   * @package     core_user
  35   * @category    test
  36   * @copyright   2019 Huong Nguyen <huongnv13@gmail.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class core_group_non_members_selector_testcase extends advanced_testcase {
  40  
  41      /**
  42       * Test find_users that only return group non members
  43       *
  44       * @throws coding_exception
  45       */
  46      public function test_find_users_only_return_group_non_member() {
  47          $this->resetAfterTest();
  48  
  49          // Create course.
  50          $course = $this->getDataGenerator()->create_course();
  51  
  52          // Create users.
  53          $user1 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '1']);
  54          $user2 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '2']);
  55          $user3 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '3']);
  56          $user4 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '4']);
  57          $user5 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '5']);
  58  
  59          // Create group.
  60          $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  61  
  62          // Enroll users to course. Except User5.
  63          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
  64          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
  65          $this->getDataGenerator()->enrol_user($user3->id, $course->id);
  66          $this->getDataGenerator()->enrol_user($user4->id, $course->id);
  67  
  68          // Assign User1 to Group.
  69          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user1->id]);
  70  
  71          // User1 and User5 will not exist in the result.
  72          // User2, User3 and User4 will exist in the result.
  73          $potentialmembersselector = new group_non_members_selector('addselect',
  74                  ['groupid' => $group->id, 'courseid' => $course->id]);
  75          foreach ($potentialmembersselector->find_users('User') as $found) {
  76              $this->assertCount(3, $found);
  77              $this->assertArrayNotHasKey($user5->id, $found);
  78              $this->assertArrayNotHasKey($user1->id, $found);
  79              $this->assertArrayHasKey($user2->id, $found);
  80              $this->assertArrayHasKey($user3->id, $found);
  81              $this->assertArrayHasKey($user4->id, $found);
  82          }
  83  
  84          // Assign User2 to Group.
  85          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user2->id]);
  86  
  87          // User1, User2 and User5 will not exist in the result.
  88          // User3 and User4 will exist in the result.
  89          $potentialmembersselector = new group_non_members_selector('addselect',
  90                  ['groupid' => $group->id, 'courseid' => $course->id]);
  91          foreach ($potentialmembersselector->find_users('User') as $found) {
  92              $this->assertCount(2, $found);
  93              $this->assertArrayNotHasKey($user5->id, $found);
  94              $this->assertArrayNotHasKey($user1->id, $found);
  95              $this->assertArrayNotHasKey($user2->id, $found);
  96              $this->assertArrayHasKey($user3->id, $found);
  97              $this->assertArrayHasKey($user4->id, $found);
  98          }
  99  
 100          // Assign User3 to Group.
 101          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user3->id]);
 102  
 103          // User1, User2, User3 and User5 will not exist in the result.
 104          // Only User4 will exist in the result.
 105          $potentialmembersselector = new group_non_members_selector('addselect',
 106                  ['groupid' => $group->id, 'courseid' => $course->id]);
 107          foreach ($potentialmembersselector->find_users('User') as $found) {
 108              $this->assertCount(1, $found);
 109              $this->assertArrayNotHasKey($user5->id, $found);
 110              $this->assertArrayNotHasKey($user1->id, $found);
 111              $this->assertArrayNotHasKey($user2->id, $found);
 112              $this->assertArrayNotHasKey($user3->id, $found);
 113              $this->assertArrayHasKey($user4->id, $found);
 114          }
 115      }
 116  
 117  }