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 |
* Unit tests for mod_survey lib
|
|
|
19 |
*
|
|
|
20 |
* @package mod_survey
|
|
|
21 |
* @category external
|
|
|
22 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @since Moodle 3.0
|
|
|
25 |
*/
|
|
|
26 |
namespace mod_survey;
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Unit tests for mod_survey lib
|
|
|
33 |
*
|
|
|
34 |
* @package mod_survey
|
|
|
35 |
* @category external
|
|
|
36 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
* @since Moodle 3.0
|
|
|
39 |
*/
|
|
|
40 |
class lib_test extends \advanced_testcase {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Prepares things before this test case is initialised
|
|
|
44 |
* @return void
|
|
|
45 |
*/
|
|
|
46 |
public static function setUpBeforeClass(): void {
|
|
|
47 |
global $CFG;
|
|
|
48 |
require_once($CFG->dirroot . '/mod/survey/lib.php');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Setup testcase.
|
|
|
53 |
*/
|
|
|
54 |
public function setUp(): void {
|
|
|
55 |
// Survey module is disabled by default, enable it for testing.
|
|
|
56 |
$manager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
|
|
57 |
$manager::enable_plugin('survey', 1);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Test survey_view
|
|
|
62 |
* @return void
|
|
|
63 |
*/
|
|
|
64 |
public function test_survey_view() {
|
|
|
65 |
global $CFG;
|
|
|
66 |
|
|
|
67 |
$CFG->enablecompletion = 1;
|
|
|
68 |
$this->resetAfterTest();
|
|
|
69 |
|
|
|
70 |
$this->setAdminUser();
|
|
|
71 |
// Setup test data.
|
|
|
72 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
73 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
|
|
|
74 |
array('completion' => 2, 'completionview' => 1));
|
|
|
75 |
$context = \context_module::instance($survey->cmid);
|
|
|
76 |
$cm = get_coursemodule_from_instance('survey', $survey->id);
|
|
|
77 |
|
|
|
78 |
// Trigger and capture the event.
|
|
|
79 |
$sink = $this->redirectEvents();
|
|
|
80 |
|
|
|
81 |
survey_view($survey, $course, $cm, $context, 'form');
|
|
|
82 |
|
|
|
83 |
$events = $sink->get_events();
|
|
|
84 |
// 2 additional events thanks to completion.
|
|
|
85 |
$this->assertCount(3, $events);
|
|
|
86 |
$event = array_shift($events);
|
|
|
87 |
|
|
|
88 |
// Checking that the event contains the expected values.
|
|
|
89 |
$this->assertInstanceOf('\mod_survey\event\course_module_viewed', $event);
|
|
|
90 |
$this->assertEquals($context, $event->get_context());
|
|
|
91 |
$moodleurl = new \moodle_url('/mod/survey/view.php', array('id' => $cm->id));
|
|
|
92 |
$this->assertEquals($moodleurl, $event->get_url());
|
|
|
93 |
$this->assertEquals('form', $event->other['viewed']);
|
|
|
94 |
$this->assertEventContextNotUsed($event);
|
|
|
95 |
$this->assertNotEmpty($event->get_name());
|
|
|
96 |
// Check completion status.
|
|
|
97 |
$completion = new \completion_info($course);
|
|
|
98 |
$completiondata = $completion->get_data($cm);
|
|
|
99 |
$this->assertEquals(1, $completiondata->completionstate);
|
|
|
100 |
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Test survey_order_questions
|
|
|
105 |
*/
|
|
|
106 |
public function test_survey_order_questions() {
|
|
|
107 |
global $DB;
|
|
|
108 |
|
|
|
109 |
$this->resetAfterTest();
|
|
|
110 |
$course = $this->getDataGenerator()->create_course();
|
|
|
111 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
|
|
|
112 |
|
|
|
113 |
$orderedquestionids = explode(',', $survey->questions);
|
|
|
114 |
$surveyquestions = $DB->get_records_list("survey_questions", "id", $orderedquestionids);
|
|
|
115 |
|
|
|
116 |
$questionsordered = survey_order_questions($surveyquestions, $orderedquestionids);
|
|
|
117 |
|
|
|
118 |
// Check one by one the correct order.
|
|
|
119 |
for ($i = 0; $i < count($orderedquestionids); $i++) {
|
|
|
120 |
$this->assertEquals($orderedquestionids[$i], $questionsordered[$i]->id);
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Test survey_save_answers
|
|
|
126 |
*/
|
|
|
127 |
public function test_survey_save_answers() {
|
|
|
128 |
global $DB;
|
|
|
129 |
|
|
|
130 |
$this->resetAfterTest();
|
|
|
131 |
$this->setAdminUser();
|
|
|
132 |
|
|
|
133 |
// Setup test data.
|
|
|
134 |
$course = $this->getDataGenerator()->create_course();
|
|
|
135 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
|
|
|
136 |
$context = \context_module::instance($survey->cmid);
|
|
|
137 |
|
|
|
138 |
// Build our questions and responses array.
|
|
|
139 |
$realquestions = array();
|
|
|
140 |
$questions = survey_get_questions($survey);
|
|
|
141 |
$i = 5;
|
|
|
142 |
foreach ($questions as $q) {
|
|
|
143 |
if ($q->type > 0) {
|
|
|
144 |
if ($q->multi) {
|
|
|
145 |
$subquestions = survey_get_subquestions($q);
|
|
|
146 |
foreach ($subquestions as $sq) {
|
|
|
147 |
$key = 'q' . $sq->id;
|
|
|
148 |
$realquestions[$key] = $i % 5 + 1;
|
|
|
149 |
$i++;
|
|
|
150 |
}
|
|
|
151 |
} else {
|
|
|
152 |
$key = 'q' . $q->id;
|
|
|
153 |
$realquestions[$key] = $i % 5 + 1;
|
|
|
154 |
$i++;
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$sink = $this->redirectEvents();
|
|
|
160 |
survey_save_answers($survey, $realquestions, $course, $context);
|
|
|
161 |
|
|
|
162 |
// Check the stored answers, they must match.
|
|
|
163 |
$dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $survey->id), '', 'question, answer1');
|
|
|
164 |
foreach ($realquestions as $key => $value) {
|
|
|
165 |
$id = str_replace('q', '', $key);
|
|
|
166 |
$this->assertEquals($value, $dbanswers[$id]);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// Check events.
|
|
|
170 |
$events = $sink->get_events();
|
|
|
171 |
$this->assertCount(1, $events);
|
|
|
172 |
$event = array_shift($events);
|
|
|
173 |
|
|
|
174 |
// Checking that the event contains the expected values.
|
|
|
175 |
$this->assertInstanceOf('\mod_survey\event\response_submitted', $event);
|
|
|
176 |
$this->assertEquals($context, $event->get_context());
|
|
|
177 |
$this->assertEquals($survey->id, $event->other['surveyid']);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
public function test_survey_core_calendar_provide_event_action() {
|
|
|
181 |
$this->resetAfterTest();
|
|
|
182 |
$this->setAdminUser();
|
|
|
183 |
|
|
|
184 |
// Create the activity.
|
|
|
185 |
$course = $this->getDataGenerator()->create_course();
|
|
|
186 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
|
|
|
187 |
|
|
|
188 |
// Create a calendar event.
|
|
|
189 |
$event = $this->create_action_event($course->id, $survey->id,
|
|
|
190 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
191 |
|
|
|
192 |
// Create an action factory.
|
|
|
193 |
$factory = new \core_calendar\action_factory();
|
|
|
194 |
|
|
|
195 |
// Decorate action event.
|
|
|
196 |
$actionevent = mod_survey_core_calendar_provide_event_action($event, $factory);
|
|
|
197 |
|
|
|
198 |
// Confirm the event was decorated.
|
|
|
199 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
200 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
201 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
202 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
203 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
public function test_survey_core_calendar_provide_event_action_for_user() {
|
|
|
207 |
global $CFG;
|
|
|
208 |
|
|
|
209 |
$this->resetAfterTest();
|
|
|
210 |
$this->setAdminUser();
|
|
|
211 |
|
|
|
212 |
// Create the activity.
|
|
|
213 |
$course = $this->getDataGenerator()->create_course();
|
|
|
214 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
|
|
|
215 |
|
|
|
216 |
// Create a student and enrol into the course.
|
|
|
217 |
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
218 |
|
|
|
219 |
// Create a calendar event.
|
|
|
220 |
$event = $this->create_action_event($course->id, $survey->id,
|
|
|
221 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
222 |
|
|
|
223 |
// Now log out.
|
|
|
224 |
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
|
|
|
225 |
$this->setUser();
|
|
|
226 |
|
|
|
227 |
// Create an action factory.
|
|
|
228 |
$factory = new \core_calendar\action_factory();
|
|
|
229 |
|
|
|
230 |
// Decorate action event for the student.
|
|
|
231 |
$actionevent = mod_survey_core_calendar_provide_event_action($event, $factory, $student->id);
|
|
|
232 |
|
|
|
233 |
// Confirm the event was decorated.
|
|
|
234 |
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
|
|
|
235 |
$this->assertEquals(get_string('view'), $actionevent->get_name());
|
|
|
236 |
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
|
|
|
237 |
$this->assertEquals(1, $actionevent->get_item_count());
|
|
|
238 |
$this->assertTrue($actionevent->is_actionable());
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
public function test_survey_core_calendar_provide_event_action_as_non_user() {
|
|
|
242 |
global $CFG;
|
|
|
243 |
|
|
|
244 |
$this->resetAfterTest();
|
|
|
245 |
$this->setAdminUser();
|
|
|
246 |
|
|
|
247 |
// Create the activity.
|
|
|
248 |
$course = $this->getDataGenerator()->create_course();
|
|
|
249 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
|
|
|
250 |
|
|
|
251 |
// Create a calendar event.
|
|
|
252 |
$event = $this->create_action_event($course->id, $survey->id,
|
|
|
253 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
254 |
|
|
|
255 |
// Log out the user and set force login to true.
|
|
|
256 |
\core\session\manager::init_empty_session();
|
|
|
257 |
$CFG->forcelogin = true;
|
|
|
258 |
|
|
|
259 |
// Create an action factory.
|
|
|
260 |
$factory = new \core_calendar\action_factory();
|
|
|
261 |
|
|
|
262 |
// Decorate action event.
|
|
|
263 |
$actionevent = mod_survey_core_calendar_provide_event_action($event, $factory);
|
|
|
264 |
|
|
|
265 |
// Ensure result was null.
|
|
|
266 |
$this->assertNull($actionevent);
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
public function test_survey_core_calendar_provide_event_action_already_completed() {
|
|
|
270 |
global $CFG;
|
|
|
271 |
|
|
|
272 |
$this->resetAfterTest();
|
|
|
273 |
$this->setAdminUser();
|
|
|
274 |
|
|
|
275 |
$CFG->enablecompletion = 1;
|
|
|
276 |
|
|
|
277 |
// Create the activity.
|
|
|
278 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
279 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
|
|
|
280 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
281 |
|
|
|
282 |
// Get some additional data.
|
|
|
283 |
$cm = get_coursemodule_from_instance('survey', $survey->id);
|
|
|
284 |
|
|
|
285 |
// Create a calendar event.
|
|
|
286 |
$event = $this->create_action_event($course->id, $survey->id,
|
|
|
287 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
288 |
|
|
|
289 |
// Mark the activity as completed.
|
|
|
290 |
$completion = new \completion_info($course);
|
|
|
291 |
$completion->set_module_viewed($cm);
|
|
|
292 |
|
|
|
293 |
// Create an action factory.
|
|
|
294 |
$factory = new \core_calendar\action_factory();
|
|
|
295 |
|
|
|
296 |
// Decorate action event.
|
|
|
297 |
$actionevent = mod_survey_core_calendar_provide_event_action($event, $factory);
|
|
|
298 |
|
|
|
299 |
// Ensure result was null.
|
|
|
300 |
$this->assertNull($actionevent);
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
public function test_survey_core_calendar_provide_event_action_already_completed_for_user() {
|
|
|
304 |
global $CFG;
|
|
|
305 |
|
|
|
306 |
$this->resetAfterTest();
|
|
|
307 |
$this->setAdminUser();
|
|
|
308 |
|
|
|
309 |
$CFG->enablecompletion = 1;
|
|
|
310 |
|
|
|
311 |
// Create the activity.
|
|
|
312 |
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
|
|
313 |
$survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
|
|
|
314 |
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
|
|
|
315 |
|
|
|
316 |
// Create 2 students and enrol them into the course.
|
|
|
317 |
$student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
318 |
$student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
319 |
|
|
|
320 |
// Get some additional data.
|
|
|
321 |
$cm = get_coursemodule_from_instance('survey', $survey->id);
|
|
|
322 |
|
|
|
323 |
// Create a calendar event.
|
|
|
324 |
$event = $this->create_action_event($course->id, $survey->id,
|
|
|
325 |
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
|
|
|
326 |
|
|
|
327 |
// Mark the activity as completed for the $student1.
|
|
|
328 |
$completion = new \completion_info($course);
|
|
|
329 |
$completion->set_module_viewed($cm, $student1->id);
|
|
|
330 |
|
|
|
331 |
// Now log in as $student2.
|
|
|
332 |
$this->setUser($student2);
|
|
|
333 |
|
|
|
334 |
// Create an action factory.
|
|
|
335 |
$factory = new \core_calendar\action_factory();
|
|
|
336 |
|
|
|
337 |
// Decorate action event for $student1.
|
|
|
338 |
$actionevent = mod_survey_core_calendar_provide_event_action($event, $factory, $student1->id);
|
|
|
339 |
|
|
|
340 |
// Ensure result was null.
|
|
|
341 |
$this->assertNull($actionevent);
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
/**
|
|
|
345 |
* Creates an action event.
|
|
|
346 |
*
|
|
|
347 |
* @param int $courseid The course id.
|
|
|
348 |
* @param int $instanceid The instance id.
|
|
|
349 |
* @param string $eventtype The event type.
|
|
|
350 |
* @return bool|calendar_event
|
|
|
351 |
*/
|
|
|
352 |
private function create_action_event($courseid, $instanceid, $eventtype) {
|
|
|
353 |
$event = new \stdClass();
|
|
|
354 |
$event->name = 'Calendar event';
|
|
|
355 |
$event->modulename = 'survey';
|
|
|
356 |
$event->courseid = $courseid;
|
|
|
357 |
$event->instance = $instanceid;
|
|
|
358 |
$event->type = CALENDAR_EVENT_TYPE_ACTION;
|
|
|
359 |
$event->eventtype = $eventtype;
|
|
|
360 |
$event->timestart = time();
|
|
|
361 |
|
|
|
362 |
return \calendar_event::create($event);
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
/**
|
|
|
366 |
* Test the callback responsible for returning the completion rule descriptions.
|
|
|
367 |
* This function should work given either an instance of the module (cm_info), such as when checking the active rules,
|
|
|
368 |
* or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
|
|
|
369 |
*/
|
|
|
370 |
public function test_mod_survey_completion_get_active_rule_descriptions() {
|
|
|
371 |
$this->resetAfterTest();
|
|
|
372 |
$this->setAdminUser();
|
|
|
373 |
|
|
|
374 |
// Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
|
|
|
375 |
$course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
|
|
|
376 |
$survey1 = $this->getDataGenerator()->create_module('survey', [
|
|
|
377 |
'course' => $course->id,
|
|
|
378 |
'completion' => 2,
|
|
|
379 |
'completionsubmit' => 1,
|
|
|
380 |
]);
|
|
|
381 |
$survey2 = $this->getDataGenerator()->create_module('survey', [
|
|
|
382 |
'course' => $course->id,
|
|
|
383 |
'completion' => 2,
|
|
|
384 |
'completionsubmit' => 0,
|
|
|
385 |
]);
|
|
|
386 |
$cm1 = \cm_info::create(get_coursemodule_from_instance('survey', $survey1->id));
|
|
|
387 |
$cm2 = \cm_info::create(get_coursemodule_from_instance('survey', $survey2->id));
|
|
|
388 |
|
|
|
389 |
// Data for the stdClass input type.
|
|
|
390 |
// This type of input would occur when checking the default completion rules for an activity type, where we don't have
|
|
|
391 |
// any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
|
|
|
392 |
$moddefaults = new \stdClass();
|
|
|
393 |
$moddefaults->customdata = ['customcompletionrules' => ['completionsubmit' => 1]];
|
|
|
394 |
$moddefaults->completion = 2;
|
|
|
395 |
|
|
|
396 |
$activeruledescriptions = [get_string('completionsubmit', 'survey')];
|
|
|
397 |
$this->assertEquals(mod_survey_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
|
|
|
398 |
$this->assertEquals(mod_survey_get_completion_active_rule_descriptions($cm2), []);
|
|
|
399 |
$this->assertEquals(mod_survey_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
|
|
|
400 |
$this->assertEquals(mod_survey_get_completion_active_rule_descriptions(new \stdClass()), []);
|
|
|
401 |
}
|
|
|
402 |
}
|