Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace enrol_meta;
18
use core\plugininfo\enrol;
19
 
20
use context_course;
21
use enrol_meta_plugin;
22
 
23
/**
24
 * Meta enrolment sync functional test.
25
 *
26
 * @package    enrol_meta
27
 * @category   phpunit
28
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class plugin_test extends \advanced_testcase {
32
 
33
    protected function enable_plugin() {
34
        $enabled = enrol_get_plugins(true);
35
        $enabled['meta'] = true;
36
        $enabled = array_keys($enabled);
37
        set_config('enrol_plugins_enabled', implode(',', $enabled));
38
    }
39
 
40
    protected function disable_plugin() {
41
        $enabled = enrol_get_plugins(true);
42
        unset($enabled['meta']);
43
        $enabled = array_keys($enabled);
44
        set_config('enrol_plugins_enabled', implode(',', $enabled));
45
    }
46
 
47
    protected function is_meta_enrolled($user, $enrol, $role = null) {
48
        global $DB;
49
 
50
        if (!$DB->record_exists('user_enrolments', array('enrolid'=>$enrol->id, 'userid'=>$user->id))) {
51
            return false;
52
        }
53
 
54
        if ($role === null) {
55
            return true;
56
        }
57
 
58
        return $this->has_role($user, $enrol, $role);
59
    }
60
 
61
    protected function has_role($user, $enrol, $role) {
62
        global $DB;
63
 
64
        $context = \context_course::instance($enrol->courseid);
65
 
66
        if ($role === false) {
67
            if ($DB->record_exists('role_assignments', array('contextid'=>$context->id, 'userid'=>$user->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id))) {
68
                return false;
69
            }
70
        } else if (!$DB->record_exists('role_assignments', array('contextid'=>$context->id, 'userid'=>$user->id, 'roleid'=>$role->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id))) {
71
            return false;
72
        }
73
 
74
        return true;
75
    }
76
 
11 efrain 77
    public function test_sync(): void {
1 efrain 78
        global $CFG, $DB;
79
 
80
        $this->resetAfterTest(true);
81
 
82
        $metalplugin = enrol_get_plugin('meta');
83
        $manplugin = enrol_get_plugin('manual');
84
 
85
        $user1 = $this->getDataGenerator()->create_user();
86
        $user2 = $this->getDataGenerator()->create_user();
87
        $user3 = $this->getDataGenerator()->create_user();
88
        $user4 = $this->getDataGenerator()->create_user();
89
        $user5 = $this->getDataGenerator()->create_user();
90
 
91
        $course1 = $this->getDataGenerator()->create_course();
92
        $course2 = $this->getDataGenerator()->create_course();
93
        $course3 = $this->getDataGenerator()->create_course();
94
        $course4 = $this->getDataGenerator()->create_course();
95
        $manual1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
96
        $manual2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
97
        $manual3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
98
        $manual4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
99
 
100
        $student = $DB->get_record('role', array('shortname'=>'student'));
101
        $teacher = $DB->get_record('role', array('shortname'=>'teacher'));
102
        $manager = $DB->get_record('role', array('shortname'=>'manager'));
103
 
104
        $this->disable_plugin();
105
 
106
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
107
        $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $student->id);
108
        $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 0);
109
        $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id);
110
        $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $manager->id);
111
 
112
        $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id);
113
        $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $teacher->id);
114
 
115
        $this->assertEquals(7, $DB->count_records('user_enrolments'));
116
        $this->assertEquals(6, $DB->count_records('role_assignments'));
117
 
118
        set_config('syncall', 0, 'enrol_meta');
119
        set_config('nosyncroleids', $manager->id, 'enrol_meta');
120
 
121
        require_once($CFG->dirroot.'/enrol/meta/locallib.php');
122
 
123
        enrol_meta_sync(null, false);
124
        $this->assertEquals(7, $DB->count_records('user_enrolments'));
125
        $this->assertEquals(6, $DB->count_records('role_assignments'));
126
 
127
        $this->enable_plugin();
128
        enrol_meta_sync(null, false);
129
        $this->assertEquals(7, $DB->count_records('user_enrolments'));
130
        $this->assertEquals(6, $DB->count_records('role_assignments'));
131
 
132
        // Disable the plugin to prevent add_instance from calling enrol_meta_sync.
133
        $this->disable_plugin();
134
        $e1 = $metalplugin->add_instance($course3, array('customint1'=>$course1->id));
135
        $e2 = $metalplugin->add_instance($course3, array('customint1'=>$course2->id));
136
        $e3 = $metalplugin->add_instance($course4, array('customint1'=>$course2->id));
137
        $enrol1 = $DB->get_record('enrol', array('id'=>$e1));
138
        $enrol2 = $DB->get_record('enrol', array('id'=>$e2));
139
        $enrol3 = $DB->get_record('enrol', array('id'=>$e3));
140
        $this->enable_plugin();
141
 
142
        enrol_meta_sync($course4->id, false);
143
        $this->assertEquals(9, $DB->count_records('user_enrolments'));
144
        $this->assertEquals(8, $DB->count_records('role_assignments'));
145
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
146
        $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher));
147
 
148
        enrol_meta_sync(null, false);
149
        $this->assertEquals(14, $DB->count_records('user_enrolments'));
150
        $this->assertEquals(13, $DB->count_records('role_assignments'));
151
 
152
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
153
        $this->assertTrue($this->is_meta_enrolled($user2, $enrol1, $student));
154
        $this->assertFalse($this->is_meta_enrolled($user3, $enrol1));
155
        $this->assertTrue($this->is_meta_enrolled($user4, $enrol1, $teacher));
156
        $this->assertFalse($this->is_meta_enrolled($user5, $enrol1));
157
 
158
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student));
159
        $this->assertTrue($this->is_meta_enrolled($user2, $enrol2, $teacher));
160
 
161
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
162
        $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher));
163
 
164
        set_config('syncall', 1, 'enrol_meta');
165
        enrol_meta_sync(null, false);
166
        $this->assertEquals(16, $DB->count_records('user_enrolments'));
167
        $this->assertEquals(13, $DB->count_records('role_assignments'));
168
 
169
        $this->assertTrue($this->is_meta_enrolled($user3, $enrol1, false));
170
        $this->assertTrue($this->is_meta_enrolled($user5, $enrol1, false));
171
 
172
        $this->assertEquals(16, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
173
        $this->disable_plugin();
174
        $manplugin->unenrol_user($manual1, $user1->id);
175
        $manplugin->unenrol_user($manual2, $user1->id);
176
 
177
        $this->assertEquals(14, $DB->count_records('user_enrolments'));
178
        $this->assertEquals(11, $DB->count_records('role_assignments'));
179
        $this->assertEquals(14, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
180
 
181
        $this->enable_plugin();
182
 
183
        set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta');
184
        enrol_meta_sync($course4->id, false);
185
        $this->assertEquals(14, $DB->count_records('user_enrolments'));
186
        $this->assertEquals(11, $DB->count_records('role_assignments'));
187
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
188
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
189
        $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol3->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
190
 
191
        enrol_meta_sync(null, false);
192
        $this->assertEquals(14, $DB->count_records('user_enrolments'));
193
        $this->assertEquals(11, $DB->count_records('role_assignments'));
194
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
195
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
196
        $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol1->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
197
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student));
198
        $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol2->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
199
 
200
        set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
201
        enrol_meta_sync($course4->id, false);
202
        $this->assertEquals(14, $DB->count_records('user_enrolments'));
203
        $this->assertEquals(10, $DB->count_records('role_assignments'));
204
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
205
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, false));
206
        $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol3->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
207
 
208
        enrol_meta_sync(null, false);
209
        $this->assertEquals(14, $DB->count_records('user_enrolments'));
210
        $this->assertEquals(8, $DB->count_records('role_assignments'));
211
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
212
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
213
        $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol1->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
214
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, false));
215
        $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol2->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
216
 
217
        set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
218
        enrol_meta_sync($course4->id, false);
219
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
220
        $this->assertEquals(8, $DB->count_records('role_assignments'));
221
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
222
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol3));
223
 
224
        enrol_meta_sync(null, false);
225
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
226
        $this->assertEquals(8, $DB->count_records('role_assignments'));
227
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
228
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
229
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol2));
230
 
231
 
232
        // Now try sync triggered by events.
233
 
234
        set_config('syncall', 1, 'enrol_meta');
235
 
236
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
237
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
238
        $this->assertEquals(10, $DB->count_records('role_assignments'));
239
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
240
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
241
        enrol_meta_sync(null, false);
242
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
243
        $this->assertEquals(10, $DB->count_records('role_assignments'));
244
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
245
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
246
 
247
        $manplugin->unenrol_user($manual1, $user1->id);
248
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
249
        $this->assertEquals(8, $DB->count_records('role_assignments'));
250
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
251
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
252
        enrol_meta_sync(null, false);
253
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
254
        $this->assertEquals(8, $DB->count_records('role_assignments'));
255
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
256
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
257
 
258
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
259
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
260
        $this->assertEquals(8, $DB->count_records('role_assignments'));
261
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
262
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
263
        enrol_meta_sync(null, false);
264
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
265
        $this->assertEquals(8, $DB->count_records('role_assignments'));
266
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
267
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
268
 
269
        $manplugin->unenrol_user($manual1, $user1->id);
270
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
271
        $this->assertEquals(8, $DB->count_records('role_assignments'));
272
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
273
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
274
        enrol_meta_sync(null, false);
275
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
276
        $this->assertEquals(8, $DB->count_records('role_assignments'));
277
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
278
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
279
 
280
        set_config('syncall', 0, 'enrol_meta');
281
        enrol_meta_sync(null, false);
282
        $this->assertEquals(9, $DB->count_records('user_enrolments'));
283
        $this->assertEquals(8, $DB->count_records('role_assignments'));
284
        $this->assertEquals(9, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
285
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
286
 
287
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
288
        $this->assertEquals(10, $DB->count_records('user_enrolments'));
289
        $this->assertEquals(8, $DB->count_records('role_assignments'));
290
        $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
291
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
292
        enrol_meta_sync(null, false);
293
        $this->assertEquals(10, $DB->count_records('user_enrolments'));
294
        $this->assertEquals(8, $DB->count_records('role_assignments'));
295
        $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
296
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
297
 
298
        role_assign($teacher->id, $user1->id, \context_course::instance($course1->id)->id);
299
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
300
        $this->assertEquals(10, $DB->count_records('role_assignments'));
301
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
302
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher));
303
        enrol_meta_sync(null, false);
304
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
305
        $this->assertEquals(10, $DB->count_records('role_assignments'));
306
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
307
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher));
308
 
309
        role_unassign($teacher->id, $user1->id, \context_course::instance($course1->id)->id);
310
        $this->assertEquals(10, $DB->count_records('user_enrolments'));
311
        $this->assertEquals(8, $DB->count_records('role_assignments'));
312
        $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
313
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
314
        enrol_meta_sync(null, false);
315
        $this->assertEquals(10, $DB->count_records('user_enrolments'));
316
        $this->assertEquals(8, $DB->count_records('role_assignments'));
317
        $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
318
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
319
 
320
        $manplugin->unenrol_user($manual1, $user1->id);
321
        $this->assertEquals(9, $DB->count_records('user_enrolments'));
322
        $this->assertEquals(8, $DB->count_records('role_assignments'));
323
        $this->assertEquals(9, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
324
        $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
325
 
326
        set_config('syncall', 1, 'enrol_meta');
327
        set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta');
328
        enrol_meta_sync(null, false);
329
        $this->assertEquals(11, $DB->count_records('user_enrolments'));
330
        $this->assertEquals(8, $DB->count_records('role_assignments'));
331
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
332
 
333
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
334
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
335
        $this->assertEquals(10, $DB->count_records('role_assignments'));
336
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
337
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
338
        enrol_meta_sync(null, false);
339
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
340
        $this->assertEquals(10, $DB->count_records('role_assignments'));
341
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
342
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
343
 
344
        $manplugin->update_user_enrol($manual1, $user1->id, ENROL_USER_SUSPENDED);
345
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
346
        $this->assertEquals(10, $DB->count_records('role_assignments'));
347
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
348
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
349
        enrol_meta_sync(null, false);
350
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
351
        $this->assertEquals(10, $DB->count_records('role_assignments'));
352
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
353
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
354
 
355
        $manplugin->unenrol_user($manual1, $user1->id);
356
        $this->assertEquals(12, $DB->count_records('user_enrolments'));
357
        $this->assertEquals(9, $DB->count_records('role_assignments'));
358
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
359
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
360
        enrol_meta_sync(null, false);
361
        $this->assertEquals(12, $DB->count_records('user_enrolments'));
362
        $this->assertEquals(9, $DB->count_records('role_assignments'));
363
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
364
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
365
 
366
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
367
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
368
        $this->assertEquals(10, $DB->count_records('role_assignments'));
369
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
370
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
371
        enrol_meta_sync(null, false);
372
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
373
        $this->assertEquals(10, $DB->count_records('role_assignments'));
374
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
375
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
376
 
377
        set_config('syncall', 1, 'enrol_meta');
378
        set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
379
        enrol_meta_sync(null, false);
380
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
381
        $this->assertEquals(10, $DB->count_records('role_assignments'));
382
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
383
 
384
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
385
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
386
        $this->assertEquals(10, $DB->count_records('role_assignments'));
387
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
388
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
389
        enrol_meta_sync(null, false);
390
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
391
        $this->assertEquals(10, $DB->count_records('role_assignments'));
392
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
393
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
394
 
395
        $manplugin->unenrol_user($manual1, $user1->id);
396
        $this->assertEquals(12, $DB->count_records('user_enrolments'));
397
        $this->assertEquals(8, $DB->count_records('role_assignments'));
398
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
399
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
400
        enrol_meta_sync(null, false);
401
        $this->assertEquals(12, $DB->count_records('user_enrolments'));
402
        $this->assertEquals(8, $DB->count_records('role_assignments'));
403
        $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
404
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
405
 
406
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
407
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
408
        $this->assertEquals(10, $DB->count_records('role_assignments'));
409
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
410
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
411
        enrol_meta_sync(null, false);
412
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
413
        $this->assertEquals(10, $DB->count_records('role_assignments'));
414
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
415
        $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
416
 
417
 
418
        set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
419
        enrol_meta_sync(null, false);
420
        $this->assertEquals(13, $DB->count_records('user_enrolments'));
421
        $this->assertEquals(10, $DB->count_records('role_assignments'));
422
        $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
423
 
424
        delete_course($course1, false);
425
        $this->assertEquals(3, $DB->count_records('user_enrolments'));
426
        $this->assertEquals(3, $DB->count_records('role_assignments'));
427
        $this->assertEquals(3, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
428
        enrol_meta_sync(null, false);
429
        $this->assertEquals(3, $DB->count_records('user_enrolments'));
430
        $this->assertEquals(3, $DB->count_records('role_assignments'));
431
        $this->assertEquals(3, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
432
 
433
        delete_course($course2, false);
434
        $this->assertEquals(0, $DB->count_records('user_enrolments'));
435
        $this->assertEquals(0, $DB->count_records('role_assignments'));
436
        $this->assertEquals(0, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
437
        enrol_meta_sync(null, false);
438
        $this->assertEquals(0, $DB->count_records('user_enrolments'));
439
        $this->assertEquals(0, $DB->count_records('role_assignments'));
440
        $this->assertEquals(0, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
441
 
442
        delete_course($course3, false);
443
        delete_course($course4, false);
444
 
445
    }
446
 
11 efrain 447
    public function test_add_to_group(): void {
1 efrain 448
        global $CFG, $DB;
449
 
450
        require_once($CFG->dirroot.'/group/lib.php');
451
 
452
        $this->resetAfterTest(true);
453
 
454
        $metalplugin = enrol_get_plugin('meta');
455
 
456
        $user1 = $this->getDataGenerator()->create_user();
457
        $user4 = $this->getDataGenerator()->create_user();
458
 
459
        $course1 = $this->getDataGenerator()->create_course();
460
        $course2 = $this->getDataGenerator()->create_course();
461
        $course3 = $this->getDataGenerator()->create_course();
462
        $manualenrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
463
        $manualenrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
464
 
465
        $student = $DB->get_record('role', array('shortname' => 'student'));
466
        $teacher = $DB->get_record('role', array('shortname' => 'teacher'));
467
 
468
        $id = groups_create_group((object)array('name' => 'Group 1 in course 3', 'courseid' => $course3->id));
469
        $group31 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
470
        $id = groups_create_group((object)array('name' => 'Group 2 in course 4', 'courseid' => $course3->id));
471
        $group32 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
472
 
473
        $this->enable_plugin();
474
 
475
        $e1 = $metalplugin->add_instance($course3, array('customint1' => $course1->id, 'customint2' => $group31->id));
476
        $e2 = $metalplugin->add_instance($course3, array('customint1' => $course2->id, 'customint2' => $group32->id));
477
 
478
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
479
        $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id);
480
 
481
        $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id);
482
 
483
        // Now make sure users are in the correct groups.
484
        $this->assertTrue(groups_is_member($group31->id, $user1->id));
485
        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user1->id,
486
            'component' => 'enrol_meta', 'itemid' => $e1)));
487
        $this->assertTrue(groups_is_member($group32->id, $user1->id));
488
        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group32->id, 'userid' => $user1->id,
489
            'component' => 'enrol_meta', 'itemid' => $e2)));
490
 
491
        $this->assertTrue(groups_is_member($group31->id, $user4->id));
492
        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user4->id,
493
            'component' => 'enrol_meta', 'itemid' => $e1)));
494
 
495
        // Make sure everything is the same after sync.
496
        enrol_meta_sync(null, false);
497
        $this->assertTrue(groups_is_member($group31->id, $user1->id));
498
        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user1->id,
499
            'component' => 'enrol_meta', 'itemid' => $e1)));
500
        $this->assertTrue(groups_is_member($group32->id, $user1->id));
501
        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group32->id, 'userid' => $user1->id,
502
            'component' => 'enrol_meta', 'itemid' => $e2)));
503
 
504
        $this->assertTrue(groups_is_member($group31->id, $user4->id));
505
        $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user4->id,
506
            'component' => 'enrol_meta', 'itemid' => $e1)));
507
 
508
        set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
509
 
510
        // When user 1 is unenrolled from course1, he is removed from group31 but still present in group32.
511
        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user1->id);
512
        $this->assertFalse(groups_is_member($group31->id, $user1->id));
513
        $this->assertTrue(groups_is_member($group32->id, $user1->id));
514
        $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user1, '', true)); // He still has active enrolment.
515
        // And the same after sync.
516
        enrol_meta_sync(null, false);
517
        $this->assertFalse(groups_is_member($group31->id, $user1->id));
518
        $this->assertTrue(groups_is_member($group32->id, $user1->id));
519
        $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user1, '', true));
520
 
521
        // Unenroll user1 from course2 and make sure he is completely unenrolled from course3.
522
        enrol_get_plugin('manual')->unenrol_user($manualenrol2, $user1->id);
523
        $this->assertFalse(groups_is_member($group32->id, $user1->id));
524
        $this->assertFalse(is_enrolled(\context_course::instance($course3->id), $user1));
525
 
526
        set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
527
 
528
        // When user is unenrolled in this case, he is still a member of a group (but enrolment is suspended).
529
        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user4->id);
530
        $this->assertTrue(groups_is_member($group31->id, $user4->id));
531
        $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user4));
532
        $this->assertFalse(is_enrolled(\context_course::instance($course3->id), $user4, '', true));
533
        enrol_meta_sync(null, false);
534
        $this->assertTrue(groups_is_member($group31->id, $user4->id));
535
        $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user4));
536
        $this->assertFalse(is_enrolled(\context_course::instance($course3->id), $user4, '', true));
537
    }
538
 
539
    /**
540
     * Enrol users from another course into a course where one of the members is already enrolled
541
     * and is a member of the same group.
542
     */
