Merge branch 'MDL-45851' of git://github.com/timhunt/moodle
[moodle.git] / enrol / flatfile / tests / flatfile_test.php
blob8f48e3751a3cd2e42c4fe41d6d4834dd93cd44ec
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 * flatfile enrolment sync tests.
20 * @package enrol_flatfile
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 class enrol_flatfile_testcase extends advanced_testcase {
31 protected function enable_plugin() {
32 $enabled = enrol_get_plugins(true);
33 $enabled['flatfile'] = true;
34 $enabled = array_keys($enabled);
35 set_config('enrol_plugins_enabled', implode(',', $enabled));
38 protected function disable_plugin() {
39 $enabled = enrol_get_plugins(true);
40 unset($enabled['flatfile']);
41 $enabled = array_keys($enabled);
42 set_config('enrol_plugins_enabled', implode(',', $enabled));
45 public function test_basics() {
46 $this->assertFalse(enrol_is_enabled('flatfile'));
47 $plugin = enrol_get_plugin('flatfile');
48 $this->assertInstanceOf('enrol_flatfile_plugin', $plugin);
49 $this->assertEquals(ENROL_EXT_REMOVED_SUSPENDNOROLES, get_config('enrol_flatfile', 'expiredaction'));
52 public function test_sync_nothing() {
53 $this->resetAfterTest();
55 $this->disable_plugin();
56 $flatfileplugin = enrol_get_plugin('flatfile');
58 // Just make sure the sync does not throw any errors when nothing to do.
59 $flatfileplugin->sync(new null_progress_trace());
60 $this->enable_plugin();
61 $flatfileplugin->sync(new null_progress_trace());
64 public function test_sync() {
65 global $CFG, $DB;
66 $this->resetAfterTest();
68 /** @var enrol_flatfile_plugin $flatfileplugin */
69 $flatfileplugin = enrol_get_plugin('flatfile');
70 /** @var enrol_manual_plugin $manualplugin */
71 $manualplugin = enrol_get_plugin('manual');
72 $this->assertNotEmpty($manualplugin);
74 $trace = new null_progress_trace();
75 $this->enable_plugin();
76 $file = "$CFG->dataroot/enrol.txt";
78 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
79 $this->assertNotEmpty($studentrole);
80 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
81 $this->assertNotEmpty($teacherrole);
82 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
83 $this->assertNotEmpty($managerrole);
85 $user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'u1'));
86 $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'u2'));
87 $user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'u3'));
88 $user4 = $this->getDataGenerator()->create_user(array('idnumber'=>'čtvrtý'));
89 $user5 = $this->getDataGenerator()->create_user(array('idnumber'=>'u5'));
90 $user6 = $this->getDataGenerator()->create_user(array('idnumber'=>'u6'));
91 $user7 = $this->getDataGenerator()->create_user(array('idnumber'=>''));
93 $course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'c1'));
94 $course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'c2'));
95 $course3 = $this->getDataGenerator()->create_course(array('idnumber'=>'c3'));
96 $context1 = context_course::instance($course1->id);
97 $context2 = context_course::instance($course2->id);
98 $context3 = context_course::instance($course3->id);
100 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
101 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
102 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
104 // Rename teacher role.
105 $flatfileplugin->set_config('map_'.$teacherrole->id, 'ucitel');
106 // Disable manager role.
107 $flatfileplugin->set_config('map_'.$managerrole->id, '');
108 // Set file location.
109 $flatfileplugin->set_config('location', $file);
111 $now = time();
112 $before = $now - 60;
113 $future = $now + 60*60*5;
114 $farfuture = $now + 60*60*24*5;
117 // Test add action.
119 $data ="'add','student','u1','c1'
121 \"add\" , \"ucitel\", u2 , c2
122 add,manager,u3,c1
123 add,student,čtvrtý,c2,$before
124 add,student,u5,c1,0,0,1
125 add,student,u5,c2,20,10
126 add,student,u6,c1,0,$future
127 add,student,u6,c2,$future,0
128 add,student,u6,c3,$future,$farfuture
129 add,student,,c2";
130 file_put_contents($file, $data);
132 $this->assertEquals(0, $DB->count_records('user_enrolments'));
133 $this->assertEquals(0, $DB->count_records('role_assignments'));
134 $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
136 $this->assertTrue(file_exists($file));
137 $flatfileplugin->sync($trace);
138 $this->assertFalse(file_exists($file));
140 $this->assertEquals(4, $DB->count_records('user_enrolments'));
141 $this->assertEquals(4, $DB->count_records('role_assignments'));
142 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
143 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user2->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_flatfile')));
144 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
145 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
148 // Test buffer.
150 $this->assertEquals(2, $DB->count_records('enrol_flatfile'));
152 $flatfileplugin->sync($trace);
153 $this->assertEquals(2, $DB->count_records('enrol_flatfile'));
154 $this->assertEquals(4, $DB->count_records('user_enrolments'));
155 $this->assertEquals(4, $DB->count_records('role_assignments'));
157 $DB->set_field('enrol_flatfile', 'timestart', time()-60, array('timestart'=>$future, 'timeend'=>0));
159 $flatfileplugin->sync($trace);
160 $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
161 $this->assertEquals(5, $DB->count_records('user_enrolments'));
162 $this->assertEquals(5, $DB->count_records('role_assignments'));
163 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
164 $this->assertTrue($DB->record_exists('enrol_flatfile', array('userid'=>$user6->id, 'roleid'=>$studentrole->id, 'timeend'=>$farfuture)));
167 // Test encoding.
169 $data = "add;student;čtvrtý;c3";
170 $data = core_text::convert($data, 'utf-8', 'iso-8859-2');
171 file_put_contents($file, $data);
172 $flatfileplugin->set_config('encoding', 'iso-8859-2');
174 $flatfileplugin->sync($trace);
175 $this->assertEquals(6, $DB->count_records('user_enrolments'));
176 $this->assertEquals(6, $DB->count_records('role_assignments'));
177 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
178 $flatfileplugin->set_config('encoding', 'UTF-8');
180 // Test unenrolling purges buffer.
182 $manualplugin->enrol_user($maninstance1, $user1->id, $teacherrole->id);
183 $manualplugin->enrol_user($maninstance3, $user5->id, $teacherrole->id);
185 $this->assertEquals(8, $DB->count_records('user_enrolments'));
186 $this->assertEquals(8, $DB->count_records('role_assignments'));
187 $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
188 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
190 $flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
193 $data = "del,student,u1,c1\ndel,teacher,u6,c3";
194 file_put_contents($file, $data);
196 $flatfileplugin->sync($trace);
197 $this->assertEquals(8, $DB->count_records('user_enrolments'));
198 $this->assertEquals(8, $DB->count_records('role_assignments'));
199 $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
201 $data = "del,student,u6,c3";
202 file_put_contents($file, $data);
204 $flatfileplugin->sync($trace);
205 $this->assertEquals(8, $DB->count_records('user_enrolments'));
206 $this->assertEquals(8, $DB->count_records('role_assignments'));
207 $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
210 $flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
212 $data = "
213 del,student,u1,c1
214 del,grrr,u5,c1
215 del,guest,u5,c2
216 del,student,u6,c2
217 del,ucitel,u5,c3";
218 file_put_contents($file, $data);
220 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
221 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
222 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
224 $flatfileplugin->sync($trace);
225 $this->assertEquals(8, $DB->count_records('user_enrolments'));
226 $this->assertEquals(5, $DB->count_records('role_assignments'));
227 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
228 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
229 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
230 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
233 $flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
235 $manualplugin->enrol_user($maninstance3, $user5->id, $teacherrole->id);
236 $data = "
237 add,student,u1,c1
238 add,student,u6,c2";
239 file_put_contents($file, $data);
241 $flatfileplugin->sync($trace);
243 $this->assertEquals(8, $DB->count_records('user_enrolments'));
244 $this->assertEquals(8, $DB->count_records('role_assignments'));
245 $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
246 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
247 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
248 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
249 $this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user5->id, 'enrolid'=>$maninstance3->id)));
250 $this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user1->id, 'enrolid'=>$maninstance1->id)));
252 $data = "
253 del,student,u1,c1
254 del,grrr,u5,c1
255 del,guest,u5,c2
256 del,student,u6,c2
257 del,ucitel,u5,c3";
258 file_put_contents($file, $data);
260 $flatfileplugin->sync($trace);
261 $this->assertEquals(5, $DB->count_records('user_enrolments'));
262 $this->assertEquals(5, $DB->count_records('role_assignments'));
263 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
264 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
265 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
266 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
267 $this->assertFalse($DB->record_exists('user_enrolments', array('userid'=>$user5->id, 'enrolid'=>$maninstance3->id)));
268 $this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user1->id, 'enrolid'=>$maninstance1->id)));
271 public function test_notification() {
272 global $CFG, $DB;
273 $this->resetAfterTest();
275 $this->preventResetByRollback();
277 /** @var enrol_flatfile_plugin $flatfileplugin */
278 $flatfileplugin = enrol_get_plugin('flatfile');
279 /** @var enrol_manual_plugin $manualplugin */
280 $manualplugin = enrol_get_plugin('manual');
281 $this->assertNotEmpty($manualplugin);
283 $this->enable_plugin();
285 $trace = new progress_trace_buffer(new text_progress_trace(), false);
286 $file = "$CFG->dataroot/enrol.txt";
287 $flatfileplugin->set_config('location', $file);
289 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
290 $this->assertNotEmpty($studentrole);
291 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
292 $this->assertNotEmpty($teacherrole);
294 $user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'u1'));
295 $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'u2'));
296 $user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'u3'));
297 $admin = get_admin();
299 $course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'c1'));
300 $course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'c2'));
301 $context1 = context_course::instance($course1->id);
302 $context2 = context_course::instance($course2->id);
304 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
306 $now = time();
307 $future = $now + 60*60*5;
308 $farfuture = $now + 60*60*24*5;
310 $manualplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id);
312 $data =
313 "add,student,u1,c1
314 add,student,u2,c2
315 add,student,u2,c1,$future,$farfuture";
316 file_put_contents($file, $data);
318 $this->assertEquals(1, $DB->count_records('user_enrolments'));
319 $this->assertEquals(1, $DB->count_records('role_assignments'));
320 $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
322 $flatfileplugin->set_config('mailadmins', 1);
323 $flatfileplugin->set_config('mailteachers', 1);
324 $flatfileplugin->set_config('mailstudents', 1);
326 $sink = $this->redirectMessages();
328 $flatfileplugin->sync($trace);
330 $this->assertEquals(3, $DB->count_records('user_enrolments'));
331 $this->assertEquals(3, $DB->count_records('role_assignments'));
332 $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
334 $messages = $sink->get_messages();
335 $this->assertCount(5, $messages);
337 // Notify student from teacher.
338 $this->assertEquals($user1->id, $messages[0]->useridto);
339 $this->assertEquals($user3->id, $messages[0]->useridfrom);
341 // Notify teacher.
342 $this->assertEquals($user3->id, $messages[1]->useridto);
343 $this->assertEquals($admin->id, $messages[1]->useridfrom);
345 // Notify student when teacher not present.
346 $this->assertEquals($user2->id, $messages[2]->useridto);
347 $this->assertEquals($admin->id, $messages[2]->useridfrom);
349 // Notify admin when teacher not present.
350 $this->assertEquals($admin->id, $messages[3]->useridto);
351 $this->assertEquals($admin->id, $messages[3]->useridfrom);
353 // Sent report to admin from self.
354 $this->assertEquals($admin->id, $messages[4]->useridto);
355 $this->assertEquals($admin->id, $messages[4]->useridfrom);
358 public function test_expired() {
359 global $DB;
360 $this->resetAfterTest();
362 /** @var enrol_flatfile_plugin $flatfileplugin */
363 $flatfileplugin = enrol_get_plugin('flatfile');
364 /** @var enrol_manual_plugin $manualplugin */
365 $manualplugin = enrol_get_plugin('manual');
366 $this->assertNotEmpty($manualplugin);
368 $now = time();
369 $trace = new null_progress_trace();
370 $this->enable_plugin();
373 // Prepare some data.
375 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
376 $this->assertNotEmpty($studentrole);
377 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
378 $this->assertNotEmpty($teacherrole);
379 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
380 $this->assertNotEmpty($managerrole);
382 $user1 = $this->getDataGenerator()->create_user();
383 $user2 = $this->getDataGenerator()->create_user();
384 $user3 = $this->getDataGenerator()->create_user();
385 $user4 = $this->getDataGenerator()->create_user();
387 $course1 = $this->getDataGenerator()->create_course();
388 $course2 = $this->getDataGenerator()->create_course();
389 $context1 = context_course::instance($course1->id);
390 $context2 = context_course::instance($course2->id);
392 $data = array('roleid'=>$studentrole->id, 'courseid'=>$course1->id);
393 $id = $flatfileplugin->add_instance($course1, $data);
394 $instance1 = $DB->get_record('enrol', array('id'=>$id));
395 $data = array('roleid'=>$studentrole->id, 'courseid'=>$course2->id);
396 $id = $flatfileplugin->add_instance($course2, $data);
397 $instance2 = $DB->get_record('enrol', array('id'=>$id));
398 $data = array('roleid'=>$teacherrole->id, 'courseid'=>$course2->id);
399 $id = $flatfileplugin->add_instance($course2, $data);
400 $instance3 = $DB->get_record('enrol', array('id'=>$id));
402 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
404 $manualplugin->enrol_user($maninstance1, $user3->id, $studentrole->id);
406 $this->assertEquals(1, $DB->count_records('user_enrolments'));
407 $this->assertEquals(1, $DB->count_records('role_assignments'));
408 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
410 $flatfileplugin->enrol_user($instance1, $user1->id, $studentrole->id);
411 $flatfileplugin->enrol_user($instance1, $user2->id, $studentrole->id);
412 $flatfileplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
414 $flatfileplugin->enrol_user($instance2, $user1->id, $studentrole->id, 0, 0);
415 $flatfileplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now-60*60);
416 $flatfileplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now+60*60);
418 $flatfileplugin->enrol_user($instance3, $user1->id, $teacherrole->id, $now-60*60*24*7, $now-60);
419 $flatfileplugin->enrol_user($instance3, $user4->id, $teacherrole->id);
421 role_assign($managerrole->id, $user3->id, $context1->id);
423 $this->assertEquals(9, $DB->count_records('user_enrolments'));
424 $this->assertEquals(10, $DB->count_records('role_assignments'));
425 $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
426 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
427 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
429 // Execute tests.
431 $flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_KEEP);
432 $code = $flatfileplugin->sync($trace);
433 $this->assertSame(0, $code);
434 $this->assertEquals(9, $DB->count_records('user_enrolments'));
435 $this->assertEquals(10, $DB->count_records('role_assignments'));
438 $flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
439 $flatfileplugin->sync($trace);
440 $this->assertEquals(9, $DB->count_records('user_enrolments'));
441 $this->assertEquals(7, $DB->count_records('role_assignments'));
442 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
443 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
444 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
445 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
446 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
447 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
448 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
451 $flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
452 role_assign($studentrole->id, $user3->id, $context1->id, 'enrol_flatfile', $instance1->id);
453 role_assign($studentrole->id, $user2->id, $context2->id, 'enrol_flatfile', $instance2->id);
454 role_assign($teacherrole->id, $user1->id, $context2->id, 'enrol_flatfile', $instance3->id);
455 $this->assertEquals(9, $DB->count_records('user_enrolments'));
456 $this->assertEquals(10, $DB->count_records('role_assignments'));
457 $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
458 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
459 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
460 $flatfileplugin->sync($trace);
461 $this->assertEquals(6, $DB->count_records('user_enrolments'));
462 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
463 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance2->id, 'userid'=>$user2->id)));
464 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
465 $this->assertEquals(6, $DB->count_records('role_assignments'));
466 $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
467 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
468 $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
472 * Flatfile enrolment sync task test.
474 public function test_flatfile_sync_task() {
475 global $CFG, $DB;
476 $this->resetAfterTest();
478 $flatfileplugin = enrol_get_plugin('flatfile');
480 $trace = new null_progress_trace();
481 $this->enable_plugin();
482 $file = "$CFG->dataroot/enrol.txt";
483 $flatfileplugin->set_config('location', $file);
485 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
486 $this->assertNotEmpty($studentrole);
488 $user1 = $this->getDataGenerator()->create_user(array('idnumber' => 'u1'));
489 $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'c1'));
490 $context1 = context_course::instance($course1->id);
492 $data =
493 "add,student,u1,c1";
494 file_put_contents($file, $data);
496 $task = new enrol_flatfile\task\flatfile_sync_task;
497 $task->execute();
499 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid' => $studentrole->id)));
503 * Test for getting user enrolment actions.
505 public function test_get_user_enrolment_actions() {
506 global $CFG, $PAGE;
507 $this->resetAfterTest();
509 // Set page URL to prevent debugging messages.
510 $PAGE->set_url('/enrol/editinstance.php');
512 $pluginname = 'flatfile';
514 // Only enable the flatfile enrol plugin.
515 $CFG->enrol_plugins_enabled = $pluginname;
517 $generator = $this->getDataGenerator();
519 // Get the enrol plugin.
520 $plugin = enrol_get_plugin($pluginname);
522 // Create a course.
523 $course = $generator->create_course();
524 // Enable this enrol plugin for the course.
525 $plugin->add_instance($course);
527 // Create a student.
528 $student = $generator->create_user();
529 // Enrol the student to the course.
530 $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
532 // Teachers don't have enrol/flatfile:manage and enrol/flatfile:unenrol capabilities by default.
533 // Login as admin for simplicity.
534 $this->setAdminUser();
536 require_once($CFG->dirroot . '/enrol/locallib.php');
537 $manager = new course_enrolment_manager($PAGE, $course);
538 $userenrolments = $manager->get_user_enrolments($student->id);
539 $this->assertCount(1, $userenrolments);
541 $ue = reset($userenrolments);
542 $actions = $plugin->get_user_enrolment_actions($manager, $ue);
543 // Flatfile enrolment has 2 enrol actions for active users -- edit and unenrol.
544 $this->assertCount(2, $actions);