MDL-64420 qtype: gapselect unit tests unsafe
[moodle.git] / question / behaviour / interactivecountback / tests / 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  * This file contains tests that walks a question through the interactive with
19  * countback behaviour.
20  *
21  * @package    qbehaviour
22  * @subpackage interactivecountback
23  * @copyright  2009 The Open University
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once(__DIR__ . '/../../../engine/lib.php');
32 require_once(__DIR__ . '/../../../engine/tests/helpers.php');
35 /**
36  * Unit tests for the interactive with countback behaviour.
37  *
38  * @copyright  2009 The Open University
39  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class qbehaviour_interactivecountback_walkthrough_test extends qbehaviour_walkthrough_test_base {
42     public function test_interactive_feedback_match_reset() {
44         // Create a matching question.
45         $m = test_question_maker::make_a_matching_question();
46         $m->shufflestems = false;
47         $m->hints = array(
48             new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true),
49             new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true),
50         );
51         $this->start_attempt_at_question($m, 'interactive', 12);
53         $choiceorder = $m->get_choice_order();
54         $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
55         $choices = [];
56         foreach ($choiceorder as $key => $choice) {
57             $choices[$key] = $m->choices[$choice];
58         }
60         // Check the initial state.
61         $this->check_current_state(question_state::$todo);
62         $this->check_current_mark(null);
63         $this->assertEquals('interactivecountback',
64                 $this->quba->get_question_attempt($this->slot)->get_behaviour_name());
65         $this->check_current_output(
66                 $this->get_contains_question_text_expectation($m),
67                 $this->get_contains_submit_button_expectation(true),
68                 $this->get_does_not_contain_feedback_expectation(),
69                 $this->get_tries_remaining_expectation(3),
70                 $this->get_does_not_contain_num_parts_correct(),
71                 $this->get_no_hint_visible_expectation());
72         $this->check_output_contains_selectoptions(
73                 $this->get_contains_select_expectation('sub0', $choices, null, true),
74                 $this->get_contains_select_expectation('sub1', $choices, null, true),
75                 $this->get_contains_select_expectation('sub2', $choices, null, true),
76                 $this->get_contains_select_expectation('sub3', $choices, null, true));
78         // Submit an answer with two right, and two wrong.
79         $this->process_submission(array('sub0' => $orderforchoice[1],
80                 'sub1' => $orderforchoice[1], 'sub2' => $orderforchoice[1],
81                 'sub3' => $orderforchoice[1], '-submit' => 1));
83         // Verify.
84         $this->check_current_state(question_state::$todo);
85         $this->check_current_mark(null);
86         $this->check_current_output(
87                 $this->get_does_not_contain_submit_button_expectation(),
88                 $this->get_contains_try_again_button_expectation(true),
89                 $this->get_does_not_contain_correctness_expectation(),
90                 new question_pattern_expectation('/Tries remaining: 2/'),
91                 $this->get_contains_hint_expectation('This is the first hint'),
92                 $this->get_contains_num_parts_correct(2),
93                 $this->get_contains_standard_partiallycorrect_combined_feedback_expectation(),
94                 $this->get_contains_hidden_expectation(
95                         $this->quba->get_field_prefix($this->slot) . 'sub0', $orderforchoice[1]),
96                 $this->get_contains_hidden_expectation(
97                         $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
98                 $this->get_contains_hidden_expectation(
99                         $this->quba->get_field_prefix($this->slot) . 'sub2', '0'),
100                 $this->get_contains_hidden_expectation(
101                         $this->quba->get_field_prefix($this->slot) . 'sub3', $orderforchoice[1]));
102         $this->check_output_contains_selectoptions(
103                 $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], false),
104                 $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[1], false),
105                 $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[1], false),
106                 $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false));
108         // Check that extract responses will return the reset data.
109         $prefix = $this->quba->get_field_prefix($this->slot);
110         $this->assertEquals(array('sub0' => 1),
111                 $this->quba->extract_responses($this->slot, array($prefix . 'sub0' => 1)));
113         // Do try again.
114         $this->process_submission(array('sub0' => $orderforchoice[1],
115                 'sub3' => $orderforchoice[1], '-tryagain' => 1));
117         // Verify.
118         $this->check_current_state(question_state::$todo);
119         $this->check_current_mark(null);
120         $this->check_current_output(
121                 $this->get_contains_submit_button_expectation(true),
122                 $this->get_does_not_contain_correctness_expectation(),
123                 $this->get_does_not_contain_feedback_expectation(),
124                 $this->get_tries_remaining_expectation(2),
125                 $this->get_no_hint_visible_expectation());
126         $this->check_output_contains_selectoptions(
127                 $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], true),
128                 $this->get_contains_select_expectation('sub1', $choices, null, true),
129                 $this->get_contains_select_expectation('sub2', $choices, null, true),
130                 $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], true));
132         // Submit the right answer.
133         $this->process_submission(array('sub0' => $orderforchoice[1],
134                 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
135                 'sub3' => $orderforchoice[1], '-submit' => 1));
137         // Verify.
138         $this->check_current_state(question_state::$gradedright);
139         $this->check_current_mark(10);
140         $this->check_current_output(
141                 $this->get_does_not_contain_submit_button_expectation(),
142                 $this->get_does_not_contain_try_again_button_expectation(),
143                 $this->get_contains_correct_expectation(),
144                 $this->get_contains_standard_correct_combined_feedback_expectation(),
145                 new question_no_pattern_expectation('/class="control\b[^"]*\bpartiallycorrect"/'));
146         $this->check_output_contains_selectoptions(
147                 $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], false),
148                 $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[2], false),
149                 $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[2], false),
150                 $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false));
151     }