Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / gapselect / 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 select missing words question question definition class.
19  *
20  * @package   qtype_gapselect
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');
32 /**
33  * Unit tests for the select missing words question definition class.
34  *
35  * @copyright 2012 The Open University
36  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class qtype_gapselect_test extends question_testcase {
39     /** @var qtype_gapselect instance of the question type class to test. */
40     protected $qtype;
42     protected function setUp(): void {
43         $this->qtype = question_bank::get_qtype('gapselect');
44     }
46     protected function tearDown(): void {
47         $this->qtype = null;
48     }
50     /**
51      * Asserts that two strings containing XML are the same ignoring the line-endings.
52      *
53      * @param string $expectedxml
54      * @param string $xml
55      */
56     public function assert_same_xml($expectedxml, $xml) {
57         $this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
58                 str_replace("\r\n", "\n", $xml));
59     }
61     public function test_save_question() {
62         $this->resetAfterTest();
64         $syscontext = context_system::instance();
65         /** @var core_question_generator $generator */
66         $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
67         $category = $generator->create_question_category(['contextid' => $syscontext->id]);
69         $fromform = test_question_maker::get_question_form_data('gapselect', 'missingchoiceno');
70         $fromform->category = $category->id . ',' . $syscontext->id;
72         $question = new stdClass();
73         $question->category = $category->id;
74         $question->qtype = 'gapselect';
75         $question->createdby = 0;
77         $this->qtype->save_question($question, $fromform);
78         $q = question_bank::load_question($question->id);
79         // We just want to verify that this does not cause errors,
80         // but also verify some of the outcome.
81         $this->assertEquals('The [[1]] sat on the [[2]].', $q->questiontext);
82         $this->assertEquals([1 => 1, 2 => 1], $q->places);
83         $this->assertEquals([1 => 1, 2 => 2], $q->rightchoices);
84     }
86     /**
87      * Get some test question data.
88      * @return object the data to construct a question like
89      * {@link test_question_maker::make_question('gapselect')}.
90      */
91     protected function get_test_question_data() {
92         return test_question_maker::get_question_data('gapselect');
93     }
95     public function test_name() {
96         $this->assertEquals($this->qtype->name(), 'gapselect');
97     }
99     public function test_can_analyse_responses() {
100         $this->assertTrue($this->qtype->can_analyse_responses());
101     }
103     public function test_initialise_question_instance() {
104         $qdata = $this->get_test_question_data();
106         $expected = test_question_maker::make_question('gapselect');
107         $expected->stamp = $qdata->stamp;
108         $expected->version = $qdata->version;
110         $q = $this->qtype->make_question($qdata);
112         $this->assertEquals($expected, $q);
113     }
115     public function test_get_random_guess_score() {
116         $q = $this->get_test_question_data();
117         $this->assertEqualsWithDelta(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
118     }
120     public function test_get_possible_responses() {
121         $q = $this->get_test_question_data();
123         $this->assertEquals(array(
124             1 => array(
125                 1 => new question_possible_response('quick', 1 / 3),
126                 2 => new question_possible_response('slow', 0),
127                 null => question_possible_response::no_response()),
128             2 => array(
129                 1 => new question_possible_response('fox', 1 / 3),
130                 2 => new question_possible_response('dog', 0),
131                 null => question_possible_response::no_response()),
132             3 => array(
133                 1 => new question_possible_response('lazy', 1 / 3),
134                 2 => new question_possible_response('assiduous', 0),
135                 null => question_possible_response::no_response()),
136         ), $this->qtype->get_possible_responses($q));
137     }
139     public function test_xml_import() {
140         $xml = '  <question type="gapselect">
141     <name>
142       <text>A select missing words question</text>
143     </name>
144     <questiontext format="moodle_auto_format">
145       <text>Put these in order: [[1]], [[2]], [[3]].</text>
146     </questiontext>
147     <generalfeedback>
148       <text>The answer is Alpha, Beta, Gamma.</text>
149     </generalfeedback>
150     <defaultgrade>3</defaultgrade>
151     <penalty>0.3333333</penalty>
152     <hidden>0</hidden>
153     <shuffleanswers>1</shuffleanswers>
154     <correctfeedback>
155       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
156     </correctfeedback>
157     <partiallycorrectfeedback>
158       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
159     </partiallycorrectfeedback>
160     <incorrectfeedback>
161       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
162     </incorrectfeedback>
163     <shownumcorrect/>
164     <selectoption>
165       <text>Alpha</text>
166       <group>1</group>
167     </selectoption>
168     <selectoption>
169       <text>Beta</text>
170       <group>1</group>
171     </selectoption>
172     <selectoption>
173       <text>Gamma</text>
174       <group>1</group>
175     </selectoption>
176     <hint format="moodle_auto_format">
177       <text>Try again.</text>
178       <shownumcorrect />
179     </hint>
180     <hint format="moodle_auto_format">
181       <text>These are the first three letters of the Greek alphabet.</text>
182       <shownumcorrect />
183       <clearwrong />
184     </hint>
185   </question>';
186         $xmldata = xmlize($xml);
188         $importer = new qformat_xml();
189         $q = $importer->try_importing_using_qtypes(
190                 $xmldata['question'], null, null, 'gapselect');
192         $expectedq = new stdClass();
193         $expectedq->qtype = 'gapselect';
194         $expectedq->name = 'A select missing words question';
195         $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
196         $expectedq->questiontextformat = FORMAT_MOODLE;
197         $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
198         $expectedq->defaultmark = 3;
199         $expectedq->length = 1;
200         $expectedq->penalty = 0.3333333;
202         $expectedq->shuffleanswers = 1;
203         $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
204                 'format' => FORMAT_MOODLE);
205         $expectedq->partiallycorrectfeedback = array(
206                 'text' => '<p>Your answer is partially correct.</p>',
207                 'format' => FORMAT_MOODLE);
208         $expectedq->shownumcorrect = true;
209         $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
210                 'format' => FORMAT_MOODLE);
212         $expectedq->choices = array(
213             array('answer' => 'Alpha', 'choicegroup' => 1),
214             array('answer' => 'Beta', 'choicegroup' => 1),
215             array('answer' => 'Gamma', 'choicegroup' => 1),
216         );
218         $expectedq->hint = array(
219                 array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
220                 array('text' => 'These are the first three letters of the Greek alphabet.',
221                         'format' => FORMAT_MOODLE));
222         $expectedq->hintshownumcorrect = array(true, true);
223         $expectedq->hintclearwrong = array(false, true);
224         $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
225         $this->assertEquals($expectedq->hint, $q->hint);
226     }
228     public function test_xml_export() {
229         $qdata = new stdClass();
230         $qdata->id = 123;
231         $qdata->contextid = \context_system::instance()->id;
232         $qdata->idnumber = null;
233         $qdata->qtype = 'gapselect';
234         $qdata->name = 'A select missing words question';
235         $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
236         $qdata->questiontextformat = FORMAT_MOODLE;
237         $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
238         $qdata->generalfeedbackformat = FORMAT_MOODLE;
239         $qdata->defaultmark = 3;
240         $qdata->length = 1;
241         $qdata->penalty = 0.3333333;
242         $qdata->hidden = 0;
244         $qdata->options = new stdClass();
245         $qdata->options->shuffleanswers = 1;
246         $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
247         $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
248         $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
249                 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
250         $qdata->options->shownumcorrect = true;
251         $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
252         $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
254         $qdata->options->answers = array(
255             13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
256             14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
257             15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
258         );
260         $qdata->hints = array(
261             1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
262             2 => new question_hint_with_parts(2,
263                     'These are the first three letters of the Greek alphabet.',
264                     FORMAT_MOODLE, true, true),
265         );
267         $exporter = new qformat_xml();
268         $xml = $exporter->writequestion($qdata);
270         $expectedxml = '<!-- question: 123  -->
271   <question type="gapselect">
272     <name>
273       <text>A select missing words question</text>
274     </name>
275     <questiontext format="moodle_auto_format">
276       <text>Put these in order: [[1]], [[2]], [[3]].</text>
277     </questiontext>
278     <generalfeedback format="moodle_auto_format">
279       <text>The answer is Alpha, Beta, Gamma.</text>
280     </generalfeedback>
281     <defaultgrade>3</defaultgrade>
282     <penalty>0.3333333</penalty>
283     <hidden>0</hidden>
284     <idnumber></idnumber>
285     <shuffleanswers>1</shuffleanswers>
286     <correctfeedback format="moodle_auto_format">
287       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
288     </correctfeedback>
289     <partiallycorrectfeedback format="moodle_auto_format">
290       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
291     </partiallycorrectfeedback>
292     <incorrectfeedback format="moodle_auto_format">
293       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
294     </incorrectfeedback>
295     <shownumcorrect/>
296     <selectoption>
297       <text>Alpha</text>
298       <group>1</group>
299     </selectoption>
300     <selectoption>
301       <text>Beta</text>
302       <group>1</group>
303     </selectoption>
304     <selectoption>
305       <text>Gamma</text>
306       <group>1</group>
307     </selectoption>
308     <hint format="moodle_auto_format">
309       <text>Try again.</text>
310       <shownumcorrect/>
311     </hint>
312     <hint format="moodle_auto_format">
313       <text>These are the first three letters of the Greek alphabet.</text>
314       <shownumcorrect/>
315       <clearwrong/>
316     </hint>
317   </question>
318 ';
320         $this->assert_same_xml($expectedxml, $xml);
321     }