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_cohort;
18
 
19
use core\plugininfo\enrol;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot.'/cohort/lib.php');
25
require_once($CFG->dirroot.'/group/lib.php');
26
 
27
/**
28
 * Contains tests for the cohort library.
29
 *
30
 * @package   enrol_cohort
31
 * @category  test
32
 * @copyright 2015 Adrian Greeve <adrian@moodle.com>
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class lib_test extends \advanced_testcase {
36
 
37
    /**
38
     * Test that a new group with the name of the cohort is created.
39
     */
11 efrain 40
    public function test_enrol_cohort_create_new_group(): void {
1 efrain 41
        global $DB;
42
        $this->resetAfterTest();
43
        // Create a category.
44
        $category = $this->getDataGenerator()->create_category();
45
        // Create two courses.
46
        $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
47
        $course2 = $this->getDataGenerator()->create_course(array('category' => $category->id));
48
        // Create a cohort.
49
        $cohort = $this->getDataGenerator()->create_cohort(array('context' => \context_coursecat::instance($category->id)->id));
50
        // Run the function.
51
        $groupid = enrol_cohort_create_new_group($course->id, $cohort->id);
52
        // Check the results.
53
        $group = $DB->get_record('groups', array('id' => $groupid));
54
        // The group name should match the cohort name.
55
        $this->assertEquals($cohort->name . ' cohort', $group->name);
56
        // Group course id should match the course id.
57
        $this->assertEquals($course->id, $group->courseid);
58
 
59
        // Create a group that will have the same name as the cohort.
60
        $groupdata = new \stdClass();
61
        $groupdata->courseid = $course2->id;
62
        $groupdata->name = $cohort->name . ' cohort';
63
        groups_create_group($groupdata);
64
        // Create a group for the cohort in course 2.
65
        $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
66
        $groupinfo = $DB->get_record('groups', array('id' => $groupid));
67
        // Check that the group name has been changed.
68
        $this->assertEquals($cohort->name . ' cohort (2)', $groupinfo->name);
69
 
70
        // Create another group that will have the same name as a generated cohort.
71
        $groupdata = new \stdClass();
72
        $groupdata->courseid = $course2->id;
73
        $groupdata->name = $cohort->name . ' cohort (2)';
74
        groups_create_group($groupdata);
75
        // Create a group for the cohort in course 2.
76
        $groupid = enrol_cohort_create_new_group($course2->id, $cohort->id);
77
        $groupinfo = $DB->get_record('groups', array('id' => $groupid));
78
        // Check that the group name has been changed.
79
        $this->assertEquals($cohort->name . ' cohort (3)', $groupinfo->name);
80
 
81
    }
82
 
83
    /**
84
     * Test for getting user enrolment actions.
85
     */
11 efrain 86
    public function test_get_user_enrolment_actions(): void {
1 efrain 87
        global $CFG, $PAGE;
88
        $this->resetAfterTest();
89
 
90
        // Set page URL to prevent debugging messages.
91
        $PAGE->set_url('/enrol/editinstance.php');
92
 
93
        $pluginname = 'cohort';
94
 
95
        // Only enable the cohort enrol plugin.
96
        $CFG->enrol_plugins_enabled = $pluginname;
97
 
98
        $generator = $this->getDataGenerator();
99
 
100
        // Get the enrol plugin.
101
        $plugin = enrol_get_plugin($pluginname);
102
 
103
        // Create a course.
104
        $course = $generator->create_course();
105
        // Enable this enrol plugin for the course.
106
        $plugin->add_instance($course);
107
 
108
        // Create a student.
109
        $student = $generator->create_user();
110
        // Enrol the student to the course.
111
        $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
112
 
113
        // Teachers don't have enrol/cohort:unenrol capability by default. Login as admin for simplicity.
114
        $this->setAdminUser();
115
        require_once($CFG->dirroot . '/enrol/locallib.php');
116
        $manager = new \course_enrolment_manager($PAGE, $course);
117
 
118
        $userenrolments = $manager->get_user_enrolments($student->id);
119
        $this->assertCount(1, $userenrolments);
120
 
121
        $ue = reset($userenrolments);
122
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
123
        // Cohort-sync has no enrol actions for active students.
124
        $this->assertCount(0, $actions);
125
 
126
        // Enrol actions for a suspended student.
127
        // Suspend the student.
128
        $ue->status = ENROL_USER_SUSPENDED;
129
 
130
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
131
        // Cohort-sync has enrol actions for suspended students -- unenrol.
132
        $this->assertCount(1, $actions);
133
    }
