weekly release 3.7dev
[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;
107 }
108
109 /**
110 * Verify get_total_users() returned number of users expected in every situation.
111 */
112 public function test_get_total_users() {
113 global $PAGE;
114
115 $this->resetAfterTest();
116
117 // All users filtering.
118 $manager = new course_enrolment_manager($PAGE, $this->course);
119 $totalusers = $manager->get_total_users();
120 $this->assertEquals(6, $totalusers, 'All users must be returned when no filtering is applied.');
121
122 // Student role filtering.
123 $manager = new course_enrolment_manager($PAGE, $this->course, null, 5);
124 $totalusers = $manager->get_total_users();
125 $this->assertEquals(5, $totalusers, 'Only students must be returned when student role filtering is applied.');
126
127 // Teacher role filtering.
128 $manager = new course_enrolment_manager($PAGE, $this->course, null, 3);
129 $totalusers = $manager->get_total_users();
130 $this->assertEquals(1, $totalusers, 'Only teacher must be returned when teacher role filtering is applied.');
131
132 // Search user filtering.
133 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, 'userall');
134 $totalusers = $manager->get_total_users();
135 $this->assertEquals(1, $totalusers, 'Only searchable user must be returned when search filtering is applied.');
136
137 // Group 1 filtering.
138 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group1']->id);
139 $totalusers = $manager->get_total_users();
140 $this->assertEquals(2, $totalusers, 'Only group members must be returned when group filtering is applied.');
141
142 // Group 2 filtering.
143 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group2']->id);
144 $totalusers = $manager->get_total_users();
145 $this->assertEquals(3, $totalusers, 'Only group members must be returned when group filtering is applied.');
146
147 // 'No groups' filtering.
148 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', -1);
149 $totalusers = $manager->get_total_users();
150 $this->assertEquals(2, $totalusers, 'Only non-group members must be returned when \'no groups\' filtering is applied.');
151
152 // Active users filtering.
153 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_ACTIVE);
154 $totalusers = $manager->get_total_users();
155 $this->assertEquals(5, $totalusers, 'Only active users must be returned when active users filtering is applied.');
156
157 // Suspended users filtering.
158 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_SUSPENDED);
159 $totalusers = $manager->get_total_users();
160 $this->assertEquals(1, $totalusers, 'Only suspended users must be returned when suspended users filtering is applied.');
161 }
162
163 /**
164 * Verify get_users() returned number of users expected in every situation.
165 */
166 public function test_get_users() {
167 global $PAGE;
168
169 $this->resetAfterTest();
170
171 // All users filtering.
172 $manager = new course_enrolment_manager($PAGE, $this->course);
173 $users = $manager->get_users('id');
174 $this->assertCount(6, $users, 'All users must be returned when no filtering is applied.');
175 $this->assertArrayHasKey($this->users['user0']->id, $users);
176 $this->assertArrayHasKey($this->users['user1']->id, $users);
177 $this->assertArrayHasKey($this->users['user21']->id, $users);
178 $this->assertArrayHasKey($this->users['user22']->id, $users);
179 $this->assertArrayHasKey($this->users['userall']->id, $users);
180 $this->assertArrayHasKey($this->users['usertch']->id, $users);
181
182 // Student role filtering.
183 $manager = new course_enrolment_manager($PAGE, $this->course, null, 5);
184 $users = $manager->get_users('id');
185 $this->assertCount(5, $users, 'Only students must be returned when student role filtering is applied.');
186 $this->assertArrayHasKey($this->users['user0']->id, $users);
187 $this->assertArrayHasKey($this->users['user1']->id, $users);
188 $this->assertArrayHasKey($this->users['user21']->id, $users);
189 $this->assertArrayHasKey($this->users['user22']->id, $users);
190 $this->assertArrayHasKey($this->users['userall']->id, $users);
191
192 // Teacher role filtering.
193 $manager = new course_enrolment_manager($PAGE, $this->course, null, 3);
194 $users = $manager->get_users('id');
195 $this->assertCount(1, $users, 'Only teacher must be returned when teacher role filtering is applied.');
196 $this->assertArrayHasKey($this->users['usertch']->id, $users);
197
198 // Search user filtering.
199 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, 'userall');
200 $users = $manager->get_users('id');
201 $this->assertCount(1, $users, 'Only searchable user must be returned when search filtering is applied.');
202 $this->assertArrayHasKey($this->users['userall']->id, $users);
203
204 // Group 1 filtering.
205 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group1']->id);
206 $users = $manager->get_users('id');
207 $this->assertCount(2, $users, 'Only group members must be returned when group filtering is applied.');
208 $this->assertArrayHasKey($this->users['user1']->id, $users);
209 $this->assertArrayHasKey($this->users['userall']->id, $users);
210
211 // Group 2 filtering.
212 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', $this->groups['group2']->id);
213 $users = $manager->get_users('id');
214 $this->assertCount(3, $users, 'Only group members must be returned when group filtering is applied.');
215 $this->assertArrayHasKey($this->users['user21']->id, $users);
216 $this->assertArrayHasKey($this->users['user22']->id, $users);
217 $this->assertArrayHasKey($this->users['userall']->id, $users);
218
219 // 'No groups' filtering.
220 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', -1);
221 $users = $manager->get_users('id');
222 $this->assertCount(2, $users, 'Only non-group members must be returned when \'no groups\' filtering is applied.');
223 $this->assertArrayHasKey($this->users['user0']->id, $users);
224 $this->assertArrayHasKey($this->users['usertch']->id, $users);
225
226 // Active users filtering.
227 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_ACTIVE);
228 $users = $manager->get_users('id');
229 $this->assertCount(5, $users, 'Only active users must be returned when active users filtering is applied.');
230 $this->assertArrayHasKey($this->users['user0']->id, $users);
231 $this->assertArrayHasKey($this->users['user1']->id, $users);
232 $this->assertArrayHasKey($this->users['user21']->id, $users);
233 $this->assertArrayHasKey($this->users['userall']->id, $users);
234 $this->assertArrayHasKey($this->users['usertch']->id, $users);
235
236 // Suspended users filtering.
237 $manager = new course_enrolment_manager($PAGE, $this->course, null, 0, '', 0, ENROL_USER_SUSPENDED);
238 $users = $manager->get_users('id');
239 $this->assertCount(1, $users, 'Only suspended users must be returned when suspended users filtering is applied.');
240 $this->assertArrayHasKey($this->users['user22']->id, $users);
241 }
242}