MDL-59057 analytics: Unit test fixes
[moodle.git] / enrol / tests / enrollib_test.php
CommitLineData
05f6da14
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Test non-plugin enrollib parts.
19 *
31ac2aef 20 * @package core_enrol
05f6da14
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
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 */
8252b7c2 37class core_enrollib_testcase extends advanced_testcase {
05f6da14
38
39 public function test_enrol_get_all_users_courses() {
40 global $DB, $CFG;
41
42 $this->resetAfterTest();
43
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);
48
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();
55
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));
62
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);
69
70 $manual = enrol_get_plugin('manual');
71 $this->assertNotEmpty($manual);
72
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);
77
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));
81
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);
86
87
88 $courses = enrol_get_all_users_courses($CFG->siteguest);
89 $this->assertSame(array(), $courses);
90
91 $courses = enrol_get_all_users_courses(0);
92 $this->assertSame(array(), $courses);
93
94 // Results are sorted by visibility, sortorder by default (in our case order of creation)
95
96 $courses = enrol_get_all_users_courses($admin->id);
97 $this->assertCount(1, $courses);
98 $this->assertEquals(array($course1->id), array_keys($courses));
99
100 $courses = enrol_get_all_users_courses($admin->id, true);
101 $this->assertCount(0, $courses);
102 $this->assertEquals(array(), array_keys($courses));
103
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));
107
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));
111
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));
115
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));
119
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));
123
124 $courses = enrol_get_all_users_courses($user3->id, true);
125 $this->assertCount(1, $courses);
126 $this->assertEquals(array($course2->id), array_keys($courses));
127
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));
131
132 $courses = enrol_get_all_users_courses($user4->id, true);
133 $this->assertCount(0, $courses);
134 $this->assertEquals(array(), array_keys($courses));
135
136 // Make sure sorting and columns work.
137
138 $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber',
d26eae4a 139 'startdate', 'visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid');
05f6da14
140
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);
146
4a3fb71c 147 $courses = enrol_get_all_users_courses($user2->id, false, 'timecreated');
05f6da14 148 $course = reset($courses);
4a3fb71c 149 $this->assertTrue(property_exists($course, 'timecreated'));
05f6da14
150
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));
153 }
154
155 public function test_enrol_user_sees_own_courses() {
156 global $DB, $CFG;
157
158 $this->resetAfterTest();
159
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);
164
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();
172
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));
179
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);
186
187 $manual = enrol_get_plugin('manual');
188 $this->assertNotEmpty($manual);
189
190 $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
191
192 $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id);
193
194 $manual->enrol_user($maninstance2, $user2->id, $studentrole->id);
195
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);
200
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);
203
204
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.
209
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));
215
216 $this->setAdminUser();
217 $this->assertFalse(enrol_user_sees_own_courses());
218
219 $this->setGuestUser();
220 $this->assertFalse(enrol_user_sees_own_courses());
221
222 $this->setUser(0);
223 $this->assertFalse(enrol_user_sees_own_courses());
224
225 $this->setUser($user1);
226 $this->assertTrue(enrol_user_sees_own_courses());
227
228 $this->setUser($user2);
229 $this->assertTrue(enrol_user_sees_own_courses());
230
231 $this->setUser($user3);
232 $this->assertFalse(enrol_user_sees_own_courses());
233
234 $this->setUser($user4);
235 $this->assertFalse(enrol_user_sees_own_courses());
236
237 $this->setUser($user5);
238 $this->assertFalse(enrol_user_sees_own_courses());
239
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());
245
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());
252 }
631d9f66
DP
253
254 public function test_enrol_get_shared_courses() {
255 $this->resetAfterTest();
256
257 $user1 = $this->getDataGenerator()->create_user();
258 $user2 = $this->getDataGenerator()->create_user();
259 $user3 = $this->getDataGenerator()->create_user();
260
261 $course1 = $this->getDataGenerator()->create_course();
262 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
263 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
264
265 $course2 = $this->getDataGenerator()->create_course();
266 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
267
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));
272
273 // Test retrieving the courses in common.
274 $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
275
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);
281 }
bb78e249 282
499d3775 283 public function test_enrol_get_shared_courses_different_methods() {
284 global $DB, $CFG;
285
286 require_once($CFG->dirroot . '/enrol/self/externallib.php');
287
288 $this->resetAfterTest();
289
290 $user1 = $this->getDataGenerator()->create_user();
291 $user2 = $this->getDataGenerator()->create_user();
292 $user3 = $this->getDataGenerator()->create_user();
293
294 $course1 = $this->getDataGenerator()->create_course();
295
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);
300
301 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
302 $this->assertNotEmpty($studentrole);
303
304 $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
305 'name' => 'Test instance 1',
306 'customint6' => 1,
307 'roleid' => $studentrole->id));
308
309 $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
310
311 self::setUser($user2);
312 // Self enrol me (user2).
313 $result = enrol_self_external::enrol_user($course1->id);
314
315 // Enrol user1 manually.
316 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'manual');
317
318 $course2 = $this->getDataGenerator()->create_course();
319 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
320
321 $course3 = $this->getDataGenerator()->create_course();
322 $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
323
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));
328
329 // Test retrieving the courses in common.
330 $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
331
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);
337 }
338
bb78e249
RT
339 /**
340 * Test user enrolment created event.
341 */
342 public function test_user_enrolment_created_event() {
343 global $DB;
344
345 $this->resetAfterTest();
346
347 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
348 $this->assertNotEmpty($studentrole);
349
350 $admin = get_admin();
351
352 $course1 = $this->getDataGenerator()->create_course();
353
354 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
355
356 $manual = enrol_get_plugin('manual');
357 $this->assertNotEmpty($manual);
358
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);
365
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);
f1f4f4fd 369 $this->assertEquals(context_course::instance($course1->id), $event->get_context());
bb78e249
RT
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);
f1f4f4fd
MN
375 $expected = array($course1->id, 'course', 'enrol', '../enrol/users.php?id=' . $course1->id, $course1->id);
376 $this->assertEventLegacyLogData($expected, $event);
623a32e5 377 $this->assertEventContextNotUsed($event);
bb78e249 378 }
63245172
MN
379
380 /**
381 * Test user_enrolment_deleted event.
382 */
383 public function test_user_enrolment_deleted_event() {
384 global $DB;
385
386 $this->resetAfterTest(true);
387
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'));
392
393 $enrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
394
395 // Enrol user.
396 $manualplugin->enrol_user($enrol, $user->id, $student->id);
397
398 // Get the user enrolment information, used to validate legacy event data.
399 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user->id));
400
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);
407
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);
623a32e5 419 $this->assertEventContextNotUsed($event);
63245172 420 }
080c7d47
MG
421
422 /**
423 * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
424 */
425 public function test_instance_events() {
426 global $DB;
427
428 $this->resetAfterTest(true);
429
430 $selfplugin = enrol_get_plugin('self');
431 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
432
433 $course = $this->getDataGenerator()->create_course();
434
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();
443
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);
450
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);
455
456 $events = $sink->get_events();
457 $sink->close();
458
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);
465
466 // Deleting enrol instance.
467 $instance = $DB->get_record('enrol', array('id' => $instanceid));
468 $sink = $this->redirectEvents();
469 $selfplugin->delete_instance($instance);
470
471 $events = $sink->get_events();
472 $sink->close();
473
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);
480 }
618336bf
DM
481
482 /**
483 * Confirms that timemodified field was updated after modification of user enrollment
484 */
485 public function test_enrollment_update_timemodified() {
486 global $DB;
487
488 $this->resetAfterTest(true);
489 $datagen = $this->getDataGenerator();
490
491 /** @var enrol_manual_plugin $manualplugin */
492 $manualplugin = enrol_get_plugin('manual');
493 $this->assertNotNull($manualplugin);
494
495 $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
496 $course = $datagen->create_course();
497 $user = $datagen->create_user();
498
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;
505 }
506 }
507 if (empty($instanceid)) {
508 $instanceid = $manualplugin->add_default_instance($course);
509 if (empty($instanceid)) {
510 $instanceid = $manualplugin->add_instance($course);
511 }
512 }
513 $this->assertNotNull($instanceid);
514
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
522 );
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
531 );
532
533 $this->assertGreaterThan($userenrolorig, $userenrolpost);
534 }
1ef06b43
RW
535
536 /**
537 * Test to confirm that enrol_get_my_courses only return the courses that
538 * the logged in user is enrolled in.
539 */
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();
546
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);
552
553 // By default this function should return all of the courses the user
554 // is enrolled in.
555 $courses = enrol_get_my_courses();
556
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);
561
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);
566
567 $this->assertCount(2, $courses);
568 $this->assertEquals($course1->id, $courses[$course1->id]->id);
569 $this->assertEquals($course2->id, $courses[$course2->id]->id);
570
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);
575
576 $this->assertCount(2, $courses);
577 $this->assertEquals($course1->id, $courses[$course1->id]->id);
578 $this->assertEquals($course2->id, $courses[$course2->id]->id);
579 }
05f6da14 580}