11 efrain 543
    public function test_add_to_group_with_member(): void {
1 efrain 544
        global $CFG, $DB;
545
 
546
        require_once($CFG->dirroot.'/group/lib.php');
547
 
548
        $this->resetAfterTest(true);
549
 
550
        $metalplugin = enrol_get_plugin('meta');
551
 
552
        $user1 = $this->getDataGenerator()->create_user();
553
        $user2 = $this->getDataGenerator()->create_user();
554
 
555
        $course1 = $this->getDataGenerator()->create_course();
556
        $course2 = $this->getDataGenerator()->create_course();
557
        $manualenrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
558
        $manualenrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
559
 
560
        $student = $DB->get_record('role', array('shortname' => 'student'));
561
 
562
        $groupid = groups_create_group((object)array('name' => 'Grp', 'courseid' => $course2->id));
563
 
564
        $this->enable_plugin();
565
        set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
566
 
567
        // Manually enrol user1 to course2 and add him to group.
568
        // Manually enrol user2 to course2 but do not add him to the group.
569
        enrol_get_plugin('manual')->enrol_user($manualenrol2, $user1->id, $student->id);
570
        groups_add_member($groupid, $user1->id);
571
        enrol_get_plugin('manual')->enrol_user($manualenrol2, $user2->id, $student->id);
572
        $this->assertTrue(groups_is_member($groupid, $user1->id));
573
        $this->assertFalse(groups_is_member($groupid, $user2->id));
574
 
575
        // Add instance of meta enrolment in course2 linking to course1 and enrol both users in course1.
576
        $metalplugin->add_instance($course2, array('customint1' => $course1->id, 'customint2' => $groupid));
577
 
578
        enrol_get_plugin('manual')->enrol_user($manualenrol1, $user1->id, $student->id);
579
        enrol_get_plugin('manual')->enrol_user($manualenrol1, $user2->id, $student->id);
580
 
581
        // Both users now should be members of the group.
582
        $this->assertTrue(groups_is_member($groupid, $user1->id));
583
        $this->assertTrue(groups_is_member($groupid, $user2->id));
584
 
585
        // Ununerol both users from course1.
586
        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user1->id);
