MDL-63457 block_myoverview: Rerender the paged content and jump
[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();
c260117d
DM
58
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 ));
05f6da14
76
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);
83
84 $manual = enrol_get_plugin('manual');
85 $this->assertNotEmpty($manual);
86
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);
91
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));
95
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);
100
101
102 $courses = enrol_get_all_users_courses($CFG->siteguest);
103 $this->assertSame(array(), $courses);
104
105 $courses = enrol_get_all_users_courses(0);
106 $this->assertSame(array(), $courses);
107
108 // Results are sorted by visibility, sortorder by default (in our case order of creation)
109
110 $courses = enrol_get_all_users_courses($admin->id);
111 $this->assertCount(1, $courses);
112 $this->assertEquals(array($course1->id), array_keys($courses));
113
114 $courses = enrol_get_all_users_courses($admin->id, true);
115 $this->assertCount(0, $courses);
116 $this->assertEquals(array(), array_keys($courses));
117
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));
121
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));
125
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));
129
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));
133
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));
137
138 $courses = enrol_get_all_users_courses($user3->id, true);
139 $this->assertCount(1, $courses);
140 $this->assertEquals(array($course2->id), array_keys($courses));
141
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));
145
146 $courses = enrol_get_all_users_courses($user4->id, true);
147 $this->assertCount(0, $courses);
148 $this->assertEquals(array(), array_keys($courses));
149
150 // Make sure sorting and columns work.
151
152 $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber',
d26eae4a 153 'startdate', 'visible', 'groupmode', 'groupmodeforce', 'defaultgroupingid');
05f6da14
154
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);
160
4a3fb71c 161 $courses = enrol_get_all_users_courses($user2->id, false, 'timecreated');
05f6da14 162 $course = reset($courses);
4a3fb71c 163 $this->assertTrue(property_exists($course, 'timecreated'));
05f6da14
164
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));
c260117d
DM
167
168 // Make sure that implicit sorting defined in navsortmycoursessort is respected.
169
170 $CFG->navsortmycoursessort = 'shortname';
171
172 $courses = enrol_get_all_users_courses($user1->id);
173 $this->assertEquals(array($course2->id, $course3->id, $course1->id), array_keys($courses));
174
175 // But still the explicit sorting takes precedence over the implicit one.
176
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));
05f6da14
179 }
180
181 public function test_enrol_user_sees_own_courses() {
182 global $DB, $CFG;
183
184 $this->resetAfterTest();
185
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);
190
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();
198
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));
205
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);
212
213 $manual = enrol_get_plugin('manual');
214 $this->assertNotEmpty($manual);
215
216 $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
217
218 $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id);
219
220 $manual->enrol_user($maninstance2, $user2->id, $studentrole->id);
221
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);
226
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);
229
230
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.
235
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));
241
242 $this->setAdminUser();
243 $this->assertFalse(enrol_user_sees_own_courses());
244
245 $this->setGuestUser();
246 $this->assertFalse(enrol_user_sees_own_courses());
247
248 $this->setUser(0);
249 $this->assertFalse(enrol_user_sees_own_courses());
250
251 $this->setUser($user1);
252 $this->assertTrue(enrol_user_sees_own_courses());
253
254 $this->setUser($user2);
255 $this->assertTrue(enrol_user_sees_own_courses());
256
257 $this->setUser($user3);
258 $this->assertFalse(enrol_user_sees_own_courses());
259
260 $this->setUser($user4);
261 $this->assertFalse(enrol_user_sees_own_courses());
262
263 $this->setUser($user5);
264 $this->assertFalse(enrol_user_sees_own_courses());
265
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());
271
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 }
631d9f66
DP
279
280 public function test_enrol_get_shared_courses() {
281 $this->resetAfterTest();
282
283 $user1 = $this->getDataGenerator()->create_user();
284 $user2 = $this->getDataGenerator()->create_user();
285 $user3 = $this->getDataGenerator()->create_user();
286
287 $course1 = $this->getDataGenerator()->create_course();
288 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
289 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
290
291 $course2 = $this->getDataGenerator()->create_course();
292 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
293
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));
298
299 // Test retrieving the courses in common.
300 $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
301
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 }
bb78e249 308
499d3775 309 public function test_enrol_get_shared_courses_different_methods() {
310 global $DB, $CFG;
311
312 require_once($CFG->dirroot . '/enrol/self/externallib.php');
313
314 $this->resetAfterTest();
315
316 $user1 = $this->getDataGenerator()->create_user();
317 $user2 = $this->getDataGenerator()->create_user();
318 $user3 = $this->getDataGenerator()->create_user();
319
320 $course1 = $this->getDataGenerator()->create_course();
321
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);
326
327 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
328 $this->assertNotEmpty($studentrole);
329
330 $instance1id = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
331 'name' => 'Test instance 1',
332 'customint6' => 1,
333 'roleid' => $studentrole->id));
334
335 $instance1 = $DB->get_record('enrol', array('id' => $instance1id), '*', MUST_EXIST);
336
337 self::setUser($user2);
338 // Self enrol me (user2).
339 $result = enrol_self_external::enrol_user($course1->id);
340
341 // Enrol user1 manually.
342 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'manual');
343
344 $course2 = $this->getDataGenerator()->create_course();
345 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
346
347 $course3 = $this->getDataGenerator()->create_course();
348 $this->getDataGenerator()->enrol_user($user2->id, $course3->id);
349
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));
354
355 // Test retrieving the courses in common.
356 $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
357
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 }
364
bb78e249
RT
365 /**
366 * Test user enrolment created event.
367 */
368 public function test_user_enrolment_created_event() {
369 global $DB;
370
371 $this->resetAfterTest();
372
373 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
374 $this->assertNotEmpty($studentrole);
375
376 $admin = get_admin();
377
378 $course1 = $this->getDataGenerator()->create_course();
379
380 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
381
382 $manual = enrol_get_plugin('manual');
383 $this->assertNotEmpty($manual);
384
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);
391
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);
f1f4f4fd 395 $this->assertEquals(context_course::instance($course1->id), $event->get_context());
bb78e249
RT
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);
f1f4f4fd
MN
401 $expected = array($course1->id, 'course', 'enrol', '../enrol/users.php?id=' . $course1->id, $course1->id);
402 $this->assertEventLegacyLogData($expected, $event);
623a32e5 403 $this->assertEventContextNotUsed($event);
bb78e249 404 }
63245172
MN
405
406 /**
407 * Test user_enrolment_deleted event.
408 */
409 public function test_user_enrolment_deleted_event() {
410 global $DB;
411
412 $this->resetAfterTest(true);
413
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'));
418
419 $enrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
420
421 // Enrol user.
422 $manualplugin->enrol_user($enrol, $user->id, $student->id);
423
424 // Get the user enrolment information, used to validate legacy event data.
425 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user->id));
426
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);
433
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);
623a32e5 445 $this->assertEventContextNotUsed($event);
63245172 446 }
080c7d47
MG
447
448 /**
449 * Test enrol_instance_created, enrol_instance_updated and enrol_instance_deleted events.
450 */
451 public function test_instance_events() {
452 global $DB;
453
454 $this->resetAfterTest(true);
455
456 $selfplugin = enrol_get_plugin('self');
457 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
458
459 $course = $this->getDataGenerator()->create_course();
460
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();
469
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);
476
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);
481
482 $events = $sink->get_events();
483 $sink->close();
484
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);
491
492 // Deleting enrol instance.
493 $instance = $DB->get_record('enrol', array('id' => $instanceid));
494 $sink = $this->redirectEvents();
495 $selfplugin->delete_instance($instance);
496
497 $events = $sink->get_events();
498 $sink->close();
499
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 }
618336bf
DM
507
508 /**
509 * Confirms that timemodified field was updated after modification of user enrollment
510 */
511 public function test_enrollment_update_timemodified() {
512 global $DB;
513
514 $this->resetAfterTest(true);
515 $datagen = $this->getDataGenerator();
516
517 /** @var enrol_manual_plugin $manualplugin */
518 $manualplugin = enrol_get_plugin('manual');
519 $this->assertNotNull($manualplugin);
520
521 $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
522 $course = $datagen->create_course();
523 $user = $datagen->create_user();
524
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);
540
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 );
558
559 $this->assertGreaterThan($userenrolorig, $userenrolpost);
560 }
1ef06b43
RW
561
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();
572
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);
578
579 // By default this function should return all of the courses the user
580 // is enrolled in.
581 $courses = enrol_get_my_courses();
582
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);
587
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);
592
593 $this->assertCount(2, $courses);
594 $this->assertEquals($course1->id, $courses[$course1->id]->id);
595 $this->assertEquals($course2->id, $courses[$course2->id]->id);
596
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);
601
602 $this->assertCount(2, $courses);
603 $this->assertEquals($course1->id, $courses[$course1->id]->id);
604 $this->assertEquals($course2->id, $courses[$course2->id]->id);
605 }
8970ff91 606
22ef1550 607 /**
608 * Tests the enrol_get_my_courses function when using the $allaccessible parameter, which
609 * includes a wider range of courses (enrolled courses + other accessible ones).
610 */
611 public function test_enrol_get_my_courses_all_accessible() {
612 global $DB, $CFG;
613
614 $this->resetAfterTest(true);
615
616 // Create test user and 4 courses, two of which have guest access enabled.
617 $user = $this->getDataGenerator()->create_user();
618 $course1 = $this->getDataGenerator()->create_course(
c260117d 619 (object)array('shortname' => 'X',
22ef1550 620 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
621 'enrol_guest_password_0' => ''));
622 $course2 = $this->getDataGenerator()->create_course(
c260117d 623 (object)array('shortname' => 'Z',
22ef1550 624 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
625 'enrol_guest_password_0' => ''));
626 $course3 = $this->getDataGenerator()->create_course(
c260117d 627 (object)array('shortname' => 'Y',
22ef1550 628 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
629 'enrol_guest_password_0' => 'frog'));
630 $course4 = $this->getDataGenerator()->create_course(
631 (object)array('shortname' => 'W',
632 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
633 'enrol_guest_password_0' => ''));
634
635 // User is enrolled in first course.
636 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
637
638 // Check enrol_get_my_courses basic use (without all accessible).
639 $this->setUser($user);
640 $courses = enrol_get_my_courses();
641 $this->assertEquals([$course1->id], array_keys($courses));
642
643 // Turn on all accessible, now they can access the second course too.
644 $courses = enrol_get_my_courses(null, 'id', 0, [], true);
645 $this->assertEquals([$course1->id, $course2->id], array_keys($courses));
646
647 // Log in as guest to third course.
648 load_temp_course_role(context_course::instance($course3->id), $CFG->guestroleid);
649 $courses = enrol_get_my_courses(null, 'id', 0, [], true);
650 $this->assertEquals([$course1->id, $course2->id, $course3->id], array_keys($courses));
651
652 // Check fields parameter still works. Fields default (certain base fields).
653 $this->assertObjectHasAttribute('id', $courses[$course3->id]);
654 $this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
655 $this->assertObjectNotHasAttribute('summary', $courses[$course3->id]);
656
657 // Specified fields (one, string).
658 $courses = enrol_get_my_courses('summary', 'id', 0, [], true);
659 $this->assertObjectHasAttribute('id', $courses[$course3->id]);
660 $this->assertObjectHasAttribute('shortname', $courses[$course3->id]);
661 $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
662 $this->assertObjectNotHasAttribute('summaryformat', $courses[$course3->id]);
663
664 // Specified fields (two, string).
665 $courses = enrol_get_my_courses('summary, summaryformat', 'id', 0, [], true);
666 $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
667 $this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
668
669 // Specified fields (two, array).
670 $courses = enrol_get_my_courses(['summary', 'summaryformat'], 'id', 0, [], true);
671 $this->assertObjectHasAttribute('summary', $courses[$course3->id]);
672 $this->assertObjectHasAttribute('summaryformat', $courses[$course3->id]);
673
c260117d
DM
674 // By default, courses are ordered by sortorder - which by default is most recent first.
675 $courses = enrol_get_my_courses(null, null, 0, [], true);
22ef1550 676 $this->assertEquals([$course3->id, $course2->id, $course1->id], array_keys($courses));
677
c260117d
DM
678 // Make sure that implicit sorting defined in navsortmycoursessort is respected.
679 $CFG->navsortmycoursessort = 'shortname';
680 $courses = enrol_get_my_courses(null, null, 0, [], true);
681 $this->assertEquals([$course1->id, $course3->id, $course2->id], array_keys($courses));
682
683 // But still the explicit sorting takes precedence over the implicit one.
684 $courses = enrol_get_my_courses(null, 'shortname DESC', 0, [], true);
685 $this->assertEquals([$course2->id, $course3->id, $course1->id], array_keys($courses));
686
22ef1550 687 // Check filter parameter still works.
688 $courses = enrol_get_my_courses(null, 'id', 0, [$course2->id, $course3->id, $course4->id], true);
689 $this->assertEquals([$course2->id, $course3->id], array_keys($courses));
690
691 // Check limit parameter.
692 $courses = enrol_get_my_courses(null, 'id', 2, [], true);
693 $this->assertEquals([$course1->id, $course2->id], array_keys($courses));
694
695 // Now try access for a different user who has manager role at system level.
696 $manager = $this->getDataGenerator()->create_user();
697 $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
698 role_assign($managerroleid, $manager->id, \context_system::instance()->id);
699 $this->setUser($manager);
700
701 // With default get enrolled, they don't have any courses.
702 $courses = enrol_get_my_courses();
703 $this->assertCount(0, $courses);
704
705 // But with all accessible, they have 4 because they have moodle/course:view everywhere.
706 $courses = enrol_get_my_courses(null, 'id', 0, [], true);
707 $this->assertEquals([$course1->id, $course2->id, $course3->id, $course4->id],
708 array_keys($courses));
709
710 // If we prohibit manager from course:view on course 1 though...
711 assign_capability('moodle/course:view', CAP_PROHIBIT, $managerroleid,
712 \context_course::instance($course1->id));
713 $courses = enrol_get_my_courses(null, 'id', 0, [], true);
714 $this->assertEquals([$course2->id, $course3->id, $course4->id], array_keys($courses));
715
716 // Check for admin user, which has a slightly different query.
717 $this->setAdminUser();
718 $courses = enrol_get_my_courses(null, 'id', 0, [], true);
719 $this->assertEquals([$course1->id, $course2->id, $course3->id, $course4->id], array_keys($courses));
720 }
721
8970ff91
DM
722 /**
723 * test_course_users
724 *
725 * @return void
726 */
727 public function test_course_users() {
728 $this->resetAfterTest();
729
730 $user1 = $this->getDataGenerator()->create_user();
731 $user2 = $this->getDataGenerator()->create_user();
732 $course1 = $this->getDataGenerator()->create_course();
733 $course2 = $this->getDataGenerator()->create_course();
734
735 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
736 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
58f86c4c 737 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
8970ff91
DM
738
739 $this->assertCount(2, enrol_get_course_users($course1->id));
740 $this->assertCount(2, enrol_get_course_users($course1->id, true));
741
58f86c4c
DM
742 $this->assertCount(1, enrol_get_course_users($course1->id, true, array($user1->id)));
743
744 $this->assertCount(2, enrol_get_course_users(false, false, array($user1->id)));
745
8970ff91
DM
746 $instances = enrol_get_instances($course1->id, true);
747 $manualinstance = reset($instances);
748
749 $manualplugin = enrol_get_plugin('manual');
750 $manualplugin->update_user_enrol($manualinstance, $user1->id, ENROL_USER_SUSPENDED);
751 $this->assertCount(2, enrol_get_course_users($course1->id, false));
752 $this->assertCount(1, enrol_get_course_users($course1->id, true));
753 }
f56e6958
PH
754
755 /**
756 * Test count of enrolled users
757 *
758 * @return void
759 */
760 public function test_count_enrolled_users() {
761 global $DB;
762
763 $this->resetAfterTest(true);
764
765 $course = $this->getDataGenerator()->create_course();
766 $context = \context_course::instance($course->id);
767
768 $user1 = $this->getDataGenerator()->create_user();
769 $user2 = $this->getDataGenerator()->create_user();
770
771 $studentrole = $DB->get_record('role', ['shortname' => 'student']);
772
773 // Add each user to the manual enrolment instance.
774 $manual = enrol_get_plugin('manual');
775
776 $manualinstance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual'], '*', MUST_EXIST);
777
778 $manual->enrol_user($manualinstance, $user1->id, $studentrole->id);
779 $manual->enrol_user($manualinstance, $user2->id, $studentrole->id);
780
781 $this->assertEquals(2, count_enrolled_users($context));
782
783 // Create a self enrolment instance, enrol first user only.
784 $self = enrol_get_plugin('self');
785
786 $selfid = $self->add_instance($course,
787 ['status' => ENROL_INSTANCE_ENABLED, 'name' => 'Self', 'customint6' => 1, 'roleid' => $studentrole->id]);
788 $selfinstance = $DB->get_record('enrol', ['id' => $selfid], '*', MUST_EXIST);
789
790 $self->enrol_user($selfinstance, $user1->id, $studentrole->id);
791
792 // There are still only two distinct users.
793 $this->assertEquals(2, count_enrolled_users($context));
794 }
6481a21f
BB
795
796 /**
797 * Test cases for the test_enrol_get_my_courses_sort_by_last_access test.
798 */
799 public function get_enrol_get_my_courses_sort_by_last_access_test_cases() {
800 $now = time();
801
802 $enrolledcoursesdata = [
803 ['shortname' => 'a', 'lastaccess' => $now - 2],
804 ['shortname' => 'b', 'lastaccess' => $now - 1],
805 ['shortname' => 'c', 'lastaccess' => $now],
806 ['shortname' => 'd', 'lastaccess' => $now - 1],
807 ['shortname' => 'e']
808 ];
809 $unenrolledcoursesdata = [
810 ['shortname' => 'x', 'lastaccess' => $now - 2],
811 ['shortname' => 'y', 'lastaccess' => $now - 1],
812 ['shortname' => 'z', 'lastaccess' => $now]
813 ];
814
815 return [
816 'empty set' => [
817 'enrolledcoursesdata' => [],
818 'unenrolledcoursesdata' => $unenrolledcoursesdata,
819 'sort' => 'ul.timeaccess asc',
820 'limit' => 0,
821 'offset' => 0,
822 'expectedcourses' => []
823 ],
824 'ul.timeaccess asc, shortname asc no limit or offset' => [
825 'enrolledcoursesdata' => $enrolledcoursesdata,
826 'unenrolledcoursesdata' => $unenrolledcoursesdata,
827 'sort' => 'ul.timeaccess asc, shortname asc',
828 'limit' => 0,
829 'offset' => 0,
830 'expectedcourses' => ['e', 'a', 'b', 'd', 'c']
831 ],
832 'ul.timeaccess asc, shortname asc with limit no offset' => [
833 'enrolledcoursesdata' => $enrolledcoursesdata,
834 'unenrolledcoursesdata' => $unenrolledcoursesdata,
835 'sort' => 'ul.timeaccess asc, shortname asc',
836 'limit' => 2,
837 'offset' => 0,
838 'expectedcourses' => ['e', 'a']
839 ],
840 'ul.timeaccess asc, shortname asc with limit and offset' => [
841 'enrolledcoursesdata' => $enrolledcoursesdata,
842 'unenrolledcoursesdata' => $unenrolledcoursesdata,
843 'sort' => 'ul.timeaccess asc, shortname asc',
844 'limit' => 2,
845 'offset' => 2,
846 'expectedcourses' => ['b', 'd']
847 ],
848 'ul.timeaccess asc, shortname asc with limit and offset beyond end of data set' => [
849 'enrolledcoursesdata' => $enrolledcoursesdata,
850 'unenrolledcoursesdata' => $unenrolledcoursesdata,
851 'sort' => 'ul.timeaccess asc, shortname asc',
852 'limit' => 2,
853 'offset' => 4,
854 'expectedcourses' => ['c']
855 ],
856 'ul.timeaccess desc, shortname asc no limit or offset' => [
857 'enrolledcoursesdata' => $enrolledcoursesdata,
858 'unenrolledcoursesdata' => $unenrolledcoursesdata,
859 'sort' => 'ul.timeaccess desc, shortname asc',
860 'limit' => 0,
861 'offset' => 0,
862 'expectedcourses' => ['c', 'b', 'd', 'a', 'e']
863 ],
864 'ul.timeaccess desc, shortname desc, no limit or offset' => [
865 'enrolledcoursesdata' => $enrolledcoursesdata,
866 'unenrolledcoursesdata' => $unenrolledcoursesdata,
867 'sort' => 'ul.timeaccess desc, shortname desc',
868 'limit' => 0,
869 'offset' => 0,
870 'expectedcourses' => ['c', 'd', 'b', 'a', 'e']
871 ],
872 'ul.timeaccess asc, shortname desc, no limit or offset' => [
873 'enrolledcoursesdata' => $enrolledcoursesdata,
874 'unenrolledcoursesdata' => $unenrolledcoursesdata,
875 'sort' => 'ul.timeaccess asc, shortname desc',
876 'limit' => 0,
877 'offset' => 0,
878 'expectedcourses' => ['e', 'a', 'd', 'b', 'c']
879 ],
880 'shortname asc, no limit or offset' => [
881 'enrolledcoursesdata' => $enrolledcoursesdata,
882 'unenrolledcoursesdata' => $unenrolledcoursesdata,
883 'sort' => 'shortname asc',
884 'limit' => 0,
885 'offset' => 0,
886 'expectedcourses' => ['a', 'b', 'c', 'd', 'e']
887 ],
888 'shortname desc, no limit or offset' => [
889 'enrolledcoursesdata' => $enrolledcoursesdata,
890 'unenrolledcoursesdata' => $unenrolledcoursesdata,
891 'sort' => 'shortname desc',
892 'limit' => 0,
893 'offset' => 0,
894 'expectedcourses' => ['e', 'd', 'c', 'b', 'a']
895 ],
896 ];
897 }
898
899 /**
900 * Test the get_enrolled_courses_by_timeline_classification function.
901 *
902 * @dataProvider get_enrol_get_my_courses_sort_by_last_access_test_cases()
903 * @param array $enrolledcoursesdata Courses to create and enrol the user in
904 * @param array $unenrolledcoursesdata Courses to create nut not enrol the user in
905 * @param string $sort Sort string for the enrol function
906 * @param int $limit Maximum number of results
907 * @param int $offset Offset the courses result set by this amount
908 * @param array $expectedcourses Expected courses in result
909 */
910 public function test_enrol_get_my_courses_sort_by_last_access(
911 $enrolledcoursesdata,
912 $unenrolledcoursesdata,
913 $sort,
914 $limit,
915 $offset,
916 $expectedcourses
917 ) {
918 global $DB, $CFG;
919
920 $this->resetAfterTest();
921 $generator = $this->getDataGenerator();
922 $student = $generator->create_user();
923 $lastaccessrecords = [];
924
925 foreach ($enrolledcoursesdata as $coursedata) {
926 $lastaccess = null;
927
928 if (isset($coursedata['lastaccess'])) {
929 $lastaccess = $coursedata['lastaccess'];
930 unset($coursedata['lastaccess']);
931 }
932
933 $course = $generator->create_course($coursedata);
934 $generator->enrol_user($student->id, $course->id, 'student');
935
936 if (!is_null($lastaccess)) {
937 $lastaccessrecords[] = [
938 'userid' => $student->id,
939 'courseid' => $course->id,
940 'timeaccess' => $lastaccess
941 ];
942 }
943 }
944
945 foreach ($unenrolledcoursesdata as $coursedata) {
946 $lastaccess = null;
947
948 if (isset($coursedata['lastaccess'])) {
949 $lastaccess = $coursedata['lastaccess'];
950 unset($coursedata['lastaccess']);
951 }
952
953 $course = $generator->create_course($coursedata);
954
955 if (!is_null($lastaccess)) {
956 $lastaccessrecords[] = [
957 'userid' => $student->id,
958 'courseid' => $course->id,
959 'timeaccess' => $lastaccess
960 ];
961 }
962 }
963
964 if (!empty($lastaccessrecords)) {
965 $DB->insert_records('user_lastaccess', $lastaccessrecords);
966 }
967
968 $this->setUser($student);
969
970 $result = enrol_get_my_courses('shortname', $sort, $limit, [], false, $offset);
971 $actual = array_map(function($course) {
972 return $course->shortname;
973 }, array_values($result));
974
975 $this->assertEquals($expectedcourses, $actual);
976 }
05f6da14 977}