Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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      public function test_find_users_only_return_group_non_member() {
  40          $this->resetAfterTest();
  41  
  42          // Create course.
  43          $course = $this->getDataGenerator()->create_course();
  44  
  45          // Create users.
  46          $user1 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '1']);
  47          $user2 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '2']);
  48          $user3 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '3']);
  49          $user4 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '4']);
  50          $user5 = $this->getDataGenerator()->create_user(['firstname' => 'User', 'lastname' => '5']);
  51  
  52          // Create group.
  53          $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  54  
  55          // Enroll users to course. Except User5.
  56          $this->getDataGenerator()->enrol_user($user1->id, $course->id);
  57          $this->getDataGenerator()->enrol_user($user2->id, $course->id);
  58          $this->getDataGenerator()->enrol_user($user3->id, $course->id);
  59          $this->getDataGenerator()->enrol_user($user4->id, $course->id);
  60  
  61          // Assign User1 to Group.
  62          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user1->id]);
  63  
  64          // User1 and User5 will not exist in the result.
  65          // User2, User3 and User4 will exist in the result.
  66          $potentialmembersselector = new group_non_members_selector('addselect',
  67                  ['groupid' => $group->id, 'courseid' => $course->id]);
  68          foreach ($potentialmembersselector->find_users('User') as $found) {
  69              $this->assertCount(3, $found);
  70              $this->assertArrayNotHasKey($user5->id, $found);
  71              $this->assertArrayNotHasKey($user1->id, $found);
  72              $this->assertArrayHasKey($user2->id, $found);
  73              $this->assertArrayHasKey($user3->id, $found);
  74              $this->assertArrayHasKey($user4->id, $found);
  75          }
  76  
  77          // Assign User2 to Group.
  78          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user2->id]);
  79  
  80          // User1, User2 and User5 will not exist in the result.
  81          // User3 and User4 will exist in the result.
  82          $potentialmembersselector = new group_non_members_selector('addselect',
  83                  ['groupid' => $group->id, 'courseid' => $course->id]);
  84          foreach ($potentialmembersselector->find_users('User') as $found) {
  85              $this->assertCount(2, $found);
  86              $this->assertArrayNotHasKey($user5->id, $found);
  87              $this->assertArrayNotHasKey($user1->id, $found);
  88              $this->assertArrayNotHasKey($user2->id, $found);
  89              $this->assertArrayHasKey($user3->id, $found);
  90              $this->assertArrayHasKey($user4->id, $found);
  91          }
  92  
  93          // Assign User3 to Group.
  94          $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user3->id]);
  95  
  96          // User1, User2, User3 and User5 will not exist in the result.
  97          // Only User4 will exist in the result.
  98          $potentialmembersselector = new group_non_members_selector('addselect',
  99                  ['groupid' => $group->id, 'courseid' => $course->id]);
 100          foreach ($potentialmembersselector->find_users('User') as $found) {
 101              $this->assertCount(1, $found);
 102              $this->assertArrayNotHasKey($user5->id, $found);
 103              $this->assertArrayNotHasKey($user1->id, $found);
 104              $this->assertArrayNotHasKey($user2->id, $found);
 105              $this->assertArrayNotHasKey($user3->id, $found);
 106              $this->assertArrayHasKey($user4->id, $found);
 107          }
 108      }
 109  
 110  }