587
        enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user2->id);
588
 
589
        // User1 should still be member of the group because he was added there manually. User2 should no longer be there.
590
        $this->assertTrue(groups_is_member($groupid, $user1->id));
591
        $this->assertFalse(groups_is_member($groupid, $user2->id));
592
 
593
        // Assert that everything is the same after sync.
594
        enrol_meta_sync();
595
 
596
        $this->assertTrue(groups_is_member($groupid, $user1->id));
597
        $this->assertFalse(groups_is_member($groupid, $user2->id));
598
 
599
    }
600
 
601
    /**
602
     * Test enrolling users in a course, where the customint2 (group) property of the instance points to an invalid group
603
     *
604
     * @covers \enrol_meta_handler::sync_with_parent_course
605
     * @covers ::enrol_meta_sync
606
     */
607
    public function test_add_to_group_invalid(): void {
608
        $this->resetAfterTest();
609
 
610
        $this->enable_plugin();
611
 
612
        $courseone = $this->getDataGenerator()->create_course();
613
        $coursetwo = $this->getDataGenerator()->create_course();
614
 
615
        /** @var enrol_meta_plugin $plugin */
616
        $plugin = enrol_get_plugin('meta');
617
        $plugin->add_instance($coursetwo, ['customint1' => $courseone->id, 'customint2' => 42]);
618
 
619
        // Ensure the event observer works for invalid groups.
620
        $userone = $this->getDataGenerator()->create_and_enrol($courseone);
621
 
622
        // Now disable the plugin, add another enrolment.
623
        $this->disable_plugin();
624
        $usertwo = $this->getDataGenerator()->create_and_enrol($courseone);
625
 
626
        // Re-enable the plugin, run sync task - should also work for invalid groups.
627
        $this->enable_plugin();
628
        enrol_meta_sync($coursetwo->id);
629
 
630
        $coursetwocontext = context_course::instance($coursetwo->id);
631
        $this->assertTrue(is_enrolled($coursetwocontext, $userone));
632
        $this->assertTrue(is_enrolled($coursetwocontext, $usertwo));
633
    }
