/question/type/match/simpletest/testwalkthrough.php

https://github.com/viggof/moodle · PHP · 488 lines · 355 code · 64 blank · 69 comment · 1 complexity · 7b61506e516c12dd91ed61b5aeab8ebf MD5 · raw file

  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. /**
  17. * This file contains tests that walks a question through the interactive
  18. * behaviour.
  19. *
  20. * @package qtype
  21. * @subpackage match
  22. * @copyright 2010 The Open University
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
  27. /**
  28. * Unit tests for the matching question type.
  29. *
  30. * @copyright 2010 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class qtype_match_walkthrough_test extends qbehaviour_walkthrough_test_base {
  34. public function test_deferred_feedback_unanswered() {
  35. // Create a matching question.
  36. $m = test_question_maker::make_a_matching_question();
  37. $m->shufflestems = false;
  38. $this->start_attempt_at_question($m, 'deferredfeedback', 4);
  39. $choiceorder = $m->get_choice_order();
  40. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  41. $choices = array(0 => get_string('choose') . '...');
  42. foreach ($choiceorder as $key => $choice) {
  43. $choices[$key] = $m->choices[$choice];
  44. }
  45. // Check the initial state.
  46. $this->check_current_state(question_state::$todo);
  47. $this->check_current_mark(null);
  48. $this->check_current_output(
  49. $this->get_contains_select_expectation('sub0', $choices, null, true),
  50. $this->get_contains_select_expectation('sub1', $choices, null, true),
  51. $this->get_contains_select_expectation('sub2', $choices, null, true),
  52. $this->get_contains_select_expectation('sub3', $choices, null, true),
  53. $this->get_contains_question_text_expectation($m),
  54. $this->get_does_not_contain_feedback_expectation());
  55. $this->check_step_count(1);
  56. // Save a blank response.
  57. $this->process_submission(array('sub0' => '0', 'sub1' => '0',
  58. 'sub2' => '0', 'sub3' => '0'));
  59. // Verify.
  60. $this->check_current_state(question_state::$todo);
  61. $this->check_current_mark(null);
  62. $this->check_current_output(
  63. $this->get_contains_select_expectation('sub0', $choices, null, true),
  64. $this->get_contains_select_expectation('sub1', $choices, null, true),
  65. $this->get_contains_select_expectation('sub2', $choices, null, true),
  66. $this->get_contains_select_expectation('sub3', $choices, null, true),
  67. $this->get_contains_question_text_expectation($m),
  68. $this->get_does_not_contain_feedback_expectation());
  69. $this->check_step_count(1);
  70. // Finish the attempt.
  71. $this->quba->finish_all_questions();
  72. // Verify.
  73. $this->check_current_state(question_state::$gaveup);
  74. $this->check_current_mark(null);
  75. $this->check_current_output(
  76. $this->get_contains_select_expectation('sub0', $choices, null, false),
  77. $this->get_contains_select_expectation('sub1', $choices, null, false),
  78. $this->get_contains_select_expectation('sub2', $choices, null, false),
  79. $this->get_contains_select_expectation('sub3', $choices, null, false));
  80. }
  81. public function test_deferred_feedback_partial_answer() {
  82. // Create a matching question.
  83. $m = test_question_maker::make_a_matching_question();
  84. $m->shufflestems = false;
  85. $this->start_attempt_at_question($m, 'deferredfeedback', 4);
  86. $choiceorder = $m->get_choice_order();
  87. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  88. $choices = array(0 => get_string('choose') . '...');
  89. foreach ($choiceorder as $key => $choice) {
  90. $choices[$key] = $m->choices[$choice];
  91. }
  92. // Check the initial state.
  93. $this->check_current_state(question_state::$todo);
  94. $this->check_current_mark(null);
  95. $this->check_current_output(
  96. $this->get_contains_select_expectation('sub0', $choices, null, true),
  97. $this->get_contains_select_expectation('sub1', $choices, null, true),
  98. $this->get_contains_select_expectation('sub2', $choices, null, true),
  99. $this->get_contains_select_expectation('sub3', $choices, null, true),
  100. $this->get_contains_question_text_expectation($m),
  101. $this->get_does_not_contain_feedback_expectation());
  102. // Save a partial response.
  103. $this->process_submission(array('sub0' => $orderforchoice[1],
  104. 'sub1' => $orderforchoice[2], 'sub2' => '0', 'sub3' => '0'));
  105. // Verify.
  106. $this->check_current_state(question_state::$invalid);
  107. $this->check_current_mark(null);
  108. $this->check_current_output(
  109. $this->get_contains_select_expectation('sub0', $choices, 1, true),
  110. $this->get_contains_select_expectation('sub1', $choices, 2, true),
  111. $this->get_contains_select_expectation('sub2', $choices, null, true),
  112. $this->get_contains_select_expectation('sub3', $choices, null, true),
  113. $this->get_contains_question_text_expectation($m),
  114. $this->get_does_not_contain_feedback_expectation());
  115. // Finish the attempt.
  116. $this->quba->finish_all_questions();
  117. // Verify.
  118. $this->check_current_state(question_state::$gradedpartial);
  119. $this->check_current_mark(2);
  120. $this->check_current_output(
  121. $this->get_contains_select_expectation('sub0', $choices, 1, false),
  122. $this->get_contains_select_expectation('sub1', $choices, 2, false),
  123. $this->get_contains_select_expectation('sub2', $choices, null, false),
  124. $this->get_contains_select_expectation('sub3', $choices, null, false),
  125. $this->get_contains_partcorrect_expectation());
  126. }
  127. public function test_interactive_correct_no_submit() {
  128. // Create a matching question.
  129. $m = test_question_maker::make_a_matching_question();
  130. $m->hints = array(
  131. new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, false),
  132. new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
  133. );
  134. $m->shufflestems = false;
  135. $this->start_attempt_at_question($m, 'interactive', 4);
  136. $choiceorder = $m->get_choice_order();
  137. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  138. $choices = array(0 => get_string('choose') . '...');
  139. foreach ($choiceorder as $key => $choice) {
  140. $choices[$key] = $m->choices[$choice];
  141. }
  142. // Check the initial state.
  143. $this->check_current_state(question_state::$todo);
  144. $this->check_current_mark(null);
  145. $this->check_current_output(
  146. $this->get_contains_select_expectation('sub0', $choices, null, true),
  147. $this->get_contains_select_expectation('sub1', $choices, null, true),
  148. $this->get_contains_select_expectation('sub2', $choices, null, true),
  149. $this->get_contains_select_expectation('sub3', $choices, null, true),
  150. $this->get_contains_submit_button_expectation(true),
  151. $this->get_does_not_contain_feedback_expectation(),
  152. $this->get_tries_remaining_expectation(3),
  153. $this->get_no_hint_visible_expectation());
  154. // Save the right answer.
  155. $this->process_submission(array('sub0' => $orderforchoice[1],
  156. 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
  157. 'sub3' => $orderforchoice[1]));
  158. // Finish the attempt without clicking check.
  159. $this->quba->finish_all_questions();
  160. // Verify.
  161. $this->check_current_state(question_state::$gradedright);
  162. $this->check_current_mark(4);
  163. $this->check_current_output(
  164. $this->get_contains_select_expectation('sub0', $choices, 1, false),
  165. $this->get_contains_select_expectation('sub1', $choices, 2, false),
  166. $this->get_contains_select_expectation('sub2', $choices, 2, false),
  167. $this->get_contains_select_expectation('sub3', $choices, 1, false),
  168. $this->get_contains_submit_button_expectation(false),
  169. $this->get_contains_correct_expectation(),
  170. $this->get_no_hint_visible_expectation());
  171. }
  172. public function test_interactive_partial_no_submit() {
  173. // Create a matching question.
  174. $m = test_question_maker::make_a_matching_question();
  175. $m->hints = array(
  176. new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, false),
  177. new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
  178. );
  179. $m->shufflestems = false;
  180. $this->start_attempt_at_question($m, 'interactive', 4);
  181. $choiceorder = $m->get_choice_order();
  182. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  183. $choices = array(0 => get_string('choose') . '...');
  184. foreach ($choiceorder as $key => $choice) {
  185. $choices[$key] = $m->choices[$choice];
  186. }
  187. // Check the initial state.
  188. $this->check_current_state(question_state::$todo);
  189. $this->check_current_mark(null);
  190. $this->check_current_output(
  191. $this->get_contains_select_expectation('sub0', $choices, null, true),
  192. $this->get_contains_select_expectation('sub1', $choices, null, true),
  193. $this->get_contains_select_expectation('sub2', $choices, null, true),
  194. $this->get_contains_select_expectation('sub3', $choices, null, true),
  195. $this->get_contains_submit_button_expectation(true),
  196. $this->get_does_not_contain_feedback_expectation(),
  197. $this->get_tries_remaining_expectation(3),
  198. $this->get_no_hint_visible_expectation());
  199. // Save the right answer.
  200. $this->process_submission(array('sub0' => $orderforchoice[1],
  201. 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[1],
  202. 'sub3' => '0'));
  203. // Finish the attempt without clicking check.
  204. $this->quba->finish_all_questions();
  205. // Verify.
  206. $this->check_current_state(question_state::$gradedpartial);
  207. $this->check_current_mark(2);
  208. $this->check_current_output(
  209. $this->get_contains_select_expectation('sub0', $choices, 1, false),
  210. $this->get_contains_select_expectation('sub1', $choices, 2, false),
  211. $this->get_contains_select_expectation('sub2', $choices, 1, false),
  212. $this->get_contains_select_expectation('sub3', $choices, null, false),
  213. $this->get_contains_submit_button_expectation(false),
  214. $this->get_contains_partcorrect_expectation(),
  215. $this->get_no_hint_visible_expectation());
  216. }
  217. public function test_interactive_with_invalid() {
  218. // Create a matching question.
  219. $m = test_question_maker::make_a_matching_question();
  220. $m->hints = array(
  221. new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, false),
  222. new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
  223. );
  224. $m->shufflestems = false;
  225. $this->start_attempt_at_question($m, 'interactive', 4);
  226. $choiceorder = $m->get_choice_order();
  227. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  228. $choices = array(0 => get_string('choose') . '...');
  229. foreach ($choiceorder as $key => $choice) {
  230. $choices[$key] = $m->choices[$choice];
  231. }
  232. // Check the initial state.
  233. $this->check_current_state(question_state::$todo);
  234. $this->check_current_mark(null);
  235. $this->check_current_output(
  236. $this->get_contains_select_expectation('sub0', $choices, null, true),
  237. $this->get_contains_select_expectation('sub1', $choices, null, true),
  238. $this->get_contains_select_expectation('sub2', $choices, null, true),
  239. $this->get_contains_select_expectation('sub3', $choices, null, true),
  240. $this->get_contains_submit_button_expectation(true),
  241. $this->get_does_not_contain_feedback_expectation(),
  242. $this->get_tries_remaining_expectation(3),
  243. $this->get_no_hint_visible_expectation());
  244. // Try to submit an invalid answer.
  245. $this->process_submission(array('sub0' => '0',
  246. 'sub1' => '0', 'sub2' => '0',
  247. 'sub3' => '0', '-submit' => '1'));
  248. // Verify.
  249. $this->check_current_state(question_state::$invalid);
  250. $this->check_current_mark(null);
  251. $this->check_current_output(
  252. $this->get_contains_select_expectation('sub0', $choices, null, true),
  253. $this->get_contains_select_expectation('sub1', $choices, null, true),
  254. $this->get_contains_select_expectation('sub2', $choices, null, true),
  255. $this->get_contains_select_expectation('sub3', $choices, null, true),
  256. $this->get_contains_submit_button_expectation(true),
  257. $this->get_does_not_contain_feedback_expectation(),
  258. $this->get_invalid_answer_expectation(),
  259. $this->get_no_hint_visible_expectation());
  260. // Now submit the right answer.
  261. $this->process_submission(array('sub0' => $orderforchoice[1],
  262. 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
  263. 'sub3' => $orderforchoice[1], '-submit' => '1'));
  264. // Verify.
  265. $this->check_current_state(question_state::$gradedright);
  266. $this->check_current_mark(4);
  267. $this->check_current_output(
  268. $this->get_contains_select_expectation('sub0', $choices, 1, false),
  269. $this->get_contains_select_expectation('sub1', $choices, 2, false),
  270. $this->get_contains_select_expectation('sub2', $choices, 2, false),
  271. $this->get_contains_select_expectation('sub3', $choices, 1, false),
  272. $this->get_contains_submit_button_expectation(false),
  273. $this->get_contains_correct_expectation(),
  274. $this->get_no_hint_visible_expectation());
  275. }
  276. public function test_match_with_tricky_html_choices() {
  277. // Create a matching question.
  278. $m = test_question_maker::make_a_matching_question();
  279. $m->stems = array(
  280. 1 => '(1, 2]',
  281. 2 => '[1, 2]',
  282. 3 => '[1, 2)',
  283. );
  284. $m->choices = array(
  285. 1 => '1 < x ≤ 2',
  286. 2 => '1 ≤ x ≤ 2',
  287. 3 => '1 ≤ x < 2',
  288. );
  289. $m->right = array(1 => 1, 2 => 2, 3 => 3);
  290. $m->shufflestems = false;
  291. $this->start_attempt_at_question($m, 'deferredfeedback', 3);
  292. $choiceorder = $m->get_choice_order();
  293. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  294. $choices = array(0 => get_string('choose') . '...');
  295. foreach ($choiceorder as $key => $choice) {
  296. $choices[$key] = $m->choices[$choice];
  297. }
  298. // Check the initial state.
  299. $this->check_current_state(question_state::$todo);
  300. $this->check_current_mark(null);
  301. $this->check_current_output(
  302. $this->get_contains_select_expectation('sub0', $choices, null, true),
  303. $this->get_contains_select_expectation('sub1', $choices, null, true),
  304. $this->get_contains_select_expectation('sub2', $choices, null, true),
  305. $this->get_contains_question_text_expectation($m),
  306. $this->get_does_not_contain_feedback_expectation());
  307. $this->check_step_count(1);
  308. $rightresponse = array('sub0' => $orderforchoice[1],
  309. 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[3]);
  310. $rightresponsesummary =
  311. '(1, 2] -> 1 < x ≤ 2; [1, 2] -> 1 ≤ x ≤ 2; [1, 2) -> 1 ≤ x < 2';
  312. $this->process_submission($rightresponse);
  313. $this->process_submission(array('-finish' => 1));
  314. $this->assertEqual($rightresponsesummary, $m->summarise_response($rightresponse));
  315. $this->displayoptions->history = 1;
  316. $this->check_current_output(
  317. new PatternExpectation('/' .
  318. preg_quote(htmlspecialchars($rightresponsesummary), '/') . '/'));
  319. }
  320. public function test_match_clear_wrong() {
  321. // Create a matching question.
  322. $m = test_question_maker::make_a_matching_question();
  323. $m->hints = array(
  324. new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, true),
  325. new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
  326. );
  327. $m->shufflestems = false;
  328. $this->start_attempt_at_question($m, 'interactive', 4);
  329. $choiceorder = $m->get_choice_order();
  330. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  331. $choices = array(0 => get_string('choose') . '...');
  332. foreach ($choiceorder as $key => $choice) {
  333. $choices[$key] = $m->choices[$choice];
  334. }
  335. // Check the initial state.
  336. $this->check_current_state(question_state::$todo);
  337. $this->check_current_mark(null);
  338. $this->check_current_output(
  339. $this->get_contains_select_expectation('sub0', $choices, null, true),
  340. $this->get_contains_select_expectation('sub1', $choices, null, true),
  341. $this->get_contains_select_expectation('sub2', $choices, null, true),
  342. $this->get_contains_select_expectation('sub3', $choices, null, true),
  343. $this->get_contains_submit_button_expectation(true),
  344. $this->get_does_not_contain_feedback_expectation(),
  345. $this->get_tries_remaining_expectation(3),
  346. $this->get_no_hint_visible_expectation());
  347. // Submit a completely wrong response.
  348. $this->process_submission(array('sub0' => $orderforchoice[3],
  349. 'sub1' => $orderforchoice[3], 'sub2' => $orderforchoice[3],
  350. 'sub3' => $orderforchoice[3], '-submit' => 1));
  351. // Verify.
  352. $this->check_current_state(question_state::$todo);
  353. $this->check_current_mark(null);
  354. $this->check_current_output(
  355. $this->get_contains_select_expectation('sub0', $choices, 3, false),
  356. $this->get_contains_select_expectation('sub1', $choices, 3, false),
  357. $this->get_contains_select_expectation('sub2', $choices, 3, false),
  358. $this->get_contains_select_expectation('sub3', $choices, 3, false),
  359. $this->get_contains_hidden_expectation(
  360. $this->quba->get_field_prefix($this->slot) . 'sub0', '0'),
  361. $this->get_contains_hidden_expectation(
  362. $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
  363. $this->get_contains_hidden_expectation(
  364. $this->quba->get_field_prefix($this->slot) . 'sub2', '0'),
  365. $this->get_contains_hidden_expectation(
  366. $this->quba->get_field_prefix($this->slot) . 'sub3', '0'),
  367. $this->get_contains_submit_button_expectation(false),
  368. $this->get_contains_hint_expectation('This is the first hint.'));
  369. // Try again.
  370. $this->process_submission(array('sub0' => 0,
  371. 'sub1' => 0, 'sub2' => 0,
  372. 'sub3' => 0, '-tryagain' => 1));
  373. // Verify.
  374. $this->check_current_state(question_state::$todo);
  375. $this->check_current_mark(null);
  376. $this->check_current_output(
  377. $this->get_contains_select_expectation('sub0', $choices, null, true),
  378. $this->get_contains_select_expectation('sub1', $choices, null, true),
  379. $this->get_contains_select_expectation('sub2', $choices, null, true),
  380. $this->get_contains_select_expectation('sub3', $choices, null, true),
  381. $this->get_contains_submit_button_expectation(true),
  382. $this->get_does_not_contain_feedback_expectation(),
  383. $this->get_tries_remaining_expectation(2),
  384. $this->get_no_hint_visible_expectation());
  385. // Submit a partially wrong response.
  386. $this->process_submission(array('sub0' => $orderforchoice[3],
  387. 'sub1' => $orderforchoice[3], 'sub2' => $orderforchoice[2],
  388. 'sub3' => $orderforchoice[1], '-submit' => 1));
  389. // Verify.
  390. $this->check_current_state(question_state::$todo);
  391. $this->check_current_mark(null);
  392. $this->check_current_output(
  393. $this->get_contains_select_expectation('sub0', $choices, 3, false),
  394. $this->get_contains_select_expectation('sub1', $choices, 3, false),
  395. $this->get_contains_select_expectation('sub2', $choices, 2, false),
  396. $this->get_contains_select_expectation('sub3', $choices, 1, false),
  397. $this->get_contains_hidden_expectation(
  398. $this->quba->get_field_prefix($this->slot) . 'sub0', '0'),
  399. $this->get_contains_hidden_expectation(
  400. $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
  401. $this->get_contains_hidden_expectation(
  402. $this->quba->get_field_prefix($this->slot) . 'sub2', $orderforchoice[2]),
  403. $this->get_contains_hidden_expectation(
  404. $this->quba->get_field_prefix($this->slot) . 'sub3', $orderforchoice[1]),
  405. $this->get_contains_submit_button_expectation(false),
  406. $this->get_contains_hint_expectation('This is the second hint.'));
  407. // Try again.
  408. $this->process_submission(array('sub0' => 0,
  409. 'sub1' => 0, 'sub2' => $orderforchoice[2],
  410. 'sub3' => $orderforchoice[1], '-tryagain' => 1));
  411. // Verify.
  412. $this->check_current_state(question_state::$todo);
  413. $this->check_current_mark(null);
  414. $this->check_current_output(
  415. $this->get_contains_select_expectation('sub0', $choices, null, true),
  416. $this->get_contains_select_expectation('sub1', $choices, null, true),
  417. $this->get_contains_select_expectation('sub2', $choices, 2, true),
  418. $this->get_contains_select_expectation('sub3', $choices, 1, true),
  419. $this->get_contains_submit_button_expectation(true),
  420. $this->get_does_not_contain_feedback_expectation(),
  421. $this->get_tries_remaining_expectation(1),
  422. $this->get_no_hint_visible_expectation());
  423. }
  424. }