2 // This file is part of Moodle - http://moodle.org/
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.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for mod/lesson/lib.php.
22 * @copyright 2017 Jun Pataleta
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/mod/lesson/lib.php');
32 * Unit tests for mod/lesson/lib.php.
34 * @copyright 2017 Jun Pataleta
35 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
37 class mod_lesson_lib_testcase extends advanced_testcase {
39 * Test for lesson_get_group_override_priorities().
41 public function test_lesson_get_group_override_priorities() {
43 $this->resetAfterTest();
44 $this->setAdminUser();
46 $dg = $this->getDataGenerator();
47 $course = $dg->create_course();
48 $lessonmodule = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
50 $this->assertNull(lesson_get_group_override_priorities($lessonmodule->id));
52 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
53 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
56 $override1 = (object)[
57 'lessonid' => $lessonmodule->id,
58 'groupid' => $group1->id,
60 'deadline' => $now + 20
62 $DB->insert_record('lesson_overrides', $override1);
64 $override2 = (object)[
65 'lessonid' => $lessonmodule->id,
66 'groupid' => $group2->id,
67 'available' => $now - 10,
68 'deadline' => $now + 10
70 $DB->insert_record('lesson_overrides', $override2);
72 $priorities = lesson_get_group_override_priorities($lessonmodule->id);
73 $this->assertNotEmpty($priorities);
75 $openpriorities = $priorities['open'];
76 // Override 2's time open has higher priority since it is sooner than override 1's.
77 $this->assertEquals(2, $openpriorities[$override1->available]);
78 $this->assertEquals(1, $openpriorities[$override2->available]);
80 $closepriorities = $priorities['close'];
81 // Override 1's time close has higher priority since it is later than override 2's.
82 $this->assertEquals(1, $closepriorities[$override1->deadline]);
83 $this->assertEquals(2, $closepriorities[$override2->deadline]);
87 * Test check_updates_since callback.
89 public function test_check_updates_since() {
92 $this->resetAfterTest();
93 $this->setAdminUser();
94 $course = new stdClass();
95 $course->groupmode = SEPARATEGROUPS;
96 $course->groupmodeforce = true;
97 $course = $this->getDataGenerator()->create_course($course);
100 $studentg1 = self::getDataGenerator()->create_user();
101 $teacherg1 = self::getDataGenerator()->create_user();
102 $studentg2 = self::getDataGenerator()->create_user();
105 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
106 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
107 $this->getDataGenerator()->enrol_user($studentg1->id, $course->id, $studentrole->id, 'manual');
108 $this->getDataGenerator()->enrol_user($teacherg1->id, $course->id, $teacherrole->id, 'manual');
109 $this->getDataGenerator()->enrol_user($studentg2->id, $course->id, $studentrole->id, 'manual');
111 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
112 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
113 groups_add_member($group1, $studentg1);
114 groups_add_member($group2, $studentg2);
116 $this->setCurrentTimeStart();
118 'course' => $course->id,
122 $lessonmodule = $this->getDataGenerator()->create_module('lesson', $record);
123 // Convert to a lesson object.
124 $lesson = new lesson($lessonmodule);
126 $cm = cm_info::create($cm);
128 // Check that upon creation, the updates are only about the new configuration created.
129 $onehourago = time() - HOURSECS;
130 $updates = lesson_check_updates_since($cm, $onehourago);
131 foreach ($updates as $el => $val) {
132 if ($el == 'configuration') {
133 $this->assertTrue($val->updated);
134 $this->assertTimeCurrent($val->timeupdated);
136 $this->assertFalse($val->updated);
140 // Set up a generator to create content.
141 $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
142 $tfrecord = $generator->create_question_truefalse($lesson);
144 // Check now for pages and answers.
145 $updates = lesson_check_updates_since($cm, $onehourago);
146 $this->assertTrue($updates->pages->updated);
147 $this->assertCount(1, $updates->pages->itemids);
149 $this->assertTrue($updates->answers->updated);
150 $this->assertCount(2, $updates->answers->itemids);
152 // Now, do something in the lesson with the two users.
153 $this->setUser($studentg1);
154 mod_lesson_external::launch_attempt($lesson->id);
157 'name' => 'answerid',
158 'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)),
161 'name' => '_qf__lesson_display_answer_form_truefalse',
165 mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data);
166 mod_lesson_external::finish_attempt($lesson->id);
168 $this->setUser($studentg2);
169 mod_lesson_external::launch_attempt($lesson->id);
172 'name' => 'answerid',
173 'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)),
176 'name' => '_qf__lesson_display_answer_form_truefalse',
180 mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data);
181 mod_lesson_external::finish_attempt($lesson->id);
183 $this->setUser($studentg1);
184 $updates = lesson_check_updates_since($cm, $onehourago);
186 // Check question attempts, timers and new grades.
187 $this->assertTrue($updates->questionattempts->updated);
188 $this->assertCount(1, $updates->questionattempts->itemids);
190 $this->assertTrue($updates->grades->updated);
191 $this->assertCount(1, $updates->grades->itemids);
193 $this->assertTrue($updates->timers->updated);
194 $this->assertCount(1, $updates->timers->itemids);
196 // Now, as teacher, check that I can see the two users (even in separate groups).
197 $this->setUser($teacherg1);
198 $updates = lesson_check_updates_since($cm, $onehourago);
199 $this->assertTrue($updates->userquestionattempts->updated);
200 $this->assertCount(2, $updates->userquestionattempts->itemids);
202 $this->assertTrue($updates->usergrades->updated);
203 $this->assertCount(2, $updates->usergrades->itemids);
205 $this->assertTrue($updates->usertimers->updated);
206 $this->assertCount(2, $updates->usertimers->itemids);
208 // Now, teacher can't access all groups.
209 groups_add_member($group1, $teacherg1);
210 assign_capability('moodle/site:accessallgroups', CAP_PROHIBIT, $teacherrole->id, context_module::instance($cm->id));
211 accesslib_clear_all_caches_for_unit_testing();
212 $updates = lesson_check_updates_since($cm, $onehourago);
213 // I will see only the studentg1 updates.
214 $this->assertTrue($updates->userquestionattempts->updated);
215 $this->assertCount(1, $updates->userquestionattempts->itemids);
217 $this->assertTrue($updates->usergrades->updated);
218 $this->assertCount(1, $updates->usergrades->itemids);
220 $this->assertTrue($updates->usertimers->updated);
221 $this->assertCount(1, $updates->usertimers->itemids);
224 public function test_lesson_core_calendar_provide_event_action_open() {
225 $this->resetAfterTest();
226 $this->setAdminUser();
228 $course = $this->getDataGenerator()->create_course();
229 // Create a lesson activity.
230 $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
231 'available' => time() - DAYSECS, 'deadline' => time() + DAYSECS));
232 // Create a calendar event.
233 $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
234 // Create an action factory.
235 $factory = new \core_calendar\action_factory();
236 // Decorate action event.
237 $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
238 // Confirm the event was decorated.
239 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
240 $this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
241 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
242 $this->assertEquals(1, $actionevent->get_item_count());
243 $this->assertTrue($actionevent->is_actionable());
246 public function test_lesson_core_calendar_provide_event_action_closed() {
247 $this->resetAfterTest();
248 $this->setAdminUser();
251 $course = $this->getDataGenerator()->create_course();
253 // Create a lesson activity.
254 $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
255 'deadline' => time() - DAYSECS));
257 // Create a calendar event.
258 $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
260 // Create an action factory.
261 $factory = new \core_calendar\action_factory();
263 // Decorate action event.
264 $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
266 // Confirm the event was decorated.
267 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
268 $this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
269 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
270 $this->assertEquals(1, $actionevent->get_item_count());
271 $this->assertFalse($actionevent->is_actionable());
274 public function test_lesson_core_calendar_provide_event_action_open_in_future() {
275 $this->resetAfterTest();
276 $this->setAdminUser();
279 $course = $this->getDataGenerator()->create_course();
281 // Create a lesson activity.
282 $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id,
283 'available' => time() + DAYSECS));
285 // Create a calendar event.
286 $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
288 // Create an action factory.
289 $factory = new \core_calendar\action_factory();
291 // Decorate action event.
292 $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
294 // Confirm the event was decorated.
295 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
296 $this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
297 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
298 $this->assertEquals(1, $actionevent->get_item_count());
299 $this->assertFalse($actionevent->is_actionable());
302 public function test_lesson_core_calendar_provide_event_action_no_time_specified() {
303 $this->resetAfterTest();
304 $this->setAdminUser();
307 $course = $this->getDataGenerator()->create_course();
309 // Create a lesson activity.
310 $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
312 // Create a calendar event.
313 $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
315 // Create an action factory.
316 $factory = new \core_calendar\action_factory();
318 // Decorate action event.
319 $actionevent = mod_lesson_core_calendar_provide_event_action($event, $factory);
321 // Confirm the event was decorated.
322 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
323 $this->assertEquals(get_string('startlesson', 'lesson'), $actionevent->get_name());
324 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
325 $this->assertEquals(1, $actionevent->get_item_count());
326 $this->assertTrue($actionevent->is_actionable());
329 public function test_lesson_core_calendar_provide_event_action_after_attempt() {
332 $this->resetAfterTest();
333 $this->setAdminUser();
336 $course = $this->getDataGenerator()->create_course();
339 $student = self::getDataGenerator()->create_user();
341 // Create a lesson activity.
342 $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course->id));
344 // Create a calendar event.
345 $event = $this->create_action_event($course->id, $lesson->id, LESSON_EVENT_TYPE_OPEN);
347 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
348 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
350 $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
351 $tfrecord = $generator->create_question_truefalse($lesson);
353 // Now, do something in the lesson.
354 $this->setUser($student);
355 mod_lesson_external::launch_attempt($lesson->id);
358 'name' => 'answerid',
359 'value' => $DB->get_field('lesson_answers', 'id', array('pageid' => $tfrecord->id, 'jumpto' => -1)),
362 'name' => '_qf__lesson_display_answer_form_truefalse',
366 mod_lesson_external::process_page($lesson->id, $tfrecord->id, $data);
367 mod_lesson_external::finish_attempt($lesson->id);
369 // Create an action factory.
370 $factory = new \core_calendar\action_factory();
372 // Decorate action event.
373 $action = mod_lesson_core_calendar_provide_event_action($event, $factory);
375 // Confirm there was no action for the user.
376 $this->assertNull($action);
380 * Creates an action event.
382 * @param int $courseid
383 * @param int $instanceid The lesson id.
384 * @param string $eventtype The event type. eg. LESSON_EVENT_TYPE_OPEN.
385 * @return bool|calendar_event
387 private function create_action_event($courseid, $instanceid, $eventtype) {
388 $event = new stdClass();
389 $event->name = 'Calendar event';
390 $event->modulename = 'lesson';
391 $event->courseid = $courseid;
392 $event->instance = $instanceid;
393 $event->type = CALENDAR_EVENT_TYPE_ACTION;
394 $event->eventtype = $eventtype;
395 $event->timestart = time();
396 return calendar_event::create($event);
400 * Test the callback responsible for returning the completion rule descriptions.
401 * This function should work given either an instance of the module (cm_info), such as when checking the active rules,
402 * or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
404 public function test_mod_lesson_completion_get_active_rule_descriptions() {
405 $this->resetAfterTest();
406 $this->setAdminUser();
408 // Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
409 $course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
410 $lesson1 = $this->getDataGenerator()->create_module('lesson', [
411 'course' => $course->id,
413 'completionendreached' => 1,
414 'completiontimespent' => 3600
416 $lesson2 = $this->getDataGenerator()->create_module('lesson', [
417 'course' => $course->id,
419 'completionendreached' => 0,
420 'completiontimespent' => 0
422 $cm1 = cm_info::create(get_coursemodule_from_instance('lesson', $lesson1->id));
423 $cm2 = cm_info::create(get_coursemodule_from_instance('lesson', $lesson2->id));
425 // Data for the stdClass input type.
426 // This type of input would occur when checking the default completion rules for an activity type, where we don't have
427 // any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
428 $moddefaults = new stdClass();
429 $moddefaults->customdata = ['customcompletionrules' => [
430 'completionendreached' => 1,
431 'completiontimespent' => 3600
433 $moddefaults->completion = 2;
435 $activeruledescriptions = [
436 get_string('completionendreached_desc', 'lesson'),
437 get_string('completiontimespentdesc', 'lesson', format_time(3600)),
439 $this->assertEquals(mod_lesson_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
440 $this->assertEquals(mod_lesson_get_completion_active_rule_descriptions($cm2), []);
441 $this->assertEquals(mod_lesson_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
442 $this->assertEquals(mod_lesson_get_completion_active_rule_descriptions(new stdClass()), []);