634
 
635
    /**
636
     * Test user_enrolment_created event.
637
     */
11 efrain 638
    public function test_user_enrolment_created_event(): void {
1 efrain 639
        global $DB;
640
 
641
        $this->resetAfterTest();
642
 
643
        $metaplugin = enrol_get_plugin('meta');
644
        $user1 = $this->getDataGenerator()->create_user();
645
        $course1 = $this->getDataGenerator()->create_course();
646
        $course2 = $this->getDataGenerator()->create_course();
647
        $student = $DB->get_record('role', array('shortname' => 'student'));
648
 
649
        $e1 = $metaplugin->add_instance($course2, array('customint1' => $course1->id));
650
        $enrol1 = $DB->get_record('enrol', array('id' => $e1));
651
 
652
        // Enrol user and capture event.
653
        $sink = $this->redirectEvents();
654
 
655
        $metaplugin->enrol_user($enrol1, $user1->id, $student->id);
656
        $events = $sink->get_events();
657
        $sink->close();
658
        $event = array_shift($events);
659
 
660
        // Test Event.
661
        $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
662
        $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
663
        $this->assertEquals($dbuserenrolled->id, $event->objectid);
664
        $this->assertEventContextNotUsed($event);
665
    }
666
 
667
    /**
668
     * Test user_enrolment_deleted event.
669
     */
