3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * Unit tests for the select missing words question question definition class.
22 * @package qtype_gapselect
23 * @copyright 2011 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
29 require_once($CFG->dirroot . '/question/type/gapselect/simpletest/helper.php');
33 * Unit tests for the select missing words question definition class.
36 * @subpackage gapselect
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class qtype_gapselect_test extends UnitTestCase {
41 /** @var qtype_gapselect instance of the question type class to test. */
44 public function setUp() {
45 $this->qtype = question_bank::get_qtype('gapselect');;
48 public function tearDown() {
52 public function assert_same_xml($expectedxml, $xml) {
53 $this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
54 str_replace("\r\n", "\n", $xml));
58 * @return object the data to construct a question like
59 * {@link qtype_gapselect_test_helper::make_a_gapselect_question()}.
61 protected function get_test_question_data() {
64 $gapselect = new stdClass;
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'),
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),
133 2 => new question_possible_response('slow', 0),
134 null => question_possible_response::no_response()),
136 1 => new question_possible_response('fox', 1),
137 2 => new question_possible_response('dog', 0),
138 null => question_possible_response::no_response()),
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));
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>', '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),
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);
228 public function test_xml_export() {
229 $qdata = new stdClass;
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;
240 $qdata->penalty = 0.3333333;
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),
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),
263 $exporter = new qformat_xml();
264 $xml = $exporter->writequestion($qdata);
266 $expectedxml = '<!-- question: 123 -->
267 <question type="gapselect">
269 <text>A select missing words question</text>
271 <questiontext format="moodle_auto_format">
272 <text>Put these in order: [[1]], [[2]], [[3]].</text>
274 <generalfeedback format="moodle_auto_format">
275 <text>The answer is Alpha, Beta, Gamma.</text>
277 <defaultgrade>3</defaultgrade>
278 <penalty>0.3333333</penalty>
280 <shuffleanswers>1</shuffleanswers>
281 <correctfeedback format="moodle_auto_format">
282 <text><![CDATA[<p>Your answer is correct.</p>]]></text>
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>
303 <hint format="moodle_auto_format">
304 <text>Try again.</text>
307 <hint format="moodle_auto_format">
308 <text>These are the first three letters of the Greek alphabet.</text>
315 $this->assert_same_xml($expectedxml, $xml);