134
 
11 efrain 135
    public function test_enrol_cohort_unenrolaction_suspend_only(): void {
1 efrain 136
        global $CFG, $DB, $PAGE;
137
        $this->resetAfterTest();
138
 
139
        $trace = new \null_progress_trace();
140
 
141
        $cohortplugin = enrol_get_plugin('cohort');
142
        $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
143
 
144
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
145
        $this->assertNotEmpty($studentrole);
146
 
147
        // Setup a test course.
148
        $course = $this->getDataGenerator()->create_course();
149
 
150
        $user1 = $this->getDataGenerator()->create_user();
151
        $user2 = $this->getDataGenerator()->create_user();
152
        $user3 = $this->getDataGenerator()->create_user();
153
        $user4 = $this->getDataGenerator()->create_user();
154
 
155
        $cohort = $this->getDataGenerator()->create_cohort();
156
 
157
        $cohortplugin->add_instance($course, ['customint1' => $cohort->id,
158
            'roleid' => $studentrole->id]
159
        );
160
 
161
        cohort_add_member($cohort->id, $user1->id);
162
        cohort_add_member($cohort->id, $user2->id);
163
        cohort_add_member($cohort->id, $user3->id);
164
        cohort_add_member($cohort->id, $user4->id);
165
 
166
        // Test sync.
167
        enrol_cohort_sync($trace, $course->id);
168
 
169
        // All users should be enrolled.
170
        $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
171
        $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user2));
172
        $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user3));
173
        $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user4));
174
 
175
        // Remove cohort member.
176
        cohort_remove_member($cohort->id, $user1->id);
177
        $this->assertTrue(is_enrolled(\context_course::instance($course->id), $user1));
178
 
179
        // Run the sync again.
180
        enrol_cohort_sync($trace, $course->id);
181
 
182
        $enrolid = $DB->get_field('enrol', 'id', ['enrol' => 'cohort', 'customint1' => $cohort->id]);
183
        $ue = $DB->get_record('user_enrolments', ['enrolid' => $enrolid, 'userid' => $user1->id]);
184
 
185
        // Check user is suspended.
186
        $this->assertEquals($ue->status, ENROL_USER_SUSPENDED);
187
        // Check that user4 still have student role.
188
        $userrole = $DB->get_record('role_assignments', ['userid' => $user1->id]);
189
        $this->assertNotEmpty($userrole);
190
        $this->assertEquals($studentrole->id, $userrole->roleid);
191
 
192
        // Delete the cohort.
193
        cohort_delete_cohort($cohort);
194
 
195
        // Run the sync again.
196
        enrol_cohort_sync($trace, $course->id);
197
 
198
        $ue = $DB->get_records('user_enrolments', ['enrolid' => $enrolid], '', 'userid, status, enrolid');
199
 
200
        // Check users are suspended.
201
        $this->assertEquals($ue[$user2->id]->status, ENROL_USER_SUSPENDED);
202
        $this->assertEquals($ue[$user3->id]->status, ENROL_USER_SUSPENDED);
203
        $this->assertEquals($ue[$user4->id]->status, ENROL_USER_SUSPENDED);
204
 
205
        // Check that users still have student role.
206
        $usersrole = $DB->get_records('role_assignments', ['itemid' => $enrolid], '', 'userid, roleid');