11 efrain 670
    public function test_user_enrolment_deleted_event(): void {
1 efrain 671
        global $DB;
672
 
673
        $this->resetAfterTest(true);
674
 
675
        $metalplugin = enrol_get_plugin('meta');
676
        $user1 = $this->getDataGenerator()->create_user();
677
        $course1 = $this->getDataGenerator()->create_course();
678
        $course2 = $this->getDataGenerator()->create_course();
679
        $student = $DB->get_record('role', array('shortname'=>'student'));
680
 
681
        $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
682
        $enrol1 = $DB->get_record('enrol', array('id' => $e1));
683
 
684
        // Enrol user.
685
        $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
686
        $this->assertEquals(1, $DB->count_records('user_enrolments'));
687
 
688
        // Unenrol user and capture event.
689
        $sink = $this->redirectEvents();
690
        $metalplugin->unenrol_user($enrol1, $user1->id);
691
        $events = $sink->get_events();
692
        $sink->close();
693
        $event = array_pop($events);
694
 
695
        $this->assertEquals(0, $DB->count_records('user_enrolments'));
696
        $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
697
        $this->assertEventContextNotUsed($event);
698
    }
699
 
700
    /**
701
     * Test user_enrolment_updated event.
702
     */
11 efrain 703
    public function test_user_enrolment_updated_event(): void {
1 efrain 704
        global $DB;
705
 
706
        $this->resetAfterTest(true);
707
 
708
        $metalplugin = enrol_get_plugin('meta');
709
        $user1 = $this->getDataGenerator()->create_user();
710
        $course1 = $this->getDataGenerator()->create_course();
711
        $course2 = $this->getDataGenerator()->create_course();
712
        $student = $DB->get_record('role', array('shortname'=>'student'));
713
 
714
        $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
715
        $enrol1 = $DB->get_record('enrol', array('id' => $e1));
716
 
717
        // Enrol user.
718
        $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
719
        $this->assertEquals(1, $DB->count_records('user_enrolments'));
720
 
721
        // Updated enrolment for user and capture event.
722
        $sink = $this->redirectEvents();
723
        $metalplugin->update_user_enrol($enrol1, $user1->id, ENROL_USER_SUSPENDED, null, time());
724
        $events = $sink->get_events();
725
        $sink->close();
726
        $event = array_shift($events);
727
 
728
        // Test Event.
729
        $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
730
        $this->assertInstanceOf('\core\event\user_enrolment_updated', $event);
731
        $this->assertEquals($dbuserenrolled->id, $event->objectid);
732
        $this->assertEventContextNotUsed($event);
733
    }
734
 
735
    /**
736
     * Test that a new group with the name of the course is created.
737
     */
11 efrain 738
    public function test_enrol_meta_create_new_group(): void {
1 efrain 739
        global $DB, $CFG;
740
        $this->resetAfterTest();
741
        // Create two courses.
742
        $course = $this->getDataGenerator()->create_course(array('fullname' => 'Mathematics'));
743
        $course2 = $this->getDataGenerator()->create_course(array('fullname' => 'Physics'));
744
        $metacourse = $this->getDataGenerator()->create_course(array('fullname' => 'All sciences'));
745
 
746
        require_once($CFG->dirroot.'/enrol/meta/locallib.php');
747
 
748
        // Run the function.
749
        $groupid = enrol_meta_create_new_group($metacourse->id, $course->id);
750
        // Check the results.
751
        $group = $DB->get_record('groups', array('id' => $groupid));
752
        // The group name should match the course name.
753
        $this->assertEquals('Mathematics course', $group->name);
754
        // Group course id should match the course id.
755
        $this->assertEquals($metacourse->id, $group->courseid);
756
 
757
        // Create a group that will have the same name as the course.
758
        $groupdata = new \stdClass();
759
        $groupdata->courseid = $metacourse->id;
760
        $groupdata->name = 'Physics course';
761
        groups_create_group($groupdata);
762
        // Create a group for the course 2 in metacourse.
763
        $groupid = enrol_meta_create_new_group($metacourse->id, $course2->id);
764
        $groupinfo = $DB->get_record('groups', array('id' => $groupid));
765
        // Check that the group name has been changed.
766
        $this->assertEquals('Physics course (2)', $groupinfo->name);
767
 
768
        // Create a group for the course 2 in metacourse.
769
        $groupid = enrol_meta_create_new_group($metacourse->id, $course2->id);
770
        $groupinfo = $DB->get_record('groups', array('id' => $groupid));
771
        // Check that the group name has been changed.
772
        $this->assertEquals('Physics course (3)', $groupinfo->name);
773
    }
774
 
775
    /**
776
     * Test that enrolment timestart-timeend is respected in meta course.
777
     */
11 efrain 778
    public function test_timeend(): void {
1 efrain 779
        global $CFG, $DB;
780
 
781
        $this->resetAfterTest(true);
782
 
783
        $timeinfuture = time() + DAYSECS;
784
        $timeinpast = time() - DAYSECS;
785
 
786
        $metalplugin = enrol_get_plugin('meta');
787
        $manplugin = enrol_get_plugin('manual');
788
 
789
        $user1 = $this->getDataGenerator()->create_user();
790
        $user2 = $this->getDataGenerator()->create_user();
791
        $user3 = $this->getDataGenerator()->create_user();
792
        $user4 = $this->getDataGenerator()->create_user();
793
        $user5 = $this->getDataGenerator()->create_user();
794
 
795
        $course1 = $this->getDataGenerator()->create_course();
796
        $course2 = $this->getDataGenerator()->create_course();
797
        $course3 = $this->getDataGenerator()->create_course();
798
        $manual1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
799
 
800
        $student = $DB->get_record('role', array('shortname' => 'student'));
801
 
802
        $this->enable_plugin();
803
 
804
        // Create instance of enrol_meta in course2 when there are no enrolments present.
805
        $meta2id = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
806
 
807
        $expectedenrolments = array(
808
            $user1->id => array(0, 0, ENROL_USER_ACTIVE),
809
            $user2->id => array($timeinpast, 0, ENROL_USER_ACTIVE),
810
            $user3->id => array(0, $timeinfuture, ENROL_USER_ACTIVE),
811
            $user4->id => array($timeinpast, $timeinfuture, ENROL_USER_ACTIVE),
812
            $user5->id => array(0, 0, ENROL_USER_SUSPENDED),
813
        );
814
        foreach ($expectedenrolments as $userid => $data) {
815
            $expectedenrolments[$userid] = (object)(array('userid' => $userid) +
816
                    array_combine(array('timestart', 'timeend', 'status'), $data));
817
        }
818
 
819
        // Enrol users manually in course 1.
820
        foreach ($expectedenrolments as $e) {
821
            $manplugin->enrol_user($manual1, $e->userid, $student->id, $e->timestart, $e->timeend, $e->status);
822
        }
823
 
824
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $manual1->id), 'userid', 'userid, timestart, timeend, status');
825
        $this->assertEquals($expectedenrolments, $enrolments);
