MDL-61804 admin: Add setting for course visibility sorting
[moodle.git] / enrol / tests / enrollib_test.php
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/>.
17 /**
18  * Test non-plugin enrollib parts.
19  *
20  * @package    core_enrol
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Test non-plugin enrollib parts.
31  *
32  * @package    core
33  * @category   phpunit
34  * @copyright  2012 Petr Skoda {@link http://skodak.org}
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_enrollib_testcase extends advanced_testcase {
39     public function test_enrol_get_all_users_courses() {
40         global $DB, $CFG;
42         $this->resetAfterTest();
44         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
45         $this->assertNotEmpty($studentrole);
46         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
47         $this->assertNotEmpty($teacherrole);
49         $admin = get_admin();
50         $user1 = $this->getDataGenerator()->create_user();
51         $user2 = $this->getDataGenerator()->create_user();
52         $user3 = $this->getDataGenerator()->create_user();
53         $user4 = $this->getDataGenerator()->create_user();
54         $user5 = $this->getDataGenerator()->create_user();
56         $category1 = $this->getDataGenerator()->create_category(array('visible'=>0));
57         $category2 = $this->getDataGenerator()->create_category();
59         $course1 = $this->getDataGenerator()->create_course(array(
60             'shortname' => 'Z',
61             'idnumber' => '123',
62             'category' => $category1->id,
63         ));
64         $course2 = $this->getDataGenerator()->create_course(array(
65             'shortname' => 'X',
66             'idnumber' => '789',
67             'category' => $category2->id,
68         ));
69         $course3 = $this->getDataGenerator()->create_course(array(
70             'shortname' => 'Y',
71             'idnumber' => '456',
72             'category' => $category2->id,
73             'visible' => 0,
74         ));
75         $course4 = $this->getDataGenerator()->create_course(array(
76             'shortname' => 'W',
77             'category' => $category2->id,
78         ));
80         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
81         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
82         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
83         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
84         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
85         $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
87         $manual = enrol_get_plugin('manual');
88         $this->assertNotEmpty($manual);
90         $manual->enrol_user($maninstance1, $user1->id, $teacherrole->id);
91         $manual->enrol_user($maninstance1, $user2->id, $studentrole->id);
92         $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
93         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
95         $manual->enrol_user($maninstance2, $user1->id);
96         $manual->enrol_user($maninstance2, $user2->id);
97         $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()+(60*60));
99         $manual->enrol_user($maninstance3, $user1->id);
100         $manual->enrol_user($maninstance3, $user2->id);
101         $manual->enrol_user($maninstance3, $user3->id, 0, 1, time()-(60*60));
102         $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
105         $courses = enrol_get_all_users_courses($CFG->siteguest);
106         $this->assertSame(array(), $courses);
108         $courses = enrol_get_all_users_courses(0);
109         $this->assertSame(array(), $courses);
111         // Results are sorted by visibility, sortorder by default (in our case order of creation)
113         $courses = enrol_get_all_users_courses($admin->id);
114         $this->assertCount(1, $courses);
115         $this->assertEquals(array($course1->id), array_keys($courses));
117         $courses = enrol_get_all_users_courses($admin->id, true);
118         $this->assertCount(0, $courses);
119         $this->assertEquals(array(), array_keys($courses));
121         $courses = enrol_get_all_users_courses($user1->id);
122         $this->assertCount(3, $courses);
123         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
125         $courses = enrol_get_all_users_courses($user1->id, true);
126         $this->assertCount(2, $courses);
127         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
129         $courses = enrol_get_all_users_courses($user2->id);
130         $this->assertCount(3, $courses);
131         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
133         $courses = enrol_get_all_users_courses($user2->id, true);
134         $this->assertCount(2, $courses);
135         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
137         $courses = enrol_get_all_users_courses($user3->id);
138         $this->assertCount(2, $courses);
139         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
141         $courses = enrol_get_all_users_courses($user3->id, true);
142         $this->assertCount(1, $courses);
143         $this->assertEquals(array($course2->id), array_keys($courses));
145         $courses = enrol_get_all_users_courses($user4->id);
146         $this->assertCount(2, $courses);
147         $this->assertEquals(array($course1->id, $course3->id), array_keys($courses));
149         $courses = enrol_get_all_users_courses($user4->id, true);
150         $this->assertCount(0, $courses);
151         $this->assertEquals(array(), array_keys($courses));
153         // Make sure sorting and columns work.
155         $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber',
156             'startdate', 'visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid');
158         $courses = enrol_get_all_users_courses($user2->id, true);
159         $course = reset($courses);
160         context_helper::preload_from_record($course);
161         $course = (array)$course;
162         $this->assertEquals($basefields, array_keys($course), '', 0, 10, true);
164         $courses = enrol_get_all_users_courses($user2->id, false, 'timecreated');
165         $course = reset($courses);
166         $this->assertTrue(property_exists($course, 'timecreated'));
168         $courses = enrol_get_all_users_courses($user2->id, false, null, 'id DESC');
169         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
171         // Make sure that implicit sorting defined in navsortmycoursessort is respected.
173         $CFG->navsortmycoursessort = 'shortname';
175         $courses = enrol_get_all_users_courses($user1->id);
176         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
178         // But still the explicit sorting takes precedence over the implicit one.
180         $courses = enrol_get_all_users_courses($user1->id, false, null, 'shortname DESC');
181         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
183         // Make sure that implicit visibility sorting defined in navsortmycourseshiddenlast is respected for all course sortings.
185         $CFG->navsortmycoursessort = 'sortorder';
186         $CFG->navsortmycourseshiddenlast = true;
187         $courses = enrol_get_all_users_courses($user1->id);
188         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
190         $CFG->navsortmycoursessort = 'sortorder';
191         $CFG->navsortmycourseshiddenlast = false;
192         $courses = enrol_get_all_users_courses($user1->id);
193         $this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
195         $CFG->navsortmycoursessort = 'fullname';
196         $CFG->navsortmycourseshiddenlast = true;
197         $courses = enrol_get_all_users_courses($user1->id);
198         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
200         $CFG->navsortmycoursessort = 'fullname';
201         $CFG->navsortmycourseshiddenlast = false;
202         $courses = enrol_get_all_users_courses($user1->id);
203         $this->assertEquals(array($course1->id, $course2->id, $course3->id), array_keys($courses));
205         $CFG->navsortmycoursessort = 'shortname';
206         $CFG->navsortmycourseshiddenlast = true;
207         $courses = enrol_get_all_users_courses($user1->id);
208         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
210         $CFG->navsortmycoursessort = 'shortname';
211         $CFG->navsortmycourseshiddenlast = false;
212         $courses = enrol_get_all_users_courses($user1->id);
213         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
215         $CFG->navsortmycoursessort = 'idnumber';
216         $CFG->navsortmycourseshiddenlast = true;
217         $courses = enrol_get_all_users_courses($user1->id);
218         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
220         $CFG->navsortmycoursessort = 'idnumber';
221         $CFG->navsortmycourseshiddenlast = false;
222         $courses = enrol_get_all_users_courses($user1->id);
223         $this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
225         // But still the explicit visibility sorting takes precedence over the implicit one.
227         $courses = enrol_get_all_users_courses($user1->id, false, null, 'visible DESC, shortname DESC');
228         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
229     }
231     public function test_enrol_user_sees_own_courses() {
232         global $DB, $CFG;
234         $this->resetAfterTest();
236         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
237         $this->assertNotEmpty($studentrole);
238         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
239         $this->assertNotEmpty($teacherrole);
241         $admin = get_admin();
242         $user1 = $this->getDataGenerator()->create_user();
243         $user2 = $this->getDataGenerator()->create_user();
244         $user3 = $this->getDataGenerator()->create_user();
245         $user4 = $this->getDataGenerator()->create_user();
246         $user5 = $this->getDataGenerator()->create_user();
247         $user6 = $this->getDataGenerator()->create_user();
249         $category1 = $this->getDataGenerator()->create_category(array('visible'=>0));
250         $category2 = $this->getDataGenerator()->create_category();
251         $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
252         $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
253         $course3 = $this->getDataGenerator()->create_course(array('category'=>$category2->id, 'visible'=>0));
254         $course4 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
256         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
257         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
258         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
259         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
260         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
261         $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
263         $manual = enrol_get_plugin('manual');
264         $this->assertNotEmpty($manual);
266         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
268         $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id);
270         $manual->enrol_user($maninstance2, $user2->id, $studentrole->id);
272         $manual->enrol_user($maninstance1, $user3->id, $studentrole->id, 1, time()+(60*60));
273         $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()-(60*60));
274         $manual->enrol_user($maninstance3, $user2->id, $studentrole->id);
275         $manual->enrol_user($maninstance4, $user2->id, 0, 0, 0, ENROL_USER_SUSPENDED);
277         $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
278         $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
281         $this->assertFalse(enrol_user_sees_own_courses($CFG->siteguest));
282         $this->assertFalse(enrol_user_sees_own_courses(0));
283         $this->assertFalse(enrol_user_sees_own_courses($admin));
284         $this->assertFalse(enrol_user_sees_own_courses(-222)); // Nonexistent user.
286         $this->assertTrue(enrol_user_sees_own_courses($user1));
287         $this->assertTrue(enrol_user_sees_own_courses($user2->id));
288         $this->assertFalse(enrol_user_sees_own_courses($user3->id));
289         $this->assertFalse(enrol_user_sees_own_courses($user4));
290         $this->assertFalse(enrol_user_sees_own_courses($user5));
292         $this->setAdminUser();
293         $this->assertFalse(enrol_user_sees_own_courses());
295         $this->setGuestUser();
296         $this->assertFalse(enrol_user_sees_own_courses());
298         $this->setUser(0);
299         $this->assertFalse(enrol_user_sees_own_courses());
301         $this->setUser($user1);
302         $this->assertTrue(enrol_user_sees_own_courses());
304         $this->setUser($user2);
305         $this->assertTrue(enrol_user_sees_own_courses());
307         $this->setUser($user3);
308         $this->assertFalse(enrol_user_sees_own_courses());
310         $this->setUser($user4);
311         $this->assertFalse(enrol_user_sees_own_courses());
313         $this->setUser($user5);
314         $this->assertFalse(enrol_user_sees_own_courses());
316         $user1 = $DB->get_record('user', array('id'=>$user1->id));
317         $this->setUser($user1);
318         $reads = $DB->perf_get_reads();
319         $this->assertTrue(enrol_user_sees_own_courses());
320         $this->assertGreaterThan($reads, $DB->perf_get_reads());
322         $user1 = $DB->get_record('user', array('id'=>$user1->id));
323         $this->setUser($user1);
324         require_login($course3);
325         $reads = $DB->perf_get_reads();
326         $this->assertTrue(enrol_user_sees_own_courses());
327         $this->assertEquals($reads, $DB->perf_get_reads());
328     }
330     public function test_enrol_get_shared_courses() {
331         $this->resetAfterTest();
333         $user1 = $this->getDataGenerator()->create_user();
334         $user2 = $this->getDataGenerator()->create_user();
335         $user3 = $this->getDataGenerator()->create_user();
337         $course1 = $this->getDataGenerator()->create_course();
338         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
339         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
341         $course2 = $this->getDataGenerator()->create_course();
342         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
344         // Test that user1 and user2 have courses in common.
345         $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
346         // Test that user1 and user3 have no courses in common.
347         $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
349         // Test retrieving the courses in common.
350         $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
352         // Only should be one shared course.
353         $this->assertCount(1, $sharedcourses);
354         $sharedcourse = array_shift($sharedcourses);
355         // It should be course 1.
356         $this->assertEquals($sharedcourse->id, $course1->id);
357     }
359     public function test_enrol_get_shared_courses_different_methods() {
360         global $DB, $CFG;
362         require_once($CFG->dirroot . '/enrol/self/externallib.php');
364         $this->resetAfterTest();
366         $user1 = $this->getDataGenerator()->create_user();
367         $user2 = $this->getDataGenerator()->create_user();
368         $user3 = $this->getDataGenerator()->create_user();
370         $course1 = $this->getDataGenerator()->create_course();
372         // Enrol user1 and user2 in course1 with a different enrolment methode.
373         // Add self enrolment method for course1.
374         $selfplugin = enrol_get_plugin('self');
375         $this->assertNotEmpty($selfplugin);
377         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
378         $this->assertNotEmpty($studentrole);
380         $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
381                                                                  'name' => 'Test instance 1',
382                                                                  'customint6' => 1,
383                                                                  'roleid' => $studentrole->id));
385         $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
387         self::setUser($user2);
388         // Self enrol me (user2).
389         $result = enrol_self_external::enrol_user($course1->id);
391         // Enrol user1 manually.
392         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'manual');
394         $course2 = $this->getDataGenerator()->create_course();
395         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
397         $course3 = $this->getDataGenerator()->create_course();
398         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
400         // Test that user1 and user2 have courses in common.
401         $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
402         // Test that user1 and user3 have no courses in common.
403         $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
405         // Test retrieving the courses in common.
406         $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
408         // Only should be one shared course.
409         $this->assertCount(1, $sharedcourses);
410         $sharedcourse = array_shift($sharedcourses);
411         // It should be course 1.
412         $this->assertEquals($sharedcourse->id, $course1->id);
413     }
415     /**
416      * Test user enrolment created event.
417      */
418     public function test_user_enrolment_created_event() {
419         global $DB;
421         $this->resetAfterTest();
423         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
424         $this->assertNotEmpty($studentrole);
426         $admin = get_admin();
428         $course1 = $this->getDataGenerator()->create_course();
430         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
432         $manual = enrol_get_plugin('manual');
433         $this->assertNotEmpty($manual);
435         // Enrol user and capture event.
436         $sink = $this->redirectEvents();
437         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
438         $events = $sink->get_events();
439         $sink->close();
440         $event = array_shift($events);
442         $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $admin->id));
443         $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
444         $this->assertEquals($dbuserenrolled->id, $event->objectid);
445         $this->assertEquals(context_course::instance($course1->id), $event->get_context());
446         $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
447         $expectedlegacyeventdata = $dbuserenrolled;
448         $expectedlegacyeventdata->enrol = $manual->get_name();
449         $expectedlegacyeventdata->courseid = $course1->id;
450         $this->assertEventLegacyData($expectedlegacyeventdata, $event);
451         $expected = array($course1->id, 'course', 'enrol', '../enrol/users.php?id=' . $course1->id, $course1->id);
452         $this->assertEventLegacyLogData($expected, $event);
453         $this->assertEventContextNotUsed($event);
454     }
456     /**
457      * Test user_enrolment_deleted event.
458      */
459     public function test_user_enrolment_deleted_event() {
460         global $DB;
462         $this->resetAfterTest(true);
464         $manualplugin = enrol_get_plugin('manual');
465         $user = $this->getDataGenerator()->create_user();
466         $course = $this->getDataGenerator()->create_course();
467         $student = $DB->get_record('role', array('shortname' => 'student'));
469         $enrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
471         // Enrol user.
472         $manualplugin->enrol_user($enrol, $user->id, $student->id);
474         // Get the user enrolment information, used to validate legacy event data.
475         $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user->id));
477         // Unenrol user and capture event.
478         $sink = $this->redirectEvents();
479         $manualplugin->unenrol_user($enrol, $user->id);
480         $events = $sink->get_events();
481         $sink->close();
482         $event = array_pop($events);
484         // Validate the event.
485         $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
486         $this->assertEquals(context_course::instance($course->id), $event->get_context());
487         $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
488         $expectedlegacyeventdata = $dbuserenrolled;
489         $expectedlegacyeventdata->enrol = $manualplugin->get_name();
490         $expectedlegacyeventdata->courseid = $course->id;
491         $expectedlegacyeventdata->lastenrol = true;
492         $this->assertEventLegacyData($expectedlegacyeventdata, $event);
493         $expected = array($course->id, 'course', 'unenrol', '../enrol/users.php?id=' . $course->id, $course->id);
494         $this->assertEventLegacyLogData($expected, $event);
495         $this->assertEventContextNotUsed($event);
496     }
498     /**
499      * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
500      */
501     public function test_instance_events() {
502         global $DB;
504         $this->resetAfterTest(true);
506         $selfplugin = enrol_get_plugin('self');
507         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
509         $course = $this->getDataGenerator()->create_course();
511         // Creating enrol instance.
512         $sink = $this->redirectEvents();
513         $instanceid = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
514                                                                 'name' => 'Test instance 1',
515                                                                 'customint6' => 1,
516                                                                 'roleid' => $studentrole->id));
517         $events = $sink->get_events();
518         $sink->close();
520         $this->assertCount(1, $events);
521         $event = array_pop($events);
522         $this->assertInstanceOf('\core\event\enrol_instance_created', $event);
523         $this->assertEquals(context_course::instance($course->id), $event->get_context());
524         $this->assertEquals('self', $event->other['enrol']);
525         $this->assertEventContextNotUsed($event);
527         // Updating enrol instance.
528         $instance = $DB->get_record('enrol', array('id' => $instanceid));
529         $sink = $this->redirectEvents();
530         $selfplugin->update_status($instance, ENROL_INSTANCE_DISABLED);
532         $events = $sink->get_events();
533         $sink->close();
535         $this->assertCount(1, $events);
536         $event = array_pop($events);
537         $this->assertInstanceOf('\core\event\enrol_instance_updated', $event);
538         $this->assertEquals(context_course::instance($course->id), $event->get_context());
539         $this->assertEquals('self', $event->other['enrol']);
540         $this->assertEventContextNotUsed($event);
542         // Deleting enrol instance.
543         $instance = $DB->get_record('enrol', array('id' => $instanceid));
544         $sink = $this->redirectEvents();
545         $selfplugin->delete_instance($instance);
547         $events = $sink->get_events();
548         $sink->close();
550         $this->assertCount(1, $events);
551         $event = array_pop($events);
552         $this->assertInstanceOf('\core\event\enrol_instance_deleted', $event);
553         $this->assertEquals(context_course::instance($course->id), $event->get_context());
554         $this->assertEquals('self', $event->other['enrol']);
555         $this->assertEventContextNotUsed($event);
556     }
558     /**
559      * Confirms that timemodified field was updated after modification of user enrollment
560      */
561     public function test_enrollment_update_timemodified() {
562         global $DB;
564         $this->resetAfterTest(true);
565         $datagen = $this->getDataGenerator();
567         /** @var enrol_manual_plugin $manualplugin */
568         $manualplugin = enrol_get_plugin('manual');
569         $this->assertNotNull($manualplugin);
571         $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
572         $course = $datagen->create_course();
573         $user = $datagen->create_user();
575         $instanceid = null;
576         $instances = enrol_get_instances($course->id, true);
577         foreach ($instances as $inst) {
578             if ($inst->enrol == 'manual') {
579                 $instanceid = (int)$inst->id;
580                 break;
581             }
582         }
583         if (empty($instanceid)) {
584             $instanceid = $manualplugin->add_default_instance($course);
585             if (empty($instanceid)) {
586                 $instanceid = $manualplugin->add_instance($course);
587             }
588         }
589         $this->assertNotNull($instanceid);
591         $instance = $DB->get_record('enrol', ['id' => $instanceid], '*', MUST_EXIST);
592         $manualplugin->enrol_user($instance, $user->id, $studentroleid, 0, 0, ENROL_USER_ACTIVE);
593         $userenrolorig = (int)$DB->get_field(
594             'user_enrolments',
595             'timemodified',
596             ['enrolid' => $instance->id, 'userid' => $user->id],
597             MUST_EXIST
598         );
599         $this->waitForSecond();
600         $this->waitForSecond();
601         $manualplugin->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED);
602         $userenrolpost = (int)$DB->get_field(
603             'user_enrolments',
604             'timemodified',
605             ['enrolid' => $instance->id, 'userid' => $user->id],
606             MUST_EXIST
607         );
609         $this->assertGreaterThan($userenrolorig, $userenrolpost);
610     }
612     /**
613      * Test to confirm that enrol_get_my_courses only return the courses that
614      * the logged in user is enrolled in.
615      */
616     public function test_enrol_get_my_courses_only_enrolled_courses() {
617         $user = $this->getDataGenerator()->create_user();
618         $course1 = $this->getDataGenerator()->create_course();
619         $course2 = $this->getDataGenerator()->create_course();
620         $course3 = $this->getDataGenerator()->create_course();
621         $course4 = $this->getDataGenerator()->create_course();
623         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
624         $this->getDataGenerator()->enrol_user($user->id, $course2->id);
625         $this->getDataGenerator()->enrol_user($user->id, $course3->id);
626         $this->resetAfterTest(true);
627         $this->setUser($user);
629         // By default this function should return all of the courses the user
630         // is enrolled in.
631         $courses = enrol_get_my_courses();
633         $this->assertCount(3, $courses);
634         $this->assertEquals($course1->id, $courses[$course1->id]->id);
635         $this->assertEquals($course2->id, $courses[$course2->id]->id);
636         $this->assertEquals($course3->id, $courses[$course3->id]->id);
638         // If a set of course ids are provided then the result set will only contain
639         // these courses.
640         $courseids = [$course1->id, $course2->id];
641         $courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
643         $this->assertCount(2, $courses);
644         $this->assertEquals($course1->id, $courses[$course1->id]->id);
645         $this->assertEquals($course2->id, $courses[$course2->id]->id);
647         // If the course ids list contains any ids for courses the user isn't enrolled in
648         // then they will be ignored (in this case $course4).
649         $courseids = [$course1->id, $course2->id, $course4->id];
650         $courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
652         $this->assertCount(2, $courses);
653         $this->assertEquals($course1->id, $courses[$course1->id]->id);
654         $this->assertEquals($course2->id, $courses[$course2->id]->id);
655     }
657     /**
658      * Tests the enrol_get_my_courses function when using the $includehidden parameter, which
659      * should remove any courses hidden from the user's timeline
660      *
661      * @throws coding_exception
662      * @throws dml_exception
663      */
664     public function test_enrol_get_my_courses_include_hidden() {
665         global $DB, $CFG;
667         $this->resetAfterTest(true);
669         // Create test user and 4 courses, two of which have guest access enabled.
670         $user = $this->getDataGenerator()->create_user();
671         $course1 = $this->getDataGenerator()->create_course(
672             (object)array('shortname' => 'X',
673                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
674                 'enrol_guest_password_0' => ''));
675         $course2 = $this->getDataGenerator()->create_course(
676             (object)array('shortname' => 'Z',
677                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
678                 'enrol_guest_password_0' => ''));
679         $course3 = $this->getDataGenerator()->create_course(
680             (object)array('shortname' => 'Y',
681                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
682                 'enrol_guest_password_0' => 'frog'));
683         $course4 = $this->getDataGenerator()->create_course(
684             (object)array('shortname' => 'W',
685                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
686                 'enrol_guest_password_0' => ''));
688         // User is enrolled in first course.
689         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
690         $this->getDataGenerator()->enrol_user($user->id, $course2->id);
691         $this->getDataGenerator()->enrol_user($user->id, $course3->id);
692         $this->getDataGenerator()->enrol_user($user->id, $course4->id);
694         // Check enrol_get_my_courses basic use (without include hidden provided).
695         $this->setUser($user);
696         $courses = enrol_get_my_courses();
697         $this->assertEquals([$course4->id, $course3->id, $course2->id, $course1->id], array_keys($courses));
699         // Hide a course.
700         set_user_preference('block_myoverview_hidden_course_' . $course3->id, true);
702         // Hidden course shouldn't be returned.
703         $courses = enrol_get_my_courses(null, null, 0, [], false, 0, [$course3->id]);
704         $this->assertEquals([$course4->id, $course2->id, $course1->id], array_keys($courses));
706         // Offset should take into account hidden course.
707         $courses = enrol_get_my_courses(null, null, 0, [], false, 2, [$course3->id]);
708         $this->assertEquals([$course1->id], array_keys($courses));
709     }
711     /**
712      * Tests the enrol_get_my_courses function when using the $allaccessible parameter, which
713      * includes a wider range of courses (enrolled courses + other accessible ones).
714      */
715     public function test_enrol_get_my_courses_all_accessible() {
716         global $DB, $CFG;
718         $this->resetAfterTest(true);
720         // Create test user and 4 courses, two of which have guest access enabled.
721         $user = $this->getDataGenerator()->create_user();
722         $course1 = $this->getDataGenerator()->create_course(
723                 (object)array('shortname' => 'X',
724                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
725                 'enrol_guest_password_0' => ''));
726         $course2 = $this->getDataGenerator()->create_course(
727                 (object)array('shortname' => 'Z',
728                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
729                 'enrol_guest_password_0' => ''));
730         $course3 = $this->getDataGenerator()->create_course(
731                 (object)array('shortname' => 'Y',
732                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
733                 'enrol_guest_password_0' => 'frog'));
734         $course4 = $this->getDataGenerator()->create_course(
735                 (object)array('shortname' => 'W',
736                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
737                 'enrol_guest_password_0' => ''));
739         // User is enrolled in first course.
740         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
742         // Check enrol_get_my_courses basic use (without all accessible).
743         $this->setUser($user);
744         $courses = enrol_get_my_courses();
745         $this->assertEquals([$course1->id], array_keys($courses));
747         // Turn on all accessible, now they can access the second course too.
748         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
749         $this->assertEquals([$course1->id, $course2->id], array_keys($courses));
751         // Log in as guest to third course.
752         load_temp_course_role(context_course::instance($course3->id), $CFG->guestroleid);
753         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
754         $this->assertEquals([$course1->id, $course2->id, $course3->id], array_keys($courses));
756         // Check fields parameter still works. Fields default (certain base fields).
757         $this->assertObjectHasAttribute('id', $courses[$course3->id]);
758         $this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
759         $this->assertObjectNotHasAttribute('summary', $courses[$course3->id]);
761         // Specified fields (one, string).
762         $courses = enrol_get_my_courses('summary', 'id', 0, [], true);
763         $this->assertObjectHasAttribute('id', $courses[$course3->id]);
764         $this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
765         $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
766         $this->assertObjectNotHasAttribute('summaryformat', $courses[$course3->id]);
768         // Specified fields (two, string).
769         $courses = enrol_get_my_courses('summary, summaryformat', 'id', 0, [], true);
770         $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
771         $this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
773         // Specified fields (two, array).
774         $courses = enrol_get_my_courses(['summary', 'summaryformat'], 'id', 0, [], true);
775         $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
776         $this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
778         // By default, courses are ordered by sortorder - which by default is most recent first.
779         $courses = enrol_get_my_courses(null, null, 0, [], true);
780         $this->assertEquals([$course3->id, $course2->id, $course1->id], array_keys($courses));
782         // Make sure that implicit sorting defined in navsortmycoursessort is respected.
783         $CFG->navsortmycoursessort = 'shortname';
784         $courses = enrol_get_my_courses(null, null, 0, [], true);
785         $this->assertEquals([$course1->id, $course3->id, $course2->id], array_keys($courses));
787         // But still the explicit sorting takes precedence over the implicit one.
788         $courses = enrol_get_my_courses(null, 'shortname DESC', 0, [], true);
789         $this->assertEquals([$course2->id, $course3->id, $course1->id], array_keys($courses));
791         // Check filter parameter still works.
792         $courses = enrol_get_my_courses(null, 'id', 0, [$course2->id, $course3->id, $course4->id], true);
793         $this->assertEquals([$course2->id, $course3->id], array_keys($courses));
795         // Check limit parameter.
796         $courses = enrol_get_my_courses(null, 'id', 2, [], true);
797         $this->assertEquals([$course1->id, $course2->id], array_keys($courses));
799         // Now try access for a different user who has manager role at system level.
800         $manager = $this->getDataGenerator()->create_user();
801         $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
802         role_assign($managerroleid, $manager->id, \context_system::instance()->id);
803         $this->setUser($manager);
805         // With default get enrolled, they don't have any courses.
806         $courses = enrol_get_my_courses();
807         $this->assertCount(0, $courses);
809         // But with all accessible, they have 4 because they have moodle/course:view everywhere.
810         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
811         $this->assertEquals([$course1->id, $course2->id, $course3->id, $course4->id],
812                 array_keys($courses));
814         // If we prohibit manager from course:view on course 1 though...
815         assign_capability('moodle/course:view', CAP_PROHIBIT, $managerroleid,
816                 \context_course::instance($course1->id));
817         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
818         $this->assertEquals([$course2->id, $course3->id, $course4->id], array_keys($courses));
820         // Check for admin user, which has a slightly different query.
821         $this->setAdminUser();
822         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
823         $this->assertEquals([$course1->id, $course2->id, $course3->id, $course4->id], array_keys($courses));
824     }
826     /**
827      * test_course_users
828      *
829      * @return void
830      */
831     public function test_course_users() {
832         $this->resetAfterTest();
834         $user1 = $this->getDataGenerator()->create_user();
835         $user2 = $this->getDataGenerator()->create_user();
836         $course1 = $this->getDataGenerator()->create_course();
837         $course2 = $this->getDataGenerator()->create_course();
839         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
840         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
841         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
843         $this->assertCount(2, enrol_get_course_users($course1->id));
844         $this->assertCount(2, enrol_get_course_users($course1->id, true));
846         $this->assertCount(1, enrol_get_course_users($course1->id, true, array($user1->id)));
848         $this->assertCount(2, enrol_get_course_users(false, false, array($user1->id)));
850         $instances = enrol_get_instances($course1->id, true);
851         $manualinstance = reset($instances);
853         $manualplugin = enrol_get_plugin('manual');
854         $manualplugin->update_user_enrol($manualinstance, $user1->id, ENROL_USER_SUSPENDED);
855         $this->assertCount(2, enrol_get_course_users($course1->id, false));
856         $this->assertCount(1, enrol_get_course_users($course1->id, true));
857     }
859     /**
860      * Test count of enrolled users
861      *
862      * @return void
863      */
864     public function test_count_enrolled_users() {
865         global $DB;
867         $this->resetAfterTest(true);
869         $course = $this->getDataGenerator()->create_course();
870         $context = \context_course::instance($course->id);
872         $user1 = $this->getDataGenerator()->create_user();
873         $user2 = $this->getDataGenerator()->create_user();
875         $studentrole = $DB->get_record('role', ['shortname' => 'student']);
877         // Add each user to the manual enrolment instance.
878         $manual = enrol_get_plugin('manual');
880         $manualinstance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual'], '*', MUST_EXIST);
882         $manual->enrol_user($manualinstance, $user1->id, $studentrole->id);
883         $manual->enrol_user($manualinstance, $user2->id, $studentrole->id);
885         $this->assertEquals(2, count_enrolled_users($context));
887         // Create a self enrolment instance, enrol first user only.
888         $self = enrol_get_plugin('self');
890         $selfid = $self->add_instance($course,
891             ['status' => ENROL_INSTANCE_ENABLED, 'name' => 'Self', 'customint6' => 1, 'roleid' => $studentrole->id]);
892         $selfinstance = $DB->get_record('enrol', ['id' => $selfid], '*', MUST_EXIST);
894         $self->enrol_user($selfinstance, $user1->id, $studentrole->id);
896         // There are still only two distinct users.
897         $this->assertEquals(2, count_enrolled_users($context));
898     }
900     /**
901      * Test cases for the test_enrol_get_my_courses_sort_by_last_access test.
902      */
903     public function get_enrol_get_my_courses_sort_by_last_access_test_cases() {
904         $now = time();
906         $enrolledcoursesdata = [
907             ['shortname' => 'a', 'lastaccess' => $now - 2],
908             ['shortname' => 'b', 'lastaccess' => $now - 1],
909             ['shortname' => 'c', 'lastaccess' => $now],
910             ['shortname' => 'd', 'lastaccess' => $now - 1],
911             ['shortname' => 'e']
912         ];
913         $unenrolledcoursesdata = [
914             ['shortname' => 'x', 'lastaccess' => $now - 2],
915             ['shortname' => 'y', 'lastaccess' => $now - 1],
916             ['shortname' => 'z', 'lastaccess' => $now]
917         ];
919         return [
920             'empty set' => [
921                 'enrolledcoursesdata' => [],
922                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
923                 'sort' => 'ul.timeaccess asc',
924                 'limit' => 0,
925                 'offset' => 0,
926                 'expectedcourses' => []
927             ],
928             'ul.timeaccess asc, shortname asc no limit or offset' => [
929                 'enrolledcoursesdata' => $enrolledcoursesdata,
930                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
931                 'sort' => 'ul.timeaccess asc, shortname asc',
932                 'limit' => 0,
933                 'offset' => 0,
934                 'expectedcourses' => ['e', 'a', 'b', 'd', 'c']
935             ],
936             'ul.timeaccess asc, shortname asc with limit no offset' => [
937                 'enrolledcoursesdata' => $enrolledcoursesdata,
938                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
939                 'sort' => 'ul.timeaccess asc, shortname asc',
940                 'limit' => 2,
941                 'offset' => 0,
942                 'expectedcourses' => ['e', 'a']
943             ],
944             'ul.timeaccess asc, shortname asc with limit and offset' => [
945                 'enrolledcoursesdata' => $enrolledcoursesdata,
946                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
947                 'sort' => 'ul.timeaccess asc, shortname asc',
948                 'limit' => 2,
949                 'offset' => 2,
950                 'expectedcourses' => ['b', 'd']
951             ],
952             'ul.timeaccess asc, shortname asc with limit and offset beyond end of data set' => [
953                 'enrolledcoursesdata' => $enrolledcoursesdata,
954                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
955                 'sort' => 'ul.timeaccess asc, shortname asc',
956                 'limit' => 2,
957                 'offset' => 4,
958                 'expectedcourses' => ['c']
959             ],
960             'ul.timeaccess desc, shortname asc no limit or offset' => [
961                 'enrolledcoursesdata' => $enrolledcoursesdata,
962                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
963                 'sort' => 'ul.timeaccess desc, shortname asc',
964                 'limit' => 0,
965                 'offset' => 0,
966                 'expectedcourses' => ['c', 'b', 'd', 'a', 'e']
967             ],
968             'ul.timeaccess desc, shortname desc, no limit or offset' => [
969                 'enrolledcoursesdata' => $enrolledcoursesdata,
970                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
971                 'sort' => 'ul.timeaccess desc, shortname desc',
972                 'limit' => 0,
973                 'offset' => 0,
974                 'expectedcourses' => ['c', 'd', 'b', 'a', 'e']
975             ],
976             'ul.timeaccess asc, shortname desc, no limit or offset' => [
977                 'enrolledcoursesdata' => $enrolledcoursesdata,
978                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
979                 'sort' => 'ul.timeaccess asc, shortname desc',
980                 'limit' => 0,
981                 'offset' => 0,
982                 'expectedcourses' => ['e', 'a', 'd', 'b', 'c']
983             ],
984             'shortname asc, no limit or offset' => [
985                 'enrolledcoursesdata' => $enrolledcoursesdata,
986                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
987                 'sort' => 'shortname asc',
988                 'limit' => 0,
989                 'offset' => 0,
990                 'expectedcourses' => ['a', 'b', 'c', 'd', 'e']
991             ],
992             'shortname desc, no limit or offset' => [
993                 'enrolledcoursesdata' => $enrolledcoursesdata,
994                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
995                 'sort' => 'shortname desc',
996                 'limit' => 0,
997                 'offset' => 0,
998                 'expectedcourses' => ['e', 'd', 'c', 'b', 'a']
999             ],
1000         ];
1001     }
1003     /**
1004      * Test the get_enrolled_courses_by_timeline_classification function.
1005      *
1006      * @dataProvider get_enrol_get_my_courses_sort_by_last_access_test_cases()
1007      * @param array $enrolledcoursesdata Courses to create and enrol the user in
1008      * @param array $unenrolledcoursesdata Courses to create nut not enrol the user in
1009      * @param string $sort Sort string for the enrol function
1010      * @param int $limit Maximum number of results
1011      * @param int $offset Offset the courses result set by this amount
1012      * @param array $expectedcourses Expected courses in result
1013      */
1014     public function test_enrol_get_my_courses_sort_by_last_access(
1015         $enrolledcoursesdata,
1016         $unenrolledcoursesdata,
1017         $sort,
1018         $limit,
1019         $offset,
1020         $expectedcourses
1021     ) {
1022         global $DB, $CFG;
1024         $this->resetAfterTest();
1025         $generator = $this->getDataGenerator();
1026         $student = $generator->create_user();
1027         $lastaccessrecords = [];
1029         foreach ($enrolledcoursesdata as $coursedata) {
1030             $lastaccess = null;
1032             if (isset($coursedata['lastaccess'])) {
1033                 $lastaccess = $coursedata['lastaccess'];
1034                 unset($coursedata['lastaccess']);
1035             }
1037             $course = $generator->create_course($coursedata);
1038             $generator->enrol_user($student->id, $course->id, 'student');
1040             if (!is_null($lastaccess)) {
1041                 $lastaccessrecords[] = [
1042                     'userid' => $student->id,
1043                     'courseid' => $course->id,
1044                     'timeaccess' => $lastaccess
1045                 ];
1046             }
1047         }
1049         foreach ($unenrolledcoursesdata as $coursedata) {
1050             $lastaccess = null;
1052             if (isset($coursedata['lastaccess'])) {
1053                 $lastaccess = $coursedata['lastaccess'];
1054                 unset($coursedata['lastaccess']);
1055             }
1057             $course = $generator->create_course($coursedata);
1059             if (!is_null($lastaccess)) {
1060                 $lastaccessrecords[] = [
1061                     'userid' => $student->id,
1062                     'courseid' => $course->id,
1063                     'timeaccess' => $lastaccess
1064                 ];
1065             }
1066         }
1068         if (!empty($lastaccessrecords)) {
1069             $DB->insert_records('user_lastaccess', $lastaccessrecords);
1070         }
1072         $this->setUser($student);
1074         $result = enrol_get_my_courses('shortname', $sort, $limit, [], false, $offset);
1075         $actual = array_map(function($course) {
1076             return $course->shortname;
1077         }, array_values($result));
1079         $this->assertEquals($expectedcourses, $actual);
1080     }
1082     /**
1083      * Test enrol_get_course_users_roles function.
1084      *
1085      * @return void
1086      */
1087     public function test_enrol_get_course_users_roles() {
1088         global $DB;
1090         $this->resetAfterTest();
1092         $user1 = $this->getDataGenerator()->create_user();
1093         $user2 = $this->getDataGenerator()->create_user();
1094         $course = $this->getDataGenerator()->create_course();
1095         $context = context_course::instance($course->id);
1097         $roles = array();
1098         $roles['student'] = $DB->get_field('role', 'id', array('shortname' => 'student'), MUST_EXIST);
1099         $roles['teacher'] = $DB->get_field('role', 'id', array('shortname' => 'teacher'), MUST_EXIST);
1101         $manual = enrol_get_plugin('manual');
1102         $this->assertNotEmpty($manual);
1104         $enrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
1106         // Test without enrolments.
1107         $this->assertEmpty(enrol_get_course_users_roles($course->id));
1109         // Test with 1 user, 1 role.
1110         $manual->enrol_user($enrol, $user1->id, $roles['student']);
1111         $return = enrol_get_course_users_roles($course->id);
1112         $this->assertArrayHasKey($user1->id, $return);
1113         $this->assertArrayHasKey($roles['student'], $return[$user1->id]);
1114         $this->assertArrayNotHasKey($roles['teacher'], $return[$user1->id]);
1116         // Test with 1 user, 2 role.
1117         $manual->enrol_user($enrol, $user1->id, $roles['teacher']);
1118         $return = enrol_get_course_users_roles($course->id);
1119         $this->assertArrayHasKey($user1->id, $return);
1120         $this->assertArrayHasKey($roles['student'], $return[$user1->id]);
1121         $this->assertArrayHasKey($roles['teacher'], $return[$user1->id]);
1123         // Test with another user, 1 role.
1124         $manual->enrol_user($enrol, $user2->id, $roles['student']);
1125         $return = enrol_get_course_users_roles($course->id);
1126         $this->assertArrayHasKey($user1->id, $return);
1127         $this->assertArrayHasKey($roles['student'], $return[$user1->id]);
1128         $this->assertArrayHasKey($roles['teacher'], $return[$user1->id]);
1129         $this->assertArrayHasKey($user2->id, $return);
1130         $this->assertArrayHasKey($roles['student'], $return[$user2->id]);
1131         $this->assertArrayNotHasKey($roles['teacher'], $return[$user2->id]);
1132     }
1134     /**
1135      * Test enrol_calculate_duration function
1136      */
1137     public function test_enrol_calculate_duration() {
1138         // Start time 07/01/2019 @ 12:00am (UTC).
1139         $timestart = 1561939200;
1140         // End time 07/05/2019 @ 12:00am (UTC).
1141         $timeend = 1562284800;
1142         $duration = enrol_calculate_duration($timestart, $timeend);
1143         $durationinday = $duration / DAYSECS;
1144         $this->assertEquals(4, $durationinday);
1146         // End time 07/10/2019 @ 12:00am (UTC).
1147         $timeend = 1562716800;
1148         $duration = enrol_calculate_duration($timestart, $timeend);
1149         $durationinday = $duration / DAYSECS;
1150         $this->assertEquals(9, $durationinday);
1151     }