MDL-62880 qtype_ddwtos: drop support for legacy import format
[moodle.git] / question / type / ddwtos / tests / questiontype_test.php
CommitLineData
61381e50 1<?php
61381e50
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
61381e50
TH
17/**
18 * Unit tests for the drag-and-drop words into sentences question definition class.
19 *
025589c7
TH
20 * @package qtype_ddwtos
21 * @copyright 2012 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50
TH
23 */
24
25
9d55f4a5 26defined('MOODLE_INTERNAL') || die();
543e4dee 27global $CFG;
9d55f4a5 28
543e4dee
JP
29require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
30require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
61381e50
TH
31
32
33/**
34 * Unit tests for the drag-and-drop words into sentences question definition class.
35 *
025589c7
TH
36 * @copyright 2012 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61381e50 38 */
543e4dee 39class qtype_ddwtos_test extends question_testcase {
61381e50
TH
40 /** @var qtype_ddwtos instance of the question type class to test. */
41 protected $qtype;
42
543e4dee 43 protected function setUp() {
61381e50
TH
44 $this->qtype = question_bank::get_qtype('ddwtos');;
45 }
46
543e4dee 47 protected function tearDown() {
61381e50
TH
48 $this->qtype = null;
49 }
50
51 public function assert_same_xml($expectedxml, $xml) {
543e4dee 52 $this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
61381e50
TH
53 str_replace("\r\n", "\n", $xml));
54 }
55
56 /**
8bc1d28b
EM
57 * Get some test question data.
58 *
61381e50 59 * @return object the data to construct a question like
6229791a 60 * {@link qtype_ddwtos_test_helper::make_ddwtos_question_fox()}.
61381e50
TH
61 */
62 protected function get_test_question_data() {
63 global $USER;
64
60cbb9ed 65 $dd = new stdClass();
61381e50
TH
66 $dd->id = 0;
67 $dd->category = 0;
f7ecca1c 68 $dd->contextid = 0;
61381e50
TH
69 $dd->parent = 0;
70 $dd->questiontextformat = FORMAT_HTML;
e9cd9902 71 $dd->generalfeedbackformat = FORMAT_HTML;
61381e50
TH
72 $dd->defaultmark = 1;
73 $dd->penalty = 0.3333333;
74 $dd->length = 1;
75 $dd->stamp = make_unique_id_code();
76 $dd->version = make_unique_id_code();
77 $dd->hidden = 0;
78 $dd->timecreated = time();
79 $dd->timemodified = time();
80 $dd->createdby = $USER->id;
81 $dd->modifiedby = $USER->id;
82
83 $dd->name = 'Drag-and-drop words into sentences question';
84 $dd->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
85 $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
86 $dd->qtype = 'ddwtos';
87
543e4dee 88 $dd->options = new stdClass();
61381e50
TH
89 $dd->options->shuffleanswers = true;
90
91 test_question_maker::set_standard_combined_feedback_fields($dd->options);
92
93 $dd->options->answers = array(
29929b5b
TH
94 (object) array('answer' => 'quick', 'feedback' =>
95 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
96 (object) array('answer' => 'fox', 'feedback' =>
97 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
98 (object) array('answer' => 'lazy', 'feedback' =>
99 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
100 (object) array('answer' => 'assiduous', 'feedback' =>
101 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
102 (object) array('answer' => 'dog', 'feedback' =>
103 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
104 (object) array('answer' => 'slow', 'feedback' =>
105 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
61381e50
TH
106 );
107
108 return $dd;
109 }
110
111 public function test_name() {
543e4dee 112 $this->assertEquals($this->qtype->name(), 'ddwtos');
61381e50
TH
113 }
114
115 public function test_can_analyse_responses() {
116 $this->assertTrue($this->qtype->can_analyse_responses());
117 }
118
119 public function test_initialise_question_instance() {
120 $qdata = $this->get_test_question_data();
121
6229791a 122 $expected = test_question_maker::make_question('ddwtos');
61381e50
TH
123 $expected->stamp = $qdata->stamp;
124 $expected->version = $qdata->version;
125
126 $q = $this->qtype->make_question($qdata);
127
543e4dee 128 $this->assertEquals($expected, $q);
61381e50
TH
129 }
130
131 public function test_get_random_guess_score() {
132 $q = $this->get_test_question_data();
543e4dee 133 $this->assertEquals(0.5, $this->qtype->get_random_guess_score($q), '', 0.0000001);
61381e50
TH
134 }
135
136 public function test_get_possible_responses() {
137 $q = $this->get_test_question_data();
138
543e4dee 139 $this->assertEquals(array(
61381e50 140 1 => array(
a9c32a6f 141 1 => new question_possible_response('quick', 1 / 3),
61381e50
TH
142 2 => new question_possible_response('slow', 0),
143 null => question_possible_response::no_response()),
144 2 => array(
a9c32a6f 145 1 => new question_possible_response('fox', 1 / 3),
61381e50
TH
146 2 => new question_possible_response('dog', 0),
147 null => question_possible_response::no_response()),
148 3 => array(
a9c32a6f 149 1 => new question_possible_response('lazy', 1 / 3),
61381e50
TH
150 2 => new question_possible_response('assiduous', 0),
151 null => question_possible_response::no_response()),
152 ), $this->qtype->get_possible_responses($q));
153 }
154
155 public function test_xml_import() {
156 $xml = ' <question type="ddwtos">
157 <name>
158 <text>A drag-and-drop question</text>
159 </name>
160 <questiontext format="moodle_auto_format">
161 <text>Put these in order: [[1]], [[2]], [[3]].</text>
162 </questiontext>
163 <generalfeedback>
164 <text>The answer is Alpha, Beta, Gamma.</text>
165 </generalfeedback>
166 <defaultgrade>3</defaultgrade>
167 <penalty>0.3333333</penalty>
168 <hidden>0</hidden>
169 <shuffleanswers>1</shuffleanswers>
170 <correctfeedback>
171 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
172 </correctfeedback>
173 <partiallycorrectfeedback>
174 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
175 </partiallycorrectfeedback>
176 <incorrectfeedback>
177 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
178 </incorrectfeedback>
179 <shownumcorrect/>
180 <dragbox>
181 <text>Alpha</text>
182 <group>1</group>
183 </dragbox>
184 <dragbox>
185 <text>Beta</text>
186 <group>1</group>
187 </dragbox>
188 <dragbox>
189 <text>Gamma</text>
190 <group>1</group>
191 <infinite/>
192 </dragbox>
193 <hint>
194 <text>Try again.</text>
195 <shownumcorrect />
196 </hint>
197 <hint>
198 <text>These are the first three letters of the Greek alphabet.</text>
199 <shownumcorrect />
200 <clearwrong />
201 </hint>
202 </question>';
203 $xmldata = xmlize($xml);
204
205 $importer = new qformat_xml();
206 $q = $importer->try_importing_using_qtypes(
207 $xmldata['question'], null, null, 'ddwtos');
208
60cbb9ed 209 $expectedq = new stdClass();
61381e50
TH
210 $expectedq->qtype = 'ddwtos';
211 $expectedq->name = 'A drag-and-drop question';
212 $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
213 $expectedq->questiontextformat = FORMAT_MOODLE;
214 $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
215 $expectedq->defaultmark = 3;
216 $expectedq->length = 1;
217 $expectedq->penalty = 0.3333333;
218
219 $expectedq->shuffleanswers = 1;
29929b5b 220 $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
1a3cd05d 221 'format' => FORMAT_MOODLE);
29929b5b
TH
222 $expectedq->partiallycorrectfeedback = array(
223 'text' => '<p>Your answer is partially correct.</p>',
1a3cd05d 224 'format' => FORMAT_MOODLE);
61381e50 225 $expectedq->shownumcorrect = true;
29929b5b 226 $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
1a3cd05d 227 'format' => FORMAT_MOODLE);
61381e50
TH
228
229 $expectedq->choices = array(
0234ddc8
TH
230 array('answer' => 'Alpha', 'choicegroup' => 1, 'infinite' => false),
231 array('answer' => 'Beta', 'choicegroup' => 1, 'infinite' => false),
232 array('answer' => 'Gamma', 'choicegroup' => 1, 'infinite' => true),
61381e50
TH
233 );
234
0234ddc8 235 $expectedq->hint = array(
1a3cd05d 236 array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
29929b5b 237 array('text' => 'These are the first three letters of the Greek alphabet.',
1a3cd05d 238 'format' => FORMAT_MOODLE));
61381e50
TH
239 $expectedq->hintshownumcorrect = array(true, true);
240 $expectedq->hintclearwrong = array(false, true);
241
543e4dee
JP
242 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
243 $this->assertEquals($expectedq->hint, $q->hint);
61381e50
TH
244 }
245
61381e50 246 public function test_xml_export() {
60cbb9ed 247 $qdata = new stdClass();
61381e50 248 $qdata->id = 123;
1796eb18 249 $qdata->contextid = \context_system::instance()->id;
61381e50
TH
250 $qdata->qtype = 'ddwtos';
251 $qdata->name = 'A drag-and-drop question';
252 $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
253 $qdata->questiontextformat = FORMAT_MOODLE;
254 $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
e9cd9902 255 $qdata->generalfeedbackformat = FORMAT_MOODLE;
61381e50
TH
256 $qdata->defaultmark = 3;
257 $qdata->length = 1;
258 $qdata->penalty = 0.3333333;
259 $qdata->hidden = 0;
260
543e4dee 261 $qdata->options = new stdClass();
61381e50
TH
262 $qdata->options->shuffleanswers = 1;
263 $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
e9cd9902 264 $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
61381e50 265 $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
e9cd9902
TH
266 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
267 $qdata->options->shownumcorrect = 1;
61381e50 268 $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
e9cd9902 269 $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
61381e50
TH
270
271 $qdata->options->answers = array(
29929b5b
TH
272 13 => new question_answer(13, 'Alpha', 0,
273 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
274 FORMAT_MOODLE),
275 14 => new question_answer(14, 'Beta', 0,
276 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
277 FORMAT_MOODLE),
278 15 => new question_answer(15, 'Gamma', 0,
279 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:1;}',
280 FORMAT_MOODLE),
61381e50
TH
281 );
282
283 $qdata->hints = array(
e9cd9902 284 1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
29929b5b
TH
285 2 => new question_hint_with_parts(2,
286 'These are the first three letters of the Greek alphabet.',
287 FORMAT_MOODLE, true, true),
61381e50
TH
288 );
289
290 $exporter = new qformat_xml();
291 $xml = $exporter->writequestion($qdata);
292
293 $expectedxml = '<!-- question: 123 -->
294 <question type="ddwtos">
295 <name>
296 <text>A drag-and-drop question</text>
297 </name>
298 <questiontext format="moodle_auto_format">
299 <text>Put these in order: [[1]], [[2]], [[3]].</text>
300 </questiontext>
e9cd9902 301 <generalfeedback format="moodle_auto_format">
61381e50
TH
302 <text>The answer is Alpha, Beta, Gamma.</text>
303 </generalfeedback>
304 <defaultgrade>3</defaultgrade>
305 <penalty>0.3333333</penalty>
306 <hidden>0</hidden>
307 <shuffleanswers>1</shuffleanswers>
e9cd9902 308 <correctfeedback format="moodle_auto_format">
61381e50
TH
309 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
310 </correctfeedback>
e9cd9902 311 <partiallycorrectfeedback format="moodle_auto_format">
61381e50
TH
312 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
313 </partiallycorrectfeedback>
e9cd9902 314 <incorrectfeedback format="moodle_auto_format">
61381e50
TH
315 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
316 </incorrectfeedback>
317 <shownumcorrect/>
318 <dragbox>
319 <text>Alpha</text>
320 <group>1</group>
321 </dragbox>
322 <dragbox>
323 <text>Beta</text>
324 <group>1</group>
325 </dragbox>
326 <dragbox>
327 <text>Gamma</text>
328 <group>1</group>
329 <infinite/>
330 </dragbox>
e9cd9902 331 <hint format="moodle_auto_format">
61381e50
TH
332 <text>Try again.</text>
333 <shownumcorrect/>
334 </hint>
e9cd9902 335 <hint format="moodle_auto_format">
61381e50
TH
336 <text>These are the first three letters of the Greek alphabet.</text>
337 <shownumcorrect/>
338 <clearwrong/>
339 </hint>
340 </question>
341';
342
343 $this->assert_same_xml($expectedxml, $xml);
344 }
345}