207
        $this->assertNotEmpty($usersrole);
208
        $this->assertEquals($studentrole->id, $usersrole[$user2->id]->roleid);
209
        $this->assertEquals($studentrole->id, $usersrole[$user3->id]->roleid);
210
        $this->assertEquals($studentrole->id, $usersrole[$user4->id]->roleid);
211
    }
212
 
213
    /**
214
     * Test the behaviour of validate_plugin_data_context().
215
     *
216
     * @covers ::validate_plugin_data_context
217
     */
11 efrain 218
    public function test_validate_plugin_data_context(): void {
1 efrain 219
        $this->resetAfterTest();
220
 
221
        $cohortplugin = enrol_get_plugin('cohort');
222
 
223
        $cat = $this->getDataGenerator()->create_category();
224
        $cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
225
        $cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
226
 
227
        $course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);
228
 
229
        $cohort1 = $this->getDataGenerator()->create_cohort([
230
            'contextid' => \context_coursecat::instance($cat1->id)->id,
231
            'idnumber' => 'one',
232
        ]);
233
        $cohort2 = $this->getDataGenerator()->create_cohort([
234
            'contextid' => \context_coursecat::instance($cat2->id)->id,
235
            'idnumber' => 'two',
236
        ]);
237
 
238
        $enrolmentdata = [
239
            'customint1' => $cohort2->id,
240
            'cohortidnumber' => $cohort2->idnumber,
241
        ];
242
        $error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
243
        $this->assertInstanceOf('lang_string', $error);
244
        $this->assertEquals('contextcohortnotallowed', $error->get_identifier());
245
 
246
        $enrolmentdata = [
247
            'customint1' => $cohort1->id,
248
            'cohortidnumber' => $cohort1->idnumber,
249
            'courseid' => $course->id,
250
            'id' => null,
251
            'status' => ENROL_INSTANCE_ENABLED,
252
        ];
253
        $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
254
        $error = $cohortplugin->validate_plugin_data_context($enrolmentdata, $course->id);
255
        $this->assertNull($error);
256
    }
257
 
258
    /**
259
     * Test the behaviour of fill_enrol_custom_fields().
260
     *
261
     * @covers ::fill_enrol_custom_fields
262
     */
11 efrain 263
    public function test_fill_enrol_custom_fields(): void {
1 efrain 264
        $this->resetAfterTest();
265
 
266
        $cohortplugin = enrol_get_plugin('cohort');
267
 
268
        $cat = $this->getDataGenerator()->create_category();
269
        $course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
270
        $cohort = $this->getDataGenerator()->create_cohort([
271
            'contextid' => \context_coursecat::instance($cat->id)->id,
272
            'idnumber' => 'one',
273
        ]);
274
        $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
275
 
276
        $enrolmentdata['cohortidnumber'] = $cohort->idnumber;
277
        $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
278
        $this->assertArrayHasKey('customint1', $enrolmentdata);
279
        $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
280
        $this->assertArrayNotHasKey('customint2', $enrolmentdata);
281
 
282
        $enrolmentdata['cohortidnumber'] = 'notexist';
283
        $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
284
        $this->assertArrayHasKey('customint1', $enrolmentdata);
285
        $this->assertFalse($enrolmentdata['customint1']);
286
        $this->assertArrayNotHasKey('customint2', $enrolmentdata);
287
 
288
        $enrolmentdata['cohortidnumber'] = $cohort->idnumber;
289
 
290
        $enrolmentdata['addtogroup'] = COHORT_NOGROUP;
291
        $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
292
        $this->assertArrayHasKey('customint1', $enrolmentdata);
293
        $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
294
        $this->assertArrayHasKey('customint2', $enrolmentdata);
295
        $this->assertEquals(COHORT_NOGROUP, $enrolmentdata['customint2']);
296
 
297
        unset($enrolmentdata['addtogroup']);
298
        $enrolmentdata['groupname'] = $group->name;
299
        $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
300
        $this->assertArrayHasKey('customint1', $enrolmentdata);
301
        $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
302
        $this->assertArrayHasKey('customint2', $enrolmentdata);
303
        $this->assertEquals($group->id, $enrolmentdata['customint2']);
304
 
305
        $enrolmentdata['groupname'] = 'notexist';
306
        $enrolmentdata = $cohortplugin->fill_enrol_custom_fields($enrolmentdata, $course->id);
307
        $this->assertArrayHasKey('customint1', $enrolmentdata);
308
        $this->assertEquals($cohort->id, $enrolmentdata['customint1']);
309
        $this->assertArrayHasKey('customint2', $enrolmentdata);
310
        $this->assertFalse($enrolmentdata['customint2']);
311
    }
