1 |
efrain |
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 |
namespace core_enrol;
|
|
|
18 |
|
|
|
19 |
use context_course;
|
|
|
20 |
use course_enrolment_manager;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Test course_enrolment_manager parts.
|
|
|
25 |
*
|
|
|
26 |
* @package core_enrol
|
|
|
27 |
* @category test
|
|
|
28 |
* @copyright 2016 Ruslan Kabalin, Lancaster University
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
* @covers \course_enrolment_manager
|
|
|
31 |
*/
|
|
|
32 |
class course_enrolment_manager_test extends \advanced_testcase {
|
|
|
33 |
/**
|
|
|
34 |
* The course used in tests.
|
|
|
35 |
* @var \stdClass
|
|
|
36 |
*/
|
|
|
37 |
private $course = null;
|
|
|
38 |
/**
|
|
|
39 |
* List of users used in tests.
|
|
|
40 |
* @var array
|
|
|
41 |
*/
|
|
|
42 |
private $users = array();
|
|
|
43 |
/**
|
|
|
44 |
* List of groups used in tests.
|
|
|
45 |
* @var array
|
|
|
46 |
*/
|
|
|
47 |
private $groups = array();
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Tests set up
|
|
|
51 |
*/
|
|
|
52 |
protected function setUp(): void {
|
|
|
53 |
global $CFG;
|
|
|
54 |
require_once($CFG->dirroot . '/enrol/locallib.php');
|
|
|
55 |
$this->setAdminUser();
|
|
|
56 |
|
|
|
57 |
$users = array();
|
|
|
58 |
$groups = array();
|
|
|
59 |
// Create the course and the users.
|
|
|
60 |
$course = $this->getDataGenerator()->create_course();
|
|
|
61 |
$users['user0'] = $this->getDataGenerator()->create_user(
|
|
|
62 |
array('username' => 'user0', 'firstname' => 'user0')); // A user without group.
|
|
|
63 |
$users['user1'] = $this->getDataGenerator()->create_user(
|
|
|
64 |
array('username' => 'user1', 'firstname' => 'user1')); // User for group 1.
|
|
|
65 |
$users['user21'] = $this->getDataGenerator()->create_user(
|
|
|
66 |
array('username' => 'user21', 'firstname' => 'user21')); // Two users for group 2.
|
|
|
67 |
$users['user22'] = $this->getDataGenerator()->create_user(
|
|
|
68 |
array('username' => 'user22', 'firstname' => 'user22'));
|
|
|
69 |
$users['userall'] = $this->getDataGenerator()->create_user(
|
|
|
70 |
array('username' => 'userall', 'firstname' => 'userall')); // A user in all groups.
|
|
|
71 |
$users['usertch'] = $this->getDataGenerator()->create_user(
|
|
|
72 |
array('username' => 'usertch', 'firstname' => 'usertch')); // A user with teacher role.
|
|
|
73 |
|
|
|
74 |
// Enrol the users in the course.
|
|
|
75 |
$this->getDataGenerator()->enrol_user($users['user0']->id, $course->id, 'student'); // Student.
|
|
|
76 |
$this->getDataGenerator()->enrol_user($users['user1']->id, $course->id, 'student'); // Student.
|
|
|
77 |
$this->getDataGenerator()->enrol_user($users['user21']->id, $course->id, 'student'); // Student.
|
|
|
78 |
$this->getDataGenerator()->enrol_user($users['user22']->id, $course->id, 'student', 'manual', 0, 0, ENROL_USER_SUSPENDED); // Suspended student.
|
|
|
79 |
$this->getDataGenerator()->enrol_user($users['userall']->id, $course->id, 'student'); // Student.
|
|
|
80 |
$this->getDataGenerator()->enrol_user($users['usertch']->id, $course->id, 'editingteacher'); // Teacher.
|
|
|
81 |
|
|
|
82 |
// Create 2 groups.
|
|
|
83 |
$groups['group1'] = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
84 |
$groups['group2'] = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
|
|
85 |
|
|
|
86 |
// Add the users to the groups.
|
|
|
87 |
$this->getDataGenerator()->create_group_member(
|
|
|
88 |
array('groupid' => $groups['group1']->id, 'userid' => $users['user1']->id));
|
|
|
89 |
$this->getDataGenerator()->create_group_member(
|
|
|
90 |
array('groupid' => $groups['group2']->id, 'userid' => $users['user21']->id));
|
|
|
91 |
$this->getDataGenerator()->create_group_member(
|
|
|
92 |
array('groupid' => $groups['group2']->id, 'userid' => $users['user22']->id));
|
|
|
93 |
$this->getDataGenerator()->create_group_member(
|
|
|
94 |
array('groupid' => $groups['group1']->id, 'userid' => $users['userall']->id));
|
|
|
95 |
$this->getDataGenerator()->create_group_member(
|
|
|
96 |
array('groupid' => $groups['group2']->id, 'userid' => $users['userall']->id));
|
|
|
97 |
|
|
|
98 |
// Make setup data accessible from test methods.
|
|
|
99 |
$this->course = $course;
|
|
|
100 |
$this->users = $users;
|
|
|
101 |
$this->groups = $groups;
|
|
|
102 |
|
|
|
103 |
// Make sample users and not enroll to any course.
|
|
|
104 |
$this->getDataGenerator()->create_user([
|
|
|
105 |
'username' => 'testapiuser1',
|
|
|
106 |
'firstname' => 'testapiuser 1'
|
|
|
107 |
]);
|
|
|
108 |
$this->getDataGenerator()->create_user([
|
|
|
109 |
'username' => 'testapiuser2',
|
|
|
110 |
'firstname' => 'testapiuser 2'
|
|
|
111 |
]);
|
|
|
112 |
$this->getDataGenerator()->create_user([
|
|
|
113 |
'username' => 'testapiuser3',
|
|
|
114 |
'firstname' => 'testapiuser 3'
|
|
|
115 |
]);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Verify get_total_users() returned number of users expected in every situation.
|
|
|
120 |
*/
|
11 |
efrain |
121 |
public function test_get_total_users(): void {
|
1 |
efrain |
122 |
global $PAGE;
|
|
|
123 |
|
|
|
124 |
$this->resetAfterTest();
|
|
|
125 |
|
|
|
126 |
// All users filtering.
|
|
|
127 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
128 |
$totalusers = $manager->get_total_users();
|
|
|
129 |
$this->assertEquals(6, $totalusers, 'All users must be returned when no filtering is applied.');
|
|
|
130 |
|
|
|
131 |
// Student role filtering.
|
|
|
132 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 5);
|
|
|
133 |
$totalusers = $manager->get_total_users();
|
|
|
134 |
$this->assertEquals(5, $totalusers, 'Only students must be returned when student role filtering is applied.');
|
|
|
135 |
|
|
|
136 |
// Teacher role filtering.
|
|
|
137 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 3);
|
|
|
138 |
$totalusers = $manager->get_total_users();
|
|
|
139 |
$this->assertEquals(1, $totalusers, 'Only teacher must be returned when teacher role filtering is applied.');
|
|
|
140 |
|
|
|
141 |
// Search user filtering.
|
|
|
142 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, 'userall');
|
|
|
143 |
$totalusers = $manager->get_total_users();
|
|
|
144 |
$this->assertEquals(1, $totalusers, 'Only searchable user must be returned when search filtering is applied.');
|
|
|
145 |
|
|
|
146 |
// Group 1 filtering.
|
|
|
147 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group1']->id);
|
|
|
148 |
$totalusers = $manager->get_total_users();
|
|
|
149 |
$this->assertEquals(2, $totalusers, 'Only group members must be returned when group filtering is applied.');
|
|
|
150 |
|
|
|
151 |
// Group 2 filtering.
|
|
|
152 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group2']->id);
|
|
|
153 |
$totalusers = $manager->get_total_users();
|
|
|
154 |
$this->assertEquals(3, $totalusers, 'Only group members must be returned when group filtering is applied.');
|
|
|
155 |
|
|
|
156 |
// 'No groups' filtering.
|
|
|
157 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', -1);
|
|
|
158 |
$totalusers = $manager->get_total_users();
|
|
|
159 |
$this->assertEquals(2, $totalusers, 'Only non-group members must be returned when \'no groups\' filtering is applied.');
|
|
|
160 |
|
|
|
161 |
// Active users filtering.
|
|
|
162 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_ACTIVE);
|
|
|
163 |
$totalusers = $manager->get_total_users();
|
|
|
164 |
$this->assertEquals(5, $totalusers, 'Only active users must be returned when active users filtering is applied.');
|
|
|
165 |
|
|
|
166 |
// Suspended users filtering.
|
|
|
167 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_SUSPENDED);
|
|
|
168 |
$totalusers = $manager->get_total_users();
|
|
|
169 |
$this->assertEquals(1, $totalusers, 'Only suspended users must be returned when suspended users filtering is applied.');
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Verify get_users() returned number of users expected in every situation.
|
|
|
174 |
*/
|
11 |
efrain |
175 |
public function test_get_users(): void {
|
1 |
efrain |
176 |
global $PAGE;
|
|
|
177 |
|
|
|
178 |
$this->resetAfterTest();
|
|
|
179 |
|
|
|
180 |
// All users filtering.
|
|
|
181 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
182 |
$users = $manager->get_users('id');
|
|
|
183 |
$this->assertCount(6, $users, 'All users must be returned when no filtering is applied.');
|
|
|
184 |
$this->assertArrayHasKey($this->users['user0']->id, $users);
|
|
|
185 |
$this->assertArrayHasKey($this->users['user1']->id, $users);
|
|
|
186 |
$this->assertArrayHasKey($this->users['user21']->id, $users);
|
|
|
187 |
$this->assertArrayHasKey($this->users['user22']->id, $users);
|
|
|
188 |
$this->assertArrayHasKey($this->users['userall']->id, $users);
|
|
|
189 |
$this->assertArrayHasKey($this->users['usertch']->id, $users);
|
|
|
190 |
|
|
|
191 |
// Student role filtering.
|
|
|
192 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 5);
|
|
|
193 |
$users = $manager->get_users('id');
|
|
|
194 |
$this->assertCount(5, $users, 'Only students must be returned when student role filtering is applied.');
|
|
|
195 |
$this->assertArrayHasKey($this->users['user0']->id, $users);
|
|
|
196 |
$this->assertArrayHasKey($this->users['user1']->id, $users);
|
|
|
197 |
$this->assertArrayHasKey($this->users['user21']->id, $users);
|
|
|
198 |
$this->assertArrayHasKey($this->users['user22']->id, $users);
|
|
|
199 |
$this->assertArrayHasKey($this->users['userall']->id, $users);
|
|
|
200 |
|
|
|
201 |
// Teacher role filtering.
|
|
|
202 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 3);
|
|
|
203 |
$users = $manager->get_users('id');
|
|
|
204 |
$this->assertCount(1, $users, 'Only teacher must be returned when teacher role filtering is applied.');
|
|
|
205 |
$this->assertArrayHasKey($this->users['usertch']->id, $users);
|
|
|
206 |
|
|
|
207 |
// Search user filtering.
|
|
|
208 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, 'userall');
|
|
|
209 |
$users = $manager->get_users('id');
|
|
|
210 |
$this->assertCount(1, $users, 'Only searchable user must be returned when search filtering is applied.');
|
|
|
211 |
$this->assertArrayHasKey($this->users['userall']->id, $users);
|
|
|
212 |
|
|
|
213 |
// Group 1 filtering.
|
|
|
214 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group1']->id);
|
|
|
215 |
$users = $manager->get_users('id');
|
|
|
216 |
$this->assertCount(2, $users, 'Only group members must be returned when group filtering is applied.');
|
|
|
217 |
$this->assertArrayHasKey($this->users['user1']->id, $users);
|
|
|
218 |
$this->assertArrayHasKey($this->users['userall']->id, $users);
|
|
|
219 |
|
|
|
220 |
// Group 2 filtering.
|
|
|
221 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group2']->id);
|
|
|
222 |
$users = $manager->get_users('id');
|
|
|
223 |
$this->assertCount(3, $users, 'Only group members must be returned when group filtering is applied.');
|
|
|
224 |
$this->assertArrayHasKey($this->users['user21']->id, $users);
|
|
|
225 |
$this->assertArrayHasKey($this->users['user22']->id, $users);
|
|
|
226 |
$this->assertArrayHasKey($this->users['userall']->id, $users);
|
|
|
227 |
|
|
|
228 |
// 'No groups' filtering.
|
|
|
229 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', -1);
|
|
|
230 |
$users = $manager->get_users('id');
|
|
|
231 |
$this->assertCount(2, $users, 'Only non-group members must be returned when \'no groups\' filtering is applied.');
|
|
|
232 |
$this->assertArrayHasKey($this->users['user0']->id, $users);
|
|
|
233 |
$this->assertArrayHasKey($this->users['usertch']->id, $users);
|
|
|
234 |
|
|
|
235 |
// Active users filtering.
|
|
|
236 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_ACTIVE);
|
|
|
237 |
$users = $manager->get_users('id');
|
|
|
238 |
$this->assertCount(5, $users, 'Only active users must be returned when active users filtering is applied.');
|
|
|
239 |
$this->assertArrayHasKey($this->users['user0']->id, $users);
|
|
|
240 |
$this->assertArrayHasKey($this->users['user1']->id, $users);
|
|
|
241 |
$this->assertArrayHasKey($this->users['user21']->id, $users);
|
|
|
242 |
$this->assertArrayHasKey($this->users['userall']->id, $users);
|
|
|
243 |
$this->assertArrayHasKey($this->users['usertch']->id, $users);
|
|
|
244 |
|
|
|
245 |
// Suspended users filtering.
|
|
|
246 |
$manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_SUSPENDED);
|
|
|
247 |
$users = $manager->get_users('id');
|
|
|
248 |
$this->assertCount(1, $users, 'Only suspended users must be returned when suspended users filtering is applied.');
|
|
|
249 |
$this->assertArrayHasKey($this->users['user22']->id, $users);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* Sets up a custom profile field and the showuseridentity option, and creates a test user
|
|
|
254 |
* with suitable values set.
|
|
|
255 |
*
|
|
|
256 |
* @return \stdClass Test user
|
|
|
257 |
*/
|
|
|
258 |
protected function setup_for_user_identity_tests(): \stdClass {
|
|
|
259 |
// Configure extra fields to include one normal user field and one profile field, and
|
|
|
260 |
// set the values for a new test user.
|
|
|
261 |
$generator = $this->getDataGenerator();
|
|
|
262 |
$generator->create_custom_profile_field(['datatype' => 'text',
|
|
|
263 |
'shortname' => 'researchtopic', 'name' => 'Research topic']);
|
|
|
264 |
set_config('showuseridentity', 'email,department,profile_field_researchtopic');
|
|
|
265 |
return $generator->create_user(
|
|
|
266 |
['username' => 'newuser', 'department' => 'Amphibian studies', 'email' => 'x@x.org',
|
|
|
267 |
'profile_field_researchtopic' => 'Frogs', 'imagealt' => 'Smart suit']);
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Checks that the get_users function returns the correct user fields.
|
|
|
272 |
*/
|
11 |
efrain |
273 |
public function test_get_users_fields(): void {
|
1 |
efrain |
274 |
global $PAGE;
|
|
|
275 |
|
|
|
276 |
$this->resetAfterTest();
|
|
|
277 |
$newuser = $this->setup_for_user_identity_tests();
|
|
|
278 |
|
|
|
279 |
// Enrol the user in test course.
|
|
|
280 |
$this->getDataGenerator()->enrol_user($newuser->id, $this->course->id, 'student');
|
|
|
281 |
|
|
|
282 |
// Get all users and fish out the one we're interested in.
|
|
|
283 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
284 |
$users = $manager->get_users('id');
|
|
|
285 |
$user = $users[$newuser->id];
|
|
|
286 |
|
|
|
287 |
// Should include core required fields...
|
|
|
288 |
$this->assertEquals($newuser->id, $user->id);
|
|
|
289 |
|
|
|
290 |
// ...And the ones specified in showuseridentity (one of which is also needed for user pics).
|
|
|
291 |
$this->assertEquals('Amphibian studies', $user->department);
|
|
|
292 |
$this->assertEquals('Frogs', $user->profile_field_researchtopic);
|
|
|
293 |
$this->assertEquals('x@x.org', $user->email);
|
|
|
294 |
|
|
|
295 |
// And the ones necessary for user pics.
|
|
|
296 |
$this->assertEquals('Smart suit', $user->imagealt);
|
|
|
297 |
|
|
|
298 |
// But not some random other field like city.
|
|
|
299 |
$this->assertObjectNotHasProperty('city', $user);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Checks that the get_other_users function returns the correct user fields.
|
|
|
304 |
*/
|
11 |
efrain |
305 |
public function test_get_other_users_fields(): void {
|
1 |
efrain |
306 |
global $PAGE, $DB;
|
|
|
307 |
|
|
|
308 |
$this->resetAfterTest();
|
|
|
309 |
|
|
|
310 |
// Configure extra fields to include one normal user field and one profile field, and
|
|
|
311 |
// set the values for a new test user.
|
|
|
312 |
$newuser = $this->setup_for_user_identity_tests();
|
|
|
313 |
$context = \context_course::instance($this->course->id);
|
|
|
314 |
role_assign($DB->get_field('role', 'id', ['shortname' => 'manager']), $newuser->id, $context->id);
|
|
|
315 |
|
|
|
316 |
// Get the 'other' (role but not enrolled) users and fish out the one we're interested in.
|
|
|
317 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
318 |
$users = array_values($manager->get_other_users('id'));
|
|
|
319 |
$user = $users[0];
|
|
|
320 |
|
|
|
321 |
// Should include core required fields...
|
|
|
322 |
$this->assertEquals($newuser->id, $user->id);
|
|
|
323 |
|
|
|
324 |
// ...And the ones specified in showuseridentity (one of which is also needed for user pics).
|
|
|
325 |
$this->assertEquals('Amphibian studies', $user->department);
|
|
|
326 |
$this->assertEquals('Frogs', $user->profile_field_researchtopic);
|
|
|
327 |
$this->assertEquals('x@x.org', $user->email);
|
|
|
328 |
|
|
|
329 |
// And the ones necessary for user pics.
|
|
|
330 |
$this->assertEquals('Smart suit', $user->imagealt);
|
|
|
331 |
|
|
|
332 |
// But not some random other field like city.
|
|
|
333 |
$this->assertObjectNotHasProperty('city', $user);
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Checks that the get_potential_users function returns the correct user fields.
|
|
|
338 |
*/
|
11 |
efrain |
339 |
public function test_get_potential_users_fields(): void {
|
1 |
efrain |
340 |
global $PAGE;
|
|
|
341 |
|
|
|
342 |
$this->resetAfterTest();
|
|
|
343 |
|
|
|
344 |
// Configure extra fields to include one normal user field and one profile field, and
|
|
|
345 |
// set the values for a new test user.
|
|
|
346 |
$newuser = $this->setup_for_user_identity_tests();
|
|
|
347 |
|
|
|
348 |
// Get the 'potential' (not enrolled) users and fish out the one we're interested in.
|
|
|
349 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
350 |
foreach (enrol_get_instances($this->course->id, true) as $enrolinstance) {
|
|
|
351 |
if ($enrolinstance->enrol === 'manual') {
|
|
|
352 |
$enrolid = $enrolinstance->id;
|
|
|
353 |
}
|
|
|
354 |
}
|
|
|
355 |
$users = array_values($manager->get_potential_users($enrolid));
|
|
|
356 |
$user = $users[0][$newuser->id];
|
|
|
357 |
|
|
|
358 |
// Should include core required fields...
|
|
|
359 |
$this->assertEquals($newuser->id, $user->id);
|
|
|
360 |
|
|
|
361 |
// ...And the ones specified in showuseridentity (one of which is also needed for user pics).
|
|
|
362 |
$this->assertEquals('Amphibian studies', $user->department);
|
|
|
363 |
$this->assertEquals('Frogs', $user->profile_field_researchtopic);
|
|
|
364 |
$this->assertEquals('x@x.org', $user->email);
|
|
|
365 |
|
|
|
366 |
// And the ones necessary for user pics.
|
|
|
367 |
$this->assertEquals('Smart suit', $user->imagealt);
|
|
|
368 |
|
|
|
369 |
// But not some random other field like city.
|
|
|
370 |
$this->assertObjectNotHasProperty('city', $user);
|
|
|
371 |
}
|
|
|
372 |
|
|
|
373 |
/**
|
|
|
374 |
* Test get_potential_users without returnexactcount param.
|
|
|
375 |
*
|
|
|
376 |
* @dataProvider search_users_provider
|
|
|
377 |
*
|
|
|
378 |
* @param int $perpage Number of users per page.
|
|
|
379 |
* @param bool $returnexactcount Return the exact count or not.
|
|
|
380 |
* @param int $expectedusers Expected number of users return.
|
|
|
381 |
* @param int $expectedtotalusers Expected total of users in database.
|
|
|
382 |
* @param bool $expectedmoreusers Expected for more users return or not.
|
|
|
383 |
*/
|
11 |
efrain |
384 |
public function test_get_potential_users($perpage, $returnexactcount, $expectedusers, $expectedtotalusers, $expectedmoreusers): void {
|
1 |
efrain |
385 |
global $DB, $PAGE;
|
|
|
386 |
$this->resetAfterTest();
|
|
|
387 |
$this->setAdminUser();
|
|
|
388 |
|
|
|
389 |
$enrol = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'));
|
|
|
390 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
391 |
$users = $manager->get_potential_users($enrol->id,
|
|
|
392 |
'testapiuser',
|
|
|
393 |
true,
|
|
|
394 |
0,
|
|
|
395 |
$perpage,
|
|
|
396 |
0,
|
|
|
397 |
$returnexactcount);
|
|
|
398 |
|
|
|
399 |
$this->assertCount($expectedusers, $users['users']);
|
|
|
400 |
$this->assertEquals($expectedmoreusers, $users['moreusers']);
|
|
|
401 |
if ($returnexactcount) {
|
|
|
402 |
$this->assertArrayHasKey('totalusers', $users);
|
|
|
403 |
$this->assertEquals($expectedtotalusers, $users['totalusers']);
|
|
|
404 |
} else {
|
|
|
405 |
$this->assertArrayNotHasKey('totalusers', $users);
|
|
|
406 |
}
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
/**
|
|
|
410 |
* Tests get_potential_users when the search term includes a custom field.
|
|
|
411 |
*/
|
11 |
efrain |
412 |
public function test_get_potential_users_search_fields(): void {
|
1 |
efrain |
413 |
global $PAGE;
|
|
|
414 |
|
|
|
415 |
$this->resetAfterTest();
|
|
|
416 |
|
|
|
417 |
// Configure extra fields to include one normal user field and one profile field, and
|
|
|
418 |
// set the values for a new test user.
|
|
|
419 |
$newuser = $this->setup_for_user_identity_tests();
|
|
|
420 |
|
|
|
421 |
// Set up the enrolment manager.
|
|
|
422 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
423 |
foreach (enrol_get_instances($this->course->id, true) as $enrolinstance) {
|
|
|
424 |
if ($enrolinstance->enrol === 'manual') {
|
|
|
425 |
$enrolid = $enrolinstance->id;
|
|
|
426 |
}
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
// Search for text included in a 'standard' (user table) identity field.
|
|
|
430 |
$users = array_values($manager->get_potential_users($enrolid, 'Amphibian studies'));
|
|
|
431 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
432 |
|
|
|
433 |
// And for text included in a custom field.
|
|
|
434 |
$users = array_values($manager->get_potential_users($enrolid, 'Frogs'));
|
|
|
435 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
436 |
|
|
|
437 |
// With partial matches.
|
|
|
438 |
$users = array_values($manager->get_potential_users($enrolid, 'Amphibian'));
|
|
|
439 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
440 |
$users = array_values($manager->get_potential_users($enrolid, 'Fro'));
|
|
|
441 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
442 |
|
|
|
443 |
// With partial in-the-middle matches.
|
|
|
444 |
$users = array_values($manager->get_potential_users($enrolid, 'phibian'));
|
|
|
445 |
$this->assertEquals([], array_keys($users[0]));
|
|
|
446 |
$users = array_values($manager->get_potential_users($enrolid, 'rog'));
|
|
|
447 |
$this->assertEquals([], array_keys($users[0]));
|
|
|
448 |
$users = array_values($manager->get_potential_users($enrolid, 'phibian', true));
|
|
|
449 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
450 |
$users = array_values($manager->get_potential_users($enrolid, 'rog', true));
|
|
|
451 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
452 |
|
|
|
453 |
// If the current user doesn't have access to identity fields then these searches won't work.
|
|
|
454 |
$this->setUser($this->getDataGenerator()->create_user());
|
|
|
455 |
$users = array_values($manager->get_potential_users($enrolid, 'Amphibian studies'));
|
|
|
456 |
$this->assertEquals([], array_keys($users[0]));
|
|
|
457 |
$users = array_values($manager->get_potential_users($enrolid, 'Frogs'));
|
|
|
458 |
$this->assertEquals([], array_keys($users[0]));
|
|
|
459 |
|
|
|
460 |
// Search for username field (there is special handling for this one field).
|
|
|
461 |
set_config('showuseridentity', 'username');
|
|
|
462 |
$this->setAdminUser();
|
|
|
463 |
$users = array_values($manager->get_potential_users($enrolid, 'newuse'));
|
|
|
464 |
$this->assertEquals([$newuser->id], array_keys($users[0]));
|
|
|
465 |
}
|
|
|
466 |
|
|
|
467 |
/**
|
|
|
468 |
* Test search_other_users with returnexactcount param.
|
|
|
469 |
*
|
|
|
470 |
* @dataProvider search_users_provider
|
|
|
471 |
*
|
|
|
472 |
* @param int $perpage Number of users per page.
|
|
|
473 |
* @param bool $returnexactcount Return the exact count or not.
|
|
|
474 |
* @param int $expectedusers Expected number of users return.
|
|
|
475 |
* @param int $expectedtotalusers Expected total of users in database.
|
|
|
476 |
* @param bool $expectedmoreusers Expected for more users return or not.
|
|
|
477 |
*/
|
11 |
efrain |
478 |
public function test_search_other_users($perpage, $returnexactcount, $expectedusers, $expectedtotalusers, $expectedmoreusers): void {
|
1 |
efrain |
479 |
global $PAGE;
|
|
|
480 |
$this->resetAfterTest();
|
|
|
481 |
$this->setAdminUser();
|
|
|
482 |
|
|
|
483 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
484 |
$users = $manager->search_other_users(
|
|
|
485 |
'testapiuser',
|
|
|
486 |
true,
|
|
|
487 |
0,
|
|
|
488 |
$perpage,
|
|
|
489 |
$returnexactcount);
|
|
|
490 |
|
|
|
491 |
$this->assertCount($expectedusers, $users['users']);
|
|
|
492 |
$this->assertEquals($expectedmoreusers, $users['moreusers']);
|
|
|
493 |
if ($returnexactcount) {
|
|
|
494 |
$this->assertArrayHasKey('totalusers', $users);
|
|
|
495 |
$this->assertEquals($expectedtotalusers, $users['totalusers']);
|
|
|
496 |
} else {
|
|
|
497 |
$this->assertArrayNotHasKey('totalusers', $users);
|
|
|
498 |
}
|
|
|
499 |
}
|
|
|
500 |
|
|
|
501 |
/**
|
|
|
502 |
* Test case for test_get_potential_users, test_search_other_users and test_search_users tests.
|
|
|
503 |
*
|
|
|
504 |
* @return array Dataset
|
|
|
505 |
*/
|
|
|
506 |
public function search_users_provider() {
|
|
|
507 |
return [
|
|
|
508 |
[2, false, 2, 3, true],
|
|
|
509 |
[5, false, 3, 3, false],
|
|
|
510 |
[2, true, 2, 3, true],
|
|
|
511 |
[5, true, 3, 3, false]
|
|
|
512 |
];
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
/**
|
|
|
516 |
* Test search_users function.
|
|
|
517 |
*
|
|
|
518 |
* @dataProvider search_users_provider
|
|
|
519 |
*
|
|
|
520 |
* @param int $perpage Number of users per page.
|
|
|
521 |
* @param bool $returnexactcount Return the exact count or not.
|
|
|
522 |
* @param int $expectedusers Expected number of users return.
|
|
|
523 |
* @param int $expectedtotalusers Expected total of users in database.
|
|
|
524 |
* @param bool $expectedmoreusers Expected for more users return or not.
|
|
|
525 |
*/
|
11 |
efrain |
526 |
public function test_search_users($perpage, $returnexactcount, $expectedusers, $expectedtotalusers, $expectedmoreusers): void {
|
1 |
efrain |
527 |
global $PAGE;
|
|
|
528 |
$this->resetAfterTest();
|
|
|
529 |
|
|
|
530 |
$this->getDataGenerator()->create_and_enrol($this->course, 'student', ['firstname' => 'sutest 1']);
|
|
|
531 |
$this->getDataGenerator()->create_and_enrol($this->course, 'student', ['firstname' => 'sutest 2']);
|
|
|
532 |
$this->getDataGenerator()->create_and_enrol($this->course, 'student', ['firstname' => 'sutest 3']);
|
|
|
533 |
|
|
|
534 |
$manager = new course_enrolment_manager($PAGE, $this->course);
|
|
|
535 |
$users = $manager->search_users(
|
|
|
536 |
'sutest',
|
|
|
537 |
true,
|
|
|
538 |
0,
|
|
|
539 |
$perpage,
|
|
|
540 |
$returnexactcount
|
|
|
541 |
);
|
|
|
542 |
|
|
|
543 |
$this->assertCount($expectedusers, $users['users']);
|
|
|
544 |
$this->assertEquals($expectedmoreusers, $users['moreusers']);
|
|
|
545 |
if ($returnexactcount) {
|
|
|
546 |
$this->assertArrayHasKey('totalusers', $users);
|
|
|
547 |
$this->assertEquals($expectedtotalusers, $users['totalusers']);
|
|
|
548 |
} else {
|
|
|
549 |
$this->assertArrayNotHasKey('totalusers', $users);
|
|
|
550 |
}
|
|
|
551 |
}
|
|
|
552 |
|
|
|
553 |
/**
|
|
|
554 |
* Test that search_users observes course group mode restrictions correctly
|
|
|
555 |
*/
|
|
|
556 |
public function test_search_users_course_groupmode(): void {
|
|
|
557 |
global $DB, $PAGE;
|
|
|
558 |
|
|
|
559 |
$this->resetAfterTest();
|
|
|
560 |
|
|
|
561 |
// Create the forum.
|
|
|
562 |
$record = new stdClass();
|
|
|
563 |
$record->introformat = FORMAT_HTML;
|
|
|
564 |
$record->course = $this->course->id;
|
|
|
565 |
$forum = self::getDataGenerator()->create_module('forum', $record, ['groupmode' => SEPARATEGROUPS]);
|
|
|
566 |
$contextid = $DB->get_field('context', 'id', ['instanceid' => $forum->cmid, 'contextlevel' => CONTEXT_MODULE]);
|
|
|
567 |
|
|
|
568 |
$teacher = $this->getDataGenerator()->create_and_enrol($this->course, 'teacher');
|
|
|
569 |
$this->getDataGenerator()->create_group_member(['groupid' => $this->groups['group1']->id, 'userid' => $teacher->id]);
|
|
|
570 |
$this->setUser($teacher);
|
|
|
571 |
|
|
|
572 |
$courseusers = (new course_enrolment_manager($PAGE, $this->course))->search_users('', false, 0, 25, true);
|
|
|
573 |
$this->assertEqualsCanonicalizing([
|
|
|
574 |
$teacher->username,
|
|
|
575 |
$this->users['user0']->username,
|
|
|
576 |
$this->users['user1']->username,
|
|
|
577 |
$this->users['user21']->username,
|
|
|
578 |
$this->users['user22']->username,
|
|
|
579 |
$this->users['userall']->username,
|
|
|
580 |
$this->users['usertch']->username,
|
|
|
581 |
], array_column($courseusers['users'], 'username'));
|
|
|
582 |
$this->assertEquals(7, $courseusers['totalusers']);
|
|
|
583 |
|
|
|
584 |
$forumusers = (new course_enrolment_manager($PAGE, $this->course))->search_users('', false, 0, 25, true, $contextid);
|
|
|
585 |
$this->assertEqualsCanonicalizing([
|
|
|
586 |
$teacher->username,
|
|
|
587 |
$this->users['user1']->username,
|
|
|
588 |
$this->users['userall']->username,
|
|
|
589 |
], array_column($forumusers['users'], 'username'));
|
|
|
590 |
$this->assertEquals(3, $forumusers['totalusers']);
|
|
|
591 |
|
|
|
592 |
// Switch course to separate groups and forum to no group.
|
|
|
593 |
$this->course->groupmode = SEPARATEGROUPS;
|
|
|
594 |
update_course($this->course);
|
|
|
595 |
set_coursemodule_groupmode($forum->cmid, NOGROUPS);
|
|
|
596 |
|
|
|
597 |
$courseusers = (new course_enrolment_manager($PAGE, $this->course))->search_users('', false, 0, 25, true);
|
|
|
598 |
$this->assertEqualsCanonicalizing([
|
|
|
599 |
$teacher->username,
|
|
|
600 |
$this->users['user1']->username,
|
|
|
601 |
$this->users['userall']->username,
|
|
|
602 |
], array_column($courseusers['users'], 'username'));
|
|
|
603 |
$this->assertEquals(3, $courseusers['totalusers']);
|
|
|
604 |
|
|
|
605 |
$forumusers = (new course_enrolment_manager($PAGE, $this->course))->search_users('', false, 0, 25, true, $contextid);
|
|
|
606 |
$this->assertEqualsCanonicalizing([
|
|
|
607 |
$teacher->username,
|
|
|
608 |
$this->users['user0']->username,
|
|
|
609 |
$this->users['user1']->username,
|
|
|
610 |
$this->users['user21']->username,
|
|
|
611 |
$this->users['user22']->username,
|
|
|
612 |
$this->users['userall']->username,
|
|
|
613 |
$this->users['usertch']->username,
|
|
|
614 |
], array_column($forumusers['users'], 'username'));
|
|
|
615 |
$this->assertEquals(7, $forumusers['totalusers']);
|
|
|
616 |
|
|
|
617 |
set_coursemodule_groupmode($forum->cmid, SEPARATEGROUPS);
|
|
|
618 |
|
|
|
619 |
// Allow teacher to access all groups.
|
|
|
620 |
$roleid = $DB->get_field('role', 'id', ['shortname' => 'teacher']);
|
|
|
621 |
assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $roleid, context_course::instance($this->course->id)->id);
|
|
|
622 |
|
|
|
623 |
$courseusers = (new course_enrolment_manager($PAGE, $this->course))->search_users('', false, 0, 25, true);
|
|
|
624 |
$this->assertEqualsCanonicalizing([
|
|
|
625 |
$teacher->username,
|
|
|
626 |
$this->users['user0']->username,
|
|
|
627 |
$this->users['user1']->username,
|
|
|
628 |
$this->users['user21']->username,
|
|
|
629 |
$this->users['user22']->username,
|
|
|
630 |
$this->users['userall']->username,
|
|
|
631 |
$this->users['usertch']->username,
|
|
|
632 |
], array_column($courseusers['users'], 'username'));
|
|
|
633 |
$this->assertEquals(7, $courseusers['totalusers']);
|
|
|
634 |
|
|
|
635 |
$forumusers = (new course_enrolment_manager($PAGE, $this->course))->search_users('', false, 0, 25, true, $contextid);
|
|
|
636 |
$this->assertEqualsCanonicalizing([
|
|
|
637 |
$teacher->username,
|
|
|
638 |
$this->users['user0']->username,
|
|
|
639 |
$this->users['user1']->username,
|
|
|
640 |
$this->users['user21']->username,
|
|
|
641 |
$this->users['user22']->username,
|
|
|
642 |
$this->users['userall']->username,
|
|
|
643 |
$this->users['usertch']->username,
|
|
|
644 |
], array_column($forumusers['users'], 'username'));
|
|
|
645 |
$this->assertEquals(7, $forumusers['totalusers']);
|
|
|
646 |
}
|
|
|
647 |
}
|