MDL-47494 gapselect: Now all the unit tests for the the ddwtos and gapselect qtypes...
[moodle.git] / question / type / gapselect / simpletest / testquestiontype.php
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.
14 //
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/>.
19 /**
20  * Unit tests for the select missing words question question definition class.
21  *
22  * @package qtype_gapselect
23  * @copyright 2011 The Open University
24  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
29 require_once($CFG->dirroot . '/question/type/gapselect/simpletest/helper.php');
32 /**
33  * Unit tests for the select missing words question definition class.
34  *
35  * @package qtype
36  * @subpackage gapselect
37  * @copyright 2011 The Open University
38  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class qtype_gapselect_test extends UnitTestCase {
41     /** @var qtype_gapselect instance of the question type class to test. */
42     protected $qtype;
44     public function setUp() {
45         $this->qtype = question_bank::get_qtype('gapselect');;
46     }
48     public function tearDown() {
49         $this->qtype = null;
50     }
52     public function assert_same_xml($expectedxml, $xml) {
53         $this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
54                 str_replace("\r\n", "\n", $xml));
55     }
57     /**
58      * @return object the data to construct a question like
59      * {@link qtype_gapselect_test_helper::make_a_gapselect_question()}.
60      */
61     protected function get_test_question_data() {
62         global $USER;
64         $gapselect = new stdClass;
65         $gapselect->id = 0;
66         $gapselect->category = 0;
67         $gapselect->parent = 0;
68         $gapselect->questiontextformat = FORMAT_HTML;
69         $gapselect->generalfeedbackformat = FORMAT_HTML;
70         $gapselect->defaultmark = 1;
71         $gapselect->penalty = 0.3333333;
72         $gapselect->length = 1;
73         $gapselect->stamp = make_unique_id_code();
74         $gapselect->version = make_unique_id_code();
75         $gapselect->hidden = 0;
76         $gapselect->timecreated = time();
77         $gapselect->timemodified = time();
78         $gapselect->createdby = $USER->id;
79         $gapselect->modifiedby = $USER->id;
81         $gapselect->name = 'Selection from drop down list question';
82         $gapselect->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
83         $gapselect->generalfeedback = 'This sentence uses each letter of the alphabet.';
84         $gapselect->qtype = 'gapselect';
86         $gapselect->options->shuffleanswers = true;
88         test_question_maker::set_standard_combined_feedback_fields($gapselect->options);
90         $gapselect->options->answers = array(
91             (object) array('answer' => 'quick', 'feedback' => '1'),
92             (object) array('answer' => 'fox', 'feedback' => '2'),
93             (object) array('answer' => 'lazy', 'feedback' => '3'),
94             (object) array('answer' => 'assiduous', 'feedback' => '3'),
95             (object) array('answer' => 'dog', 'feedback' => '2'),
96             (object) array('answer' => 'slow', 'feedback' => '1'),
97         );
99         return $gapselect;
100     }
102     public function test_name() {
103         $this->assertEqual($this->qtype->name(), 'gapselect');
104     }
106     public function test_can_analyse_responses() {
107         $this->assertTrue($this->qtype->can_analyse_responses());
108     }
110     public function test_initialise_question_instance() {
111         $qdata = $this->get_test_question_data();
113         $expected = qtype_gapselect_test_helper::make_a_gapselect_question();
114         $expected->stamp = $qdata->stamp;
115         $expected->version = $qdata->version;
117         $q = $this->qtype->make_question($qdata);
119         $this->assertEqual($expected, $q);
120     }
122     public function test_get_random_guess_score() {
123         $q = $this->get_test_question_data();
124         $this->assertWithinMargin(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
125     }
127     public function test_get_possible_responses() {
128         $q = $this->get_test_question_data();
130         $this->assertEqual(array(
131             1 => array(
132                 1 => new question_possible_response('quick', 1),
133                 2 => new question_possible_response('slow', 0),
134                 null => question_possible_response::no_response()),
135             2 => array(
136                 1 => new question_possible_response('fox', 1),
137                 2 => new question_possible_response('dog', 0),
138                 null => question_possible_response::no_response()),
139             3 => array(
140                 1 => new question_possible_response('lazy', 1),
141                 2 => new question_possible_response('assiduous', 0),
142                 null => question_possible_response::no_response()),
143         ), $this->qtype->get_possible_responses($q));
144     }
146     public function test_xml_import() {
147         $xml = '  <question type="gapselect">
148     <name>
149       <text>A select missing words question</text>
150     </name>
151     <questiontext format="moodle_auto_format">
152       <text>Put these in order: [[1]], [[2]], [[3]].</text>
153     </questiontext>
154     <generalfeedback>
155       <text>The answer is Alpha, Beta, Gamma.</text>
156     </generalfeedback>
157     <defaultgrade>3</defaultgrade>
158     <penalty>0.3333333</penalty>
159     <hidden>0</hidden>
160     <shuffleanswers>1</shuffleanswers>
161     <correctfeedback>
162       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
163     </correctfeedback>
164     <partiallycorrectfeedback>
165       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
166     </partiallycorrectfeedback>
167     <incorrectfeedback>
168       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
169     </incorrectfeedback>
170     <shownumcorrect/>
171     <selectoption>
172       <text>Alpha</text>
173       <group>1</group>
174     </selectoption>
175     <selectoption>
176       <text>Beta</text>
177       <group>1</group>
178     </selectoption>
179     <selectoption>
180       <text>Gamma</text>
181       <group>1</group>
182     </selectoption>
183     <hint>
184       <text>Try again.</text>
185       <shownumcorrect />
186     </hint>
187     <hint>
188       <text>These are the first three letters of the Greek alphabet.</text>
189       <shownumcorrect />
190       <clearwrong />
191     </hint>
192   </question>';
193         $xmldata = xmlize($xml);
195         $importer = new qformat_xml();
196         $q = $importer->try_importing_using_qtypes(
197                 $xmldata['question'], null, null, 'gapselect');
199         $expectedq = new stdClass;
200         $expectedq->qtype = 'gapselect';
201         $expectedq->name = 'A select missing words question';
202         $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
203         $expectedq->questiontextformat = FORMAT_MOODLE;
204         $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
205         $expectedq->defaultmark = 3;
206         $expectedq->length = 1;
207         $expectedq->penalty = 0.3333333;
209         $expectedq->shuffleanswers = 1;
210         $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>', 'format' => FORMAT_MOODLE, 'files' => array());
211         $expectedq->partiallycorrectfeedback = array('text' => '<p>Your answer is partially correct.</p>', 'format' => FORMAT_MOODLE, 'files' => array());
212         $expectedq->shownumcorrect = true;
213         $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>', 'format' => FORMAT_MOODLE, 'files' => array());
215         $expectedq->choices = array(
216             array('answer' => 'Alpha', 'selectgroup' => 1),
217             array('answer' => 'Beta', 'selectgroup' => 1),
218             array('answer' => 'Gamma', 'selectgroup' => 1),
219         );
221         $expectedq->hint = array('Try again.', 'These are the first three letters of the Greek alphabet.');
222         $expectedq->hintshownumcorrect = array(true, true);
223         $expectedq->hintclearwrong = array(false, true);
225         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
226     }
228     public function test_xml_export() {
229         $qdata = new stdClass;
230         $qdata->id = 123;
231         $qdata->contextid = 0;
232         $qdata->qtype = 'gapselect';
233         $qdata->name = 'A select missing words question';
234         $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
235         $qdata->questiontextformat = FORMAT_MOODLE;
236         $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
237         $qdata->generalfeedbackformat = FORMAT_MOODLE;
238         $qdata->defaultmark = 3;
239         $qdata->length = 1;
240         $qdata->penalty = 0.3333333;
241         $qdata->hidden = 0;
243         $qdata->options->shuffleanswers = 1;
244         $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
245         $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
246         $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
247                 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
248         $qdata->options->shownumcorrect = true;
249         $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
250         $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
252         $qdata->options->answers = array(
253             13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
254             14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
255             15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
256         );
258         $qdata->hints = array(
259             1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
260             2 => new question_hint_with_parts(2, 'These are the first three letters of the Greek alphabet.', FORMAT_MOODLE, true, true),
261         );
263         $exporter = new qformat_xml();
264         $xml = $exporter->writequestion($qdata);
266         $expectedxml = '<!-- question: 123  -->
267   <question type="gapselect">
268     <name>
269       <text>A select missing words question</text>
270     </name>
271     <questiontext format="moodle_auto_format">
272       <text>Put these in order: [[1]], [[2]], [[3]].</text>
273     </questiontext>
274     <generalfeedback format="moodle_auto_format">
275       <text>The answer is Alpha, Beta, Gamma.</text>
276     </generalfeedback>
277     <defaultgrade>3</defaultgrade>
278     <penalty>0.3333333</penalty>
279     <hidden>0</hidden>
280     <shuffleanswers>1</shuffleanswers>
281     <correctfeedback format="moodle_auto_format">
282       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
283     </correctfeedback>
284     <partiallycorrectfeedback format="moodle_auto_format">
285       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
286     </partiallycorrectfeedback>
287     <incorrectfeedback format="moodle_auto_format">
288       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
289     </incorrectfeedback>
290     <shownumcorrect/>
291     <selectoption>
292       <text>Alpha</text>
293       <group>1</group>
294     </selectoption>
295     <selectoption>
296       <text>Beta</text>
297       <group>1</group>
298     </selectoption>
299     <selectoption>
300       <text>Gamma</text>
301       <group>1</group>
302     </selectoption>
303     <hint format="moodle_auto_format">
304       <text>Try again.</text>
305       <shownumcorrect/>
306     </hint>
307     <hint format="moodle_auto_format">
308       <text>These are the first three letters of the Greek alphabet.</text>
309       <shownumcorrect/>
310       <clearwrong/>
311     </hint>
312   </question>
313 ';
315         $this->assert_same_xml($expectedxml, $xml);
316     }