312
 
313
    /**
314
     * Test the behaviour of validate_enrol_plugin_data().
315
     *
316
     * @covers ::validate_enrol_plugin_data
317
     */
11 efrain 318
    public function test_validate_enrol_plugin_data(): void {
1 efrain 319
        $this->resetAfterTest();
320
        $this->setAdminUser();
321
 
322
        $cat = $this->getDataGenerator()->create_category();
323
        $cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
324
        $cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
325
 
326
        $course = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON']);
327
 
328
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'Group 1']);
329
 
330
        $cohort1 = $this->getDataGenerator()->create_cohort([
331
            'contextid' => \context_coursecat::instance($cat1->id)->id,
332
            'idnumber' => 'one',
333
        ]);
334
        $cohort2 = $this->getDataGenerator()->create_cohort([
335
            'contextid' => \context_coursecat::instance($cat2->id)->id,
336
            'idnumber' => 'two',
337
        ]);
338
 
339
        enrol::enable_plugin('cohort', false);
340
 
341
        $cohortplugin = enrol_get_plugin('cohort');
342
 
343
        // Plugin is disabled in system and cohort name is missing in csv.
344
        $enrolmentdata = [];
345
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata);
346
        $this->assertArrayHasKey('plugindisabled', $errors);
347
        $this->assertArrayHasKey('missingmandatoryfields', $errors);
348
 
349
        enrol::enable_plugin('cohort', true);
350
 
351
        // Unknown cohort idnumber and missing role.
352
        $enrolmentdata['cohortidnumber'] = 'test';
353
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata);
354
        $this->assertArrayHasKey('missingmandatoryfields', $errors);
355
        $this->assertArrayHasKey('unknowncohort', $errors);
356
 
357
        // Non-valid 'addtogroup' option.
358
        $enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
359
        $enrolmentdata['addtogroup'] = 2;
360
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
361
        $this->assertArrayHasKey('erroraddtogroup', $errors);
362
 
363
        // Options 'addtogroup' and 'groupname' are not allowed together.
364
        $enrolmentdata['addtogroup'] = 0;
365
        $enrolmentdata['groupname'] = 'test';
366
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
367
        $this->assertArrayHasKey('erroraddtogroupgroupname', $errors);
368
 
369
        // Cohort is not allowed on a given category context.
370
        $enrolmentdata['cohortidnumber'] = $cohort2->idnumber;
371
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
372
        $this->assertArrayHasKey('contextnotallowed', $errors);
373
 
374
        // Group does not exist.
375
        unset($enrolmentdata['addtogroup']);
376
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
377
        $this->assertArrayHasKey('errorinvalidgroup', $errors);
378
 
379
        // Unknown role.
380
        $enrolmentdata['role'] = 'test';
381
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
382
        $this->assertArrayHasKey('unknownrole', $errors);
383
 
384
        // Valid data when trying to create a group.
385
        $enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
386
        $enrolmentdata['role'] = 'student';
387
        $enrolmentdata['addtogroup'] = 1;
388
        unset($enrolmentdata['groupname']);
389
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
390
        $this->assertEmpty($errors);
391
 
