Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / gapselect / tests / walkthrough_test.php
CommitLineData
0d24b17a 1<?php
0d24b17a
TH
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
0d24b17a 17/**
647d9373 18 * This file contains tests that simulate a user attempt a gapselect question.
0d24b17a 19 *
bbe27b2d
TH
20 * @package qtype_gapselect
21 * @copyright 2012 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
0d24b17a
TH
23 */
24
25
b28ad86a 26defined('MOODLE_INTERNAL') || die();
b3adccfb 27global $CFG;
b28ad86a 28
b3adccfb
JP
29require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
30require_once($CFG->dirroot . '/question/type/gapselect/tests/helper.php');
0d24b17a
TH
31
32
9df0480d
TH
33/**
34 * Unit tests for the gap-select question type.
35 *
bbe27b2d
TH
36 * @copyright 2012 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9df0480d 38 */
0d24b17a 39class qtype_gapselect_walkthrough_test extends qbehaviour_walkthrough_test_base {
43df6cac 40 public function test_interactive_behaviour() {
0d24b17a 41
43df6cac 42 // Create a gapselect question.
fdbff6ce 43 $q = test_question_maker::make_question('gapselect');
43df6cac 44 $q->hints = array(
7540fcdc
TH
45 new question_hint_with_parts(1, 'This is the first hint.', FORMAT_HTML, false, false),
46 new question_hint_with_parts(2, 'This is the second hint.', FORMAT_HTML, true, true),
43df6cac 47 );
7540fcdc 48 $q->shufflechoices = false;
43df6cac 49 $this->start_attempt_at_question($q, 'interactive', 3);
0d24b17a 50
43df6cac
TH
51 // Check the initial state.
52 $this->check_current_state(question_state::$todo);
53 $this->check_current_mark(null);
54 $this->check_current_output(
43df6cac
TH
55 $this->get_contains_submit_button_expectation(true),
56 $this->get_does_not_contain_feedback_expectation(),
57 $this->get_tries_remaining_expectation(3),
58 $this->get_no_hint_visible_expectation());
0d3be6d6
JB
59 // Note it is possible to check the first select option as below, but it is not required.
60 // Also note the ' ' in the p2 example below is a nbsp (used when names are short).
61 $this->check_output_contains_selectoptions(
62 $this->get_contains_select_expectation('p1',
3e697596 63 ['0' => '&nbsp;', '1' => 'quick', '2' => 'slow'], null, true),
0d3be6d6 64 $this->get_contains_select_expectation('p2',
3e697596 65 ['0' => '&nbsp;', '1' => 'fox', '2' => 'dog'], null, true),
0d3be6d6
JB
66 $this->get_contains_select_expectation('p3',
67 ['1' => 'lazy', '2' => 'assiduous'], null, true));
43df6cac
TH
68
69 // Save the wrong answer.
70 $this->process_submission(array('p1' => '2', 'p2' => '2', 'p3' => '2'));
71
72 // Verify.
73 $this->check_current_state(question_state::$todo);
74 $this->check_current_mark(null);
75 $this->check_current_output(
43df6cac
TH
76 $this->get_contains_submit_button_expectation(true),
77 $this->get_does_not_contain_correctness_expectation(),
78 $this->get_does_not_contain_feedback_expectation(),
79 $this->get_tries_remaining_expectation(3),
80 $this->get_no_hint_visible_expectation());
0d3be6d6
JB
81 $this->check_output_contains_selectoptions(
82 $this->get_contains_select_expectation('p1',
83 ['1' => 'quick', '2' => 'slow'], 2, true),
84 $this->get_contains_select_expectation('p2',
85 ['1' => 'fox', '2' => 'dog'], 2, true),
86 $this->get_contains_select_expectation('p3',
87 ['1' => 'lazy', '2' => 'assiduous'], 2, true));
43df6cac
TH
88
89 // Submit the wrong answer.
90 $this->process_submission(array('p1' => '2', 'p2' => '2', 'p3' => '2', '-submit' => 1));
91
92 // Verify.
93 $this->check_current_state(question_state::$todo);
94 $this->check_current_mark(null);
95 $this->check_current_output(
0d3be6d6 96 $this->get_does_not_contain_submit_button_expectation(),
43df6cac
TH
97 $this->get_contains_try_again_button_expectation(true),
98 $this->get_does_not_contain_correctness_expectation(),
43df6cac 99 $this->get_contains_hint_expectation('This is the first hint'));
0d3be6d6
JB
100 $this->check_output_contains_selectoptions(
101 $this->get_contains_select_expectation('p1',
102 ['1' => 'quick', '2' => 'slow'], 2, false),
103 $this->get_contains_select_expectation('p2',
104 ['1' => 'fox', '2' => 'dog'], 2, false),
105 $this->get_contains_select_expectation('p3',
106 ['1' => 'lazy', '2' => 'assiduous'], 2, false));
43df6cac
TH
107
108 // Do try again.
109 $this->process_submission(array('-tryagain' => 1));
110
111 // Verify.
112 $this->check_current_state(question_state::$todo);
113 $this->check_current_mark(null);
114 $this->check_current_output(
43df6cac
TH
115 $this->get_contains_submit_button_expectation(true),
116 $this->get_does_not_contain_correctness_expectation(),
117 $this->get_does_not_contain_feedback_expectation(),
118 $this->get_tries_remaining_expectation(2),
119 $this->get_no_hint_visible_expectation());
0d3be6d6
JB
120 $this->check_output_contains_selectoptions(
121 $this->get_contains_select_expectation('p1',
122 ['1' => 'quick', '2' => 'slow'], 2, true),
123 $this->get_contains_select_expectation('p2',
124 ['1' => 'fox', '2' => 'dog'], 2, true),
125 $this->get_contains_select_expectation('p3',
126 ['1' => 'lazy', '2' => 'assiduous'], 2, true));
43df6cac
TH
127
128 // Submit the right answer.
129 $this->process_submission(array('p1' => '1', 'p2' => '1', 'p3' => '1', '-submit' => 1));
130
131 // Verify.
132 $this->check_current_state(question_state::$gradedright);
133 $this->check_current_mark(2);
134 $this->check_current_output(
81e47a35 135 $this->get_does_not_contain_submit_button_expectation(),
43df6cac
TH
136 $this->get_contains_correct_expectation(),
137 $this->get_no_hint_visible_expectation());
0d3be6d6
JB
138 $this->check_output_contains_selectoptions(
139 $this->get_contains_select_expectation('p1',
140 ['1' => 'quick', '2' => 'slow'], 1, false),
141 $this->get_contains_select_expectation('p2',
142 ['1' => 'fox', '2' => 'dog'], 1, false),
143 $this->get_contains_select_expectation('p3',
144 ['1' => 'lazy', '2' => 'assiduous'], 1, false));
43df6cac
TH
145
146 // Check regrading does not mess anything up.
147 $this->quba->regrade_all_questions();
148
149 // Verify.
150 $this->check_current_state(question_state::$gradedright);
151 $this->check_current_mark(2);
152 }
adb21080
LB
153
154 public function test_multilang_behaviour() {
155
156 // Enable multilang filter to on content and heading.
157 filter_set_global_state('multilang', TEXTFILTER_ON);
158 filter_set_applies_to_strings('multilang', 1);
159 $filtermanager = filter_manager::instance();
160 $filtermanager->reset_caches();
161
162 // Create a multilang gapselect question.
fdbff6ce 163 $q = test_question_maker::make_question('gapselect', 'multilang');
adb21080
LB
164 $q->shufflechoices = false;
165 $this->start_attempt_at_question($q, 'interactive', 3);
166
167 // Check the initial state.
168 $this->check_current_state(question_state::$todo);
169 $this->check_current_mark(null);
0d3be6d6 170 $this->check_output_contains_selectoptions(
adb21080 171 $this->get_contains_select_expectation('p1',
0d3be6d6 172 ['1' => 'cat', '2' => 'dog'], null, true),
adb21080 173 $this->get_contains_select_expectation('p2',
0d3be6d6 174 ['1' => 'mat', '2' => 'bat'], null, true));
adb21080 175 }
410a088a
JB
176
177 public function test_choices_containing_dollars() {
178
179 // Choices with a currency like entry (e.g. $3) should display.
fdbff6ce 180 $q = test_question_maker::make_question('gapselect', 'currency');
410a088a
JB
181 $q->shufflechoices = false;
182 $this->start_attempt_at_question($q, 'interactive', 1);
183
184 // Check the initial state.
185 $this->check_current_state(question_state::$todo);
186 $this->check_current_mark(null);
187 $html = $this->quba->render_question($this->slot, $this->displayoptions);
188 preg_match_all('/<option value="([^>]*)">([^<]*)<\/option>/', $html, $matches);
189 $this->assertEquals('$2', $matches[2][1]);
190 $this->assertEquals('$3', $matches[2][2]);
191 $this->assertEquals('$4.99', $matches[2][3]);
192 $this->assertEquals('-1', $matches[2][4]);
193 }
0d24b17a 194}