Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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