MDL-63457 block_myoverview: Update getters for enrolled courses
[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             'category' => $category1->id,
62         ));
63         $course2 = $this->getDataGenerator()->create_course(array(
64             'shortname' => 'X',
65             'category' => $category2->id,
66         ));
67         $course3 = $this->getDataGenerator()->create_course(array(
68             'shortname' => 'Y',
69             'category' => $category2->id,
70             'visible' => 0,
71         ));
72         $course4 = $this->getDataGenerator()->create_course(array(
73             'shortname' => 'W',
74             'category' => $category2->id,
75         ));
77         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
78         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
79         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
80         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
81         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
82         $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
84         $manual = enrol_get_plugin('manual');
85         $this->assertNotEmpty($manual);
87         $manual->enrol_user($maninstance1, $user1->id, $teacherrole->id);
88         $manual->enrol_user($maninstance1, $user2->id, $studentrole->id);
89         $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
90         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
92         $manual->enrol_user($maninstance2, $user1->id);
93         $manual->enrol_user($maninstance2, $user2->id);
94         $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()+(60*60));
96         $manual->enrol_user($maninstance3, $user1->id);
97         $manual->enrol_user($maninstance3, $user2->id);
98         $manual->enrol_user($maninstance3, $user3->id, 0, 1, time()-(60*60));
99         $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
102         $courses = enrol_get_all_users_courses($CFG->siteguest);
103         $this->assertSame(array(), $courses);
105         $courses = enrol_get_all_users_courses(0);
106         $this->assertSame(array(), $courses);
108         // Results are sorted by visibility, sortorder by default (in our case order of creation)
110         $courses = enrol_get_all_users_courses($admin->id);
111         $this->assertCount(1, $courses);
112         $this->assertEquals(array($course1->id), array_keys($courses));
114         $courses = enrol_get_all_users_courses($admin->id, true);
115         $this->assertCount(0, $courses);
116         $this->assertEquals(array(), array_keys($courses));
118         $courses = enrol_get_all_users_courses($user1->id);
119         $this->assertCount(3, $courses);
120         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
122         $courses = enrol_get_all_users_courses($user1->id, true);
123         $this->assertCount(2, $courses);
124         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
126         $courses = enrol_get_all_users_courses($user2->id);
127         $this->assertCount(3, $courses);
128         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
130         $courses = enrol_get_all_users_courses($user2->id, true);
131         $this->assertCount(2, $courses);
132         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
134         $courses = enrol_get_all_users_courses($user3->id);
135         $this->assertCount(2, $courses);
136         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
138         $courses = enrol_get_all_users_courses($user3->id, true);
139         $this->assertCount(1, $courses);
140         $this->assertEquals(array($course2->id), array_keys($courses));
142         $courses = enrol_get_all_users_courses($user4->id);
143         $this->assertCount(2, $courses);
144         $this->assertEquals(array($course1->id, $course3->id), array_keys($courses));
146         $courses = enrol_get_all_users_courses($user4->id, true);
147         $this->assertCount(0, $courses);
148         $this->assertEquals(array(), array_keys($courses));
150         // Make sure sorting and columns work.
152         $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber',
153             'startdate', 'visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid');
155         $courses = enrol_get_all_users_courses($user2->id, true);
156         $course = reset($courses);
157         context_helper::preload_from_record($course);
158         $course = (array)$course;
159         $this->assertEquals($basefields, array_keys($course), '', 0, 10, true);
161         $courses = enrol_get_all_users_courses($user2->id, false, 'timecreated');
162         $course = reset($courses);
163         $this->assertTrue(property_exists($course, 'timecreated'));
165         $courses = enrol_get_all_users_courses($user2->id, false, null, 'id DESC');
166         $this->assertEquals(array($course3->id, $course2->id, $course1->id), array_keys($courses));
168         // Make sure that implicit sorting defined in navsortmycoursessort is respected.
170         $CFG->navsortmycoursessort = 'shortname';
172         $courses = enrol_get_all_users_courses($user1->id);
173         $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
175         // But still the explicit sorting takes precedence over the implicit one.
177         $courses = enrol_get_all_users_courses($user1->id, false, null, 'shortname DESC');
178         $this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
179     }
181     public function test_enrol_user_sees_own_courses() {
182         global $DB, $CFG;
184         $this->resetAfterTest();
186         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
187         $this->assertNotEmpty($studentrole);
188         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
189         $this->assertNotEmpty($teacherrole);
191         $admin = get_admin();
192         $user1 = $this->getDataGenerator()->create_user();
193         $user2 = $this->getDataGenerator()->create_user();
194         $user3 = $this->getDataGenerator()->create_user();
195         $user4 = $this->getDataGenerator()->create_user();
196         $user5 = $this->getDataGenerator()->create_user();
197         $user6 = $this->getDataGenerator()->create_user();
199         $category1 = $this->getDataGenerator()->create_category(array('visible'=>0));
200         $category2 = $this->getDataGenerator()->create_category();
201         $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
202         $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
203         $course3 = $this->getDataGenerator()->create_course(array('category'=>$category2->id, 'visible'=>0));
204         $course4 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
206         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
207         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
208         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
209         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
210         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
211         $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
213         $manual = enrol_get_plugin('manual');
214         $this->assertNotEmpty($manual);
216         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
218         $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id);
220         $manual->enrol_user($maninstance2, $user2->id, $studentrole->id);
222         $manual->enrol_user($maninstance1, $user3->id, $studentrole->id, 1, time()+(60*60));
223         $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()-(60*60));
224         $manual->enrol_user($maninstance3, $user2->id, $studentrole->id);
225         $manual->enrol_user($maninstance4, $user2->id, 0, 0, 0, ENROL_USER_SUSPENDED);
227         $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
228         $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
231         $this->assertFalse(enrol_user_sees_own_courses($CFG->siteguest));
232         $this->assertFalse(enrol_user_sees_own_courses(0));
233         $this->assertFalse(enrol_user_sees_own_courses($admin));
234         $this->assertFalse(enrol_user_sees_own_courses(-222)); // Nonexistent user.
236         $this->assertTrue(enrol_user_sees_own_courses($user1));
237         $this->assertTrue(enrol_user_sees_own_courses($user2->id));
238         $this->assertFalse(enrol_user_sees_own_courses($user3->id));
239         $this->assertFalse(enrol_user_sees_own_courses($user4));
240         $this->assertFalse(enrol_user_sees_own_courses($user5));
242         $this->setAdminUser();
243         $this->assertFalse(enrol_user_sees_own_courses());
245         $this->setGuestUser();
246         $this->assertFalse(enrol_user_sees_own_courses());
248         $this->setUser(0);
249         $this->assertFalse(enrol_user_sees_own_courses());
251         $this->setUser($user1);
252         $this->assertTrue(enrol_user_sees_own_courses());
254         $this->setUser($user2);
255         $this->assertTrue(enrol_user_sees_own_courses());
257         $this->setUser($user3);
258         $this->assertFalse(enrol_user_sees_own_courses());
260         $this->setUser($user4);
261         $this->assertFalse(enrol_user_sees_own_courses());
263         $this->setUser($user5);
264         $this->assertFalse(enrol_user_sees_own_courses());
266         $user1 = $DB->get_record('user', array('id'=>$user1->id));
267         $this->setUser($user1);
268         $reads = $DB->perf_get_reads();
269         $this->assertTrue(enrol_user_sees_own_courses());
270         $this->assertGreaterThan($reads, $DB->perf_get_reads());
272         $user1 = $DB->get_record('user', array('id'=>$user1->id));
273         $this->setUser($user1);
274         require_login($course3);
275         $reads = $DB->perf_get_reads();
276         $this->assertTrue(enrol_user_sees_own_courses());
277         $this->assertEquals($reads, $DB->perf_get_reads());
278     }
280     public function test_enrol_get_shared_courses() {
281         $this->resetAfterTest();
283         $user1 = $this->getDataGenerator()->create_user();
284         $user2 = $this->getDataGenerator()->create_user();
285         $user3 = $this->getDataGenerator()->create_user();
287         $course1 = $this->getDataGenerator()->create_course();
288         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
289         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
291         $course2 = $this->getDataGenerator()->create_course();
292         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
294         // Test that user1 and user2 have courses in common.
295         $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
296         // Test that user1 and user3 have no courses in common.
297         $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
299         // Test retrieving the courses in common.
300         $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
302         // Only should be one shared course.
303         $this->assertCount(1, $sharedcourses);
304         $sharedcourse = array_shift($sharedcourses);
305         // It should be course 1.
306         $this->assertEquals($sharedcourse->id, $course1->id);
307     }
309     public function test_enrol_get_shared_courses_different_methods() {
310         global $DB, $CFG;
312         require_once($CFG->dirroot . '/enrol/self/externallib.php');
314         $this->resetAfterTest();
316         $user1 = $this->getDataGenerator()->create_user();
317         $user2 = $this->getDataGenerator()->create_user();
318         $user3 = $this->getDataGenerator()->create_user();
320         $course1 = $this->getDataGenerator()->create_course();
322         // Enrol user1 and user2 in course1 with a different enrolment methode.
323         // Add self enrolment method for course1.
324         $selfplugin = enrol_get_plugin('self');
325         $this->assertNotEmpty($selfplugin);
327         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
328         $this->assertNotEmpty($studentrole);
330         $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
331                                                                  'name' => 'Test instance 1',
332                                                                  'customint6' => 1,
333                                                                  'roleid' => $studentrole->id));
335         $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
337         self::setUser($user2);
338         // Self enrol me (user2).
339         $result = enrol_self_external::enrol_user($course1->id);
341         // Enrol user1 manually.
342         $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'manual');
344         $course2 = $this->getDataGenerator()->create_course();
345         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
347         $course3 = $this->getDataGenerator()->create_course();
348         $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
350         // Test that user1 and user2 have courses in common.
351         $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
352         // Test that user1 and user3 have no courses in common.
353         $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
355         // Test retrieving the courses in common.
356         $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
358         // Only should be one shared course.
359         $this->assertCount(1, $sharedcourses);
360         $sharedcourse = array_shift($sharedcourses);
361         // It should be course 1.
362         $this->assertEquals($sharedcourse->id, $course1->id);
363     }
365     /**
366      * Test user enrolment created event.
367      */
368     public function test_user_enrolment_created_event() {
369         global $DB;
371         $this->resetAfterTest();
373         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
374         $this->assertNotEmpty($studentrole);
376         $admin = get_admin();
378         $course1 = $this->getDataGenerator()->create_course();
380         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
382         $manual = enrol_get_plugin('manual');
383         $this->assertNotEmpty($manual);
385         // Enrol user and capture event.
386         $sink = $this->redirectEvents();
387         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
388         $events = $sink->get_events();
389         $sink->close();
390         $event = array_shift($events);
392         $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $admin->id));
393         $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
394         $this->assertEquals($dbuserenrolled->id, $event->objectid);
395         $this->assertEquals(context_course::instance($course1->id), $event->get_context());
396         $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
397         $expectedlegacyeventdata = $dbuserenrolled;
398         $expectedlegacyeventdata->enrol = $manual->get_name();
399         $expectedlegacyeventdata->courseid = $course1->id;
400         $this->assertEventLegacyData($expectedlegacyeventdata, $event);
401         $expected = array($course1->id, 'course', 'enrol', '../enrol/users.php?id=' . $course1->id, $course1->id);
402         $this->assertEventLegacyLogData($expected, $event);
403         $this->assertEventContextNotUsed($event);
404     }
406     /**
407      * Test user_enrolment_deleted event.
408      */
409     public function test_user_enrolment_deleted_event() {
410         global $DB;
412         $this->resetAfterTest(true);
414         $manualplugin = enrol_get_plugin('manual');
415         $user = $this->getDataGenerator()->create_user();
416         $course = $this->getDataGenerator()->create_course();
417         $student = $DB->get_record('role', array('shortname' => 'student'));
419         $enrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
421         // Enrol user.
422         $manualplugin->enrol_user($enrol, $user->id, $student->id);
424         // Get the user enrolment information, used to validate legacy event data.
425         $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user->id));
427         // Unenrol user and capture event.
428         $sink = $this->redirectEvents();
429         $manualplugin->unenrol_user($enrol, $user->id);
430         $events = $sink->get_events();
431         $sink->close();
432         $event = array_pop($events);
434         // Validate the event.
435         $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
436         $this->assertEquals(context_course::instance($course->id), $event->get_context());
437         $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
438         $expectedlegacyeventdata = $dbuserenrolled;
439         $expectedlegacyeventdata->enrol = $manualplugin->get_name();
440         $expectedlegacyeventdata->courseid = $course->id;
441         $expectedlegacyeventdata->lastenrol = true;
442         $this->assertEventLegacyData($expectedlegacyeventdata, $event);
443         $expected = array($course->id, 'course', 'unenrol', '../enrol/users.php?id=' . $course->id, $course->id);
444         $this->assertEventLegacyLogData($expected, $event);
445         $this->assertEventContextNotUsed($event);
446     }
448     /**
449      * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
450      */
451     public function test_instance_events() {
452         global $DB;
454         $this->resetAfterTest(true);
456         $selfplugin = enrol_get_plugin('self');
457         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
459         $course = $this->getDataGenerator()->create_course();
461         // Creating enrol instance.
462         $sink = $this->redirectEvents();
463         $instanceid = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
464                                                                 'name' => 'Test instance 1',
465                                                                 'customint6' => 1,
466                                                                 'roleid' => $studentrole->id));
467         $events = $sink->get_events();
468         $sink->close();
470         $this->assertCount(1, $events);
471         $event = array_pop($events);
472         $this->assertInstanceOf('\core\event\enrol_instance_created', $event);
473         $this->assertEquals(context_course::instance($course->id), $event->get_context());
474         $this->assertEquals('self', $event->other['enrol']);
475         $this->assertEventContextNotUsed($event);
477         // Updating enrol instance.
478         $instance = $DB->get_record('enrol', array('id' => $instanceid));
479         $sink = $this->redirectEvents();
480         $selfplugin->update_status($instance, ENROL_INSTANCE_DISABLED);
482         $events = $sink->get_events();
483         $sink->close();
485         $this->assertCount(1, $events);
486         $event = array_pop($events);
487         $this->assertInstanceOf('\core\event\enrol_instance_updated', $event);
488         $this->assertEquals(context_course::instance($course->id), $event->get_context());
489         $this->assertEquals('self', $event->other['enrol']);
490         $this->assertEventContextNotUsed($event);
492         // Deleting enrol instance.
493         $instance = $DB->get_record('enrol', array('id' => $instanceid));
494         $sink = $this->redirectEvents();
495         $selfplugin->delete_instance($instance);
497         $events = $sink->get_events();
498         $sink->close();
500         $this->assertCount(1, $events);
501         $event = array_pop($events);
502         $this->assertInstanceOf('\core\event\enrol_instance_deleted', $event);
503         $this->assertEquals(context_course::instance($course->id), $event->get_context());
504         $this->assertEquals('self', $event->other['enrol']);
505         $this->assertEventContextNotUsed($event);
506     }
508     /**
509      * Confirms that timemodified field was updated after modification of user enrollment
510      */
511     public function test_enrollment_update_timemodified() {
512         global $DB;
514         $this->resetAfterTest(true);
515         $datagen = $this->getDataGenerator();
517         /** @var enrol_manual_plugin $manualplugin */
518         $manualplugin = enrol_get_plugin('manual');
519         $this->assertNotNull($manualplugin);
521         $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
522         $course = $datagen->create_course();
523         $user = $datagen->create_user();
525         $instanceid = null;
526         $instances = enrol_get_instances($course->id, true);
527         foreach ($instances as $inst) {
528             if ($inst->enrol == 'manual') {
529                 $instanceid = (int)$inst->id;
530                 break;
531             }
532         }
533         if (empty($instanceid)) {
534             $instanceid = $manualplugin->add_default_instance($course);
535             if (empty($instanceid)) {
536                 $instanceid = $manualplugin->add_instance($course);
537             }
538         }
539         $this->assertNotNull($instanceid);
541         $instance = $DB->get_record('enrol', ['id' => $instanceid], '*', MUST_EXIST);
542         $manualplugin->enrol_user($instance, $user->id, $studentroleid, 0, 0, ENROL_USER_ACTIVE);
543         $userenrolorig = (int)$DB->get_field(
544             'user_enrolments',
545             'timemodified',
546             ['enrolid' => $instance->id, 'userid' => $user->id],
547             MUST_EXIST
548         );
549         $this->waitForSecond();
550         $this->waitForSecond();
551         $manualplugin->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED);
552         $userenrolpost = (int)$DB->get_field(
553             'user_enrolments',
554             'timemodified',
555             ['enrolid' => $instance->id, 'userid' => $user->id],
556             MUST_EXIST
557         );
559         $this->assertGreaterThan($userenrolorig, $userenrolpost);
560     }
562     /**
563      * Test to confirm that enrol_get_my_courses only return the courses that
564      * the logged in user is enrolled in.
565      */
566     public function test_enrol_get_my_courses_only_enrolled_courses() {
567         $user = $this->getDataGenerator()->create_user();
568         $course1 = $this->getDataGenerator()->create_course();
569         $course2 = $this->getDataGenerator()->create_course();
570         $course3 = $this->getDataGenerator()->create_course();
571         $course4 = $this->getDataGenerator()->create_course();
573         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
574         $this->getDataGenerator()->enrol_user($user->id, $course2->id);
575         $this->getDataGenerator()->enrol_user($user->id, $course3->id);
576         $this->resetAfterTest(true);
577         $this->setUser($user);
579         // By default this function should return all of the courses the user
580         // is enrolled in.
581         $courses = enrol_get_my_courses();
583         $this->assertCount(3, $courses);
584         $this->assertEquals($course1->id, $courses[$course1->id]->id);
585         $this->assertEquals($course2->id, $courses[$course2->id]->id);
586         $this->assertEquals($course3->id, $courses[$course3->id]->id);
588         // If a set of course ids are provided then the result set will only contain
589         // these courses.
590         $courseids = [$course1->id, $course2->id];
591         $courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
593         $this->assertCount(2, $courses);
594         $this->assertEquals($course1->id, $courses[$course1->id]->id);
595         $this->assertEquals($course2->id, $courses[$course2->id]->id);
597         // If the course ids list contains any ids for courses the user isn't enrolled in
598         // then they will be ignored (in this case $course4).
599         $courseids = [$course1->id, $course2->id, $course4->id];
600         $courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
602         $this->assertCount(2, $courses);
603         $this->assertEquals($course1->id, $courses[$course1->id]->id);
604         $this->assertEquals($course2->id, $courses[$course2->id]->id);
605     }
607     /**
608      * Tests the enrol_get_my_courses function when using the $includehidden parameter, which
609      * should remove any courses hidden from the user's timeline
610      *
611      * @throws coding_exception
612      * @throws dml_exception
613      */
614     public function test_enrol_get_my_courses_include_hidden() {
615         global $DB, $CFG;
617         $this->resetAfterTest(true);
619         // Create test user and 4 courses, two of which have guest access enabled.
620         $user = $this->getDataGenerator()->create_user();
621         $course1 = $this->getDataGenerator()->create_course(
622             (object)array('shortname' => 'X',
623                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
624                 'enrol_guest_password_0' => ''));
625         $course2 = $this->getDataGenerator()->create_course(
626             (object)array('shortname' => 'Z',
627                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
628                 'enrol_guest_password_0' => ''));
629         $course3 = $this->getDataGenerator()->create_course(
630             (object)array('shortname' => 'Y',
631                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
632                 'enrol_guest_password_0' => 'frog'));
633         $course4 = $this->getDataGenerator()->create_course(
634             (object)array('shortname' => 'W',
635                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
636                 'enrol_guest_password_0' => ''));
638         // User is enrolled in first course.
639         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
640         $this->getDataGenerator()->enrol_user($user->id, $course2->id);
641         $this->getDataGenerator()->enrol_user($user->id, $course3->id);
642         $this->getDataGenerator()->enrol_user($user->id, $course4->id);
644         // Check enrol_get_my_courses basic use (without include hidden provided).
645         $this->setUser($user);
646         $courses = enrol_get_my_courses();
647         $this->assertEquals([$course4->id, $course3->id, $course2->id, $course1->id], array_keys($courses));
649         // Hide a course.
650         set_user_preference('block_myoverview_hidden_course_' . $course3->id, true);
652         // Hidden course shouldn't be returned.
653         $courses = enrol_get_my_courses(null, null, 0, [], false, 0, [$course3->id]);
654         $this->assertEquals([$course4->id, $course2->id, $course1->id], array_keys($courses));
656         // Offset should take into account hidden course.
657         $courses = enrol_get_my_courses(null, null, 0, [], false, 2, [$course3->id]);
658         $this->assertEquals([$course1->id], array_keys($courses));
659     }
661     /**
662      * Tests the enrol_get_my_courses function when using the $allaccessible parameter, which
663      * includes a wider range of courses (enrolled courses + other accessible ones).
664      */
665     public function test_enrol_get_my_courses_all_accessible() {
666         global $DB, $CFG;
668         $this->resetAfterTest(true);
670         // Create test user and 4 courses, two of which have guest access enabled.
671         $user = $this->getDataGenerator()->create_user();
672         $course1 = $this->getDataGenerator()->create_course(
673                 (object)array('shortname' => 'X',
674                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
675                 'enrol_guest_password_0' => ''));
676         $course2 = $this->getDataGenerator()->create_course(
677                 (object)array('shortname' => 'Z',
678                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
679                 'enrol_guest_password_0' => ''));
680         $course3 = $this->getDataGenerator()->create_course(
681                 (object)array('shortname' => 'Y',
682                 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
683                 'enrol_guest_password_0' => 'frog'));
684         $course4 = $this->getDataGenerator()->create_course(
685                 (object)array('shortname' => 'W',
686                 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
687                 'enrol_guest_password_0' => ''));
689         // User is enrolled in first course.
690         $this->getDataGenerator()->enrol_user($user->id, $course1->id);
692         // Check enrol_get_my_courses basic use (without all accessible).
693         $this->setUser($user);
694         $courses = enrol_get_my_courses();
695         $this->assertEquals([$course1->id], array_keys($courses));
697         // Turn on all accessible, now they can access the second course too.
698         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
699         $this->assertEquals([$course1->id, $course2->id], array_keys($courses));
701         // Log in as guest to third course.
702         load_temp_course_role(context_course::instance($course3->id), $CFG->guestroleid);
703         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
704         $this->assertEquals([$course1->id, $course2->id, $course3->id], array_keys($courses));
706         // Check fields parameter still works. Fields default (certain base fields).
707         $this->assertObjectHasAttribute('id', $courses[$course3->id]);
708         $this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
709         $this->assertObjectNotHasAttribute('summary', $courses[$course3->id]);
711         // Specified fields (one, string).
712         $courses = enrol_get_my_courses('summary', 'id', 0, [], true);
713         $this->assertObjectHasAttribute('id', $courses[$course3->id]);
714         $this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
715         $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
716         $this->assertObjectNotHasAttribute('summaryformat', $courses[$course3->id]);
718         // Specified fields (two, string).
719         $courses = enrol_get_my_courses('summary, summaryformat', 'id', 0, [], true);
720         $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
721         $this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
723         // Specified fields (two, array).
724         $courses = enrol_get_my_courses(['summary', 'summaryformat'], 'id', 0, [], true);
725         $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
726         $this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
728         // By default, courses are ordered by sortorder - which by default is most recent first.
729         $courses = enrol_get_my_courses(null, null, 0, [], true);
730         $this->assertEquals([$course3->id, $course2->id, $course1->id], array_keys($courses));
732         // Make sure that implicit sorting defined in navsortmycoursessort is respected.
733         $CFG->navsortmycoursessort = 'shortname';
734         $courses = enrol_get_my_courses(null, null, 0, [], true);
735         $this->assertEquals([$course1->id, $course3->id, $course2->id], array_keys($courses));
737         // But still the explicit sorting takes precedence over the implicit one.
738         $courses = enrol_get_my_courses(null, 'shortname DESC', 0, [], true);
739         $this->assertEquals([$course2->id, $course3->id, $course1->id], array_keys($courses));
741         // Check filter parameter still works.
742         $courses = enrol_get_my_courses(null, 'id', 0, [$course2->id, $course3->id, $course4->id], true);
743         $this->assertEquals([$course2->id, $course3->id], array_keys($courses));
745         // Check limit parameter.
746         $courses = enrol_get_my_courses(null, 'id', 2, [], true);
747         $this->assertEquals([$course1->id, $course2->id], array_keys($courses));
749         // Now try access for a different user who has manager role at system level.
750         $manager = $this->getDataGenerator()->create_user();
751         $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
752         role_assign($managerroleid, $manager->id, \context_system::instance()->id);
753         $this->setUser($manager);
755         // With default get enrolled, they don't have any courses.
756         $courses = enrol_get_my_courses();
757         $this->assertCount(0, $courses);
759         // But with all accessible, they have 4 because they have moodle/course:view everywhere.
760         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
761         $this->assertEquals([$course1->id, $course2->id, $course3->id, $course4->id],
762                 array_keys($courses));
764         // If we prohibit manager from course:view on course 1 though...
765         assign_capability('moodle/course:view', CAP_PROHIBIT, $managerroleid,
766                 \context_course::instance($course1->id));
767         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
768         $this->assertEquals([$course2->id, $course3->id, $course4->id], array_keys($courses));
770         // Check for admin user, which has a slightly different query.
771         $this->setAdminUser();
772         $courses = enrol_get_my_courses(null, 'id', 0, [], true);
773         $this->assertEquals([$course1->id, $course2->id, $course3->id, $course4->id], array_keys($courses));
774     }
776     /**
777      * test_course_users
778      *
779      * @return void
780      */
781     public function test_course_users() {
782         $this->resetAfterTest();
784         $user1 = $this->getDataGenerator()->create_user();
785         $user2 = $this->getDataGenerator()->create_user();
786         $course1 = $this->getDataGenerator()->create_course();
787         $course2 = $this->getDataGenerator()->create_course();
789         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
790         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
791         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
793         $this->assertCount(2, enrol_get_course_users($course1->id));
794         $this->assertCount(2, enrol_get_course_users($course1->id, true));
796         $this->assertCount(1, enrol_get_course_users($course1->id, true, array($user1->id)));
798         $this->assertCount(2, enrol_get_course_users(false, false, array($user1->id)));
800         $instances = enrol_get_instances($course1->id, true);
801         $manualinstance = reset($instances);
803         $manualplugin = enrol_get_plugin('manual');
804         $manualplugin->update_user_enrol($manualinstance, $user1->id, ENROL_USER_SUSPENDED);
805         $this->assertCount(2, enrol_get_course_users($course1->id, false));
806         $this->assertCount(1, enrol_get_course_users($course1->id, true));
807     }
809     /**
810      * Test count of enrolled users
811      *
812      * @return void
813      */
814     public function test_count_enrolled_users() {
815         global $DB;
817         $this->resetAfterTest(true);
819         $course = $this->getDataGenerator()->create_course();
820         $context = \context_course::instance($course->id);
822         $user1 = $this->getDataGenerator()->create_user();
823         $user2 = $this->getDataGenerator()->create_user();
825         $studentrole = $DB->get_record('role', ['shortname' => 'student']);
827         // Add each user to the manual enrolment instance.
828         $manual = enrol_get_plugin('manual');
830         $manualinstance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual'], '*', MUST_EXIST);
832         $manual->enrol_user($manualinstance, $user1->id, $studentrole->id);
833         $manual->enrol_user($manualinstance, $user2->id, $studentrole->id);
835         $this->assertEquals(2, count_enrolled_users($context));
837         // Create a self enrolment instance, enrol first user only.
838         $self = enrol_get_plugin('self');
840         $selfid = $self->add_instance($course,
841             ['status' => ENROL_INSTANCE_ENABLED, 'name' => 'Self', 'customint6' => 1, 'roleid' => $studentrole->id]);
842         $selfinstance = $DB->get_record('enrol', ['id' => $selfid], '*', MUST_EXIST);
844         $self->enrol_user($selfinstance, $user1->id, $studentrole->id);
846         // There are still only two distinct users.
847         $this->assertEquals(2, count_enrolled_users($context));
848     }
850     /**
851      * Test cases for the test_enrol_get_my_courses_sort_by_last_access test.
852      */
853     public function get_enrol_get_my_courses_sort_by_last_access_test_cases() {
854         $now = time();
856         $enrolledcoursesdata = [
857             ['shortname' => 'a', 'lastaccess' => $now - 2],
858             ['shortname' => 'b', 'lastaccess' => $now - 1],
859             ['shortname' => 'c', 'lastaccess' => $now],
860             ['shortname' => 'd', 'lastaccess' => $now - 1],
861             ['shortname' => 'e']
862         ];
863         $unenrolledcoursesdata = [
864             ['shortname' => 'x', 'lastaccess' => $now - 2],
865             ['shortname' => 'y', 'lastaccess' => $now - 1],
866             ['shortname' => 'z', 'lastaccess' => $now]
867         ];
869         return [
870             'empty set' => [
871                 'enrolledcoursesdata' => [],
872                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
873                 'sort' => 'ul.timeaccess asc',
874                 'limit' => 0,
875                 'offset' => 0,
876                 'expectedcourses' => []
877             ],
878             'ul.timeaccess asc, shortname asc no limit or offset' => [
879                 'enrolledcoursesdata' => $enrolledcoursesdata,
880                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
881                 'sort' => 'ul.timeaccess asc, shortname asc',
882                 'limit' => 0,
883                 'offset' => 0,
884                 'expectedcourses' => ['e', 'a', 'b', 'd', 'c']
885             ],
886             'ul.timeaccess asc, shortname asc with limit no offset' => [
887                 'enrolledcoursesdata' => $enrolledcoursesdata,
888                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
889                 'sort' => 'ul.timeaccess asc, shortname asc',
890                 'limit' => 2,
891                 'offset' => 0,
892                 'expectedcourses' => ['e', 'a']
893             ],
894             'ul.timeaccess asc, shortname asc with limit and offset' => [
895                 'enrolledcoursesdata' => $enrolledcoursesdata,
896                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
897                 'sort' => 'ul.timeaccess asc, shortname asc',
898                 'limit' => 2,
899                 'offset' => 2,
900                 'expectedcourses' => ['b', 'd']
901             ],
902             'ul.timeaccess asc, shortname asc with limit and offset beyond end of data set' => [
903                 'enrolledcoursesdata' => $enrolledcoursesdata,
904                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
905                 'sort' => 'ul.timeaccess asc, shortname asc',
906                 'limit' => 2,
907                 'offset' => 4,
908                 'expectedcourses' => ['c']
909             ],
910             'ul.timeaccess desc, shortname asc no limit or offset' => [
911                 'enrolledcoursesdata' => $enrolledcoursesdata,
912                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
913                 'sort' => 'ul.timeaccess desc, shortname asc',
914                 'limit' => 0,
915                 'offset' => 0,
916                 'expectedcourses' => ['c', 'b', 'd', 'a', 'e']
917             ],
918             'ul.timeaccess desc, shortname desc, no limit or offset' => [
919                 'enrolledcoursesdata' => $enrolledcoursesdata,
920                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
921                 'sort' => 'ul.timeaccess desc, shortname desc',
922                 'limit' => 0,
923                 'offset' => 0,
924                 'expectedcourses' => ['c', 'd', 'b', 'a', 'e']
925             ],
926             'ul.timeaccess asc, shortname desc, no limit or offset' => [
927                 'enrolledcoursesdata' => $enrolledcoursesdata,
928                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
929                 'sort' => 'ul.timeaccess asc, shortname desc',
930                 'limit' => 0,
931                 'offset' => 0,
932                 'expectedcourses' => ['e', 'a', 'd', 'b', 'c']
933             ],
934             'shortname asc, no limit or offset' => [
935                 'enrolledcoursesdata' => $enrolledcoursesdata,
936                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
937                 'sort' => 'shortname asc',
938                 'limit' => 0,
939                 'offset' => 0,
940                 'expectedcourses' => ['a', 'b', 'c', 'd', 'e']
941             ],
942             'shortname desc, no limit or offset' => [
943                 'enrolledcoursesdata' => $enrolledcoursesdata,
944                 'unenrolledcoursesdata' => $unenrolledcoursesdata,
945                 'sort' => 'shortname desc',
946                 'limit' => 0,
947                 'offset' => 0,
948                 'expectedcourses' => ['e', 'd', 'c', 'b', 'a']
949             ],
950         ];
951     }
953     /**
954      * Test the get_enrolled_courses_by_timeline_classification function.
955      *
956      * @dataProvider get_enrol_get_my_courses_sort_by_last_access_test_cases()
957      * @param array $enrolledcoursesdata Courses to create and enrol the user in
958      * @param array $unenrolledcoursesdata Courses to create nut not enrol the user in
959      * @param string $sort Sort string for the enrol function
960      * @param int $limit Maximum number of results
961      * @param int $offset Offset the courses result set by this amount
962      * @param array $expectedcourses Expected courses in result
963      */
964     public function test_enrol_get_my_courses_sort_by_last_access(
965         $enrolledcoursesdata,
966         $unenrolledcoursesdata,
967         $sort,
968         $limit,
969         $offset,
970         $expectedcourses
971     ) {
972         global $DB, $CFG;
974         $this->resetAfterTest();
975         $generator = $this->getDataGenerator();
976         $student = $generator->create_user();
977         $lastaccessrecords = [];
979         foreach ($enrolledcoursesdata as $coursedata) {
980             $lastaccess = null;
982             if (isset($coursedata['lastaccess'])) {
983                 $lastaccess = $coursedata['lastaccess'];
984                 unset($coursedata['lastaccess']);
985             }
987             $course = $generator->create_course($coursedata);
988             $generator->enrol_user($student->id, $course->id, 'student');
990             if (!is_null($lastaccess)) {
991                 $lastaccessrecords[] = [
992                     'userid' => $student->id,
993                     'courseid' => $course->id,
994                     'timeaccess' => $lastaccess
995                 ];
996             }
997         }
999         foreach ($unenrolledcoursesdata as $coursedata) {
1000             $lastaccess = null;
1002             if (isset($coursedata['lastaccess'])) {
1003                 $lastaccess = $coursedata['lastaccess'];
1004                 unset($coursedata['lastaccess']);
1005             }
1007             $course = $generator->create_course($coursedata);
1009             if (!is_null($lastaccess)) {
1010                 $lastaccessrecords[] = [
1011                     'userid' => $student->id,
1012                     'courseid' => $course->id,
1013                     'timeaccess' => $lastaccess
1014                 ];
1015             }
1016         }
1018         if (!empty($lastaccessrecords)) {
1019             $DB->insert_records('user_lastaccess', $lastaccessrecords);
1020         }
1022         $this->setUser($student);
1024         $result = enrol_get_my_courses('shortname', $sort, $limit, [], false, $offset);
1025         $actual = array_map(function($course) {
1026             return $course->shortname;
1027         }, array_values($result));
1029         $this->assertEquals($expectedcourses, $actual);
1030     }