2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for the select missing words question question definition class.
21 * @subpackage gapselect
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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');
34 * Unit tests for the select missing words question definition class.
36 * @copyright 2011 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class qtype_gapselect_test extends UnitTestCase {
40 /** @var qtype_gapselect instance of the question type class to test. */
43 public function setUp() {
44 $this->qtype = question_bank::get_qtype('gapselect');;
47 public function tearDown() {
51 public function assert_same_xml($expectedxml, $xml) {
52 $this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
53 str_replace("\r\n", "\n", $xml));
57 * @return object the data to construct a question like
58 * {@link qtype_gapselect_test_helper::make_a_gapselect_question()}.
60 protected function get_test_question_data() {
63 $gapselect = new stdClass();
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'),
102 public function test_name() {
103 $this->assertEqual($this->qtype->name(), 'gapselect');
106 public function test_can_analyse_responses() {
107 $this->assertTrue($this->qtype->can_analyse_responses());
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);
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);
127 public function test_get_possible_responses() {
128 $q = $this->get_test_question_data();
130 $this->assertEqual(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()),
136 1 => new question_possible_response('fox', 1/3),
137 2 => new question_possible_response('dog', 0),
138 null => question_possible_response::no_response()),
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));
146 public function test_xml_import() {
147 $xml = ' <question type="gapselect">
149 <text>A select missing words question</text>
151 <questiontext format="moodle_auto_format">
152 <text>Put these in order: [[1]], [[2]], [[3]].</text>
155 <text>The answer is Alpha, Beta, Gamma.</text>
157 <defaultgrade>3</defaultgrade>
158 <penalty>0.3333333</penalty>
160 <shuffleanswers>1</shuffleanswers>
162 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
164 <partiallycorrectfeedback>
165 <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
166 </partiallycorrectfeedback>
168 <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
184 <text>Try again.</text>
188 <text>These are the first three letters of the Greek alphabet.</text>
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),
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);
236 public function test_xml_export() {
237 $qdata = new stdClass();
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;
248 $qdata->penalty = 0.3333333;
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),
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),
273 $exporter = new qformat_xml();
274 $xml = $exporter->writequestion($qdata);
276 $expectedxml = '<!-- question: 123 -->
277 <question type="gapselect">
279 <text>A select missing words question</text>
281 <questiontext format="moodle_auto_format">
282 <text>Put these in order: [[1]], [[2]], [[3]].</text>
284 <generalfeedback format="moodle_auto_format">
285 <text>The answer is Alpha, Beta, Gamma.</text>
287 <defaultgrade>3</defaultgrade>
288 <penalty>0.3333333</penalty>
290 <shuffleanswers>1</shuffleanswers>
291 <correctfeedback format="moodle_auto_format">
292 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
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>
313 <hint format="moodle_auto_format">
314 <text>Try again.</text>
317 <hint format="moodle_auto_format">
318 <text>These are the first three letters of the Greek alphabet.</text>
325 $this->assert_same_xml($expectedxml, $xml);