MDL-62880 qtype_ddwtos: drop support for legacy import format
[moodle.git] / question / type / ddwtos / tests / questiontype_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  * Unit tests for the drag-and-drop words into sentences question definition class.
19  *
20  * @package   qtype_ddwtos
21  * @copyright 2012 The Open University
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
30 require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
33 /**
34  * Unit tests for the drag-and-drop words into sentences question definition class.
35  *
36  * @copyright 2012 The Open University
37  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class qtype_ddwtos_test extends question_testcase {
40     /** @var qtype_ddwtos instance of the question type class to test. */
41     protected $qtype;
43     protected function setUp() {
44         $this->qtype = question_bank::get_qtype('ddwtos');;
45     }
47     protected function tearDown() {
48         $this->qtype = null;
49     }
51     public function assert_same_xml($expectedxml, $xml) {
52         $this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
53                 str_replace("\r\n", "\n", $xml));
54     }
56     /**
57      * Get some test question data.
58      *
59      * @return object the data to construct a question like
60      * {@link qtype_ddwtos_test_helper::make_ddwtos_question_fox()}.
61      */
62     protected function get_test_question_data() {
63         global $USER;
65         $dd = new stdClass();
66         $dd->id = 0;
67         $dd->category = 0;
68         $dd->contextid = 0;
69         $dd->parent = 0;
70         $dd->questiontextformat = FORMAT_HTML;
71         $dd->generalfeedbackformat = FORMAT_HTML;
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;
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';
88         $dd->options = new stdClass();
89         $dd->options->shuffleanswers = true;
91         test_question_maker::set_standard_combined_feedback_fields($dd->options);
93         $dd->options->answers = array(
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;}'),
106         );
108         return $dd;
109     }
111     public function test_name() {
112         $this->assertEquals($this->qtype->name(), 'ddwtos');
113     }
115     public function test_can_analyse_responses() {
116         $this->assertTrue($this->qtype->can_analyse_responses());
117     }
119     public function test_initialise_question_instance() {
120         $qdata = $this->get_test_question_data();
122         $expected = test_question_maker::make_question('ddwtos');
123         $expected->stamp = $qdata->stamp;
124         $expected->version = $qdata->version;
126         $q = $this->qtype->make_question($qdata);
128         $this->assertEquals($expected, $q);
129     }
131     public function test_get_random_guess_score() {
132         $q = $this->get_test_question_data();
133         $this->assertEquals(0.5, $this->qtype->get_random_guess_score($q), '', 0.0000001);
134     }
136     public function test_get_possible_responses() {
137         $q = $this->get_test_question_data();
139         $this->assertEquals(array(
140             1 => array(
141                 1 => new question_possible_response('quick', 1 / 3),
142                 2 => new question_possible_response('slow', 0),
143                 null => question_possible_response::no_response()),
144             2 => array(
145                 1 => new question_possible_response('fox', 1 / 3),
146                 2 => new question_possible_response('dog', 0),
147                 null => question_possible_response::no_response()),
148             3 => array(
149                 1 => new question_possible_response('lazy', 1 / 3),
150                 2 => new question_possible_response('assiduous', 0),
151                 null => question_possible_response::no_response()),
152         ), $this->qtype->get_possible_responses($q));
153     }
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);
205         $importer = new qformat_xml();
206         $q = $importer->try_importing_using_qtypes(
207                 $xmldata['question'], null, null, 'ddwtos');
209         $expectedq = new stdClass();
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;
219         $expectedq->shuffleanswers = 1;
220         $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
221                 'format' => FORMAT_MOODLE);
222         $expectedq->partiallycorrectfeedback = array(
223                 'text' => '<p>Your answer is partially correct.</p>',
224                 'format' => FORMAT_MOODLE);
225         $expectedq->shownumcorrect = true;
226         $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
227                 'format' => FORMAT_MOODLE);
229         $expectedq->choices = array(
230             array('answer' => 'Alpha', 'choicegroup' => 1, 'infinite' => false),
231             array('answer' => 'Beta', 'choicegroup' => 1, 'infinite' => false),
232             array('answer' => 'Gamma', 'choicegroup' => 1, 'infinite' => true),
233         );
235         $expectedq->hint = array(
236                 array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
237                 array('text' => 'These are the first three letters of the Greek alphabet.',
238                         'format' => FORMAT_MOODLE));
239         $expectedq->hintshownumcorrect = array(true, true);
240         $expectedq->hintclearwrong = array(false, true);
242         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
243         $this->assertEquals($expectedq->hint, $q->hint);
244     }
246     public function test_xml_export() {
247         $qdata = new stdClass();
248         $qdata->id = 123;
249         $qdata->contextid = \context_system::instance()->id;
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.';
255         $qdata->generalfeedbackformat = FORMAT_MOODLE;
256         $qdata->defaultmark = 3;
257         $qdata->length = 1;
258         $qdata->penalty = 0.3333333;
259         $qdata->hidden = 0;
261         $qdata->options = new stdClass();
262         $qdata->options->shuffleanswers = 1;
263         $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
264         $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
265         $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
266         $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
267         $qdata->options->shownumcorrect = 1;
268         $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
269         $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
271         $qdata->options->answers = array(
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),
281         );
283         $qdata->hints = array(
284             1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
285             2 => new question_hint_with_parts(2,
286                     'These are the first three letters of the Greek alphabet.',
287                     FORMAT_MOODLE, true, true),
288         );
290         $exporter = new qformat_xml();
291         $xml = $exporter->writequestion($qdata);
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>
301     <generalfeedback format="moodle_auto_format">
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>
308     <correctfeedback format="moodle_auto_format">
309       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
310     </correctfeedback>
311     <partiallycorrectfeedback format="moodle_auto_format">
312       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
313     </partiallycorrectfeedback>
314     <incorrectfeedback format="moodle_auto_format">
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>
331     <hint format="moodle_auto_format">
332       <text>Try again.</text>
333       <shownumcorrect/>
334     </hint>
335     <hint format="moodle_auto_format">
336       <text>These are the first three letters of the Greek alphabet.</text>
337       <shownumcorrect/>
338       <clearwrong/>
339     </hint>
340   </question>
341 ';
343         $this->assert_same_xml($expectedxml, $xml);
344     }