392
        // Valid data when trying to add to existing group.
393
        $enrolmentdata['groupname'] = $group1->name;
394
        unset($enrolmentdata['addtogroup']);
395
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
396
        $this->assertEmpty($errors);
397
 
398
        // Valid data when trying without group mode.
399
        $enrolmentdata['addtogroup'] = 0;
400
        unset($enrolmentdata['groupname']);
401
        $errors = $cohortplugin->validate_enrol_plugin_data($enrolmentdata, $course->id);
402
        $this->assertEmpty($errors);
403
    }
404
 
405
    /**
406
     * Test the behaviour of find_instance().
407
     *
408
     * @covers ::find_instance
409
     */
11 efrain 410
    public function test_find_instance(): void {
1 efrain 411
        global $DB;
412
        $this->resetAfterTest();
413
 
414
        $cat = $this->getDataGenerator()->create_category();
415
        $course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
416
 
417
        $cohort1 = $this->getDataGenerator()->create_cohort([
418
            'contextid' => \context_coursecat::instance($cat->id)->id,
419
            'idnumber' => 'one',
420
        ]);
421
        $cohort2 = $this->getDataGenerator()->create_cohort([
422
            'contextid' => \context_coursecat::instance($cat->id)->id,
423
            'idnumber' => 'two',
424
        ]);
425
 
426
        $cohort3 = $this->getDataGenerator()->create_cohort([
427
            'contextid' => \context_coursecat::instance($cat->id)->id,
428
            'idnumber' => 'three',
429
        ]);
430
 
431
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
432
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
433
        $managerrole = $DB->get_record('role', ['shortname' => 'manager']);
434
        $cohortplugin = enrol_get_plugin('cohort');
435
 
436
        // Add three cohort enrol instances.
437
        $instanceid1 = $cohortplugin->add_instance($course, ['customint1' => $cohort1->id, 'roleid' => $teacherrole->id]);
438
        $instanceid2 = $cohortplugin->add_instance($course, ['customint1' => $cohort2->id, 'roleid' => $managerrole->id]);
439
        $instanceid3 = $cohortplugin->add_instance($course, ['customint1' => $cohort2->id, 'roleid' => $studentrole->id]);
440
 
441
        $instance1 = $DB->get_record('enrol', ['id' => $instanceid1]);
442
        $instance2 = $DB->get_record('enrol', ['id' => $instanceid2]);
443
 
444
        $enrolmentdata = [];
445
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
446
        $this->assertNull($instance);
447
 
448
        // Unknown idnumber.
449
        $enrolmentdata['cohortidnumber'] = 'test';
450
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
451
        $this->assertNull($instance);
452
 
453
        // Unknown role.
454
        $enrolmentdata['role'] = 'test';
455
        $enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
456
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
457
        $this->assertNull($instance);
458
 
459
        // Cohort3 instance has not matching role and cohort.
460
        $enrolmentdata['role'] = $teacherrole->shortname;
461
        $enrolmentdata['cohortidnumber'] = $cohort3->idnumber;
462
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
463
        $this->assertNull($instance);
464
 
465
        // Cohort2 instance has matching cohort, but not matching role.
466
        $enrolmentdata['role'] = $teacherrole->shortname;
467
        $enrolmentdata['cohortidnumber'] = $cohort2->idnumber;
468
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
469
        $this->assertNull($instance);
470
 
471
        $enrolmentdata['role'] = $teacherrole->shortname;
472
        $enrolmentdata['cohortidnumber'] = $cohort1->idnumber;
473
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
474
        $this->assertEquals($instance1->id, $instance->id);
475
 
476
        $enrolmentdata['role'] = $managerrole->shortname;
477
        $enrolmentdata['cohortidnumber'] = $cohort2->idnumber;
478
        $instance = $cohortplugin->find_instance($enrolmentdata, $course->id);
479
        $this->assertEquals($instance2->id, $instance->id);
480
    }
481
}