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_lti lib
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
31 * Unit tests for mod_lti lib
35 * @copyright 2015 Juan Leyva <juan@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class mod_lti_lib_testcase
extends advanced_testcase
{
42 * Prepares things before this test case is initialised
45 public static function setUpBeforeClass() {
47 require_once($CFG->dirroot
. '/mod/lti/lib.php');
54 public function test_lti_view() {
57 $CFG->enablecompletion
= 1;
58 $this->resetAfterTest();
60 $this->setAdminUser();
62 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
63 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
),
64 array('completion' => 2, 'completionview' => 1));
65 $context = context_module
::instance($lti->cmid
);
66 $cm = get_coursemodule_from_instance('lti', $lti->id
);
68 // Trigger and capture the event.
69 $sink = $this->redirectEvents();
71 lti_view($lti, $course, $cm, $context);
73 $events = $sink->get_events();
74 // 2 additional events thanks to completion.
75 $this->assertCount(3, $events);
76 $event = array_shift($events);
78 // Checking that the event contains the expected values.
79 $this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event);
80 $this->assertEquals($context, $event->get_context());
81 $moodleurl = new \
moodle_url('/mod/lti/view.php', array('id' => $cm->id
));
82 $this->assertEquals($moodleurl, $event->get_url());
83 $this->assertEventContextNotUsed($event);
84 $this->assertNotEmpty($event->get_name());
86 // Check completion status.
87 $completion = new completion_info($course);
88 $completiondata = $completion->get_data($cm);
89 $this->assertEquals(1, $completiondata->completionstate
);
94 * Test deleting LTI instance.
96 public function test_lti_delete_instance() {
97 $this->resetAfterTest();
99 $this->setAdminUser();
100 $course = $this->getDataGenerator()->create_course(array());
101 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
));
102 $cm = get_coursemodule_from_instance('lti', $lti->id
);
104 // Must not throw notices.
105 course_delete_module($cm->id
);
108 public function test_lti_core_calendar_provide_event_action() {
109 $this->resetAfterTest();
110 $this->setAdminUser();
112 // Create the activity.
113 $course = $this->getDataGenerator()->create_course();
114 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
));
116 // Create a calendar event.
117 $event = $this->create_action_event($course->id
, $lti->id
,
118 \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
);
120 // Create an action factory.
121 $factory = new \core_calendar\action_factory
();
123 // Decorate action event.
124 $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
126 // Confirm the event was decorated.
127 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
128 $this->assertEquals(get_string('view'), $actionevent->get_name());
129 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
130 $this->assertEquals(1, $actionevent->get_item_count());
131 $this->assertTrue($actionevent->is_actionable());
134 public function test_lti_core_calendar_provide_event_action_as_non_user() {
137 $this->resetAfterTest();
138 $this->setAdminUser();
140 // Create the activity.
141 $course = $this->getDataGenerator()->create_course();
142 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
));
144 // Create a calendar event.
145 $event = $this->create_action_event($course->id
, $lti->id
,
146 \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
);
149 $CFG->forcelogin
= true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
152 // Create an action factory.
153 $factory = new \core_calendar\action_factory
();
155 // Decorate action event.
156 $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
158 // Confirm the event is not shown at all.
159 $this->assertNull($actionevent);
162 public function test_lti_core_calendar_provide_event_action_for_user() {
165 $this->resetAfterTest();
166 $this->setAdminUser();
168 // Create the activity.
169 $course = $this->getDataGenerator()->create_course();
170 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
));
172 // Enrol a student in the course.
173 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
175 // Create a calendar event.
176 $event = $this->create_action_event($course->id
, $lti->id
,
177 \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
);
180 $CFG->forcelogin
= true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
183 // Create an action factory.
184 $factory = new \core_calendar\action_factory
();
186 // Decorate action event for the student.
187 $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory, $student->id
);
189 // Confirm the event was decorated.
190 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
191 $this->assertEquals(get_string('view'), $actionevent->get_name());
192 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
193 $this->assertEquals(1, $actionevent->get_item_count());
194 $this->assertTrue($actionevent->is_actionable());
197 public function test_lti_core_calendar_provide_event_action_already_completed() {
200 $this->resetAfterTest();
201 $this->setAdminUser();
203 $CFG->enablecompletion
= 1;
205 // Create the activity.
206 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
207 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
),
208 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS
));
210 // Get some additional data.
211 $cm = get_coursemodule_from_instance('lti', $lti->id
);
213 // Create a calendar event.
214 $event = $this->create_action_event($course->id
, $lti->id
,
215 \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
);
217 // Mark the activity as completed.
218 $completion = new completion_info($course);
219 $completion->set_module_viewed($cm);
221 // Create an action factory.
222 $factory = new \core_calendar\action_factory
();
224 // Decorate action event.
225 $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
227 // Ensure result was null.
228 $this->assertNull($actionevent);
231 public function test_lti_core_calendar_provide_event_action_already_completed_as_non_user() {
234 $this->resetAfterTest();
235 $this->setAdminUser();
237 $CFG->enablecompletion
= 1;
239 // Create the activity.
240 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
241 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
),
242 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS
));
244 // Get some additional data.
245 $cm = get_coursemodule_from_instance('lti', $lti->id
);
247 // Create a calendar event.
248 $event = $this->create_action_event($course->id
, $lti->id
,
249 \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
);
251 // Mark the activity as completed.
252 $completion = new completion_info($course);
253 $completion->set_module_viewed($cm);
256 $CFG->forcelogin
= true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
259 // Create an action factory.
260 $factory = new \core_calendar\action_factory
();
262 // Decorate action event.
263 $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory);
265 // Ensure result was null.
266 $this->assertNull($actionevent);
269 public function test_lti_core_calendar_provide_event_action_already_completed_for_user() {
272 $this->resetAfterTest();
273 $this->setAdminUser();
275 $CFG->enablecompletion
= 1;
277 // Create the activity.
278 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
279 $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id
),
280 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS
));
282 // Enrol 2 students in the course.
283 $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
284 $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
286 // Get some additional data.
287 $cm = get_coursemodule_from_instance('lti', $lti->id
);
289 // Create a calendar event.
290 $event = $this->create_action_event($course->id
, $lti->id
,
291 \core_completion\api
::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED
);
293 // Mark the activity as completed for $student1.
294 $completion = new completion_info($course);
295 $completion->set_module_viewed($cm, $student1->id
);
297 // Now, log in as $student2.
298 $this->setUser($student2);
300 // Create an action factory.
301 $factory = new \core_calendar\action_factory
();
303 // Decorate action event for $student1.
304 $actionevent = mod_lti_core_calendar_provide_event_action($event, $factory, $student1->id
);
306 // Ensure result was null.
307 $this->assertNull($actionevent);
311 * Creates an action event.
313 * @param int $courseid The course id.
314 * @param int $instanceid The instance id.
315 * @param string $eventtype The event type.
316 * @return bool|calendar_event
318 private function create_action_event($courseid, $instanceid, $eventtype) {
319 $event = new stdClass();
320 $event->name
= 'Calendar event';
321 $event->modulename
= 'lti';
322 $event->courseid
= $courseid;
323 $event->instance
= $instanceid;
324 $event->type
= CALENDAR_EVENT_TYPE_ACTION
;
325 $event->eventtype
= $eventtype;
326 $event->timestart
= time();
328 return calendar_event
::create($event);