MDL-51629 mod_survey: New WS mod_survey_submit_answers
[moodle.git] / mod / survey / tests / lib_test.php
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/>.
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  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Unit tests for mod_survey lib
32  *
33  * @package    mod_survey
34  * @category   external
35  * @copyright  2015 Juan Leyva <juan@moodle.com>
36  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  * @since      Moodle 3.0
38  */
39 class mod_survey_lib_testcase extends advanced_testcase {
41     /**
42      * Prepares things before this test case is initialised
43      * @return void
44      */
45     public static function setUpBeforeClass() {
46         global $CFG;
47         require_once($CFG->dirroot . '/mod/survey/lib.php');
48     }
50     /**
51      * Test survey_view
52      * @return void
53      */
54     public function test_survey_view() {
55         global $CFG;
57         $CFG->enablecompletion = 1;
58         $this->resetAfterTest();
60         $this->setAdminUser();
61         // Setup test data.
62         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
63         $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id),
64                                                             array('completion' => 2, 'completionview' => 1));
65         $context = context_module::instance($survey->cmid);
66         $cm = get_coursemodule_from_instance('survey', $survey->id);
68         // Trigger and capture the event.
69         $sink = $this->redirectEvents();
71         survey_view($survey, $course, $cm, $context, 'form');
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_survey\event\course_module_viewed', $event);
80         $this->assertEquals($context, $event->get_context());
81         $moodleurl = new \moodle_url('/mod/survey/view.php', array('id' => $cm->id));
82         $this->assertEquals($moodleurl, $event->get_url());
83         $this->assertEquals('form', $event->other['viewed']);
84         $this->assertEventContextNotUsed($event);
85         $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);
91     }
93     /**
94      * Test survey_order_questions
95      */
96     public function test_survey_order_questions() {
97         global $DB;
99         $this->resetAfterTest();
100         $course = $this->getDataGenerator()->create_course();
101         $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
103         $orderedquestionids = explode(',', $survey->questions);
104         $surveyquestions = $DB->get_records_list("survey_questions", "id", $orderedquestionids);
106         $questionsordered = survey_order_questions($surveyquestions, $orderedquestionids);
108         // Check one by one the correct order.
109         for ($i = 0; $i < count($orderedquestionids); $i++) {
110             $this->assertEquals($orderedquestionids[$i], $questionsordered[$i]->id);
111         }
112     }
114     /**
115      * Test survey_save_answers
116      */
117     public function test_survey_save_answers() {
118         global $DB;
120         $this->resetAfterTest();
121         $this->setAdminUser();
123         // Setup test data.
124         $course = $this->getDataGenerator()->create_course();
125         $survey = $this->getDataGenerator()->create_module('survey', array('course' => $course->id));
126         $context = context_module::instance($survey->cmid);
128         // Build our questions and responses array.
129         $realquestions = array();
130         $questions = survey_get_questions($survey);
131         $i = 5;
132         foreach ($questions as $q) {
133             if ($q->type > 0) {
134                 if ($q->multi) {
135                     $subquestions = survey_get_subquestions($q);
136                     foreach ($subquestions as $sq) {
137                         $key = 'q' . $sq->id;
138                         $realquestions[$key] = $i % 5 + 1;
139                         $i++;
140                     }
141                 } else {
142                     $key = 'q' . $q->id;
143                     $realquestions[$key] = $i % 5 + 1;
144                     $i++;
145                 }
146             }
147         }
149         $sink = $this->redirectEvents();
150         survey_save_answers($survey, $realquestions, $course, $context);
152         // Check the stored answers, they must match.
153         $dbanswers = $DB->get_records_menu('survey_answers', array('survey' => $survey->id), '', 'question, answer1');
154         foreach ($realquestions as $key => $value) {
155             $id = str_replace('q', '', $key);
156             $this->assertEquals($value, $dbanswers[$id]);
157         }
159         // Check events.
160         $events = $sink->get_events();
161         $this->assertCount(1, $events);
162         $event = array_shift($events);
164         // Checking that the event contains the expected values.
165         $this->assertInstanceOf('\mod_survey\event\response_submitted', $event);
166         $this->assertEquals($context, $event->get_context());
167         $this->assertEquals($survey->id, $event->other['surveyid']);
168     }