826
 
827
        // Make sure that the same enrolments are now present in course2 under meta enrolment.
828
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
829
        $this->assertEquals($expectedenrolments, $enrolments);
830
 
831
        // Create instance of enrol_meta in course3 and run sync.
832
        $meta3id = $metalplugin->add_instance($course3, array('customint1' => $course1->id));
833
        enrol_meta_sync($course3->id);
834
 
835
        // Make sure that the same enrolments are now present in course3 under meta enrolment.
836
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
837
        $this->assertEquals($expectedenrolments, $enrolments);
838
 
839
        // Update some of the manual enrolments.
840
        $expectedenrolments[$user2->id]->timestart = $timeinpast - 60;
841
        $expectedenrolments[$user3->id]->timeend = $timeinfuture + 60;
842
        $expectedenrolments[$user4->id]->status = ENROL_USER_SUSPENDED;
843
        $expectedenrolments[$user5->id]->status = ENROL_USER_ACTIVE;
844
        foreach ($expectedenrolments as $e) {
845
            $manplugin->update_user_enrol($manual1, $e->userid, $e->status, $e->timestart, $e->timeend);
846
        }
847
 
848
        // Make sure meta courses are also updated.
849
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
850
        $this->assertEquals($expectedenrolments, $enrolments);
851
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
852
        $this->assertEquals($expectedenrolments, $enrolments);
853
 
854
        // Test meta sync. Imagine events are not working.
855
        $sink = $this->redirectEvents();
856
        $expectedenrolments[$user2->id]->timestart = $timeinpast;
857
        $expectedenrolments[$user3->id]->timeend = $timeinfuture;
858
        $expectedenrolments[$user4->id]->status = ENROL_USER_ACTIVE;
859
        $expectedenrolments[$user5->id]->status = ENROL_USER_SUSPENDED;
860
        foreach ($expectedenrolments as $e) {
861
            $manplugin->update_user_enrol($manual1, $e->userid, $e->status, $e->timestart, $e->timeend);
862
        }
863
 
864
        // Make sure meta courses are updated only for the course that was synced.
865
        enrol_meta_sync($course3->id);
866
 
867
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
868
        $this->assertNotEquals($expectedenrolments, $enrolments);
869
 
870
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
871
        $this->assertEquals($expectedenrolments, $enrolments);
872
 
873
        $sink->close();
874
 
875
        // Disable manual enrolment in course1 and make sure all user enrolments in course2 are suspended.
876
        $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
877
        $allsuspendedenrolemnts = array_combine(array_keys($expectedenrolments), array_fill(0, 5, ENROL_USER_SUSPENDED));
878
        $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta2id), '', 'userid, status');
879
        $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
880
 
881
        $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
882
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta2id), '', 'userid, timestart, timeend, status');
883
        $this->assertEquals($expectedenrolments, $enrolments);
884
 
885
        // Disable events and repeat the same for course3 (testing sync):
886
        $sink = $this->redirectEvents();
887
        $manplugin->update_status($manual1, ENROL_INSTANCE_DISABLED);
888
        enrol_meta_sync($course3->id);
889
        $enrolmentstatuses = $DB->get_records_menu('user_enrolments', array('enrolid' => $meta3id), '', 'userid, status');
890
        $this->assertEquals($allsuspendedenrolemnts, $enrolmentstatuses);
891
 
892
        $manplugin->update_status($manual1, ENROL_INSTANCE_ENABLED);
893
        enrol_meta_sync($course3->id);
894
        $enrolments = $DB->get_records('user_enrolments', array('enrolid' => $meta3id), '', 'userid, timestart, timeend, status');
895
        $this->assertEquals($expectedenrolments, $enrolments);
896
        $sink->close();
897
    }
898
 
899
    /**
900
     * Test for getting user enrolment actions.
901
     */
11 efrain 902
    public function test_get_user_enrolment_actions(): void {
1 efrain 903
        global $CFG, $PAGE;
904
        $this->resetAfterTest();
905
 
906
        // Set page URL to prevent debugging messages.
907
        $PAGE->set_url('/enrol/editinstance.php');
908
 
909
        $pluginname = 'meta';
910
 
911
        // Only enable the meta enrol plugin.
912
        $CFG->enrol_plugins_enabled = $pluginname;
913
 
914
        $generator = $this->getDataGenerator();
915
 
916
        // Get the enrol plugin.
917
        $plugin = enrol_get_plugin($pluginname);
918
 
919
        // Create a course.
920
        $course = $generator->create_course();
921
        // Enable this enrol plugin for the course.
922
        $plugin->add_instance($course);
923
 
924
        // Create a student.
925
        $student = $generator->create_user();
926
        // Enrol the student to the course.
927
        $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
928
 
929
        // Teachers don't have enrol/meta:unenrol capability by default. Login as admin for simplicity.
930
        $this->setAdminUser();
931
        require_once($CFG->dirroot . '/enrol/locallib.php');
932
        $manager = new \course_enrolment_manager($PAGE, $course);
933
 
934
        $userenrolments = $manager->get_user_enrolments($student->id);
935
        $this->assertCount(1, $userenrolments);
936
 
937
        $ue = reset($userenrolments);
938
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
939
        // Meta-link enrolment has no enrol actions for active students.
940
        $this->assertCount(0, $actions);
941
 
942
        // Enrol actions for a suspended student.
943
        // Suspend the student.
944
        $ue->status = ENROL_USER_SUSPENDED;
945
 
946
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
947
        // Meta-link enrolment has enrol actions for suspended students -- unenrol.
948
        $this->assertCount(1, $actions);
949
    }
950
 
951
    /**
952
     * Test how data for instance editing is validated.
953
     */
