PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/ddwtos/tests/question_type_test.php

https://bitbucket.org/moodle/moodle
PHP | 374 lines | 299 code | 44 blank | 31 comment | 1 complexity | 8d558f43b52fd825235f8c7c00c89df2 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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/>.
  16. namespace qtype_ddwtos;
  17. use question_answer;
  18. use question_bank;
  19. use question_hint_with_parts;
  20. use question_possible_response;
  21. defined('MOODLE_INTERNAL') || die();
  22. global $CFG;
  23. require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  24. require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
  25. /**
  26. * Unit tests for the drag-and-drop words into sentences question definition class.
  27. *
  28. * @package qtype_ddwtos
  29. * @copyright 2012 The Open University
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class question_type_test extends \question_testcase {
  33. /** @var qtype_ddwtos instance of the question type class to test. */
  34. protected $qtype;
  35. protected function setUp(): void {
  36. $this->qtype = question_bank::get_qtype('ddwtos');;
  37. }
  38. protected function tearDown(): void {
  39. $this->qtype = null;
  40. }
  41. public function assert_same_xml($expectedxml, $xml) {
  42. $this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
  43. str_replace("\r\n", "\n", $xml));
  44. }
  45. /**
  46. * Get some test question data.
  47. *
  48. * @return object the data to construct a question like
  49. * {@link qtype_ddwtos_test_helper::make_ddwtos_question_fox()}.
  50. */
  51. protected function get_test_question_data() {
  52. global $USER;
  53. $dd = new \stdClass();
  54. $dd->id = 0;
  55. $dd->category = 0;
  56. $dd->contextid = 0;
  57. $dd->parent = 0;
  58. $dd->questiontextformat = FORMAT_HTML;
  59. $dd->generalfeedbackformat = FORMAT_HTML;
  60. $dd->defaultmark = 1;
  61. $dd->penalty = 0.3333333;
  62. $dd->length = 1;
  63. $dd->stamp = make_unique_id_code();
  64. $dd->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
  65. $dd->version = 1;
  66. $dd->versionid = 0;
  67. $dd->questionbankentryid = 0;
  68. $dd->idnumber = null;
  69. $dd->timecreated = time();
  70. $dd->timemodified = time();
  71. $dd->createdby = $USER->id;
  72. $dd->modifiedby = $USER->id;
  73. $dd->name = 'Drag-and-drop words into sentences question';
  74. $dd->questiontext = 'The [[1]] brown [[2]] jumped over the [[3]] dog.';
  75. $dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
  76. $dd->qtype = 'ddwtos';
  77. $dd->options = new \stdClass();
  78. $dd->options->shuffleanswers = true;
  79. \test_question_maker::set_standard_combined_feedback_fields($dd->options);
  80. $dd->options->answers = array(
  81. (object) array('answer' => 'quick', 'feedback' =>
  82. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
  83. (object) array('answer' => 'fox', 'feedback' =>
  84. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
  85. (object) array('answer' => 'lazy', 'feedback' =>
  86. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
  87. (object) array('answer' => 'assiduous', 'feedback' =>
  88. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"3";s:8:"infinite";i:0;}'),
  89. (object) array('answer' => 'dog', 'feedback' =>
  90. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"2";s:8:"infinite";i:0;}'),
  91. (object) array('answer' => 'slow', 'feedback' =>
  92. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";i:0;}'),
  93. );
  94. return $dd;
  95. }
  96. public function test_name() {
  97. $this->assertEquals($this->qtype->name(), 'ddwtos');
  98. }
  99. public function test_can_analyse_responses() {
  100. $this->assertTrue($this->qtype->can_analyse_responses());
  101. }
  102. public function test_save_question() {
  103. $this->resetAfterTest();
  104. $syscontext = \context_system::instance();
  105. /** @var core_question_generator $generator */
  106. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  107. $category = $generator->create_question_category(['contextid' => $syscontext->id]);
  108. $fromform = \test_question_maker::get_question_form_data('ddwtos', 'missingchoiceno');
  109. $fromform->category = $category->id . ',' . $syscontext->id;
  110. $question = new \stdClass();
  111. $question->category = $category->id;
  112. $question->qtype = 'ddwtos';
  113. $question->createdby = 0;
  114. $this->qtype->save_question($question, $fromform);
  115. $q = question_bank::load_question($question->id);
  116. // We just want to verify that this does not cause errors,
  117. // but also verify some of the outcome.
  118. $this->assertEquals('The [[1]] sat on the [[2]].', $q->questiontext);
  119. $this->assertEquals([1 => 1, 2 => 1], $q->places);
  120. $this->assertEquals([1 => 1, 2 => 2], $q->rightchoices);
  121. }
  122. public function test_initialise_question_instance() {
  123. $qdata = $this->get_test_question_data();
  124. $expected = \test_question_maker::make_question('ddwtos');
  125. $expected->stamp = $qdata->stamp;
  126. $expected->idnumber = null;
  127. $q = $this->qtype->make_question($qdata);
  128. $this->assertEquals($expected, $q);
  129. }
  130. public function test_get_random_guess_score() {
  131. $q = $this->get_test_question_data();
  132. $this->assertEqualsWithDelta(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
  133. }
  134. public function test_get_possible_responses() {
  135. $q = $this->get_test_question_data();
  136. $this->assertEquals(array(
  137. 1 => array(
  138. 1 => new question_possible_response('quick', 1 / 3),
  139. 2 => new question_possible_response('slow', 0),
  140. null => question_possible_response::no_response()),
  141. 2 => array(
  142. 1 => new question_possible_response('fox', 1 / 3),
  143. 2 => new question_possible_response('dog', 0),
  144. null => question_possible_response::no_response()),
  145. 3 => array(
  146. 1 => new question_possible_response('lazy', 1 / 3),
  147. 2 => new question_possible_response('assiduous', 0),
  148. null => question_possible_response::no_response()),
  149. ), $this->qtype->get_possible_responses($q));
  150. }
  151. public function test_xml_import() {
  152. $xml = ' <question type="ddwtos">
  153. <name>
  154. <text>A drag-and-drop question</text>
  155. </name>
  156. <questiontext format="moodle_auto_format">
  157. <text>Put these in order: [[1]], [[2]], [[3]].</text>
  158. </questiontext>
  159. <generalfeedback>
  160. <text>The answer is Alpha, Beta, Gamma.</text>
  161. </generalfeedback>
  162. <defaultgrade>3</defaultgrade>
  163. <penalty>0.3333333</penalty>
  164. <hidden>0</hidden>
  165. <shuffleanswers>1</shuffleanswers>
  166. <correctfeedback>
  167. <text><![CDATA[<p>Your answer is correct.</p>]]></text>
  168. </correctfeedback>
  169. <partiallycorrectfeedback>
  170. <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
  171. </partiallycorrectfeedback>
  172. <incorrectfeedback>
  173. <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
  174. </incorrectfeedback>
  175. <shownumcorrect/>
  176. <dragbox>
  177. <text>Alpha</text>
  178. <group>1</group>
  179. </dragbox>
  180. <dragbox>
  181. <text>Beta</text>
  182. <group>1</group>
  183. </dragbox>
  184. <dragbox>
  185. <text>Gamma</text>
  186. <group>1</group>
  187. <infinite/>
  188. </dragbox>
  189. <hint>
  190. <text>Try again.</text>
  191. <shownumcorrect />
  192. </hint>
  193. <hint>
  194. <text>These are the first three letters of the Greek alphabet.</text>
  195. <shownumcorrect />
  196. <clearwrong />
  197. </hint>
  198. </question>';
  199. $xmldata = xmlize($xml);
  200. $importer = new \qformat_xml();
  201. $q = $importer->try_importing_using_qtypes(
  202. $xmldata['question'], null, null, 'ddwtos');
  203. $expectedq = new \stdClass();
  204. $expectedq->qtype = 'ddwtos';
  205. $expectedq->name = 'A drag-and-drop question';
  206. $expectedq->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
  207. $expectedq->questiontextformat = FORMAT_MOODLE;
  208. $expectedq->generalfeedback = 'The answer is Alpha, Beta, Gamma.';
  209. $expectedq->defaultmark = 3;
  210. $expectedq->length = 1;
  211. $expectedq->penalty = 0.3333333;
  212. $expectedq->shuffleanswers = 1;
  213. $expectedq->correctfeedback = array('text' => '<p>Your answer is correct.</p>',
  214. 'format' => FORMAT_MOODLE);
  215. $expectedq->partiallycorrectfeedback = array(
  216. 'text' => '<p>Your answer is partially correct.</p>',
  217. 'format' => FORMAT_MOODLE);
  218. $expectedq->shownumcorrect = true;
  219. $expectedq->incorrectfeedback = array('text' => '<p>Your answer is incorrect.</p>',
  220. 'format' => FORMAT_MOODLE);
  221. $expectedq->choices = array(
  222. array('answer' => 'Alpha', 'choicegroup' => 1, 'infinite' => false),
  223. array('answer' => 'Beta', 'choicegroup' => 1, 'infinite' => false),
  224. array('answer' => 'Gamma', 'choicegroup' => 1, 'infinite' => true),
  225. );
  226. $expectedq->hint = array(
  227. array('text' => 'Try again.', 'format' => FORMAT_MOODLE),
  228. array('text' => 'These are the first three letters of the Greek alphabet.',
  229. 'format' => FORMAT_MOODLE));
  230. $expectedq->hintshownumcorrect = array(true, true);
  231. $expectedq->hintclearwrong = array(false, true);
  232. $this->assert(new \question_check_specified_fields_expectation($expectedq), $q);
  233. $this->assertEquals($expectedq->hint, $q->hint);
  234. }
  235. public function test_xml_export() {
  236. $qdata = new \stdClass();
  237. $qdata->id = 123;
  238. $qdata->contextid = \context_system::instance()->id;
  239. $qdata->idnumber = null;
  240. $qdata->qtype = 'ddwtos';
  241. $qdata->name = 'A drag-and-drop 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->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
  250. $qdata->options = new \stdClass();
  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 = 1;
  257. $qdata->options->incorrectfeedback = '<p>Your answer is incorrect.</p>';
  258. $qdata->options->incorrectfeedbackformat = FORMAT_MOODLE;
  259. $qdata->options->answers = array(
  260. 13 => new question_answer(13, 'Alpha', 0,
  261. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
  262. FORMAT_MOODLE),
  263. 14 => new question_answer(14, 'Beta', 0,
  264. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:0;}',
  265. FORMAT_MOODLE),
  266. 15 => new question_answer(15, 'Gamma', 0,
  267. 'O:8:"stdClass":2:{s:9:"draggroup";s:1:"1";s:8:"infinite";b:1;}',
  268. FORMAT_MOODLE),
  269. );
  270. $qdata->hints = array(
  271. 1 => new question_hint_with_parts(1, 'Try again.', FORMAT_MOODLE, true, false),
  272. 2 => new question_hint_with_parts(2,
  273. 'These are the first three letters of the Greek alphabet.',
  274. FORMAT_MOODLE, true, true),
  275. );
  276. $exporter = new \qformat_xml();
  277. $xml = $exporter->writequestion($qdata);
  278. $expectedxml = '<!-- question: 123 -->
  279. <question type="ddwtos">
  280. <name>
  281. <text>A drag-and-drop question</text>
  282. </name>
  283. <questiontext format="moodle_auto_format">
  284. <text>Put these in order: [[1]], [[2]], [[3]].</text>
  285. </questiontext>
  286. <generalfeedback format="moodle_auto_format">
  287. <text>The answer is Alpha, Beta, Gamma.</text>
  288. </generalfeedback>
  289. <defaultgrade>3</defaultgrade>
  290. <penalty>0.3333333</penalty>
  291. <hidden>0</hidden>
  292. <idnumber></idnumber>
  293. <shuffleanswers>1</shuffleanswers>
  294. <correctfeedback format="moodle_auto_format">
  295. <text><![CDATA[<p>Your answer is correct.</p>]]></text>
  296. </correctfeedback>
  297. <partiallycorrectfeedback format="moodle_auto_format">
  298. <text><![CDATA[<p>Your answer is partially correct.</p>]]></text>
  299. </partiallycorrectfeedback>
  300. <incorrectfeedback format="moodle_auto_format">
  301. <text><![CDATA[<p>Your answer is incorrect.</p>]]></text>
  302. </incorrectfeedback>
  303. <shownumcorrect/>
  304. <dragbox>
  305. <text>Alpha</text>
  306. <group>1</group>
  307. </dragbox>
  308. <dragbox>
  309. <text>Beta</text>
  310. <group>1</group>
  311. </dragbox>
  312. <dragbox>
  313. <text>Gamma</text>
  314. <group>1</group>
  315. <infinite/>
  316. </dragbox>
  317. <hint format="moodle_auto_format">
  318. <text>Try again.</text>
  319. <shownumcorrect/>
  320. </hint>
  321. <hint format="moodle_auto_format">
  322. <text>These are the first three letters of the Greek alphabet.</text>
  323. <shownumcorrect/>
  324. <clearwrong/>
  325. </hint>
  326. </question>
  327. ';
  328. $this->assert_same_xml($expectedxml, $xml);
  329. }
  330. }