MDL-59174 enrollib: More flexibility for enrol_get_course_users
[moodle.git] / enrol / tests / enrollib_test.php
blob4508ae051d4487b6d638e81037f0410f16134b83
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.
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.
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
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Test non-plugin enrollib parts.
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
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();
58 $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
59 $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
60 $course3 = $this->getDataGenerator()->create_course(array('category'=>$category2->id, 'visible'=>0));
61 $course4 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
63 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
64 $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
65 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
66 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
67 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
68 $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
70 $manual = enrol_get_plugin('manual');
71 $this->assertNotEmpty($manual);
73 $manual->enrol_user($maninstance1, $user1->id, $teacherrole->id);
74 $manual->enrol_user($maninstance1, $user2->id, $studentrole->id);
75 $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
76 $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
78 $manual->enrol_user($maninstance2, $user1->id);
79 $manual->enrol_user($maninstance2, $user2->id);
80 $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()+(60*60));
82 $manual->enrol_user($maninstance3, $user1->id);
83 $manual->enrol_user($maninstance3, $user2->id);
84 $manual->enrol_user($maninstance3, $user3->id, 0, 1, time()-(60*60));
85 $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
88 $courses = enrol_get_all_users_courses($CFG->siteguest);
89 $this->assertSame(array(), $courses);
91 $courses = enrol_get_all_users_courses(0);
92 $this->assertSame(array(), $courses);
94 // Results are sorted by visibility, sortorder by default (in our case order of creation)
96 $courses = enrol_get_all_users_courses($admin->id);
97 $this->assertCount(1, $courses);
98 $this->assertEquals(array($course1->id), array_keys($courses));
100 $courses = enrol_get_all_users_courses($admin->id, true);
101 $this->assertCount(0, $courses);
102 $this->assertEquals(array(), array_keys($courses));
104 $courses = enrol_get_all_users_courses($user1->id);
105 $this->assertCount(3, $courses);
106 $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
108 $courses = enrol_get_all_users_courses($user1->id, true);
109 $this->assertCount(2, $courses);
110 $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
112 $courses = enrol_get_all_users_courses($user2->id);
113 $this->assertCount(3, $courses);
114 $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
116 $courses = enrol_get_all_users_courses($user2->id, true);
117 $this->assertCount(2, $courses);
118 $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
120 $courses = enrol_get_all_users_courses($user3->id);
121 $this->assertCount(2, $courses);
122 $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
124 $courses = enrol_get_all_users_courses($user3->id, true);
125 $this->assertCount(1, $courses);
126 $this->assertEquals(array($course2->id), array_keys($courses));
128 $courses = enrol_get_all_users_courses($user4->id);
129 $this->assertCount(2, $courses);
130 $this->assertEquals(array($course1->id, $course3->id), array_keys($courses));
132 $courses = enrol_get_all_users_courses($user4->id, true);
133 $this->assertCount(0, $courses);
134 $this->assertEquals(array(), array_keys($courses));
136 // Make sure sorting and columns work.
138 $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber',
139 'startdate', 'visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid');
141 $courses = enrol_get_all_users_courses($user2->id, true);
142 $course = reset($courses);
143 context_helper::preload_from_record($course);
144 $course = (array)$course;
145 $this->assertEquals($basefields, array_keys($course), '', 0, 10, true);
147 $courses = enrol_get_all_users_courses($user2->id, false, 'timecreated');
148 $course = reset($courses);
149 $this->assertTrue(property_exists($course, 'timecreated'));
151 $courses = enrol_get_all_users_courses($user2->id, false, null, 'id DESC');
152 $this->assertEquals(array($course3->id, $course2->id, $course1->id), array_keys($courses));
155 public function test_enrol_user_sees_own_courses() {
156 global $DB, $CFG;
158 $this->resetAfterTest();
160 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
161 $this->assertNotEmpty($studentrole);
162 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
163 $this->assertNotEmpty($teacherrole);
165 $admin = get_admin();
166 $user1 = $this->getDataGenerator()->create_user();
167 $user2 = $this->getDataGenerator()->create_user();
168 $user3 = $this->getDataGenerator()->create_user();
169 $user4 = $this->getDataGenerator()->create_user();
170 $user5 = $this->getDataGenerator()->create_user();
171 $user6 = $this->getDataGenerator()->create_user();
173 $category1 = $this->getDataGenerator()->create_category(array('visible'=>0));
174 $category2 = $this->getDataGenerator()->create_category();
175 $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
176 $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
177 $course3 = $this->getDataGenerator()->create_course(array('category'=>$category2->id, 'visible'=>0));
178 $course4 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
180 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
181 $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
182 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
183 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
184 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
185 $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
187 $manual = enrol_get_plugin('manual');
188 $this->assertNotEmpty($manual);
190 $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
192 $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id);
194 $manual->enrol_user($maninstance2, $user2->id, $studentrole->id);
196 $manual->enrol_user($maninstance1, $user3->id, $studentrole->id, 1, time()+(60*60));
197 $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()-(60*60));
198 $manual->enrol_user($maninstance3, $user2->id, $studentrole->id);
199 $manual->enrol_user($maninstance4, $user2->id, 0, 0, 0, ENROL_USER_SUSPENDED);
201 $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
202 $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
205 $this->assertFalse(enrol_user_sees_own_courses($CFG->siteguest));
206 $this->assertFalse(enrol_user_sees_own_courses(0));
207 $this->assertFalse(enrol_user_sees_own_courses($admin));
208 $this->assertFalse(enrol_user_sees_own_courses(-222)); // Nonexistent user.
210 $this->assertTrue(enrol_user_sees_own_courses($user1));
211 $this->assertTrue(enrol_user_sees_own_courses($user2->id));
212 $this->assertFalse(enrol_user_sees_own_courses($user3->id));
213 $this->assertFalse(enrol_user_sees_own_courses($user4));
214 $this->assertFalse(enrol_user_sees_own_courses($user5));
216 $this->setAdminUser();
217 $this->assertFalse(enrol_user_sees_own_courses());
219 $this->setGuestUser();
220 $this->assertFalse(enrol_user_sees_own_courses());
222 $this->setUser(0);
223 $this->assertFalse(enrol_user_sees_own_courses());
225 $this->setUser($user1);
226 $this->assertTrue(enrol_user_sees_own_courses());
228 $this->setUser($user2);
229 $this->assertTrue(enrol_user_sees_own_courses());
231 $this->setUser($user3);
232 $this->assertFalse(enrol_user_sees_own_courses());
234 $this->setUser($user4);
235 $this->assertFalse(enrol_user_sees_own_courses());
237 $this->setUser($user5);
238 $this->assertFalse(enrol_user_sees_own_courses());
240 $user1 = $DB->get_record('user', array('id'=>$user1->id));
241 $this->setUser($user1);
242 $reads = $DB->perf_get_reads();
243 $this->assertTrue(enrol_user_sees_own_courses());
244 $this->assertGreaterThan($reads, $DB->perf_get_reads());
246 $user1 = $DB->get_record('user', array('id'=>$user1->id));
247 $this->setUser($user1);
248 require_login($course3);
249 $reads = $DB->perf_get_reads();
250 $this->assertTrue(enrol_user_sees_own_courses());
251 $this->assertEquals($reads, $DB->perf_get_reads());
254 public function test_enrol_get_shared_courses() {
255 $this->resetAfterTest();
257 $user1 = $this->getDataGenerator()->create_user();
258 $user2 = $this->getDataGenerator()->create_user();
259 $user3 = $this->getDataGenerator()->create_user();
261 $course1 = $this->getDataGenerator()->create_course();
262 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
263 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
265 $course2 = $this->getDataGenerator()->create_course();
266 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
268 // Test that user1 and user2 have courses in common.
269 $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
270 // Test that user1 and user3 have no courses in common.
271 $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
273 // Test retrieving the courses in common.
274 $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
276 // Only should be one shared course.
277 $this->assertCount(1, $sharedcourses);
278 $sharedcourse = array_shift($sharedcourses);
279 // It should be course 1.
280 $this->assertEquals($sharedcourse->id, $course1->id);
283 public function test_enrol_get_shared_courses_different_methods() {
284 global $DB, $CFG;
286 require_once($CFG->dirroot . '/enrol/self/externallib.php');
288 $this->resetAfterTest();
290 $user1 = $this->getDataGenerator()->create_user();
291 $user2 = $this->getDataGenerator()->create_user();
292 $user3 = $this->getDataGenerator()->create_user();
294 $course1 = $this->getDataGenerator()->create_course();
296 // Enrol user1 and user2 in course1 with a different enrolment methode.
297 // Add self enrolment method for course1.
298 $selfplugin = enrol_get_plugin('self');
299 $this->assertNotEmpty($selfplugin);
301 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
302 $this->assertNotEmpty($studentrole);
304 $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
305 'name' => 'Test instance 1',
306 'customint6' => 1,
307 'roleid' => $studentrole->id));
309 $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
311 self::setUser($user2);
312 // Self enrol me (user2).
313 $result = enrol_self_external::enrol_user($course1->id);
315 // Enrol user1 manually.
316 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'manual');
318 $course2 = $this->getDataGenerator()->create_course();
319 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
321 $course3 = $this->getDataGenerator()->create_course();
322 $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
324 // Test that user1 and user2 have courses in common.
325 $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
326 // Test that user1 and user3 have no courses in common.
327 $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
329 // Test retrieving the courses in common.
330 $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
332 // Only should be one shared course.
333 $this->assertCount(1, $sharedcourses);
334 $sharedcourse = array_shift($sharedcourses);
335 // It should be course 1.
336 $this->assertEquals($sharedcourse->id, $course1->id);
340 * Test user enrolment created event.
342 public function test_user_enrolment_created_event() {
343 global $DB;
345 $this->resetAfterTest();
347 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
348 $this->assertNotEmpty($studentrole);
350 $admin = get_admin();
352 $course1 = $this->getDataGenerator()->create_course();
354 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
356 $manual = enrol_get_plugin('manual');
357 $this->assertNotEmpty($manual);
359 // Enrol user and capture event.
360 $sink = $this->redirectEvents();
361 $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
362 $events = $sink->get_events();
363 $sink->close();
364 $event = array_shift($events);
366 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $admin->id));
367 $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
368 $this->assertEquals($dbuserenrolled->id, $event->objectid);
369 $this->assertEquals(context_course::instance($course1->id), $event->get_context());
370 $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
371 $expectedlegacyeventdata = $dbuserenrolled;
372 $expectedlegacyeventdata->enrol = $manual->get_name();
373 $expectedlegacyeventdata->courseid = $course1->id;
374 $this->assertEventLegacyData($expectedlegacyeventdata, $event);
375 $expected = array($course1->id, 'course', 'enrol', '../enrol/users.php?id=' . $course1->id, $course1->id);
376 $this->assertEventLegacyLogData($expected, $event);
377 $this->assertEventContextNotUsed($event);
381 * Test user_enrolment_deleted event.
383 public function test_user_enrolment_deleted_event() {
384 global $DB;
386 $this->resetAfterTest(true);
388 $manualplugin = enrol_get_plugin('manual');
389 $user = $this->getDataGenerator()->create_user();
390 $course = $this->getDataGenerator()->create_course();
391 $student = $DB->get_record('role', array('shortname' => 'student'));
393 $enrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
395 // Enrol user.
396 $manualplugin->enrol_user($enrol, $user->id, $student->id);
398 // Get the user enrolment information, used to validate legacy event data.
399 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user->id));
401 // Unenrol user and capture event.
402 $sink = $this->redirectEvents();
403 $manualplugin->unenrol_user($enrol, $user->id);
404 $events = $sink->get_events();
405 $sink->close();
406 $event = array_pop($events);
408 // Validate the event.
409 $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
410 $this->assertEquals(context_course::instance($course->id), $event->get_context());
411 $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
412 $expectedlegacyeventdata = $dbuserenrolled;
413 $expectedlegacyeventdata->enrol = $manualplugin->get_name();
414 $expectedlegacyeventdata->courseid = $course->id;
415 $expectedlegacyeventdata->lastenrol = true;
416 $this->assertEventLegacyData($expectedlegacyeventdata, $event);
417 $expected = array($course->id, 'course', 'unenrol', '../enrol/users.php?id=' . $course->id, $course->id);
418 $this->assertEventLegacyLogData($expected, $event);
419 $this->assertEventContextNotUsed($event);
423 * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
425 public function test_instance_events() {
426 global $DB;
428 $this->resetAfterTest(true);
430 $selfplugin = enrol_get_plugin('self');
431 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
433 $course = $this->getDataGenerator()->create_course();
435 // Creating enrol instance.
436 $sink = $this->redirectEvents();
437 $instanceid = $selfplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
438 'name' => 'Test instance 1',
439 'customint6' => 1,
440 'roleid' => $studentrole->id));
441 $events = $sink->get_events();
442 $sink->close();
444 $this->assertCount(1, $events);
445 $event = array_pop($events);
446 $this->assertInstanceOf('\core\event\enrol_instance_created', $event);
447 $this->assertEquals(context_course::instance($course->id), $event->get_context());
448 $this->assertEquals('self', $event->other['enrol']);
449 $this->assertEventContextNotUsed($event);
451 // Updating enrol instance.
452 $instance = $DB->get_record('enrol', array('id' => $instanceid));
453 $sink = $this->redirectEvents();
454 $selfplugin->update_status($instance, ENROL_INSTANCE_DISABLED);
456 $events = $sink->get_events();
457 $sink->close();
459 $this->assertCount(1, $events);
460 $event = array_pop($events);
461 $this->assertInstanceOf('\core\event\enrol_instance_updated', $event);
462 $this->assertEquals(context_course::instance($course->id), $event->get_context());
463 $this->assertEquals('self', $event->other['enrol']);
464 $this->assertEventContextNotUsed($event);
466 // Deleting enrol instance.
467 $instance = $DB->get_record('enrol', array('id' => $instanceid));
468 $sink = $this->redirectEvents();
469 $selfplugin->delete_instance($instance);
471 $events = $sink->get_events();
472 $sink->close();
474 $this->assertCount(1, $events);
475 $event = array_pop($events);
476 $this->assertInstanceOf('\core\event\enrol_instance_deleted', $event);
477 $this->assertEquals(context_course::instance($course->id), $event->get_context());
478 $this->assertEquals('self', $event->other['enrol']);
479 $this->assertEventContextNotUsed($event);
483 * Confirms that timemodified field was updated after modification of user enrollment
485 public function test_enrollment_update_timemodified() {
486 global $DB;
488 $this->resetAfterTest(true);
489 $datagen = $this->getDataGenerator();
491 /** @var enrol_manual_plugin $manualplugin */
492 $manualplugin = enrol_get_plugin('manual');
493 $this->assertNotNull($manualplugin);
495 $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
496 $course = $datagen->create_course();
497 $user = $datagen->create_user();
499 $instanceid = null;
500 $instances = enrol_get_instances($course->id, true);
501 foreach ($instances as $inst) {
502 if ($inst->enrol == 'manual') {
503 $instanceid = (int)$inst->id;
504 break;
507 if (empty($instanceid)) {
508 $instanceid = $manualplugin->add_default_instance($course);
509 if (empty($instanceid)) {
510 $instanceid = $manualplugin->add_instance($course);
513 $this->assertNotNull($instanceid);
515 $instance = $DB->get_record('enrol', ['id' => $instanceid], '*', MUST_EXIST);
516 $manualplugin->enrol_user($instance, $user->id, $studentroleid, 0, 0, ENROL_USER_ACTIVE);
517 $userenrolorig = (int)$DB->get_field(
518 'user_enrolments',
519 'timemodified',
520 ['enrolid' => $instance->id, 'userid' => $user->id],
521 MUST_EXIST
523 $this->waitForSecond();
524 $this->waitForSecond();
525 $manualplugin->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED);
526 $userenrolpost = (int)$DB->get_field(
527 'user_enrolments',
528 'timemodified',
529 ['enrolid' => $instance->id, 'userid' => $user->id],
530 MUST_EXIST
533 $this->assertGreaterThan($userenrolorig, $userenrolpost);
537 * Test to confirm that enrol_get_my_courses only return the courses that
538 * the logged in user is enrolled in.
540 public function test_enrol_get_my_courses_only_enrolled_courses() {
541 $user = $this->getDataGenerator()->create_user();
542 $course1 = $this->getDataGenerator()->create_course();
543 $course2 = $this->getDataGenerator()->create_course();
544 $course3 = $this->getDataGenerator()->create_course();
545 $course4 = $this->getDataGenerator()->create_course();
547 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
548 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
549 $this->getDataGenerator()->enrol_user($user->id, $course3->id);
550 $this->resetAfterTest(true);
551 $this->setUser($user);
553 // By default this function should return all of the courses the user
554 // is enrolled in.
555 $courses = enrol_get_my_courses();
557 $this->assertCount(3, $courses);
558 $this->assertEquals($course1->id, $courses[$course1->id]->id);
559 $this->assertEquals($course2->id, $courses[$course2->id]->id);
560 $this->assertEquals($course3->id, $courses[$course3->id]->id);
562 // If a set of course ids are provided then the result set will only contain
563 // these courses.
564 $courseids = [$course1->id, $course2->id];
565 $courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
567 $this->assertCount(2, $courses);
568 $this->assertEquals($course1->id, $courses[$course1->id]->id);
569 $this->assertEquals($course2->id, $courses[$course2->id]->id);
571 // If the course ids list contains any ids for courses the user isn't enrolled in
572 // then they will be ignored (in this case $course4).
573 $courseids = [$course1->id, $course2->id, $course4->id];
574 $courses = enrol_get_my_courses(['id'], 'visible DESC,sortorder ASC', 0, $courseids);
576 $this->assertCount(2, $courses);
577 $this->assertEquals($course1->id, $courses[$course1->id]->id);
578 $this->assertEquals($course2->id, $courses[$course2->id]->id);
582 * test_course_users
584 * @return void
586 public function test_course_users() {
587 $this->resetAfterTest();
589 $user1 = $this->getDataGenerator()->create_user();
590 $user2 = $this->getDataGenerator()->create_user();
591 $course1 = $this->getDataGenerator()->create_course();
592 $course2 = $this->getDataGenerator()->create_course();
594 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
595 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
596 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
598 $this->assertCount(2, enrol_get_course_users($course1->id));
599 $this->assertCount(2, enrol_get_course_users($course1->id, true));
601 $this->assertCount(1, enrol_get_course_users($course1->id, true, array($user1->id)));
603 $this->assertCount(2, enrol_get_course_users(false, false, array($user1->id)));
605 $instances = enrol_get_instances($course1->id, true);
606 $manualinstance = reset($instances);
608 $manualplugin = enrol_get_plugin('manual');
609 $manualplugin->update_user_enrol($manualinstance, $user1->id, ENROL_USER_SUSPENDED);
610 $this->assertCount(2, enrol_get_course_users($course1->id, false));
611 $this->assertCount(1, enrol_get_course_users($course1->id, true));