11 efrain 954
    public function test_edit_instance_validation(): void {
1 efrain 955
        global $DB;
956
 
957
        $this->resetAfterTest();
958
 
959
        $metaplugin = enrol_get_plugin('meta');
960
 
961
        // A course with meta enrolment.
962
        $course = $this->getDataGenerator()->create_course();
963
        $coursecontext = \context_course::instance($course->id);
964
 
965
        // Create a meta enrolment instance.
966
        $instance = (object)$metaplugin->get_instance_defaults();
967
        $instance->id       = null;
968
        $instance->courseid = $course->id;
969
        $instance->status   = ENROL_INSTANCE_ENABLED;
970
        // Emulate the form data.
971
        $data = [
972
            'customint1' => 0,
973
            'customint2' => 0
974
        ];
975
        // Test when no valid 'customint1' field (meta courses links) is provided.
976
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
977
        // We're going to check the string contents of the errors returned as this is the only way
978
        // to differentiate the errors produced by the 'edit_instance_validation()' method somehow.
979
        // The method always returns what the edit instance form expects and this is an array of form fields
980
        // with the corresponding errors messages.
981
        $this->assertEquals('Required', $errors['customint1']);
982
 
983
        // Test when 'customint1' contains an unknown course.
984
        // Fetch the max course id from the courses table and increment it to get
985
        // the course id which surely doesn't exist.
986
        $maxid = $DB->get_field_sql('SELECT MAX(id) FROM {course}');
987
        // Use the same instance as before but set another data.
988
        $data = [
989
            'customint1' => [$maxid + 1],
990
            'customint2' => 0
991
        ];
992
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
993
        $this->assertEquals('You are trying to use an invalid course ID', $errors['customint1']);
994
 
995
        // Test when 'customint1' field already contains courses meta linked with the current one.
996
        $metacourse1 = $this->getDataGenerator()->create_course();
997
        $metaplugin->add_instance($course, array('customint1' => $metacourse1->id));
998
        // Use the same instance as before but set another data.
999
        $data = [
1000
            'customint1' => [$metacourse1->id],
1001
            'customint2' => 0
1002
        ];
1003
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1004
        $this->assertEquals('You are trying to use an invalid course ID', $errors['customint1']);
1005
 
1006
        // Test when a course is set as a not visible and a user doesn't have the capability to use it here.
1007
        $metacourse2record = new \stdClass();
1008
        $metacourse2record->visible = 0;
1009
        $metacourse2 = $this->getDataGenerator()->create_course($metacourse2record);
1010
        $metacourse2context = \context_course::instance($metacourse2->id);
1011
 
1012
        $user = $this->getDataGenerator()->create_user();
1013
        $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
1014
        role_assign($teacherrole->id, $user->id, $metacourse2context->id);
1015
        unassign_capability('moodle/course:viewhiddencourses', $teacherrole->id);
1016
        $this->setUser($user);
1017
 
1018
        // Use the same instance as before but set another data.
1019
        $data = [
1020
            'customint1' => [$metacourse2->id],
1021
            'customint2' => 0
1022
        ];
1023
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1024
        $this->assertEquals('Sorry, but you do not currently have permissions to do that (moodle/course:viewhiddencourses).',
1025
            $errors['customint1']);
1026
 
1027
        // Revert some changes from the last assertion to reuse the course.
1028
        $metacourse2->visible = 1;
1029
        $DB->update_record('course', $metacourse2);
1030
        assign_capability('moodle/course:viewhiddencourses', CAP_ALLOW,
1031
            $teacherrole->id, \context_course::instance($metacourse2->id));
1032
 
1033
        // Test with no 'enrol/meta:selectaslinked' capability.
1034
        unassign_capability('enrol/meta:selectaslinked', $teacherrole->id);
1035
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1036
        $this->assertEquals('Sorry, but you do not currently have permissions to do that (enrol/meta:selectaslinked).',
1037
            $errors['customint1']);
1038
 
1039
        // Back to admin user to regain the capabilities quickly.
1040
        $this->setAdminUser();
1041
 
1042
        // Test when meta course id is the site id.
1043
        $site = $DB->get_record('course', ['id' => SITEID]);
1044
        // Use the same instance as before but set another data.
1045
        $data = [
1046
            'customint1' => [$site->id],
1047
            'customint2' => 0
1048
        ];
1049
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1050
        $this->assertEquals('You are trying to use an invalid course ID', $errors['customint1']);
1051
 
1052
        // Test when meta course id is id of the current course.
1053
        // Use the same instance as before but set another data.
1054
        $data = [
1055
            'customint1' => [$course->id],
1056
            'customint2' => 0
1057
        ];
1058
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1059
        $this->assertEquals('You are trying to use an invalid course ID', $errors['customint1']);
1060
 
1061
        // Test with the 'customint2' field set (which is groups).
1062
        // Prepare some groups data.
1063
        $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1064
        $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1065
        $groups = [];
1066
        foreach (groups_get_all_groups($course->id) as $group) {
1067
            $groups[$group->id] = format_string($group->name, true, array('context' => $coursecontext));
1068
        }
1069
 
1070
        // Use the same instance as before but set another data.
1071
        // Use a non-existing group id.
1072
        if (!$maxid = $DB->get_field_sql('SELECT MAX(id) FROM {groups}')) {
1073
            $maxid = 0;
1074
        }
1075
        $data = [
1076
            'customint1' => [$metacourse2->id],
1077
            'customint2' => [$maxid + 1]
1078
        ];
1079
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1080
        $this->assertArrayHasKey('customint2', $errors);
1081
 
1082
        // Test with valid data.
1083
        reset($groups);
1084
        $validgroup = key($groups);
1085
        $data = [
1086
            'customint1' => [$metacourse2->id],
1087
            'customint2' => $validgroup
1088
        ];
1089
        $errors = $metaplugin->edit_instance_validation($data, [], $instance, $coursecontext);
1090
        $this->assertArrayNotHasKey('customint1', $errors);
1091
        $this->assertArrayNotHasKey('customint2', $errors);
1092
    }
1093
 
1094
    /**
1095
     * Test the behaviour of fill_enrol_custom_fields().
1096
     *
1097
     * @covers ::fill_enrol_custom_fields
1098
     */
