MDL-66269 mod_forum: removed the unnecessary hidden element
[moodle.git] / enrol / tests / course_enrolment_manager_test.php
CommitLineData
d76749b4
RK
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/**
18 * Test course_enrolment_manager parts.
19 *
20 * @package core_enrol
21 * @category test
22 * @copyright 2016 Ruslan Kabalin, Lancaster University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Test course_enrolment_manager parts.
31 *
32 * @package core
33 * @category test
34 * @copyright 2016 Ruslan Kabalin, Lancaster University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_course_enrolment_manager_testcase extends advanced_testcase {
38 /**
39 * The course context used in tests.
40 * @var stdClass
41 */
42 private $course = null;
43 /**
44 * List of users used in tests.
45 * @var array
46 */
47 private $users = array();
48 /**
49 * List of groups used in tests.
50 * @var array
51 */
52 private $groups = array();
53
54 /**
55 * Tests set up
56 */
57 protected function setUp() {
58 global $CFG;
59 require_once($CFG->dirroot . '/enrol/locallib.php');
60 $this->setAdminUser();
61
62 $users = array();
63 $groups = array();
64 // Create the course and the users.
65 $course = $this->getDataGenerator()->create_course();
66 $users['user0'] = $this->getDataGenerator()->create_user(
67 array('username' => 'user0', 'firstname' => 'user0')); // A user without group.
68 $users['user1'] = $this->getDataGenerator()->create_user(
69 array('username' => 'user1', 'firstname' => 'user1')); // User for group 1.
70 $users['user21'] = $this->getDataGenerator()->create_user(
71 array('username' => 'user21', 'firstname' => 'user21')); // Two users for group 2.
72 $users['user22'] = $this->getDataGenerator()->create_user(
73 array('username' => 'user22', 'firstname' => 'user22'));
74 $users['userall'] = $this->getDataGenerator()->create_user(
75 array('username' => 'userall', 'firstname' => 'userall')); // A user in all groups.
76 $users['usertch'] = $this->getDataGenerator()->create_user(
77 array('username' => 'usertch', 'firstname' => 'usertch')); // A user with teacher role.
78
79 // Enrol the users in the course.
80 $this->getDataGenerator()->enrol_user($users['user0']->id, $course->id, 'student'); // Student.
81 $this->getDataGenerator()->enrol_user($users['user1']->id, $course->id, 'student'); // Student.
82 $this->getDataGenerator()->enrol_user($users['user21']->id, $course->id, 'student'); // Student.
83 $this->getDataGenerator()->enrol_user($users['user22']->id, $course->id, 'student', 'manual', 0, 0, ENROL_USER_SUSPENDED); // Suspended student.
84 $this->getDataGenerator()->enrol_user($users['userall']->id, $course->id, 'student'); // Student.
85 $this->getDataGenerator()->enrol_user($users['usertch']->id, $course->id, 'editingteacher'); // Teacher.
86
87 // Create 2 groups.
88 $groups['group1'] = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
89 $groups['group2'] = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
90
91 // Add the users to the groups.
92 $this->getDataGenerator()->create_group_member(
93 array('groupid' => $groups['group1']->id, 'userid' => $users['user1']->id));
94 $this->getDataGenerator()->create_group_member(
95 array('groupid' => $groups['group2']->id, 'userid' => $users['user21']->id));
96 $this->getDataGenerator()->create_group_member(
97 array('groupid' => $groups['group2']->id, 'userid' => $users['user22']->id));
98 $this->getDataGenerator()->create_group_member(
99 array('groupid' => $groups['group1']->id, 'userid' => $users['userall']->id));
100 $this->getDataGenerator()->create_group_member(
101 array('groupid' => $groups['group2']->id, 'userid' => $users['userall']->id));
102
103 // Make setup data accessible from test methods.
104 $this->course = $course;
105 $this->users = $users;
106 $this->groups = $groups;
1f00fdb0
HN
107
108 // Make sample users and not enroll to any course.
109 $this->getDataGenerator()->create_user([
110 'username' => 'testapiuser1',
111 'firstname' => 'testapiuser 1'
112 ]);
113 $this->getDataGenerator()->create_user([
114 'username' => 'testapiuser2',
115 'firstname' => 'testapiuser 2'
116 ]);
117 $this->getDataGenerator()->create_user([
118 'username' => 'testapiuser3',
119 'firstname' => 'testapiuser 3'
120 ]);
d76749b4
RK
121 }
122
123 /**
124 * Verify get_total_users() returned number of users expected in every situation.
125 */
126 public function test_get_total_users() {
127 global $PAGE;
128
129 $this->resetAfterTest();
130
131 // All users filtering.
132 $manager = new course_enrolment_manager($PAGE, $this->course);
133 $totalusers = $manager->get_total_users();
134 $this->assertEquals(6, $totalusers, 'All users must be returned when no filtering is applied.');
135
136 // Student role filtering.
137 $manager = new course_enrolment_manager($PAGE, $this->course, null, 5);
138 $totalusers = $manager->get_total_users();
139 $this->assertEquals(5, $totalusers, 'Only students must be returned when student role filtering is applied.');
140
141 // Teacher role filtering.
142 $manager = new course_enrolment_manager($PAGE, $this->course, null, 3);
143 $totalusers = $manager->get_total_users();
144 $this->assertEquals(1, $totalusers, 'Only teacher must be returned when teacher role filtering is applied.');
145
146 // Search user filtering.
147 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, 'userall');
148 $totalusers = $manager->get_total_users();
149 $this->assertEquals(1, $totalusers, 'Only searchable user must be returned when search filtering is applied.');
150
151 // Group 1 filtering.
152 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group1']->id);
153 $totalusers = $manager->get_total_users();
154 $this->assertEquals(2, $totalusers, 'Only group members must be returned when group filtering is applied.');
155
156 // Group 2 filtering.
157 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group2']->id);
158 $totalusers = $manager->get_total_users();
159 $this->assertEquals(3, $totalusers, 'Only group members must be returned when group filtering is applied.');
160
161 // 'No groups' filtering.
162 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', -1);
163 $totalusers = $manager->get_total_users();
164 $this->assertEquals(2, $totalusers, 'Only non-group members must be returned when \'no groups\' filtering is applied.');
165
166 // Active users filtering.
167 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_ACTIVE);
168 $totalusers = $manager->get_total_users();
169 $this->assertEquals(5, $totalusers, 'Only active users must be returned when active users filtering is applied.');
170
171 // Suspended users filtering.
172 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_SUSPENDED);
173 $totalusers = $manager->get_total_users();
174 $this->assertEquals(1, $totalusers, 'Only suspended users must be returned when suspended users filtering is applied.');
175 }
176
177 /**
178 * Verify get_users() returned number of users expected in every situation.
179 */
180 public function test_get_users() {
181 global $PAGE;
182
183 $this->resetAfterTest();
184
185 // All users filtering.
186 $manager = new course_enrolment_manager($PAGE, $this->course);
187 $users = $manager->get_users('id');
188 $this->assertCount(6, $users, 'All users must be returned when no filtering is applied.');
189 $this->assertArrayHasKey($this->users['user0']->id, $users);
190 $this->assertArrayHasKey($this->users['user1']->id, $users);
191 $this->assertArrayHasKey($this->users['user21']->id, $users);
192 $this->assertArrayHasKey($this->users['user22']->id, $users);
193 $this->assertArrayHasKey($this->users['userall']->id, $users);
194 $this->assertArrayHasKey($this->users['usertch']->id, $users);
195
196 // Student role filtering.
197 $manager = new course_enrolment_manager($PAGE, $this->course, null, 5);
198 $users = $manager->get_users('id');
199 $this->assertCount(5, $users, 'Only students must be returned when student role filtering is applied.');
200 $this->assertArrayHasKey($this->users['user0']->id, $users);
201 $this->assertArrayHasKey($this->users['user1']->id, $users);
202 $this->assertArrayHasKey($this->users['user21']->id, $users);
203 $this->assertArrayHasKey($this->users['user22']->id, $users);
204 $this->assertArrayHasKey($this->users['userall']->id, $users);
205
206 // Teacher role filtering.
207 $manager = new course_enrolment_manager($PAGE, $this->course, null, 3);
208 $users = $manager->get_users('id');
209 $this->assertCount(1, $users, 'Only teacher must be returned when teacher role filtering is applied.');
210 $this->assertArrayHasKey($this->users['usertch']->id, $users);
211
212 // Search user filtering.
213 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, 'userall');
214 $users = $manager->get_users('id');
215 $this->assertCount(1, $users, 'Only searchable user must be returned when search filtering is applied.');
216 $this->assertArrayHasKey($this->users['userall']->id, $users);
217
218 // Group 1 filtering.
219 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group1']->id);
220 $users = $manager->get_users('id');
221 $this->assertCount(2, $users, 'Only group members must be returned when group filtering is applied.');
222 $this->assertArrayHasKey($this->users['user1']->id, $users);
223 $this->assertArrayHasKey($this->users['userall']->id, $users);
224
225 // Group 2 filtering.
226 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group2']->id);
227 $users = $manager->get_users('id');
228 $this->assertCount(3, $users, 'Only group members must be returned when group filtering is applied.');
229 $this->assertArrayHasKey($this->users['user21']->id, $users);
230 $this->assertArrayHasKey($this->users['user22']->id, $users);
231 $this->assertArrayHasKey($this->users['userall']->id, $users);
232
233 // 'No groups' filtering.
234 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', -1);
235 $users = $manager->get_users('id');
236 $this->assertCount(2, $users, 'Only non-group members must be returned when \'no groups\' filtering is applied.');
237 $this->assertArrayHasKey($this->users['user0']->id, $users);
238 $this->assertArrayHasKey($this->users['usertch']->id, $users);
239
240 // Active users filtering.
241 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_ACTIVE);
242 $users = $manager->get_users('id');
243 $this->assertCount(5, $users, 'Only active users must be returned when active users filtering is applied.');
244 $this->assertArrayHasKey($this->users['user0']->id, $users);
245 $this->assertArrayHasKey($this->users['user1']->id, $users);
246 $this->assertArrayHasKey($this->users['user21']->id, $users);
247 $this->assertArrayHasKey($this->users['userall']->id, $users);
248 $this->assertArrayHasKey($this->users['usertch']->id, $users);
249
250 // Suspended users filtering.
251 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_SUSPENDED);
252 $users = $manager->get_users('id');
253 $this->assertCount(1, $users, 'Only suspended users must be returned when suspended users filtering is applied.');
254 $this->assertArrayHasKey($this->users['user22']->id, $users);
255 }
1f00fdb0
HN
256
257 /**
258 * Test get_potential_users without returnexactcount param.
259 *
260 * @dataProvider search_users_provider
261 *
262 * @param int $perpage Number of users per page.
263 * @param bool $returnexactcount Return the exact count or not.
264 * @param int $expectedusers Expected number of users return.
265 * @param int $expectedtotalusers Expected total of users in database.
266 * @param bool $expectedmoreusers Expected for more users return or not.
267 */
268 public function test_get_potential_users($perpage, $returnexactcount, $expectedusers, $expectedtotalusers, $expectedmoreusers) {
269 global $DB, $PAGE;
270 $this->resetAfterTest();
271 $this->setAdminUser();
272
273 $enrol = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'));
274 $manager = new course_enrolment_manager($PAGE, $this->course);
275 $users = $manager->get_potential_users($enrol->id,
276 'testapiuser',
277 true,
278 0,
279 $perpage,
280 0,
281 $returnexactcount);
282
283 $this->assertCount($expectedusers, $users['users']);
284 $this->assertEquals($expectedmoreusers, $users['moreusers']);
285 if ($returnexactcount) {
286 $this->assertArrayHasKey('totalusers', $users);
287 $this->assertEquals($expectedtotalusers, $users['totalusers']);
288 } else {
289 $this->assertArrayNotHasKey('totalusers', $users);
290 }
291 }
292
293 /**
294 * Test search_other_users with returnexactcount param.
295 *
296 * @dataProvider search_users_provider
297 *
298 * @param int $perpage Number of users per page.
299 * @param bool $returnexactcount Return the exact count or not.
300 * @param int $expectedusers Expected number of users return.
301 * @param int $expectedtotalusers Expected total of users in database.
302 * @param bool $expectedmoreusers Expected for more users return or not.
303 */
304 public function test_search_other_users($perpage, $returnexactcount, $expectedusers, $expectedtotalusers, $expectedmoreusers) {
305 global $PAGE;
306 $this->resetAfterTest();
307 $this->setAdminUser();
308
309 $manager = new course_enrolment_manager($PAGE, $this->course);
310 $users = $manager->search_other_users(
311 'testapiuser',
312 true,
313 0,
314 $perpage,
315 $returnexactcount);
316
317 $this->assertCount($expectedusers, $users['users']);
318 $this->assertEquals($expectedmoreusers, $users['moreusers']);
319 if ($returnexactcount) {
320 $this->assertArrayHasKey('totalusers', $users);
321 $this->assertEquals($expectedtotalusers, $users['totalusers']);
322 } else {
323 $this->assertArrayNotHasKey('totalusers', $users);
324 }
325 }
326
327 /**
328 * Test case for test_get_potential_users and test_search_other_users tests.
329 *
330 * @return array Dataset
331 */
332 public function search_users_provider() {
333 return [
334 [2, false, 2, 3, true],
335 [5, false, 3, 3, false],
336 [2, true, 2, 3, true],
337 [5, true, 3, 3, false]
338 ];
339 }
d76749b4 340}