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 |
/**
|
|
|
18 |
* Manual enrolment tests.
|
|
|
19 |
*
|
|
|
20 |
* @package enrol_manual
|
|
|
21 |
* @category phpunit
|
|
|
22 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
namespace enrol_manual;
|
|
|
26 |
|
|
|
27 |
use course_enrolment_manager;
|
|
|
28 |
use stdClass;
|
|
|
29 |
|
|
|
30 |
defined('MOODLE_INTERNAL') || die();
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Manual enrolment tests.
|
|
|
35 |
*
|
|
|
36 |
* @package enrol_manual
|
|
|
37 |
* @category phpunit
|
|
|
38 |
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
39 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
40 |
*/
|
|
|
41 |
class lib_test extends \advanced_testcase {
|
|
|
42 |
/**
|
|
|
43 |
* Test enrol migration function used when uninstalling enrol plugins.
|
|
|
44 |
*/
|
|
|
45 |
public function test_migrate_plugin_enrolments() {
|
|
|
46 |
global $DB, $CFG;
|
|
|
47 |
require_once($CFG->dirroot.'/enrol/manual/locallib.php');
|
|
|
48 |
|
|
|
49 |
$this->resetAfterTest();
|
|
|
50 |
|
|
|
51 |
/** @var $manplugin enrol_manual_plugin */
|
|
|
52 |
$manplugin = enrol_get_plugin('manual');
|
|
|
53 |
|
|
|
54 |
// Setup a few courses and users.
|
|
|
55 |
|
|
|
56 |
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
|
|
57 |
$this->assertNotEmpty($studentrole);
|
|
|
58 |
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
|
|
|
59 |
$this->assertNotEmpty($teacherrole);
|
|
|
60 |
|
|
|
61 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
62 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
63 |
$course3 = $this->getDataGenerator()->create_course();
|
|
|
64 |
$course4 = $this->getDataGenerator()->create_course();
|
|
|
65 |
$course5 = $this->getDataGenerator()->create_course();
|
|
|
66 |
|
|
|
67 |
$context1 = \context_course::instance($course1->id);
|
|
|
68 |
$context2 = \context_course::instance($course2->id);
|
|
|
69 |
$context3 = \context_course::instance($course3->id);
|
|
|
70 |
$context4 = \context_course::instance($course4->id);
|
|
|
71 |
|
|
|
72 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
73 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
74 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
75 |
$user4 = $this->getDataGenerator()->create_user();
|
|
|
76 |
|
|
|
77 |
// We expect manual, self and guest instances to be created by default.
|
|
|
78 |
|
|
|
79 |
$this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'manual')));
|
|
|
80 |
$this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'self')));
|
|
|
81 |
$this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'guest')));
|
|
|
82 |
$this->assertEquals(15, $DB->count_records('enrol', array()));
|
|
|
83 |
|
|
|
84 |
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
|
|
|
85 |
|
|
|
86 |
// Enrol some users to manual instances.
|
|
|
87 |
|
|
|
88 |
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
89 |
$DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
|
|
|
90 |
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
91 |
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
92 |
$DB->delete_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'));
|
|
|
93 |
$DB->delete_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'));
|
|
|
94 |
$DB->delete_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual'));
|
|
|
95 |
|
|
|
96 |
$manplugin->enrol_user($maninstance1, $user1->id, $studentrole->id);
|
|
|
97 |
$manplugin->enrol_user($maninstance1, $user2->id, $studentrole->id);
|
|
|
98 |
$manplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id);
|
|
|
99 |
$manplugin->enrol_user($maninstance2, $user3->id, $teacherrole->id);
|
|
|
100 |
|
|
|
101 |
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
|
|
|
102 |
|
|
|
103 |
// Set up some bogus enrol plugin instances and enrolments.
|
|
|
104 |
|
|
|
105 |
$xxxinstance1 = $DB->insert_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED));
|
|
|
106 |
$xxxinstance1 = $DB->get_record('enrol', array('id'=>$xxxinstance1));
|
|
|
107 |
$xxxinstance3 = $DB->insert_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED));
|
|
|
108 |
$xxxinstance3 = $DB->get_record('enrol', array('id'=>$xxxinstance3));
|
|
|
109 |
$xxxinstance4 = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED));
|
|
|
110 |
$xxxinstance4 = $DB->get_record('enrol', array('id'=>$xxxinstance4));
|
|
|
111 |
$xxxinstance4b = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED));
|
|
|
112 |
$xxxinstance4b = $DB->get_record('enrol', array('id'=>$xxxinstance4b));
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED));
|
|
|
116 |
role_assign($studentrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
|
|
|
117 |
role_assign($teacherrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
|
|
|
118 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE));
|
|
|
119 |
role_assign($studentrole->id, $user4->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
|
|
|
120 |
$this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id)));
|
|
|
121 |
$this->assertEquals(6, $DB->count_records('role_assignments', array('contextid'=>$context1->id)));
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE));
|
|
|
125 |
role_assign($studentrole->id, $user1->id, $context3->id, 'enrol_xxx', $xxxinstance3->id);
|
|
|
126 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED));
|
|
|
127 |
$this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id)));
|
|
|
128 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id)));
|
|
|
129 |
|
|
|
130 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE));
|
|
|
131 |
role_assign($studentrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4->id);
|
|
|
132 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE));
|
|
|
133 |
role_assign($studentrole->id, $user2->id, $context4->id, 'enrol_xxx', $xxxinstance4->id);
|
|
|
134 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED));
|
|
|
135 |
role_assign($teacherrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id);
|
|
|
136 |
$DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE));
|
|
|
137 |
role_assign($teacherrole->id, $user4->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id);
|
|
|
138 |
$this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id)));
|
|
|
139 |
$this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id)));
|
|
|
140 |
$this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id)));
|
|
|
141 |
|
|
|
142 |
// Finally do the migration.
|
|
|
143 |
|
|
|
144 |
enrol_manual_migrate_plugin_enrolments('xxx');
|
|
|
145 |
|
|
|
146 |
// Verify results.
|
|
|
147 |
|
|
|
148 |
$this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual')));
|
|
|
149 |
$this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx')));
|
|
|
150 |
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
151 |
$this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance1->status);
|
|
|
152 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
153 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
154 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
155 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
156 |
$this->assertEquals(4, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance1->id)));
|
|
|
157 |
$this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id)));
|
|
|
158 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
|
|
|
159 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id)));
|
|
|
160 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
|
|
|
161 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id)));
|
|
|
162 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
|
|
|
163 |
$this->assertEquals(5, $DB->count_records('role_assignments', array('contextid'=>$context1->id)));
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
$this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual')));
|
|
|
167 |
$this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'xxx')));
|
|
|
168 |
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
169 |
$this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance2->status);
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
$this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual')));
|
|
|
173 |
$this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx')));
|
|
|
174 |
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
175 |
$this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance3->status);
|
|
|
176 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
177 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED)));
|
|
|
178 |
$this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance3->id)));
|
|
|
179 |
$this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id)));
|
|
|
180 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context3->id)));
|
|
|
181 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id)));
|
|
|
182 |
|
|
|
183 |
|
|
|
184 |
$this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual')));
|
|
|
185 |
$this->assertEquals(2, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx')));
|
|
|
186 |
$maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
187 |
$this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance4->status);
|
|
|
188 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
189 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
190 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_SUSPENDED)));
|
|
|
191 |
$this->assertEquals(3, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance4->id)));
|
|
|
192 |
$this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id)));
|
|
|
193 |
$this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id)));
|
|
|
194 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id)));
|
|
|
195 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id)));
|
|
|
196 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id)));
|
|
|
197 |
$this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user4->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id)));
|
|
|
198 |
$this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id)));
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
$this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual')));
|
|
|
202 |
$this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'xxx')));
|
|
|
203 |
|
|
|
204 |
// Make sure wrong params do not produce errors or notices.
|
|
|
205 |
|
|
|
206 |
enrol_manual_migrate_plugin_enrolments('manual');
|
|
|
207 |
enrol_manual_migrate_plugin_enrolments('yyyy');
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
public function test_expired() {
|
|
|
211 |
global $DB;
|
|
|
212 |
$this->resetAfterTest();
|
|
|
213 |
|
|
|
214 |
/** @var $manualplugin enrol_manual_plugin */
|
|
|
215 |
$manualplugin = enrol_get_plugin('manual');
|
|
|
216 |
|
|
|
217 |
$trace = new \null_progress_trace();
|
|
|
218 |
|
|
|
219 |
$now = time();
|
|
|
220 |
|
|
|
221 |
// Prepare some data.
|
|
|
222 |
|
|
|
223 |
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
|
|
224 |
$this->assertNotEmpty($studentrole);
|
|
|
225 |
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
|
|
|
226 |
$this->assertNotEmpty($teacherrole);
|
|
|
227 |
$managerrole = $DB->get_record('role', array('shortname'=>'manager'));
|
|
|
228 |
$this->assertNotEmpty($managerrole);
|
|
|
229 |
|
|
|
230 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
231 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
232 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
233 |
$user4 = $this->getDataGenerator()->create_user();
|
|
|
234 |
|
|
|
235 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
236 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
237 |
$course3 = $this->getDataGenerator()->create_course();
|
|
|
238 |
$context1 = \context_course::instance($course1->id);
|
|
|
239 |
$context2 = \context_course::instance($course2->id);
|
|
|
240 |
$context3 = \context_course::instance($course3->id);
|
|
|
241 |
|
|
|
242 |
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual')));
|
|
|
243 |
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
244 |
$this->assertEquals($studentrole->id, $instance1->roleid);
|
|
|
245 |
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
246 |
$this->assertEquals($studentrole->id, $instance2->roleid);
|
|
|
247 |
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
248 |
$this->assertEquals($studentrole->id, $instance3->roleid);
|
|
|
249 |
|
|
|
250 |
$this->assertEquals(0, $DB->count_records('user_enrolments'));
|
|
|
251 |
$this->assertEquals(0, $DB->count_records('role_assignments'));
|
|
|
252 |
|
|
|
253 |
$manualplugin->enrol_user($instance1, $user1->id, $studentrole->id);
|
|
|
254 |
$manualplugin->enrol_user($instance1, $user2->id, $studentrole->id);
|
|
|
255 |
$manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
|
|
|
256 |
|
|
|
257 |
$manualplugin->enrol_user($instance3, $user1->id, $studentrole->id, 0, 0);
|
|
|
258 |
$manualplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now+60*60);
|
|
|
259 |
$manualplugin->enrol_user($instance3, $user3->id, $teacherrole->id, 0, $now-60*60);
|
|
|
260 |
|
|
|
261 |
role_assign($managerrole->id, $user4->id, $context1->id);
|
|
|
262 |
|
|
|
263 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
264 |
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
|
|
265 |
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
|
|
266 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
|
|
267 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
|
|
|
268 |
|
|
|
269 |
// Execute tests.
|
|
|
270 |
|
|
|
271 |
$this->assertEquals(ENROL_EXT_REMOVED_KEEP, $manualplugin->get_config('expiredaction'));
|
|
|
272 |
$manualplugin->sync($trace, null);
|
|
|
273 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
274 |
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
$manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
|
|
|
278 |
$manualplugin->sync($trace, $course2->id);
|
|
|
279 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
280 |
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
|
|
281 |
|
|
|
282 |
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
|
|
|
283 |
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id)));
|
|
|
284 |
$manualplugin->sync($trace, null);
|
|
|
285 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
286 |
$this->assertEquals(5, $DB->count_records('role_assignments'));
|
|
|
287 |
$this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
|
|
288 |
$this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
|
|
289 |
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
|
|
|
290 |
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id)));
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
$manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
|
|
|
294 |
|
|
|
295 |
role_assign($studentrole->id, $user3->id, $context1->id);
|
|
|
296 |
role_assign($teacherrole->id, $user3->id, $context3->id);
|
|
|
297 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
298 |
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
|
|
299 |
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
|
|
300 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
|
|
301 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
|
|
|
302 |
|
|
|
303 |
$manualplugin->sync($trace, null);
|
|
|
304 |
$this->assertEquals(4, $DB->count_records('user_enrolments'));
|
|
|
305 |
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
|
|
|
306 |
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
|
|
|
307 |
$this->assertEquals(5, $DB->count_records('role_assignments'));
|
|
|
308 |
$this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
|
|
309 |
$this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
|
|
310 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
|
|
|
311 |
|
|
|
312 |
|
|
|
313 |
$manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPEND);
|
|
|
314 |
$manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
|
|
|
315 |
$manualplugin->enrol_user($instance3, $user3->id, $teacherrole->id, 0, $now-60*60);
|
|
|
316 |
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
317 |
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
318 |
|
|
|
319 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
320 |
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
|
|
321 |
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
|
|
322 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
|
|
323 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
324 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
|
|
|
325 |
|
|
|
326 |
$manualplugin->sync($trace, null);
|
|
|
327 |
$this->assertEquals(6, $DB->count_records('user_enrolments'));
|
|
|
328 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
|
|
|
329 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
|
|
|
330 |
$this->assertEquals(7, $DB->count_records('role_assignments'));
|
|
|
331 |
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
|
|
|
332 |
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
|
|
|
333 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_SUSPENDED)));
|
|
|
334 |
$this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_SUSPENDED)));
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
public function test_send_expiry_notifications() {
|
|
|
338 |
global $DB, $CFG;
|
|
|
339 |
$this->resetAfterTest();
|
|
|
340 |
$this->preventResetByRollback(); // Messaging does not like transactions...
|
|
|
341 |
|
|
|
342 |
$trace = new \null_progress_trace();
|
|
|
343 |
|
|
|
344 |
/** @var $manualplugin enrol_manual_plugin */
|
|
|
345 |
$manualplugin = enrol_get_plugin('manual');
|
|
|
346 |
$now = time();
|
|
|
347 |
$admin = get_admin();
|
|
|
348 |
|
|
|
349 |
// Note: hopefully nobody executes the unit tests the last second before midnight...
|
|
|
350 |
|
|
|
351 |
$manualplugin->set_config('expirynotifylast', $now - 60*60*24);
|
|
|
352 |
$manualplugin->set_config('expirynotifyhour', 0);
|
|
|
353 |
|
|
|
354 |
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
|
|
|
355 |
$this->assertNotEmpty($studentrole);
|
|
|
356 |
$editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
|
|
|
357 |
$this->assertNotEmpty($editingteacherrole);
|
|
|
358 |
$managerrole = $DB->get_record('role', array('shortname'=>'manager'));
|
|
|
359 |
$this->assertNotEmpty($managerrole);
|
|
|
360 |
|
|
|
361 |
$user1 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser1'));
|
|
|
362 |
$user2 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser2'));
|
|
|
363 |
$user3 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser3'));
|
|
|
364 |
$user4 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser4'));
|
|
|
365 |
$user5 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser5'));
|
|
|
366 |
$user6 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
|
|
|
367 |
$user7 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
|
|
|
368 |
$user8 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
|
|
|
369 |
|
|
|
370 |
$course1 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse1'));
|
|
|
371 |
$course2 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse2'));
|
|
|
372 |
$course3 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse3'));
|
|
|
373 |
$course4 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse4'));
|
|
|
374 |
|
|
|
375 |
$this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'manual')));
|
|
|
376 |
|
|
|
377 |
$instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
378 |
$instance1->expirythreshold = 60*60*24*4;
|
|
|
379 |
$instance1->expirynotify = 1;
|
|
|
380 |
$instance1->notifyall = 1;
|
|
|
381 |
$DB->update_record('enrol', $instance1);
|
|
|
382 |
|
|
|
383 |
$instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
384 |
$instance2->expirythreshold = 60*60*24*1;
|
|
|
385 |
$instance2->expirynotify = 1;
|
|
|
386 |
$instance2->notifyall = 1;
|
|
|
387 |
$DB->update_record('enrol', $instance2);
|
|
|
388 |
|
|
|
389 |
$instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
390 |
$instance3->expirythreshold = 60*60*24*1;
|
|
|
391 |
$instance3->expirynotify = 1;
|
|
|
392 |
$instance3->notifyall = 0;
|
|
|
393 |
$DB->update_record('enrol', $instance3);
|
|
|
394 |
|
|
|
395 |
$instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
|
|
|
396 |
$instance4->expirythreshold = 60*60*24*1;
|
|
|
397 |
$instance4->expirynotify = 0;
|
|
|
398 |
$instance4->notifyall = 0;
|
|
|
399 |
$DB->update_record('enrol', $instance4);
|
|
|
400 |
|
|
|
401 |
$manualplugin->enrol_user($instance1, $user1->id, $editingteacherrole->id, 0, $now + 60*60*24*1, ENROL_USER_SUSPENDED); // Suspended users are not notified.
|
|
|
402 |
$manualplugin->enrol_user($instance1, $user2->id, $studentrole->id, 0, $now + 60*60*24*5); // Above threshold are not notified.
|
|
|
403 |
$manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now + 60*60*24*3 + 60*60); // Less than one day after threshold - should be notified.
|
|
|
404 |
$manualplugin->enrol_user($instance1, $user4->id, $studentrole->id, 0, $now + 60*60*24*4 - 60*3); // Less than one day after threshold - should be notified.
|
|
|
405 |
$manualplugin->enrol_user($instance1, $user5->id, $studentrole->id, 0, $now + 60*60); // Should have been already notified.
|
|
|
406 |
$manualplugin->enrol_user($instance1, $user6->id, $studentrole->id, 0, $now - 60); // Already expired.
|
|
|
407 |
$manualplugin->enrol_user($instance1, $user7->id, $editingteacherrole->id);
|
|
|
408 |
$manualplugin->enrol_user($instance1, $user8->id, $managerrole->id); // Highest role --> enroller.
|
|
|
409 |
|
|
|
410 |
$manualplugin->enrol_user($instance2, $user1->id, $studentrole->id);
|
|
|
411 |
$manualplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60*3); // Above threshold are not notified.
|
|
|
412 |
$manualplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified.
|
|
|
413 |
|
|
|
414 |
$manualplugin->enrol_user($instance3, $user1->id, $editingteacherrole->id);
|
|
|
415 |
$manualplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60); // Above threshold are not notified.
|
|
|
416 |
$manualplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60); // Less than one day after threshold - should be notified.
|
|
|
417 |
|
|
|
418 |
$manualplugin->enrol_user($instance4, $user4->id, $editingteacherrole->id);
|
|
|
419 |
$manualplugin->enrol_user($instance4, $user5->id, $studentrole->id, 0, $now + 60*60*24*1 + 60);
|
|
|
420 |
$manualplugin->enrol_user($instance4, $user6->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);
|
|
|
421 |
|
|
|
422 |
// The notification is sent out in fixed order first individual users,
|
|
|
423 |
// then summary per course by enrolid, user lastname, etc.
|
|
|
424 |
$this->assertGreaterThan($instance1->id, $instance2->id);
|
|
|
425 |
$this->assertGreaterThan($instance2->id, $instance3->id);
|
|
|
426 |
|
|
|
427 |
$sink = $this->redirectMessages();
|
|
|
428 |
|
|
|
429 |
$manualplugin->send_expiry_notifications($trace);
|
|
|
430 |
|
|
|
431 |
$messages = $sink->get_messages();
|
|
|
432 |
|
|
|
433 |
$this->assertEquals(2+1 + 1+1 + 1 + 0, count($messages));
|
|
|
434 |
|
|
|
435 |
// First individual notifications from course1.
|
|
|
436 |
$this->assertEquals($user3->id, $messages[0]->useridto);
|
|
|
437 |
$this->assertEquals($user8->id, $messages[0]->useridfrom);
|
|
|
438 |
$this->assertStringContainsString('xcourse1', $messages[0]->fullmessagehtml);
|
|
|
439 |
|
|
|
440 |
$this->assertEquals($user4->id, $messages[1]->useridto);
|
|
|
441 |
$this->assertEquals($user8->id, $messages[1]->useridfrom);
|
|
|
442 |
$this->assertStringContainsString('xcourse1', $messages[1]->fullmessagehtml);
|
|
|
443 |
|
|
|
444 |
// Then summary for course1.
|
|
|
445 |
$this->assertEquals($user8->id, $messages[2]->useridto);
|
|
|
446 |
$this->assertEquals($admin->id, $messages[2]->useridfrom);
|
|
|
447 |
$this->assertStringContainsString('xcourse1', $messages[2]->fullmessagehtml);
|
|
|
448 |
$this->assertStringNotContainsString('xuser1', $messages[2]->fullmessagehtml);
|
|
|
449 |
$this->assertStringNotContainsString('xuser2', $messages[2]->fullmessagehtml);
|
|
|
450 |
$this->assertStringContainsString('xuser3', $messages[2]->fullmessagehtml);
|
|
|
451 |
$this->assertStringContainsString('xuser4', $messages[2]->fullmessagehtml);
|
|
|
452 |
$this->assertStringContainsString('xuser5', $messages[2]->fullmessagehtml);
|
|
|
453 |
$this->assertStringNotContainsString('xuser6', $messages[2]->fullmessagehtml);
|
|
|
454 |
|
|
|
455 |
// First individual notifications from course2.
|
|
|
456 |
$this->assertEquals($user3->id, $messages[3]->useridto);
|
|
|
457 |
$this->assertEquals($admin->id, $messages[3]->useridfrom);
|
|
|
458 |
$this->assertStringContainsString('xcourse2', $messages[3]->fullmessagehtml);
|
|
|
459 |
|
|
|
460 |
// Then summary for course2.
|
|
|
461 |
$this->assertEquals($admin->id, $messages[4]->useridto);
|
|
|
462 |
$this->assertEquals($admin->id, $messages[4]->useridfrom);
|
|
|
463 |
$this->assertStringContainsString('xcourse2', $messages[4]->fullmessagehtml);
|
|
|
464 |
$this->assertStringNotContainsString('xuser1', $messages[4]->fullmessagehtml);
|
|
|
465 |
$this->assertStringNotContainsString('xuser2', $messages[4]->fullmessagehtml);
|
|
|
466 |
$this->assertStringContainsString('xuser3', $messages[4]->fullmessagehtml);
|
|
|
467 |
$this->assertStringNotContainsString('xuser4', $messages[4]->fullmessagehtml);
|
|
|
468 |
$this->assertStringNotContainsString('xuser5', $messages[4]->fullmessagehtml);
|
|
|
469 |
$this->assertStringNotContainsString('xuser6', $messages[4]->fullmessagehtml);
|
|
|
470 |
|
|
|
471 |
// Only summary in course3.
|
|
|
472 |
$this->assertEquals($user1->id, $messages[5]->useridto);
|
|
|
473 |
$this->assertEquals($admin->id, $messages[5]->useridfrom);
|
|
|
474 |
$this->assertStringContainsString('xcourse3', $messages[5]->fullmessagehtml);
|
|
|
475 |
$this->assertStringNotContainsString('xuser1', $messages[5]->fullmessagehtml);
|
|
|
476 |
$this->assertStringNotContainsString('xuser2', $messages[5]->fullmessagehtml);
|
|
|
477 |
$this->assertStringContainsString('xuser3', $messages[5]->fullmessagehtml);
|
|
|
478 |
$this->assertStringNotContainsString('xuser4', $messages[5]->fullmessagehtml);
|
|
|
479 |
$this->assertStringNotContainsString('xuser5', $messages[5]->fullmessagehtml);
|
|
|
480 |
$this->assertStringNotContainsString('xuser6', $messages[5]->fullmessagehtml);
|
|
|
481 |
|
|
|
482 |
|
|
|
483 |
// Make sure that notifications are not repeated.
|
|
|
484 |
$sink->clear();
|
|
|
485 |
|
|
|
486 |
$manualplugin->send_expiry_notifications($trace);
|
|
|
487 |
$this->assertEquals(0, $sink->count());
|
|
|
488 |
|
|
|
489 |
// use invalid notification hour to verify that before the hour the notifications are not sent.
|
|
|
490 |
$manualplugin->set_config('expirynotifylast', time() - 60*60*24);
|
|
|
491 |
$manualplugin->set_config('expirynotifyhour', '24');
|
|
|
492 |
|
|
|
493 |
$manualplugin->send_expiry_notifications($trace);
|
|
|
494 |
$this->assertEquals(0, $sink->count());
|
|
|
495 |
|
|
|
496 |
$manualplugin->set_config('expirynotifyhour', '0');
|
|
|
497 |
$manualplugin->send_expiry_notifications($trace);
|
|
|
498 |
$this->assertEquals(6, $sink->count());
|
|
|
499 |
}
|
|
|
500 |
|
|
|
501 |
/**
|
|
|
502 |
* Test for getting user enrolment actions.
|
|
|
503 |
*/
|
|
|
504 |
public function test_get_user_enrolment_actions() {
|
|
|
505 |
global $CFG, $PAGE;
|
|
|
506 |
$this->resetAfterTest();
|
|
|
507 |
|
|
|
508 |
// Set page URL to prevent debugging messages.
|
|
|
509 |
$PAGE->set_url('/enrol/editinstance.php');
|
|
|
510 |
|
|
|
511 |
$pluginname = 'manual';
|
|
|
512 |
|
|
|
513 |
// Only enable the manual enrol plugin.
|
|
|
514 |
$CFG->enrol_plugins_enabled = $pluginname;
|
|
|
515 |
|
|
|
516 |
$generator = $this->getDataGenerator();
|
|
|
517 |
|
|
|
518 |
// Get the enrol plugin.
|
|
|
519 |
$plugin = enrol_get_plugin($pluginname);
|
|
|
520 |
|
|
|
521 |
// Create a course.
|
|
|
522 |
$course = $generator->create_course();
|
|
|
523 |
// Enable this enrol plugin for the course.
|
|
|
524 |
$plugin->add_instance($course);
|
|
|
525 |
|
|
|
526 |
// Create a teacher.
|
|
|
527 |
$teacher = $generator->create_user();
|
|
|
528 |
// Enrol the teacher to the course.
|
|
|
529 |
$generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname);
|
|
|
530 |
// Create a student.
|
|
|
531 |
$student = $generator->create_user();
|
|
|
532 |
// Enrol the student to the course.
|
|
|
533 |
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
|
|
|
534 |
|
|
|
535 |
// Login as the teacher.
|
|
|
536 |
$this->setUser($teacher);
|
|
|
537 |
require_once($CFG->dirroot . '/enrol/locallib.php');
|
|
|
538 |
$manager = new course_enrolment_manager($PAGE, $course);
|
|
|
539 |
$userenrolments = $manager->get_user_enrolments($student->id);
|
|
|
540 |
$this->assertCount(1, $userenrolments);
|
|
|
541 |
|
|
|
542 |
$ue = reset($userenrolments);
|
|
|
543 |
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
|
|
|
544 |
// Manual enrol has 2 enrol actions -- edit and unenrol.
|
|
|
545 |
$this->assertCount(2, $actions);
|
|
|
546 |
}
|
|
|
547 |
|
|
|
548 |
/**
|
|
|
549 |
* Test how the default enrolment instance inherits its settings from the global plugin settings.
|
|
|
550 |
*
|
|
|
551 |
* @dataProvider default_enrolment_instance_data_provider
|
|
|
552 |
* @param stdClass $expectation
|
|
|
553 |
* @param stdClass $globalsettings
|
|
|
554 |
* @covers \enrol_manual::add_default_instance
|
|
|
555 |
*/
|
|
|
556 |
public function test_default_enrolment_instance_acquires_correct_settings(stdClass $expectation, stdClass $globalsettings) {
|
|
|
557 |
global $DB;
|
|
|
558 |
|
|
|
559 |
$this->resetAfterTest();
|
|
|
560 |
|
|
|
561 |
$generator = $this->getDataGenerator();
|
|
|
562 |
|
|
|
563 |
// Given the plugin is globally configured with the following settings.
|
|
|
564 |
$plugin = enrol_get_plugin('manual');
|
|
|
565 |
$plugin->set_config('status', $globalsettings->status);
|
|
|
566 |
$plugin->set_config('roleid', $globalsettings->roleid);
|
|
|
567 |
$plugin->set_config('enrolperiod', $globalsettings->enrolperiod);
|
|
|
568 |
$plugin->set_config('expirynotify', $globalsettings->expirynotify);
|
|
|
569 |
$plugin->set_config('expirythreshold', $globalsettings->expirythreshold);
|
|
|
570 |
|
|
|
571 |
// When creating a course.
|
|
|
572 |
$course = $generator->create_course();
|
|
|
573 |
|
|
|
574 |
// Then the default manual enrolment instance being created is properly configured.
|
|
|
575 |
$enrolinstance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
|
|
|
576 |
$this->assertEquals($expectation->status, $enrolinstance->status);
|
|
|
577 |
$this->assertEquals($expectation->roleid, $enrolinstance->roleid);
|
|
|
578 |
$this->assertEquals($expectation->enrolperiod, $enrolinstance->enrolperiod);
|
|
|
579 |
$this->assertEquals($expectation->expirynotify, $enrolinstance->expirynotify);
|
|
|
580 |
$this->assertEquals($expectation->notifyall, $enrolinstance->notifyall);
|
|
|
581 |
$this->assertEquals($expectation->expirythreshold, $enrolinstance->expirythreshold);
|
|
|
582 |
}
|
|
|
583 |
|
|
|
584 |
/**
|
|
|
585 |
* Data provider for test_default_enrolment_instance_acquires_correct_settings().
|
|
|
586 |
*
|
|
|
587 |
* @return array
|
|
|
588 |
*/
|
|
|
589 |
public function default_enrolment_instance_data_provider(): array {
|
|
|
590 |
$studentroles = get_archetype_roles('student');
|
|
|
591 |
$studentrole = array_shift($studentroles);
|
|
|
592 |
|
|
|
593 |
$teacherroles = get_archetype_roles('teacher');
|
|
|
594 |
$teacherrole = array_shift($teacherroles);
|
|
|
595 |
|
|
|
596 |
return [
|
|
|
597 |
'enabled, student role, no duration set, notify no one on expiry, 12 hours notification threshold' => [
|
|
|
598 |
'expectation' => (object) [
|
|
|
599 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
600 |
'roleid' => $studentrole->id,
|
|
|
601 |
'enrolperiod' => 0,
|
|
|
602 |
'expirynotify' => 0,
|
|
|
603 |
'notifyall' => 0,
|
|
|
604 |
'expirythreshold' => 12 * HOURSECS,
|
|
|
605 |
],
|
|
|
606 |
'global settings' => (object) [
|
|
|
607 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
608 |
'roleid' => $studentrole->id,
|
|
|
609 |
'enrolperiod' => 0,
|
|
|
610 |
'expirynotify' => 0,
|
|
|
611 |
'expirythreshold' => 12 * HOURSECS,
|
|
|
612 |
],
|
|
|
613 |
],
|
|
|
614 |
'enabled, student role, 72 hours duration, notify enroller only on expiry, 1 day notification threshold' => [
|
|
|
615 |
'expectation' => (object) [
|
|
|
616 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
617 |
'roleid' => $studentrole->id,
|
|
|
618 |
'enrolperiod' => 72 * HOURSECS,
|
|
|
619 |
'expirynotify' => 1,
|
|
|
620 |
'notifyall' => 0,
|
|
|
621 |
'expirythreshold' => DAYSECS,
|
|
|
622 |
],
|
|
|
623 |
'global settings' => (object) [
|
|
|
624 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
625 |
'roleid' => $studentrole->id,
|
|
|
626 |
'enrolperiod' => 72 * HOURSECS,
|
|
|
627 |
'expirynotify' => 1,
|
|
|
628 |
'expirythreshold' => DAYSECS,
|
|
|
629 |
],
|
|
|
630 |
],
|
|
|
631 |
'disabled, teacher role, no duration set, notify enroller and enrolled on expiry, 0 notification threshold' => [
|
|
|
632 |
'expectation' => (object) [
|
|
|
633 |
'status' => ENROL_INSTANCE_DISABLED,
|
|
|
634 |
'roleid' => $teacherrole->id,
|
|
|
635 |
'enrolperiod' => 0,
|
|
|
636 |
'expirynotify' => 2,
|
|
|
637 |
'notifyall' => 1,
|
|
|
638 |
'expirythreshold' => 0
|
|
|
639 |
],
|
|
|
640 |
'global settings' => (object) [
|
|
|
641 |
'status' => ENROL_INSTANCE_DISABLED,
|
|
|
642 |
'roleid' => $teacherrole->id,
|
|
|
643 |
'enrolperiod' => 0,
|
|
|
644 |
'expirynotify' => 2,
|
|
|
645 |
'expirythreshold' => 0,
|
|
|
646 |
],
|
|
|
647 |
],
|
|
|
648 |
];
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
/**
|
|
|
652 |
* Tests an enrolment instance is updated properly.
|
|
|
653 |
*
|
|
|
654 |
* @covers \enrol_manual::update_instance
|
|
|
655 |
* @dataProvider update_enrolment_instance_data_provider
|
|
|
656 |
*
|
|
|
657 |
* @param stdClass $expectation
|
|
|
658 |
* @param stdClass $updatedata
|
|
|
659 |
*/
|
|
|
660 |
public function test_enrolment_instance_is_updated(stdClass $expectation, stdClass $updatedata): void {
|
|
|
661 |
global $DB;
|
|
|
662 |
|
|
|
663 |
$this->resetAfterTest();
|
|
|
664 |
|
|
|
665 |
$generator = $this->getDataGenerator();
|
|
|
666 |
|
|
|
667 |
$studentroles = get_archetype_roles('student');
|
|
|
668 |
$studentrole = array_shift($studentroles);
|
|
|
669 |
|
|
|
670 |
// Given the plugin is globally configured with the following settings.
|
|
|
671 |
$plugin = enrol_get_plugin('manual');
|
|
|
672 |
$plugin->set_config('status', ENROL_INSTANCE_ENABLED);
|
|
|
673 |
$plugin->set_config('roleid', $studentrole->id);
|
|
|
674 |
$plugin->set_config('enrolperiod', 30 * DAYSECS);
|
|
|
675 |
$plugin->set_config('expirynotify', 1);
|
|
|
676 |
$plugin->set_config('expirythreshold', 2 * DAYSECS);
|
|
|
677 |
|
|
|
678 |
// And a course is created with the default enrolment instance.
|
|
|
679 |
$course = $generator->create_course();
|
|
|
680 |
|
|
|
681 |
// When the enrolment instance is being updated.
|
|
|
682 |
$enrolinstance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
|
|
|
683 |
$successfullyupdated = $plugin->update_instance($enrolinstance, $updatedata);
|
|
|
684 |
|
|
|
685 |
// Then the update is successful.
|
|
|
686 |
$this->assertTrue($successfullyupdated);
|
|
|
687 |
|
|
|
688 |
// And the updated enrolment instance contains the expected values.
|
|
|
689 |
$enrolinstance = $DB->get_record('enrol', ['id' => $enrolinstance->id]);
|
|
|
690 |
$this->assertEquals($expectation->status, $enrolinstance->status);
|
|
|
691 |
$this->assertEquals($expectation->roleid, $enrolinstance->roleid);
|
|
|
692 |
$this->assertEquals($expectation->enrolperiod, $enrolinstance->enrolperiod);
|
|
|
693 |
$this->assertEquals($expectation->expirynotify, $enrolinstance->expirynotify);
|
|
|
694 |
$this->assertEquals($expectation->notifyall, $enrolinstance->notifyall);
|
|
|
695 |
$this->assertEquals($expectation->expirythreshold, $enrolinstance->expirythreshold);
|
|
|
696 |
}
|
|
|
697 |
|
|
|
698 |
/**
|
|
|
699 |
* Data provider for test_enrolment_instance_is_updated().
|
|
|
700 |
*
|
|
|
701 |
* @return array
|
|
|
702 |
*/
|
|
|
703 |
public function update_enrolment_instance_data_provider(): array {
|
|
|
704 |
$studentroles = get_archetype_roles('student');
|
|
|
705 |
$studentrole = array_shift($studentroles);
|
|
|
706 |
|
|
|
707 |
$teacherroles = get_archetype_roles('teacher');
|
|
|
708 |
$teacherrole = array_shift($teacherroles);
|
|
|
709 |
|
|
|
710 |
return [
|
|
|
711 |
'disabled, all the others are default' => [
|
|
|
712 |
'expectation' => (object) [
|
|
|
713 |
'status' => ENROL_INSTANCE_DISABLED,
|
|
|
714 |
'roleid' => $studentrole->id,
|
|
|
715 |
'enrolperiod' => 30 * DAYSECS,
|
|
|
716 |
'expirynotify' => 1,
|
|
|
717 |
'notifyall' => 0,
|
|
|
718 |
'expirythreshold' => 2 * DAYSECS,
|
|
|
719 |
],
|
|
|
720 |
'update data' => (object) [
|
|
|
721 |
'status' => ENROL_INSTANCE_DISABLED,
|
|
|
722 |
'roleid' => $studentrole->id,
|
|
|
723 |
'enrolperiod' => 30 * DAYSECS,
|
|
|
724 |
'expirynotify' => 1,
|
|
|
725 |
'expirythreshold' => 2 * DAYSECS,
|
|
|
726 |
],
|
|
|
727 |
],
|
|
|
728 |
'enabled, teacher role, no duration set, notify no one on expiry, 0 notification threshold' => [
|
|
|
729 |
'expectation' => (object) [
|
|
|
730 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
731 |
'roleid' => $teacherrole->id,
|
|
|
732 |
'enrolperiod' => 0,
|
|
|
733 |
'expirynotify' => 0,
|
|
|
734 |
'notifyall' => 0,
|
|
|
735 |
'expirythreshold' => 0,
|
|
|
736 |
],
|
|
|
737 |
'update data' => (object) [
|
|
|
738 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
739 |
'roleid' => $teacherrole->id,
|
|
|
740 |
'enrolperiod' => 0,
|
|
|
741 |
'expirynotify' => 0,
|
|
|
742 |
'expirythreshold' => 0,
|
|
|
743 |
],
|
|
|
744 |
],
|
|
|
745 |
'notify enroller and enrolled on expiry, all the others are default' => [
|
|
|
746 |
'expectation' => (object) [
|
|
|
747 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
748 |
'roleid' => $studentrole->id,
|
|
|
749 |
'enrolperiod' => 30 * DAYSECS,
|
|
|
750 |
'expirynotify' => 2,
|
|
|
751 |
'notifyall' => 1,
|
|
|
752 |
'expirythreshold' => 2 * DAYSECS,
|
|
|
753 |
],
|
|
|
754 |
'update data' => (object) [
|
|
|
755 |
'status' => ENROL_INSTANCE_ENABLED,
|
|
|
756 |
'roleid' => $studentrole->id,
|
|
|
757 |
'enrolperiod' => 30 * DAYSECS,
|
|
|
758 |
'expirynotify' => 2,
|
|
|
759 |
'expirythreshold' => 2 * DAYSECS,
|
|
|
760 |
],
|
|
|
761 |
],
|
|
|
762 |
];
|
|
|
763 |
}
|
|
|
764 |
|
|
|
765 |
/**
|
|
|
766 |
* Test the behaviour of find_instance().
|
|
|
767 |
*
|
|
|
768 |
* @covers ::find_instance
|
|
|
769 |
*/
|
|
|
770 |
public function test_find_instance() {
|
|
|
771 |
global $DB;
|
|
|
772 |
$this->resetAfterTest();
|
|
|
773 |
|
|
|
774 |
$cat = $this->getDataGenerator()->create_category();
|
|
|
775 |
// When we create a course, a manual enrolment instance is also created.
|
|
|
776 |
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
|
|
|
777 |
|
|
|
778 |
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
|
|
|
779 |
$manualplugin = enrol_get_plugin('manual');
|
|
|
780 |
|
|
|
781 |
$expected = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'manual']);
|
|
|
782 |
|
|
|
783 |
// Let's try to add second instance - only 1 manual instance is possible.
|
|
|
784 |
$instanceid2 = $manualplugin->add_instance($course, ['roleid' => $teacherrole->id]);
|
|
|
785 |
$this->assertNull($instanceid2);
|
|
|
786 |
|
|
|
787 |
$enrolmentdata = [];
|
|
|
788 |
$actual = $manualplugin->find_instance($enrolmentdata, $course->id);
|
|
|
789 |
$this->assertEquals($expected->id, $actual->id);
|
|
|
790 |
}
|
|
|
791 |
|
|
|
792 |
/**
|
|
|
793 |
* Test send_course_welcome_message_to_user() method.
|
|
|
794 |
*
|
|
|
795 |
* @covers \enrol_plugin::send_course_welcome_message_to_user
|
|
|
796 |
*/
|
|
|
797 |
public function test_send_course_welcome_message(): void {
|
|
|
798 |
global $DB;
|
|
|
799 |
$this->resetAfterTest();
|
|
|
800 |
|
|
|
801 |
// Create course.
|
|
|
802 |
$course = $this->getDataGenerator()->create_course([
|
|
|
803 |
'fullname' => 'Course 1',
|
|
|
804 |
'shortname' => 'C1',
|
|
|
805 |
]);
|
|
|
806 |
// Create users.
|
|
|
807 |
$student = $this->getDataGenerator()->create_user();
|
|
|
808 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
809 |
$teacher2 = $this->getDataGenerator()->create_user();
|
|
|
810 |
$noreplyuser = \core_user::get_noreply_user();
|
|
|
811 |
// Enrol users.
|
|
|
812 |
$this->getDataGenerator()->enrol_user($teacher1->id, $course->id, 'editingteacher');
|
|
|
813 |
$this->getDataGenerator()->enrol_user($teacher2->id, $course->id, 'editingteacher');
|
|
|
814 |
// Get manual plugin.
|
|
|
815 |
$manualplugin = enrol_get_plugin('manual');
|
|
|
816 |
$maninstance = $DB->get_record(
|
|
|
817 |
'enrol',
|
|
|
818 |
['courseid' => $course->id, 'enrol' => 'manual'],
|
|
|
819 |
'*',
|
|
|
820 |
MUST_EXIST,
|
|
|
821 |
);
|
|
|
822 |
|
|
|
823 |
// Test 1: Send welcome message to user from course contact with default message.
|
|
|
824 |
// Redirect messages.
|
|
|
825 |
$messagesink = $this->redirectMessages();
|
|
|
826 |
$manualplugin->send_course_welcome_message_to_user(
|
|
|
827 |
instance: $maninstance,
|
|
|
828 |
userid: $student->id,
|
|
|
829 |
sendoption: ENROL_SEND_EMAIL_FROM_COURSE_CONTACT,
|
|
|
830 |
message: '',
|
|
|
831 |
);
|
|
|
832 |
$messages = $messagesink->get_messages_by_component_and_type(
|
|
|
833 |
'moodle',
|
|
|
834 |
'enrolcoursewelcomemessage',
|
|
|
835 |
);
|
|
|
836 |
$this->assertNotEmpty($messages);
|
|
|
837 |
$message = reset($messages);
|
|
|
838 |
|
|
|
839 |
// The message should be sent from the first teacher.
|
|
|
840 |
$this->assertEquals($teacher1->id, $message->useridfrom);
|
|
|
841 |
$this->assertStringContainsString($course->fullname, $message->subject);
|
|
|
842 |
$this->assertEquals(
|
|
|
843 |
get_string(
|
|
|
844 |
'customwelcomemessageplaceholder',
|
|
|
845 |
'core_enrol',
|
|
|
846 |
['fullname' => fullname($student), 'coursename' => $course->fullname],
|
|
|
847 |
),
|
|
|
848 |
$message->fullmessage,
|
|
|
849 |
);
|
|
|
850 |
|
|
|
851 |
// Clear sink.
|
|
|
852 |
$messagesink->clear();
|
|
|
853 |
|
|
|
854 |
// Test 2: Send welcome message to user from course contact with a custom message.
|
|
|
855 |
// Unenrol the first teacher from course.
|
|
|
856 |
$manualplugin->unenrol_user($maninstance, $teacher1->id);
|
|
|
857 |
// Redirect messages.
|
|
|
858 |
$messagesink = $this->redirectMessages();
|
|
|
859 |
$manualplugin->send_course_welcome_message_to_user(
|
|
|
860 |
instance: $maninstance,
|
|
|
861 |
userid: $student->id,
|
|
|
862 |
sendoption: ENROL_SEND_EMAIL_FROM_COURSE_CONTACT,
|
|
|
863 |
message: 'Your email address: {$a->email}, your first name: {$a->firstname}, your last name: {$a->lastname}',
|
|
|
864 |
);
|
|
|
865 |
$messages = $messagesink->get_messages_by_component_and_type(
|
|
|
866 |
'moodle',
|
|
|
867 |
'enrolcoursewelcomemessage',
|
|
|
868 |
);
|
|
|
869 |
$this->assertNotEmpty($messages);
|
|
|
870 |
$message = reset($messages);
|
|
|
871 |
|
|
|
872 |
// The message should be sent from the second teacher.
|
|
|
873 |
$this->assertEquals($teacher2->id, $message->useridfrom);
|
|
|
874 |
$this->assertStringContainsString($course->fullname, $message->subject);
|
|
|
875 |
$this->assertEquals(
|
|
|
876 |
'Your email address: ' . $student->email . ', your first name: ' . $student->firstname . ', your last name: ' .
|
|
|
877 |
$student->lastname,
|
|
|
878 |
$message->fullmessage,
|
|
|
879 |
);
|
|
|
880 |
// Clear sink.
|
|
|
881 |
$messagesink->clear();
|
|
|
882 |
|
|
|
883 |
// Test 3: Send welcome message to user from no-reply user with a custom message.
|
|
|
884 |
// Redirect messages.
|
|
|
885 |
$messagesink = $this->redirectMessages();
|
|
|
886 |
$manualplugin->send_course_welcome_message_to_user(
|
|
|
887 |
instance: $maninstance,
|
|
|
888 |
userid: $student->id,
|
|
|
889 |
sendoption: ENROL_SEND_EMAIL_FROM_NOREPLY,
|
|
|
890 |
message: 'Your email address: {$a->email}, your first name: {$a->firstname}, your last name: {$a->lastname}',
|
|
|
891 |
);
|
|
|
892 |
$messages = $messagesink->get_messages_by_component_and_type(
|
|
|
893 |
'moodle',
|
|
|
894 |
'enrolcoursewelcomemessage',
|
|
|
895 |
);
|
|
|
896 |
$this->assertNotEmpty($messages);
|
|
|
897 |
$message = reset($messages);
|
|
|
898 |
|
|
|
899 |
// The message should be sent from the noreply user.
|
|
|
900 |
$this->assertEquals($noreplyuser->id, $message->useridfrom);
|
|
|
901 |
$this->assertStringContainsString($course->fullname, $message->subject);
|
|
|
902 |
$this->assertEquals(
|
|
|
903 |
'Your email address: ' . $student->email . ', your first name: ' . $student->firstname . ', your last name: ' .
|
|
|
904 |
$student->lastname,
|
|
|
905 |
$message->fullmessage,
|
|
|
906 |
);
|
|
|
907 |
// Clear sink.
|
|
|
908 |
$messagesink->clear();
|
|
|
909 |
|
|
|
910 |
}
|
|
|
911 |
|
|
|
912 |
/**
|
|
|
913 |
* Test send_course_welcome_message_to_user() method via hook.
|
|
|
914 |
*
|
|
|
915 |
* @covers \enrol_plugin::send_course_welcome_message_to_user
|
|
|
916 |
*/
|
|
|
917 |
public function test_send_course_welcome_message_via_hook(): void {
|
|
|
918 |
global $DB;
|
|
|
919 |
$this->resetAfterTest();
|
|
|
920 |
$messagesink = $this->redirectMessages();
|
|
|
921 |
$course = $this->getDataGenerator()->create_course([
|
|
|
922 |
'fullname' => 'Course 1',
|
|
|
923 |
'shortname' => 'C1',
|
|
|
924 |
]);
|
|
|
925 |
$maninstance = $DB->get_record(
|
|
|
926 |
'enrol',
|
|
|
927 |
['courseid' => $course->id, 'enrol' => 'manual'],
|
|
|
928 |
'*',
|
|
|
929 |
MUST_EXIST,
|
|
|
930 |
);
|
|
|
931 |
$maninstance->customint1 = ENROL_SEND_EMAIL_FROM_NOREPLY;
|
|
|
932 |
$DB->update_record('enrol', $maninstance);
|
|
|
933 |
$student = $this->getDataGenerator()->create_user();
|
|
|
934 |
$this->getDataGenerator()->enrol_user($student->id, $course->id);
|
|
|
935 |
$messages = $messagesink->get_messages_by_component_and_type(
|
|
|
936 |
'moodle',
|
|
|
937 |
'enrolcoursewelcomemessage',
|
|
|
938 |
);
|
|
|
939 |
$this->assertNotEmpty($messages);
|
|
|
940 |
$message = reset($messages);
|
|
|
941 |
$this->assertStringContainsString($course->fullname, $message->subject);
|
|
|
942 |
}
|
|
|
943 |
|
|
|
944 |
}
|