MDL-74463 enrol_lti: fix location of scopes in registration json
[moodle.git] / user / tests / group_non_members_selector_test.php
bloba4837f88fa4dd56491c4ed65242897ffea7586e7
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.
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/>.
17 /**
18 * Unit tests for {@link group_non_members_selector} class.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/user/selector/lib.php');
31 /**
32 * Unit tests for {@link group_non_members_selector} class.
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
39 class core_group_non_members_selector_testcase extends advanced_testcase {
41 /**
42 * Test find_users that only return group non members
44 * @throws coding_exception
46 public function test_find_users_only_return_group_non_member() {
47 $this->resetAfterTest();
49 // Create course.
50 $course = $this->getDataGenerator()->create_course();
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']);
59 // Create group.
60 $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
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);
68 // Assign User1 to Group.
69 $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user1->id]);
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);
84 // Assign User2 to Group.
85 $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user2->id]);
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);
100 // Assign User3 to Group.
101 $this->getDataGenerator()->create_group_member(['groupid' => $group->id, 'userid' => $user3->id]);
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);