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 |
* coursecompleted enrolment plugin other tests.
|
|
|
19 |
*
|
|
|
20 |
* @package enrol_coursecompleted
|
|
|
21 |
* @copyright 2017 eWallah (www.eWallah.net)
|
|
|
22 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace enrol_coursecompleted;
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
|
|
|
30 |
global $CFG;
|
|
|
31 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
32 |
require_once($CFG->libdir . '/grouplib.php');
|
|
|
33 |
require_once($CFG->dirroot . '/group/lib.php');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* coursecompleted enrolment plugin other tests.
|
|
|
37 |
*
|
|
|
38 |
* @package enrol_coursecompleted
|
|
|
39 |
* @copyright 2017 eWallah (www.eWallah.net)
|
|
|
40 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
* @coversDefaultClass \enrol_coursecompleted_plugin
|
|
|
43 |
*/
|
|
|
44 |
final class other_test extends \advanced_testcase {
|
|
|
45 |
/**
|
|
|
46 |
* Tests initial setup.
|
|
|
47 |
*/
|
|
|
48 |
protected function setUp(): void {
|
|
|
49 |
global $CFG;
|
|
|
50 |
$CFG->enablecompletion = true;
|
|
|
51 |
$this->resetAfterTest(true);
|
|
|
52 |
$enabled = enrol_get_plugins(true);
|
|
|
53 |
$enabled['coursecompleted'] = true;
|
|
|
54 |
set_config('enrol_plugins_enabled', implode(',', array_keys($enabled)));
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Basic test.
|
|
|
59 |
* @covers \enrol_coursecompleted_plugin
|
|
|
60 |
*/
|
|
|
61 |
public function test_basics(): void {
|
|
|
62 |
$this->assertTrue(enrol_is_enabled('coursecompleted'));
|
|
|
63 |
$this->assertEquals(ENROL_EXT_REMOVED_SUSPENDNOROLES, get_config('enrol_coursecompleted', 'expiredaction'));
|
|
|
64 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
65 |
$this->assertNotEmpty($plugin);
|
|
|
66 |
$this->assertInstanceOf('\enrol_coursecompleted_plugin', $plugin);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Test other files.
|
|
|
71 |
* @covers \enrol_coursecompleted_plugin
|
|
|
72 |
*/
|
|
|
73 |
public function test_files(): void {
|
|
|
74 |
global $CFG;
|
|
|
75 |
include($CFG->dirroot . '/enrol/coursecompleted/db/tasks.php');
|
|
|
76 |
include($CFG->dirroot . '/enrol/coursecompleted/db/access.php');
|
|
|
77 |
include($CFG->dirroot . '/enrol/coursecompleted/db/events.php');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Test invalid instance.
|
|
|
82 |
* @covers \enrol_coursecompleted_plugin
|
|
|
83 |
*/
|
|
|
84 |
public function test_invalid_instance(): void {
|
|
|
85 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
86 |
$tst = new \stdClass();
|
|
|
87 |
$tst->enrol = 'wrong';
|
|
|
88 |
$this->expectException('moodle_exception');
|
|
|
89 |
$this->expectExceptionMessage('invalid enrol instance!');
|
|
|
90 |
$this->assertCount(0, $plugin->get_action_icons($tst));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Test disabled.
|
|
|
95 |
* @covers \enrol_coursecompleted_plugin
|
|
|
96 |
* @covers \enrol_coursecompleted\observer
|
|
|
97 |
*/
|
|
|
98 |
public function test_disabled(): void {
|
|
|
99 |
global $CFG;
|
|
|
100 |
require_once($CFG->libdir . '/completionlib.php');
|
|
|
101 |
$generator = $this->getDataGenerator();
|
|
|
102 |
$course1 = $generator->create_course(['enablecompletion' => 1]);
|
|
|
103 |
$course2 = $generator->create_course(['enablecompletion' => 1]);
|
|
|
104 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
105 |
$this->setAdminUser();
|
|
|
106 |
|
|
|
107 |
$student1 = $generator->create_and_enrol($course1, 'student')->id;
|
|
|
108 |
$student2 = $generator->create_and_enrol($course1, 'student')->id;
|
|
|
109 |
$plugin->add_instance($course1, ['customint1' => $course2->id, 'roleid' => 5, 'enrolstartdate' => time() + 66666666]);
|
|
|
110 |
$compevent = \core\event\course_completed::create(
|
|
|
111 |
[
|
|
|
112 |
'objectid' => $course2->id,
|
|
|
113 |
'relateduserid' => $student1,
|
|
|
114 |
'context' => \context_course::instance($course2->id),
|
|
|
115 |
'courseid' => $course2->id,
|
|
|
116 |
'other' => ['relateduserid' => $student1],
|
|
|
117 |
]
|
|
|
118 |
);
|
|
|
119 |
$observer = new observer();
|
|
|
120 |
|
|
|
121 |
$observer->enroluser($compevent);
|
|
|
122 |
$plugin->add_instance($course1, ['customint1' => $course2->id, 'roleid' => 5, 'enrolenddate' => time() - 66666666]);
|
|
|
123 |
$compevent = \core\event\course_completed::create(
|
|
|
124 |
[
|
|
|
125 |
'objectid' => $course2->id,
|
|
|
126 |
'relateduserid' => $student2,
|
|
|
127 |
'context' => \context_course::instance($course2->id),
|
|
|
128 |
'courseid' => $course2->id,
|
|
|
129 |
'other' => ['relateduserid' => $student2],
|
|
|
130 |
]
|
|
|
131 |
);
|
|
|
132 |
$observer = new observer();
|
|
|
133 |
$observer->enroluser($compevent);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Test static enrol from past.
|
|
|
138 |
* @covers \enrol_coursecompleted_plugin
|
|
|
139 |
*/
|
|
|
140 |
public function test_static_past(): void {
|
|
|
141 |
global $CFG;
|
|
|
142 |
require_once($CFG->libdir . '/completionlib.php');
|
|
|
143 |
$generator = $this->getDataGenerator();
|
|
|
144 |
$course1 = $generator->create_course(['enablecompletion' => 1]);
|
|
|
145 |
$course2 = $generator->create_course(['enablecompletion' => 1]);
|
|
|
146 |
$studentid = $generator->create_and_enrol($course1, 'student')->id;
|
|
|
147 |
|
|
|
148 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
149 |
$this->setAdminUser();
|
|
|
150 |
$ccompletion = new \completion_completion(['course' => $course1->id, 'userid' => $studentid]);
|
|
|
151 |
$ccompletion->mark_complete(time());
|
|
|
152 |
$ccompletion = new \completion_completion(['course' => $course2->id, 'userid' => $studentid]);
|
|
|
153 |
$ccompletion->mark_complete(time());
|
|
|
154 |
mark_user_dirty($studentid);
|
|
|
155 |
$plugin->add_instance($course1, ['customint1' => $course2->id, 'roleid' => 5, 'enrolperiod' => 300000]);
|
|
|
156 |
\enrol_coursecompleted_plugin::enrol_past($course1->id);
|
|
|
157 |
\enrol_coursecompleted_plugin::enrol_past($course2->id);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Test invalid role.
|
|
|
162 |
* @covers \enrol_coursecompleted_plugin
|
|
|
163 |
* @covers \enrol_coursecompleted\observer
|
|
|
164 |
*/
|
|
|
165 |
public function test_invalid_role(): void {
|
|
|
166 |
global $DB;
|
|
|
167 |
$generator = $this->getDataGenerator();
|
|
|
168 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
169 |
$course1 = $generator->create_course(['shortname' => 'B1', 'enablecompletion' => 1]);
|
|
|
170 |
$course2 = $generator->create_course(['shortname' => 'B2', 'enablecompletion' => 1]);
|
|
|
171 |
$this->setAdminUser();
|
|
|
172 |
$plugin->add_instance($course1, ['customint1' => $course2->id, 'roleid' => 9999]);
|
|
|
173 |
$instance = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'coursecompleted'], '*', MUST_EXIST);
|
|
|
174 |
$this->assertCount(2, $plugin->build_course_path($instance));
|
|
|
175 |
$studentid = $generator->create_and_enrol($course2, 'student')->id;
|
|
|
176 |
$compevent = \core\event\course_completed::create(
|
|
|
177 |
[
|
|
|
178 |
'objectid' => $course2->id,
|
|
|
179 |
'relateduserid' => $studentid,
|
|
|
180 |
'context' => \context_course::instance($course2->id),
|
|
|
181 |
'courseid' => $course2->id,
|
|
|
182 |
'other' => ['relateduserid' => $studentid],
|
|
|
183 |
]
|
|
|
184 |
);
|
|
|
185 |
$observer = new observer();
|
|
|
186 |
$observer->enroluser($compevent);
|
|
|
187 |
$this->assertDebuggingCalled('Role does not exist');
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Test group member.
|
|
|
192 |
* @covers \enrol_coursecompleted\observer
|
|
|
193 |
* @covers \enrol_coursecompleted_plugin
|
|
|
194 |
*/
|
|
|
195 |
public function test_groups_child(): void {
|
|
|
196 |
global $DB;
|
|
|
197 |
$generator = $this->getDataGenerator();
|
|
|
198 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
199 |
$course1 = $generator->create_course(['shortname' => 'B1', 'enablecompletion' => 1]);
|
|
|
200 |
$data = new \stdClass();
|
|
|
201 |
$data->courseid = $course1->id;
|
|
|
202 |
$data->idnumber = $course1->id . 'A';
|
|
|
203 |
$data->name = 'A group';
|
|
|
204 |
$data->description = '';
|
|
|
205 |
$data->descriptionformat = FORMAT_HTML;
|
|
|
206 |
$groupid1 = groups_create_group($data);
|
|
|
207 |
rebuild_course_cache($course1->id, true);
|
|
|
208 |
$course2 = $generator->create_course(['shortname' => 'B2', 'enablecompletion' => 1]);
|
|
|
209 |
$data->courseid = $course2->id;
|
|
|
210 |
$data->idnumber = $course2->id . 'A';
|
|
|
211 |
$groupid2 = groups_create_group($data);
|
|
|
212 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
213 |
$this->setAdminUser();
|
|
|
214 |
$plugin->add_instance($course1, ['customint1' => $course2->id, 'roleid' => $studentrole->id]);
|
|
|
215 |
$instance = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'coursecompleted'], '*', MUST_EXIST);
|
|
|
216 |
$this->assertCount(2, $plugin->build_course_path($instance));
|
|
|
217 |
$studentid = $generator->create_and_enrol($course2)->id;
|
|
|
218 |
groups_add_member($groupid2, $studentid);
|
|
|
219 |
rebuild_course_cache($course2->id, true);
|
|
|
220 |
$compevent = \core\event\course_completed::create(
|
|
|
221 |
[
|
|
|
222 |
'objectid' => $course2->id,
|
|
|
223 |
'relateduserid' => $studentid,
|
|
|
224 |
'context' => \context_course::instance($course2->id),
|
|
|
225 |
'courseid' => $course2->id,
|
|
|
226 |
'other' => ['relateduserid' => $studentid],
|
|
|
227 |
]
|
|
|
228 |
);
|
|
|
229 |
$observer = new observer();
|
|
|
230 |
$observer->enroluser($compevent);
|
|
|
231 |
$this->assertTrue(groups_is_member($groupid2, $studentid));
|
|
|
232 |
rebuild_course_cache($course1->id, true);
|
|
|
233 |
rebuild_course_cache($course2->id, true);
|
|
|
234 |
$this->assertTrue(groups_is_member($groupid1, $studentid));
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Test expiration task.
|
|
|
239 |
* @covers \enrol_coursecompleted\task\process_expirations
|
|
|
240 |
*/
|
|
|
241 |
public function test_task(): void {
|
|
|
242 |
$task = new \enrol_coursecompleted\task\process_expirations();
|
|
|
243 |
$this->assertEquals('Course completed enrolment expiry task', $task->get_name());
|
|
|
244 |
ob_start();
|
|
|
245 |
$task->execute();
|
|
|
246 |
$output = ob_get_contents();
|
|
|
247 |
ob_end_clean();
|
|
|
248 |
$this->assertStringContainsString('No expired enrol_coursecompleted enrolments detected', $output);
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Test adhoc sending of welcome messages.
|
|
|
253 |
* @covers \enrol_coursecompleted\task\send_welcome
|
|
|
254 |
*/
|
|
|
255 |
public function test_adhoc_email_welcome_message(): void {
|
|
|
256 |
global $DB;
|
|
|
257 |
$generator = $this->getDataGenerator();
|
|
|
258 |
$sink = $this->redirectEmails();
|
|
|
259 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
260 |
$studentid = $generator->create_user()->id;
|
|
|
261 |
$course = $generator->create_course(['shortname' => 'B0', 'enablecompletion' => 1]);
|
|
|
262 |
$courseid1 = $generator->create_course(['shortname' => 'B1', 'enablecompletion' => 1])->id;
|
|
|
263 |
$courseid2 = $generator->create_course(['shortname' => 'B2', 'enablecompletion' => 1])->id;
|
|
|
264 |
$courseid3 = $generator->create_course(['shortname' => 'B3', 'enablecompletion' => 1])->id;
|
|
|
265 |
$courseid4 = $generator->create_course(['shortname' => 'B4', 'enablecompletion' => 1])->id;
|
|
|
266 |
$plugin->add_instance($course, ['customint1' => $courseid1, 'roleid' => 5, 'customint2' => 0]);
|
|
|
267 |
$i2 = $plugin->add_instance($course, ['customint1' => $courseid2, 'roleid' => 5, 'customint2' => 1]);
|
|
|
268 |
$i3 = $plugin->add_instance($course, ['customint1' => $courseid3, 'customtext1' => 'boe', 'customint2' => 1]);
|
|
|
269 |
$i4 = $plugin->add_instance(
|
|
|
270 |
$course,
|
|
|
271 |
['customint1' => $courseid4, 'customtext1' => '{$a->fullname} <b>boe</b>
|
|
|
272 |
<a>another line</a>', 'customint2' => 1]
|
|
|
273 |
);
|
|
|
274 |
$compevent = \core\event\course_completed::create(
|
|
|
275 |
[
|
|
|
276 |
'objectid' => $courseid1,
|
|
|
277 |
'relateduserid' => $studentid,
|
|
|
278 |
'context' => \context_course::instance($courseid1),
|
|
|
279 |
'courseid' => $courseid1,
|
|
|
280 |
'other' => ['relateduserid' => $studentid],
|
|
|
281 |
]
|
|
|
282 |
);
|
|
|
283 |
$observer = new observer();
|
|
|
284 |
$observer->enroluser($compevent);
|
|
|
285 |
$adhock = new \enrol_coursecompleted\task\send_welcome();
|
|
|
286 |
$adhock->set_custom_data(
|
|
|
287 |
['userid' => $studentid, 'enrolid' => $i2, 'courseid' => $course->id, 'completedid' => $courseid2]
|
|
|
288 |
);
|
|
|
289 |
$adhock->set_component('enrol_coursecompleted');
|
|
|
290 |
$adhock->execute();
|
|
|
291 |
\core\task\manager::queue_adhoc_task($adhock);
|
|
|
292 |
$adhock->set_custom_data(
|
|
|
293 |
['userid' => $studentid, 'enrolid' => $i3, 'courseid' => $course->id, 'completedid' => $courseid3]
|
|
|
294 |
);
|
|
|
295 |
\core\task\manager::queue_adhoc_task($adhock);
|
|
|
296 |
$adhock->set_custom_data(
|
|
|
297 |
['userid' => $studentid, 'enrolid' => $i4, 'courseid' => $course->id, 'completedid' => $courseid4]
|
|
|
298 |
);
|
|
|
299 |
\core\task\manager::queue_adhoc_task($adhock);
|
|
|
300 |
$this->assertCount(3, $DB->get_records('task_adhoc', ['component' => 'enrol_coursecompleted']));
|
|
|
301 |
\phpunit_util::run_all_adhoc_tasks();
|
|
|
302 |
$messages = $sink->get_messages();
|
|
|
303 |
$this->assertCount(4, $messages);
|
|
|
304 |
$sink->close();
|
|
|
305 |
foreach ($messages as $message) {
|
|
|
306 |
$this->assertStringNotContainsString('{a->', $message->header);
|
|
|
307 |
$this->assertStringNotContainsString('{a->', $message->body);
|
|
|
308 |
}
|
|
|
309 |
$this->assertCount(0, $DB->get_records('task_adhoc', ['component' => 'enrol_coursecompleted']));
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
* Test access.
|
|
|
314 |
* @covers \enrol_coursecompleted_plugin
|
|
|
315 |
*/
|
|
|
316 |
public function test_access(): void {
|
|
|
317 |
global $CFG;
|
|
|
318 |
$CFG->enablecompletion = true;
|
|
|
319 |
$this->resetAfterTest(true);
|
|
|
320 |
$enabled = enrol_get_plugins(true);
|
|
|
321 |
$enabled['coursecompleted'] = true;
|
|
|
322 |
set_config('enrol_plugins_enabled', implode(',', array_keys($enabled)));
|
|
|
323 |
$generator = $this->getDataGenerator();
|
|
|
324 |
$course = $generator->create_course(['enablecompletion' => 1]);
|
|
|
325 |
$student = $generator->create_and_enrol($course, 'student');
|
|
|
326 |
$editor = $generator->create_and_enrol($course, 'editingteacher');
|
|
|
327 |
$this->setAdminUser();
|
|
|
328 |
$context = \context_course::instance($course->id);
|
|
|
329 |
$this->assertTrue(has_capability('enrol/coursecompleted:config', $context));
|
|
|
330 |
$this->assertTrue(has_capability('enrol/coursecompleted:enrolpast', $context));
|
|
|
331 |
$this->assertTrue(has_capability('enrol/coursecompleted:manage', $context));
|
|
|
332 |
$this->assertTrue(has_capability('enrol/coursecompleted:unenrol', $context));
|
|
|
333 |
$this->assertTrue(has_capability('enrol/coursecompleted:unenrolself', $context));
|
|
|
334 |
$this->setUser($student);
|
|
|
335 |
$this->assertFalse(has_capability('enrol/coursecompleted:config', $context));
|
|
|
336 |
$this->assertFalse(has_capability('enrol/coursecompleted:enrolpast', $context));
|
|
|
337 |
$this->assertFalse(has_capability('enrol/coursecompleted:manage', $context));
|
|
|
338 |
$this->assertFalse(has_capability('enrol/coursecompleted:unenrol', $context));
|
|
|
339 |
$this->assertFalse(has_capability('enrol/coursecompleted:unenrolself', $context));
|
|
|
340 |
$this->setUser($editor);
|
|
|
341 |
$this->assertTrue(has_capability('enrol/coursecompleted:config', $context));
|
|
|
342 |
$this->assertFalse(has_capability('enrol/coursecompleted:enrolpast', $context));
|
|
|
343 |
$this->assertTrue(has_capability('enrol/coursecompleted:manage', $context));
|
|
|
344 |
$this->assertTrue(has_capability('enrol/coursecompleted:unenrol', $context));
|
|
|
345 |
$this->assertFalse(has_capability('enrol/coursecompleted:unenrolself', $context));
|
|
|
346 |
}
|
|
|
347 |
}
|