MDL-47494 ddwtos: Split question_definition::init_first_step into start_attempt and...
[moodle.git] / question / type / ddwtos / simpletest / testquestion.php
blob9bce14087fafeadc6944b43f309300b1862692b4
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Unit tests for the drag-and-drop words into sentences question definition class.
21 * @package qtype
22 * @subpackage ddwtos
23 * @copyright 2010 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
31 require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php');
34 /**
35 * Unit tests for the matching question definition class.
37 * @copyright 2009 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class qtype_ddwtos_question_test extends UnitTestCase {
42 public function test_get_question_summary() {
43 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
44 $this->assertEqual('The [[1]] brown [[2]] jumped over the [[3]] dog.; [[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
45 $dd->get_question_summary());
48 public function test_get_question_summary_maths() {
49 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
50 $this->assertEqual('Fill in the operators to make this equation work: ' .
51 '7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
52 $dd->get_question_summary());
55 public function test_summarise_response() {
56 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
57 $dd->shufflechoices = false;
58 $dd->start_attempt(new question_attempt_step());
60 $this->assertEqual('{quick} {fox} {lazy}',
61 $dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
64 public function test_summarise_response_maths() {
65 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
66 $dd->shufflechoices = false;
67 $dd->start_attempt(new question_attempt_step());
69 $this->assertEqual('{+} {-} {+} {-}',
70 $dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
73 public function test_get_random_guess_score() {
74 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
75 $this->assertEqual(0.5, $dd->get_random_guess_score());
78 public function test_get_random_guess_score_maths() {
79 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
80 $this->assertEqual(0.25, $dd->get_random_guess_score());
83 public function test_get_right_choice_for() {
84 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
85 $dd->shufflechoices = false;
86 $dd->start_attempt(new question_attempt_step());
88 $this->assertEqual(1, $dd->get_right_choice_for(1));
89 $this->assertEqual(1, $dd->get_right_choice_for(2));
92 public function test_get_right_choice_for_maths() {
93 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
94 $dd->shufflechoices = false;
95 $dd->start_attempt(new question_attempt_step());
97 $this->assertEqual(1, $dd->get_right_choice_for(1));
98 $this->assertEqual(2, $dd->get_right_choice_for(2));
101 public function test_clear_wrong_from_response() {
102 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
103 $dd->shufflechoices = false;
104 $dd->start_attempt(new question_attempt_step());
106 $initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
107 $this->assertEqual(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
108 $dd->clear_wrong_from_response($initialresponse));
111 public function test_get_num_parts_right() {
112 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
113 $dd->shufflechoices = false;
114 $dd->start_attempt(new question_attempt_step());
116 $this->assertEqual(array(2, 3),
117 $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
118 $this->assertEqual(array(3, 3),
119 $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
122 public function test_get_num_parts_right_maths() {
123 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
124 $dd->shufflechoices = false;
125 $dd->start_attempt(new question_attempt_step());
127 $this->assertEqual(array(2, 4),
128 $dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
131 public function test_get_expected_data() {
132 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
133 $dd->start_attempt(new question_attempt_step());
135 $this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
136 $dd->get_expected_data());
139 public function test_get_correct_response() {
140 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
141 $dd->shufflechoices = false;
142 $dd->start_attempt(new question_attempt_step());
144 $this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
145 $dd->get_correct_response());
148 public function test_get_correct_response_maths() {
149 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
150 $dd->shufflechoices = false;
151 $dd->start_attempt(new question_attempt_step());
153 $this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
154 $dd->get_correct_response());
157 public function test_is_same_response() {
158 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
159 $dd->start_attempt(new question_attempt_step());
161 $this->assertTrue($dd->is_same_response(
162 array(),
163 array('p1' => '0', 'p2' => '0', 'p3' => '0')));
165 $this->assertFalse($dd->is_same_response(
166 array(),
167 array('p1' => '1', 'p2' => '0', 'p3' => '0')));
169 $this->assertFalse($dd->is_same_response(
170 array('p1' => '0', 'p2' => '0', 'p3' => '0'),
171 array('p1' => '1', 'p2' => '0', 'p3' => '0')));
173 $this->assertTrue($dd->is_same_response(
174 array('p1' => '1', 'p2' => '2', 'p3' => '3'),
175 array('p1' => '1', 'p2' => '2', 'p3' => '3')));
177 $this->assertFalse($dd->is_same_response(
178 array('p1' => '1', 'p2' => '2', 'p3' => '3'),
179 array('p1' => '1', 'p2' => '2', 'p3' => '2')));
181 public function test_is_complete_response() {
182 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
183 $dd->start_attempt(new question_attempt_step());
185 $this->assertFalse($dd->is_complete_response(array()));
186 $this->assertFalse($dd->is_complete_response(
187 array('p1' => '1', 'p2' => '1', 'p3' => '0')));
188 $this->assertFalse($dd->is_complete_response(array('p1' => '1')));
189 $this->assertTrue($dd->is_complete_response(
190 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
193 public function test_is_gradable_response() {
194 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
195 $dd->start_attempt(new question_attempt_step());
197 $this->assertFalse($dd->is_gradable_response(array()));
198 $this->assertFalse($dd->is_gradable_response(
199 array('p1' => '0', 'p2' => '0', 'p3' => '0')));
200 $this->assertTrue($dd->is_gradable_response(
201 array('p1' => '1', 'p2' => '1', 'p3' => '0')));
202 $this->assertTrue($dd->is_gradable_response(array('p1' => '1')));
203 $this->assertTrue($dd->is_gradable_response(
204 array('p1' => '1', 'p2' => '1', 'p3' => '1')));
207 public function test_grading() {
208 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
209 $dd->shufflechoices = false;
210 $dd->start_attempt(new question_attempt_step());
212 $this->assertEqual(array(1, question_state::$gradedright),
213 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
214 $this->assertEqual(array(1/3, question_state::$gradedpartial),
215 $dd->grade_response(array('p1' => '1')));
216 $this->assertEqual(array(0, question_state::$gradedwrong),
217 $dd->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
220 public function test_grading_maths() {
221 $dd = qtype_ddwtos_test_helper::make_a_maths_ddwtos_question();
222 $dd->shufflechoices = false;
223 $dd->start_attempt(new question_attempt_step());
225 $this->assertEqual(array(1, question_state::$gradedright),
226 $dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
227 $this->assertEqual(array(0.5, question_state::$gradedpartial),
228 $dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
229 $this->assertEqual(array(0, question_state::$gradedwrong),
230 $dd->grade_response(array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
233 public function test_classify_response() {
234 $dd = qtype_ddwtos_test_helper::make_a_ddwtos_question();
235 $dd->shufflechoices = false;
236 $dd->start_attempt(new question_attempt_step());
238 $this->assertEqual(array(
239 1 => new question_classified_response(1, 'quick', 1),
240 2 => new question_classified_response(2, 'dog', 0),
241 3 => new question_classified_response(1, 'lazy', 1),
242 ), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
243 $this->assertEqual(array(
244 1 => question_classified_response::no_response(),
245 2 => new question_classified_response(1, 'fox', 1),
246 3 => new question_classified_response(2, 'assiduous', 0),
247 ), $dd->classify_response(array('p1' => '0', 'p2' => '1', 'p3' => '2')));