Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
namespace quiz_responses;
18
 
19
use mod_quiz\quiz_attempt;
20
use question_bank;
21
 
22
/**
23
 * Quiz attempt walk through using data from csv file.
24
 *
25
 * @package    quiz_responses
26
 * @category   test
27
 * @copyright  2013 The Open University
28
 * @author     Jamie Pratt <me@jamiep.org>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class responses_from_steps_walkthrough_test extends \mod_quiz\tests\attempt_walkthrough_testcase {
32
    #[\Override]
33
    public static function setUpBeforeClass(): void {
34
        global $CFG;
35
 
36
        parent::setUpBeforeClass();
37
 
38
        require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
39
        require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
1 efrain 40
    }
41
 
1441 ariadna 42
    #[\Override]
43
    protected static function get_test_files(): array {
44
        return ['questions', 'steps', 'responses'];
45
    }
1 efrain 46
 
47
    /**
48
     * Create a quiz add questions to it, walk through quiz attempts and then check results.
49
     *
50
     * @param array $quizsettings settings to override default settings for quiz created by generator. Taken from quizzes.csv.
51
     * @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "responsesXX.csv".
1441 ariadna 52
     * // phpcs:ignore moodle.PHPUnit.TestCaseProvider.dataProviderSyntaxMethodNotFound
1 efrain 53
     * @dataProvider get_data_for_walkthrough
54
     */
11 efrain 55
    public function test_walkthrough_from_csv($quizsettings, $csvdata): void {
1 efrain 56
        $this->resetAfterTest(true);
57
        question_bank::get_qtype('random')->clear_caches_before_testing();
58
 
59
        $this->create_quiz($quizsettings, $csvdata['questions']);
60
 
61
        $quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
62
 
63
        foreach ($csvdata['responses'] as $responsesfromcsv) {
64
            $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
65
 
66
            if (!isset($quizattemptids[$responses['quizattempt']])) {
67
                throw new \coding_exception("There is no quizattempt {$responses['quizattempt']}!");
68
            }
69
            $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses);
70
        }
71
    }
72
 
1441 ariadna 73
    /**
74
     * Helper to assert a response.
75
     *
76
     * @param mixed $quizattemptid
77
     * @param mixed $responses
78
     * @throws \coding_exception
79
     */
80
    protected function assert_response_test($quizattemptid, $responses): void {
1 efrain 81
        $quizattempt = quiz_attempt::create($quizattemptid);
82
 
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) {
1441 ariadna 97
                throw new \coding_exception("There is no step no {$responses['submittedstepno']} " .
1 efrain 98
                                           "for slot $slot in quizattempt {$responses['quizattempt']}!");
99
            }
100
            foreach (['responsesummary', 'fraction', 'state'] as $column) {
101
                if (isset($tests[$column]) && $tests[$column] != '') {
1441 ariadna 102
                    switch ($column) {
103
                        case 'responsesummary':
1 efrain 104
                            $actual = $qa->get_question()->summarise_response($step->get_qt_data());
105
                            break;
1441 ariadna 106
                        case 'fraction':
1 efrain 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
                            }
1441 ariadna 114
                            break;
115
                        case 'state':
1 efrain 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];
1441 ariadna 126
                    $failuremessage = "Error in  quizattempt {$responses['quizattempt']} in $column, slot $slot, " .
1 efrain 127
                    "submittedstepno {$responses['submittedstepno']}";
128
                    $this->assertEquals($expected, $actual, $failuremessage);
129
                }
130
            }
131
        }
132
    }
133
}