4b81229978fa3b7aa7ddbd8654ddea66e6802082
[moodle.git] / question / type / gapselect / simpletest / testquestiontype.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
21  * @subpackage gapselect
22  * @copyright  2011 The Open University
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
30 require_once($CFG->dirroot . '/question/type/gapselect/simpletest/helper.php');
33 /**
34  * Unit tests for the select missing words question definition class.
35  *
36  * @copyright  2011 The Open University
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class qtype_gapselect_test extends UnitTestCase {
40     /** @var qtype_gapselect instance of the question type class to test. */
41     protected $qtype;
43     public function setUp() {
44         $this->qtype = question_bank::get_qtype('gapselect');;
45     }
47     public function tearDown() {
48         $this->qtype = null;
49     }
51     public function assert_same_xml($expectedxml, $xml) {
52         $this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
53                 str_replace("\r\n", "\n", $xml));
54     }
56     /**
57      * @return object the data to construct a question like
58      * {@link qtype_gapselect_test_helper::make_a_gapselect_question()}.
59      */
60     protected function get_test_question_data() {
61         global $USER;
63         $gapselect = new stdClass();
64         $gapselect->id = 0;
65         $gapselect->category = 0;
66         $gapselect->contextid = 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/3),
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/3),
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/3),
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>',
211                 'format' => FORMAT_MOODLE, 'files' => array());
212         $expectedq->partiallycorrectfeedback = array(
213                 'text' => '<p>Your answer is partially correct.</p>',
214                 'format' => FORMAT_MOODLE, 'files' => array());
215         $expectedq->shownumcorrect = true;
216         $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
217                 'format' => FORMAT_MOODLE, 'files' => array());
219         $expectedq->choices = array(
220             array('answer' => 'Alpha', 'choicegroup' => 1),
221             array('answer' => 'Beta', 'choicegroup' => 1),
222             array('answer' => 'Gamma', 'choicegroup' => 1),
223         );
225         $expectedq->hint = array(
226                 array('text' => 'Try again.', 'format' => FORMAT_MOODLE, 'files' => array()),
227                 array('text' => 'These are the first three letters of the Greek alphabet.',
228                         'format' => FORMAT_MOODLE, 'files' => array()));
229         $expectedq->hintshownumcorrect = array(true, true);
230         $expectedq->hintclearwrong = array(false, true);
232         $this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
233         $this->assertEqual($expectedq->hint, $q->hint);
234     }
236     public function test_xml_export() {
237         $qdata = new stdClass();
238         $qdata->id = 123;
239         $qdata->contextid = 0;
240         $qdata->qtype = 'gapselect';
241         $qdata->name = 'A select missing words question';
242         $qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
243         $qdata->questiontextformat = FORMAT_MOODLE;
244         $qdata->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
245         $qdata->generalfeedbackformat = FORMAT_MOODLE;
246         $qdata->defaultmark = 3;
247         $qdata->length = 1;
248         $qdata->penalty = 0.3333333;
249         $qdata->hidden = 0;
251         $qdata->options->shuffleanswers = 1;
252         $qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
253         $qdata->options->correctfeedbackformat = FORMAT_MOODLE;
254         $qdata->options->partiallycorrectfeedback = '<p>Your answer is partially correct.</p>';
255                 $qdata->options->partiallycorrectfeedbackformat = FORMAT_MOODLE;
256         $qdata->options->shownumcorrect = true;
257         $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
258         $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
260         $qdata->options->answers = array(
261             13 => new question_answer(13, 'Alpha', 0, '1', FORMAT_MOODLE),
262             14 => new question_answer(14, 'Beta', 0, '1', FORMAT_MOODLE),
263             15 => new question_answer(15, 'Gamma', 0, '1', FORMAT_MOODLE),
264         );
266         $qdata->hints = array(
267             1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
268             2 => new question_hint_with_parts(2,
269                     'These are the first three letters of the Greek alphabet.',
270                     FORMAT_MOODLE, true, true),
271         );
273         $exporter = new qformat_xml();
274         $xml = $exporter->writequestion($qdata);
276         $expectedxml = '<!-- question: 123  -->
277   <question type="gapselect">
278     <name>
279       <text>A select missing words question</text>
280     </name>
281     <questiontext format="moodle_auto_format">
282       <text>Put these in order: [[1]], [[2]], [[3]].</text>
283     </questiontext>
284     <generalfeedback format="moodle_auto_format">
285       <text>The answer is Alpha, Beta, Gamma.</text>
286     </generalfeedback>
287     <defaultgrade>3</defaultgrade>
288     <penalty>0.3333333</penalty>
289     <hidden>0</hidden>
290     <shuffleanswers>1</shuffleanswers>
291     <correctfeedback format="moodle_auto_format">
292       <text><![CDATA[<p>Your answer is correct.</p>]]></text>
293     </correctfeedback>
294     <partiallycorrectfeedback format="moodle_auto_format">
295       <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
296     </partiallycorrectfeedback>
297     <incorrectfeedback format="moodle_auto_format">
298       <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
299     </incorrectfeedback>
300     <shownumcorrect/>
301     <selectoption>
302       <text>Alpha</text>
303       <group>1</group>
304     </selectoption>
305     <selectoption>
306       <text>Beta</text>
307       <group>1</group>
308     </selectoption>
309     <selectoption>
310       <text>Gamma</text>
311       <group>1</group>
312     </selectoption>
313     <hint format="moodle_auto_format">
314       <text>Try again.</text>
315       <shownumcorrect/>
316     </hint>
317     <hint format="moodle_auto_format">
318       <text>These are the first three letters of the Greek alphabet.</text>
319       <shownumcorrect/>
320       <clearwrong/>
321     </hint>
322   </question>
323 ';
325         $this->assert_same_xml($expectedxml, $xml);
326     }