11 efrain 1099
    public function test_fill_enrol_custom_fields(): void {
1 efrain 1100
        $this->resetAfterTest();
1101
 
1102
        $metaplugin = enrol_get_plugin('meta');
1103
 
1104
        $cat = $this->getDataGenerator()->create_category();
1105
        $course1 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course1']);
1106
        $course2 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course2']);
1107
 
1108
        $group = $this->getDataGenerator()->create_group(['courseid' => $course1->id]);
1109
 
1110
        $enrolmentdata['metacoursename'] = $course2->shortname;
1111
        $enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
1112
        $this->assertArrayHasKey('customint1', $enrolmentdata);
1113
        $this->assertEquals($course2->id, $enrolmentdata['customint1']);
1114
        $this->assertNull($enrolmentdata['customint2']);
1115
 
1116
        $enrolmentdata['metacoursename'] = 'notexist';
1117
        $enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
1118
        $this->assertArrayHasKey('customint1', $enrolmentdata);
1119
        $this->assertFalse($enrolmentdata['customint1']);
1120
        $this->assertNull($enrolmentdata['customint2']);
1121
 
1122
        $enrolmentdata['metacoursename'] = $course2->shortname;
1123
 
1124
        $enrolmentdata['addtogroup'] = 0;
1125
        $enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
1126
        $this->assertArrayHasKey('customint1', $enrolmentdata);
1127
        $this->assertEquals($course2->id, $enrolmentdata['customint1']);
1128
        $this->assertArrayHasKey('customint2', $enrolmentdata);
1129
        $this->assertEquals(0, $enrolmentdata['customint2']);
1130
 
1131
        unset($enrolmentdata['addtogroup']);
1132
        $enrolmentdata['groupname'] = $group->name;
1133
        $enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
1134
        $this->assertArrayHasKey('customint1', $enrolmentdata);
1135
        $this->assertEquals($course2->id, $enrolmentdata['customint1']);
1136
        $this->assertArrayHasKey('customint2', $enrolmentdata);
1137
        $this->assertEquals($group->id, $enrolmentdata['customint2']);
1138
 
1139
        $enrolmentdata['groupname'] = 'notexist';
1140
        $enrolmentdata = $metaplugin->fill_enrol_custom_fields($enrolmentdata, $course1->id);
1141
        $this->assertArrayHasKey('customint1', $enrolmentdata);
1142
        $this->assertEquals($course2->id, $enrolmentdata['customint1']);
1143
        $this->assertArrayHasKey('customint2', $enrolmentdata);
1144
        $this->assertFalse($enrolmentdata['customint2']);
1145
    }
1146
 
1147
    /**
1148
     * Test the behaviour of validate_enrol_plugin_data().
1149
     *
1150
     * @covers ::validate_enrol_plugin_data
1151
     */
11 efrain 1152
    public function test_validate_enrol_plugin_data(): void {
1 efrain 1153
        $this->resetAfterTest();
1154
 
1155
        $cat = $this->getDataGenerator()->create_category();
1156
 
1157
        $course1 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course1']);
1158
        $course2 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course2']);
1159
 
1160
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course1->id, 'name' => 'Group 1']);
1161
 
1162
        enrol::enable_plugin('meta', false);
1163
 
1164
        $metaplugin = enrol_get_plugin('meta');
1165
 
1166
        // Plugin is disabled in system and meta course shortname is missing in csv.
1167
        $enrolmentdata = [];
1168
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata);
1169
        $this->assertArrayHasKey('plugindisabled', $errors);
1170
        $this->assertArrayHasKey('missingmandatoryfields', $errors);
1171
 
1172
        enrol::enable_plugin('meta', true);
1173
 
1174
        // Unknown meta course name.
1175
        $enrolmentdata['metacoursename'] = 'test';
1176
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata);
1177
        $this->assertArrayHasKey('unknownmetacourse', $errors);
1178
 
1179
        // Meta course is same as original course.
1180
        $enrolmentdata['metacoursename'] = 'course1';
1181
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1182
        $this->assertArrayHasKey('samemetacourse', $errors);
1183
 
1184
        // Non-valid 'addtogroup' option.
1185
        $enrolmentdata['metacoursename'] = $course2->shortname;
1186
        $enrolmentdata['addtogroup'] = 2;
1187
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1188
        $this->assertArrayHasKey('erroraddtogroup', $errors);
1189
 
1190
        // Options 'addtogroup' and 'groupname' are not allowed together.
1191
        $enrolmentdata['addtogroup'] = 0;
1192
        $enrolmentdata['groupname'] = 'test';
1193
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1194
        $this->assertArrayHasKey('erroraddtogroupgroupname', $errors);
1195
 
1196
        // Group does not exist.
1197
        unset($enrolmentdata['addtogroup']);
1198
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1199
        $this->assertArrayHasKey('errorinvalidgroup', $errors);
1200
 
1201
        // Valid data when trying to create a group.
1202
        $enrolmentdata['metacoursename'] = $course2->shortname;
1203
        $enrolmentdata['addtogroup'] = 1;
1204
        unset($enrolmentdata['groupname']);
1205
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1206
        $this->assertEmpty($errors);
1207
 
1208
        // Valid data when trying to add to existing group.
1209
        $enrolmentdata['groupname'] = $group1->name;
1210
        unset($enrolmentdata['addtogroup']);
1211
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1212
        $this->assertEmpty($errors);
1213
 
1214
        // Valid data when trying without group mode.
1215
        $enrolmentdata['addtogroup'] = 0;
1216
        unset($enrolmentdata['groupname']);
1217
        $errors = $metaplugin->validate_enrol_plugin_data($enrolmentdata, $course1->id);
1218
        $this->assertEmpty($errors);
1219
    }
1220
 
1221
    /**
1222
     * Test the behaviour of find_instance().
1223
     *
1224
     * @covers ::find_instance
1225
     */
11 efrain 1226
    public function test_find_instance(): void {
1 efrain 1227
        global $DB;
1228
        $this->resetAfterTest();
1229
 
1230
        $cat = $this->getDataGenerator()->create_category();
1231
        $course1 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course1']);
1232
        $course2 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course2']);
1233
        $course3 = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'course3']);
1234
 
1235
        $metaplugin = enrol_get_plugin('meta');
1236
 
1237
        // Add two meta enrol instances.
1238
        $instanceid1 = $metaplugin->add_instance($course1, ['customint1' => $course2->id]);
1239
        $instanceid2 = $metaplugin->add_instance($course1, ['customint1' => $course3->id]);
1240
 
1241
        $instance1 = $DB->get_record('enrol', ['id' => $instanceid1]);
1242
        $instance2 = $DB->get_record('enrol', ['id' => $instanceid2]);
1243
 
1244
        $enrolmentdata = [];
1245
        $instance = $metaplugin->find_instance($enrolmentdata, $course1->id);
1246
        $this->assertNull($instance);
1247
 
1248
        // Unknown meta course shortname.
1249
        $enrolmentdata['metacoursename'] = 'test';
1250
        $instance = $metaplugin->find_instance($enrolmentdata, $course1->id);
1251
        $this->assertNull($instance);
1252
 
1253
        $enrolmentdata['metacoursename'] = $course2->shortname;
1254
        $instance = $metaplugin->find_instance($enrolmentdata, $course1->id);
1255
        $this->assertEquals($instance1->id, $instance->id);
1256
 
1257
        $enrolmentdata['metacoursename'] = $course3->shortname;
1258
        $instance = $metaplugin->find_instance($enrolmentdata, $course1->id);
1259
        $this->assertEquals($instance2->id, $instance->id);
1260
    }
1261
}