PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/question/behaviour/deferredfeedback/tests/walkthrough_test.php

https://github.com/pauln/moodle
PHP | 309 lines | 185 code | 55 blank | 69 comment | 1 complexity | 0266bb3ec97c5b316f9512b03f366758 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 deferred feedback
  18. * behaviour.
  19. *
  20. * @package qbehaviour
  21. * @subpackage deferredfeedback
  22. * @copyright 2009 The Open University
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once(dirname(__FILE__) . '/../../../engine/lib.php');
  28. require_once(dirname(__FILE__) . '/../../../engine/tests/helpers.php');
  29. /**
  30. * Unit tests for the deferred feedback behaviour.
  31. *
  32. * @copyright 2009 The Open University
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class qbehaviour_deferredfeedback_walkthrough_test extends qbehaviour_walkthrough_test_base {
  36. public function test_deferredfeedback_feedback_truefalse() {
  37. // Create a true-false question with correct answer true.
  38. $tf = test_question_maker::make_question('truefalse', 'true');
  39. $this->start_attempt_at_question($tf, 'deferredfeedback', 2);
  40. // Check the initial state.
  41. $this->check_current_state(question_state::$todo);
  42. $this->check_output_contains_lang_string('notyetanswered', 'question');
  43. $this->check_current_mark(null);
  44. $this->check_current_output($this->get_contains_question_text_expectation($tf),
  45. $this->get_does_not_contain_feedback_expectation());
  46. $this->assertEquals(get_string('true', 'qtype_truefalse'),
  47. $this->quba->get_right_answer_summary($this->slot));
  48. $this->assertRegExp('/' . preg_quote($tf->questiontext, '/') . '/',
  49. $this->quba->get_question_summary($this->slot));
  50. $this->assertNull($this->quba->get_response_summary($this->slot));
  51. // Process a true answer and check the expected result.
  52. $this->process_submission(array('answer' => 1));
  53. $this->check_current_state(question_state::$complete);
  54. $this->check_output_contains_lang_string('answersaved', 'question');
  55. $this->check_current_mark(null);
  56. $this->check_current_output($this->get_contains_tf_true_radio_expectation(true, true),
  57. $this->get_does_not_contain_correctness_expectation(),
  58. $this->get_does_not_contain_feedback_expectation());
  59. // Process the same data again, check it does not create a new step.
  60. $numsteps = $this->get_step_count();
  61. $this->process_submission(array('answer' => 1));
  62. $this->check_step_count($numsteps);
  63. // Process different data, check it creates a new step.
  64. $this->process_submission(array('answer' => 0));
  65. $this->check_step_count($numsteps + 1);
  66. $this->check_current_state(question_state::$complete);
  67. // Change back, check it creates a new step.
  68. $this->process_submission(array('answer' => 1));
  69. $this->check_step_count($numsteps + 2);
  70. // Finish the attempt.
  71. $this->quba->finish_all_questions();
  72. // Verify.
  73. $this->check_current_state(question_state::$gradedright);
  74. $this->check_current_mark(2);
  75. $this->check_current_output($this->get_contains_correct_expectation(),
  76. $this->get_contains_tf_true_radio_expectation(false, true),
  77. new question_pattern_expectation('/class="r0 correct"/'));
  78. $this->assertEquals(get_string('true', 'qtype_truefalse'),
  79. $this->quba->get_response_summary($this->slot));
  80. // Process a manual comment.
  81. $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
  82. $this->check_current_state(question_state::$mangrpartial);
  83. $this->check_current_mark(1);
  84. $this->check_current_output(
  85. new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
  86. // Now change the correct answer to the question, and regrade.
  87. $tf->rightanswer = false;
  88. $this->quba->regrade_all_questions();
  89. // Verify.
  90. $this->check_current_state(question_state::$mangrpartial);
  91. $this->check_current_mark(1);
  92. $autogradedstep = $this->get_step($this->get_step_count() - 2);
  93. $this->assertEquals($autogradedstep->get_fraction(), 0, '', 0.0000001);
  94. }
  95. public function test_deferredfeedback_feedback_multichoice_single() {
  96. // Create a true-false question with correct answer true.
  97. $mc = test_question_maker::make_a_multichoice_single_question();
  98. $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
  99. // Start a deferred feedback attempt and add the question to it.
  100. $rightindex = $this->get_mc_right_answer_index($mc);
  101. $this->check_current_state(question_state::$todo);
  102. $this->check_output_contains_lang_string('notyetanswered', 'question');
  103. $this->check_current_mark(null);
  104. $this->check_current_output(
  105. $this->get_contains_question_text_expectation($mc),
  106. $this->get_contains_mc_radio_expectation(0, true, false),
  107. $this->get_contains_mc_radio_expectation(1, true, false),
  108. $this->get_contains_mc_radio_expectation(2, true, false),
  109. $this->get_does_not_contain_feedback_expectation());
  110. // Process the data extracted for this question.
  111. $this->process_submission(array('answer' => $rightindex));
  112. // Verify.
  113. $this->check_current_state(question_state::$complete);
  114. $this->check_output_contains_lang_string('answersaved', 'question');
  115. $this->check_current_mark(null);
  116. $this->check_current_output(
  117. $this->get_contains_mc_radio_expectation($rightindex, true, true),
  118. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
  119. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
  120. $this->get_does_not_contain_correctness_expectation(),
  121. $this->get_does_not_contain_feedback_expectation());
  122. // Finish the attempt.
  123. $this->quba->finish_all_questions();
  124. // Verify.
  125. $this->check_current_state(question_state::$gradedright);
  126. $this->check_current_mark(3);
  127. $this->check_current_output(
  128. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  129. $this->get_contains_correct_expectation());
  130. // Now change the correct answer to the question, and regrade.
  131. $mc->answers[13]->fraction = -0.33333333;
  132. $mc->answers[14]->fraction = 1;
  133. $this->quba->regrade_all_questions();
  134. // Verify.
  135. $this->check_current_state(question_state::$gradedwrong);
  136. $this->check_current_mark(-1);
  137. $this->check_current_output(
  138. $this->get_contains_incorrect_expectation());
  139. }
  140. public function test_deferredfeedback_resume_multichoice_single() {
  141. // Create a multiple-choice question.
  142. $mc = test_question_maker::make_a_multichoice_single_question();
  143. // Attempt it getting it wrong.
  144. $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
  145. $rightindex = $this->get_mc_right_answer_index($mc);
  146. $wrongindex = ($rightindex + 1) % 3;
  147. $this->check_current_state(question_state::$todo);
  148. $this->check_output_contains_lang_string('notyetanswered', 'question');
  149. $this->check_current_mark(null);
  150. $this->process_submission(array('answer' => $wrongindex));
  151. $this->quba->finish_all_questions();
  152. // Verify.
  153. $this->check_current_state(question_state::$gradedwrong);
  154. $this->check_current_mark(-1);
  155. $this->check_current_output(
  156. $this->get_contains_mc_radio_expectation($wrongindex, false, true),
  157. $this->get_contains_incorrect_expectation());
  158. // Save the old attempt.
  159. $oldqa = $this->quba->get_question_attempt($this->slot);
  160. // Reinitialise.
  161. $this->setUp();
  162. $this->quba->set_preferred_behaviour('deferredfeedback');
  163. $this->slot = $this->quba->add_question($mc, 3);
  164. $this->quba->start_question_based_on($this->slot, $oldqa);
  165. // Verify.
  166. $this->check_current_state(question_state::$complete);
  167. $this->check_output_contains_lang_string('notchanged', 'question');
  168. $this->check_current_mark(null);
  169. $this->check_current_output(
  170. $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  171. $this->get_does_not_contain_feedback_expectation(),
  172. $this->get_does_not_contain_correctness_expectation());
  173. // Now get it right.
  174. $this->process_submission(array('answer' => $rightindex));
  175. $this->quba->finish_all_questions();
  176. // Verify.
  177. $this->check_current_state(question_state::$gradedright);
  178. $this->check_current_mark(3);
  179. $this->check_current_output(
  180. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  181. $this->get_contains_correct_expectation());
  182. }
  183. public function test_deferredfeedback_resume_multichoice_single_emptyanswer_first() {
  184. // Create a multiple-choice question.
  185. $mc = test_question_maker::make_a_multichoice_single_question();
  186. // Attempt it and submit empty.
  187. $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
  188. $rightindex = $this->get_mc_right_answer_index($mc);
  189. $wrongindex = ($rightindex + 1) % 3;
  190. $this->check_current_state(question_state::$todo);
  191. $this->check_output_contains_lang_string('notyetanswered', 'question');
  192. $this->check_current_mark(null);
  193. $this->process_submission(array('-submit' => 1));
  194. $this->quba->finish_all_questions();
  195. // Verify.
  196. $this->check_current_state(question_state::$gaveup);
  197. $this->check_current_mark(null);
  198. $this->check_current_output(
  199. $this->get_contains_mc_radio_expectation(0, false, false),
  200. $this->get_contains_mc_radio_expectation(1, false, false),
  201. $this->get_contains_mc_radio_expectation(2, false, false),
  202. $this->get_contains_general_feedback_expectation($mc));
  203. // Save the old attempt.
  204. $oldqa = $this->quba->get_question_attempt($this->slot);
  205. // Reinitialise.
  206. $this->setUp();
  207. $this->quba->set_preferred_behaviour('deferredfeedback');
  208. $this->slot = $this->quba->add_question($mc, 3);
  209. $this->quba->start_question_based_on($this->slot, $oldqa);
  210. // Verify.
  211. $this->check_current_state(question_state::$todo);
  212. $this->check_output_contains_lang_string('notyetanswered', 'question');
  213. $this->check_current_mark(null);
  214. $this->check_current_output(
  215. $this->get_contains_mc_radio_expectation(0, true, false),
  216. $this->get_contains_mc_radio_expectation(1, true, false),
  217. $this->get_contains_mc_radio_expectation(2, true, false),
  218. $this->get_does_not_contain_feedback_expectation(),
  219. $this->get_does_not_contain_correctness_expectation());
  220. // Now get it wrong.
  221. $this->process_submission(array('answer' => $wrongindex));
  222. $this->quba->finish_all_questions();
  223. // Verify.
  224. $this->check_current_state(question_state::$gradedwrong);
  225. $this->check_current_mark(-1);
  226. $this->check_current_output(
  227. $this->get_contains_mc_radio_expectation($wrongindex, false, true),
  228. $this->get_contains_incorrect_expectation());
  229. // Save the old attempt.
  230. $oldqa = $this->quba->get_question_attempt($this->slot);
  231. // Reinitialise.
  232. $this->setUp();
  233. $this->quba->set_preferred_behaviour('deferredfeedback');
  234. $this->slot = $this->quba->add_question($mc, 3);
  235. $this->quba->start_question_based_on($this->slot, $oldqa);
  236. // Verify.
  237. $this->check_current_state(question_state::$complete);
  238. $this->check_output_contains_lang_string('notchanged', 'question');
  239. $this->check_current_mark(null);
  240. $this->check_current_output(
  241. $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  242. $this->get_does_not_contain_feedback_expectation(),
  243. $this->get_does_not_contain_correctness_expectation());
  244. // Now get it right.
  245. $this->process_submission(array('answer' => $rightindex));
  246. $this->quba->finish_all_questions();
  247. // Verify.
  248. $this->check_current_state(question_state::$gradedright);
  249. $this->check_current_mark(3);
  250. $this->check_current_output(
  251. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  252. $this->get_contains_correct_expectation());
  253. }
  254. }