2b63724e056cf3cbaab4bea7bf9aadd53510b39a
[moodle.git] / mod / quiz / report / responses / tests / responses_from_steps_walkthrough_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  * Quiz attempt walk through using data from csv file.
19  *
20  * @package    quiz_statistics
21  * @category   phpunit
22  * @copyright  2013 The Open University
23  * @author     Jamie Pratt <me@jamiep.org>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/mod/quiz/tests/attempt_walkthrough_from_csv_test.php');
31 require_once($CFG->dirroot . '/mod/quiz/report/default.php');
32 require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
33 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
35 /**
36  * Quiz attempt walk through using data from csv file.
37  *
38  * @package    quiz_statistics
39  * @category   phpunit
40  * @copyright  2013 The Open University
41  * @author     Jamie Pratt <me@jamiep.org>
42  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43  */
44 class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthrough_from_csv_testcase {
45     protected function get_full_path_of_csv_file($setname, $test) {
46         // Overridden here so that __DIR__ points to the path of this file.
47         return  __DIR__."/fixtures/{$setname}{$test}.csv";
48     }
50     protected $files = array('questions', 'steps', 'responses');
52     /**
53      * Create a quiz add questions to it, walk through quiz attempts and then check results.
54      *
55      * @param array $quizsettings settings to override default settings for quiz created by generator. Taken from quizzes.csv.
56      * @param PHPUnit\DbUnit\DataSet\ITable[] $csvdata of data read from csv file "questionsXX.csv",
57      *                                                                                  "stepsXX.csv" and "responsesXX.csv".
58      * @dataProvider get_data_for_walkthrough
59      */
60     public function test_walkthrough_from_csv($quizsettings, $csvdata) {
62         $this->resetAfterTest(true);
63         question_bank::get_qtype('random')->clear_caches_before_testing();
65         $this->create_quiz($quizsettings, $csvdata['questions']);
67         $quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
69         for ($rowno = 0; $rowno < $csvdata['responses']->getRowCount(); $rowno++) {
70             $responsesfromcsv = $csvdata['responses']->getRow($rowno);
71             $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
73             if (!isset($quizattemptids[$responses['quizattempt']])) {
74                 throw new coding_exception("There is no quizattempt {$responses['quizattempt']}!");
75             }
76             $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses);
77         }
78     }
80     protected function assert_response_test($quizattemptid, $responses) {
81         $quizattempt = quiz_attempt::create($quizattemptid);
83         foreach ($responses['slot'] as $slot => $tests) {
84             $slothastests = false;
85             foreach ($tests as $test) {
86                 if ('' !== $test) {
87                     $slothastests = true;
88                 }
89             }
90             if (!$slothastests) {
91                 continue;
92             }
93             $qa = $quizattempt->get_question_attempt($slot);
94             $stepswithsubmit = $qa->get_steps_with_submitted_response_iterator();
95             $step = $stepswithsubmit[$responses['submittedstepno']];
96             if (null === $step) {
97                 throw new coding_exception("There is no step no {$responses['submittedstepno']} ".
98                                            "for slot $slot in quizattempt {$responses['quizattempt']}!");
99             }
100             foreach (array('responsesummary', 'fraction', 'state') as $column) {
101                 if (isset($tests[$column]) && $tests[$column] != '') {
102                     switch($column) {
103                         case 'responsesummary' :
104                             $actual = $qa->get_question()->summarise_response($step->get_qt_data());
105                             break;
106                         case 'fraction' :
107                             if (count($stepswithsubmit) == $responses['submittedstepno']) {
108                                 // If this is the last step then we need to look at the fraction after the question has been
109                                 // finished.
110                                 $actual = $qa->get_fraction();
111                             } else {
112                                 $actual = $step->get_fraction();
113                             }
114                            break;
115                         case 'state' :
116                             if (count($stepswithsubmit) == $responses['submittedstepno']) {
117                                 // If this is the last step then we need to look at the state after the question has been
118                                 // finished.
119                                 $state = $qa->get_state();
120                             } else {
121                                 $state = $step->get_state();
122                             }
123                             $actual = substr(get_class($state), strlen('question_state_'));
124                     }
125                     $expected = $tests[$column];
126                     $failuremessage = "Error in  quizattempt {$responses['quizattempt']} in $column, slot $slot, ".
127                     "submittedstepno {$responses['submittedstepno']}";
128                     $this->assertEquals($expected, $actual, $failuremessage);
129                 }
130             }
131         }
132     }