Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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