MDL-37506 question code: incorrect preg_quote uses.
[moodle.git] / question / behaviour / interactivecountback / tests / walkthrough_test.php
CommitLineData
603bd001
PS
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 * 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 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once(dirname(__FILE__) . '/../../../engine/lib.php');
32require_once(dirname(__FILE__) . '/../../../engine/tests/helpers.php');
33
34
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 */
41class qbehaviour_interactivecountback_walkthrough_test extends qbehaviour_walkthrough_test_base {
42 public function test_interactive_feedback_match_reset() {
43
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);
52
53 $choiceorder = $m->get_choice_order();
54 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
55 $choices = array(0 => get_string('choose') . '...');
56 foreach ($choiceorder as $key => $choice) {
57 $choices[$key] = $m->choices[$choice];
58 }
59
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_select_expectation('sub0', $choices, null, true),
67 $this->get_contains_select_expectation('sub1', $choices, null, true),
68 $this->get_contains_select_expectation('sub2', $choices, null, true),
69 $this->get_contains_select_expectation('sub3', $choices, null, true),
70 $this->get_contains_question_text_expectation($m),
71 $this->get_contains_submit_button_expectation(true),
72 $this->get_does_not_contain_feedback_expectation(),
73 $this->get_tries_remaining_expectation(3),
74 $this->get_does_not_contain_num_parts_correct(),
75 $this->get_no_hint_visible_expectation());
76
77 // Submit an answer with two right, and two wrong.
78 $this->process_submission(array('sub0' => $orderforchoice[1],
79 'sub1' => $orderforchoice[1], 'sub2' => $orderforchoice[1],
80 'sub3' => $orderforchoice[1], '-submit' => 1));
81
82 // Verify.
83 $this->check_current_state(question_state::$todo);
84 $this->check_current_mark(null);
85 $this->check_current_output(
86 $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], false),
87 $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[1], false),
88 $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[1], false),
89 $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false),
90 $this->get_contains_submit_button_expectation(false),
91 $this->get_contains_try_again_button_expectation(true),
92 $this->get_does_not_contain_correctness_expectation(),
93 new question_pattern_expectation('/' .
03cefcc9 94 preg_quote(get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
603bd001
PS
95 $this->get_contains_hint_expectation('This is the first hint'),
96 $this->get_contains_num_parts_correct(2),
97 $this->get_contains_standard_partiallycorrect_combined_feedback_expectation(),
98 $this->get_contains_hidden_expectation(
99 $this->quba->get_field_prefix($this->slot) . 'sub0', $orderforchoice[1]),
100 $this->get_contains_hidden_expectation(
101 $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
102 $this->get_contains_hidden_expectation(
103 $this->quba->get_field_prefix($this->slot) . 'sub2', '0'),
104 $this->get_contains_hidden_expectation(
105 $this->quba->get_field_prefix($this->slot) . 'sub3', $orderforchoice[1]));
106
107 // Check that extract responses will return the reset data.
108 $prefix = $this->quba->get_field_prefix($this->slot);
109 $this->assertEquals(array('sub0' => 1),
110 $this->quba->extract_responses($this->slot, array($prefix . 'sub0' => 1)));
111
112 // Do try again.
113 $this->process_submission(array('sub0' => $orderforchoice[1],
114 'sub3' => $orderforchoice[1], '-tryagain' => 1));
115
116 // Verify.
117 $this->check_current_state(question_state::$todo);
118 $this->check_current_mark(null);
119 $this->check_current_output(
120 $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], true),
121 $this->get_contains_select_expectation('sub1', $choices, null, true),
122 $this->get_contains_select_expectation('sub2', $choices, null, true),
123 $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], true),
124 $this->get_contains_submit_button_expectation(true),
125 $this->get_does_not_contain_correctness_expectation(),
126 $this->get_does_not_contain_feedback_expectation(),
127 $this->get_tries_remaining_expectation(2),
128 $this->get_no_hint_visible_expectation());
129
130 // Submit the right answer.
131 $this->process_submission(array('sub0' => $orderforchoice[1],
132 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
133 'sub3' => $orderforchoice[1], '-submit' => 1));
134
135 // Verify.
136 $this->check_current_state(question_state::$gradedright);
137 $this->check_current_mark(10);
138 $this->check_current_output(
139 $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[1], false),
140 $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[2], false),
141 $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[2], false),
142 $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[1], false),
143 $this->get_contains_submit_button_expectation(false),
144 $this->get_does_not_contain_try_again_button_expectation(),
145 $this->get_contains_correct_expectation(),
146 $this->get_contains_standard_correct_combined_feedback_expectation(),
147 new question_no_pattern_expectation('/class="control\b[^"]*\bpartiallycorrect"/'));
148 }
149}