PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/question/behaviour/immediatecbm/simpletest/testwalkthrough.php

https://github.com/DeanLennard/Moodle-Question-Engine-2
PHP | 291 lines | 175 code | 51 blank | 65 comment | 1 complexity | dd9d834321121868888037d4a22d17ba 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 immediate cbm
  18. * behaviour.
  19. *
  20. * @package qbehaviour
  21. * @subpackage immediatecbm
  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. require_once(dirname(__FILE__) . '/../../../engine/lib.php');
  27. require_once(dirname(__FILE__) . '/../../../engine/simpletest/helpers.php');
  28. /**
  29. * Unit tests for the immediate cbm behaviour.
  30. *
  31. * @copyright 2009 The Open University
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class qbehaviour_immediatecbm_walkthrough_test extends qbehaviour_walkthrough_test_base {
  35. public function test_immediatecbm_feedback_multichoice_right() {
  36. // Create a true-false question with correct answer true.
  37. $mc = test_question_maker::make_a_multichoice_single_question();
  38. $this->start_attempt_at_question($mc, 'immediatecbm');
  39. $rightindex = $this->get_mc_right_answer_index($mc);
  40. $wrongindex = ($rightindex + 1) % 3;
  41. // Check the initial state.
  42. $this->check_current_state(question_state::$todo);
  43. $this->check_current_mark(null);
  44. $this->check_current_output(
  45. $this->get_contains_question_text_expectation($mc),
  46. $this->get_contains_mc_radio_expectation(0, true, false),
  47. $this->get_contains_mc_radio_expectation(1, true, false),
  48. $this->get_contains_mc_radio_expectation(2, true, false),
  49. $this->get_contains_submit_button_expectation(true),
  50. $this->get_does_not_contain_feedback_expectation());
  51. $this->assertEqual('A [' . question_cbm::get_string(question_cbm::HIGH) . ']',
  52. $this->quba->get_right_answer_summary($this->slot));
  53. $this->assertPattern('/' . preg_quote($mc->questiontext) . '/',
  54. $this->quba->get_question_summary($this->slot));
  55. $this->assertNull($this->quba->get_response_summary($this->slot));
  56. // Save the wrong answer.
  57. $this->process_submission(array('answer' => $wrongindex, '-certainty' => 1));
  58. // Verify.
  59. $this->check_current_state(question_state::$todo);
  60. $this->check_current_mark(null);
  61. $this->check_current_output(
  62. $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  63. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  64. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  65. $this->get_contains_submit_button_expectation(true),
  66. $this->get_does_not_contain_correctness_expectation(),
  67. $this->get_does_not_contain_feedback_expectation());
  68. // Submit the right answer.
  69. $this->process_submission(array('answer' => $rightindex, '-certainty' => 2, '-submit' => 1));
  70. // Verify.
  71. $this->check_current_state(question_state::$gradedright);
  72. $this->check_current_mark(2/3);
  73. $this->check_current_output(
  74. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  75. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  76. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  77. $this->get_contains_correct_expectation());
  78. $this->assertEqual('A [' . question_cbm::get_string(2) . ']',
  79. $this->quba->get_response_summary($this->slot));
  80. $numsteps = $this->get_step_count();
  81. // Finish the attempt - should not need to add a new state.
  82. $this->quba->finish_all_questions();
  83. // Verify.
  84. $this->assertEqual($numsteps, $this->get_step_count());
  85. $this->check_current_state(question_state::$gradedright);
  86. $this->check_current_mark(2/3);
  87. $this->check_current_output(
  88. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  89. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  90. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  91. $this->get_contains_correct_expectation());
  92. // Process a manual comment.
  93. $this->manual_grade('Not good enough!', 0.5);
  94. // Verify.
  95. $this->check_current_state(question_state::$mangrpartial);
  96. $this->check_current_mark(0.5);
  97. $this->check_current_output(
  98. $this->get_contains_partcorrect_expectation(),
  99. new PatternExpectation('/' . preg_quote('Not good enough!') . '/'));
  100. // Now change the correct answer to the question, and regrade.
  101. $mc->answers[13]->fraction = -0.33333333;
  102. $mc->answers[15]->fraction = 1;
  103. $this->quba->regrade_all_questions();
  104. // Verify.
  105. $this->check_current_state(question_state::$mangrpartial);
  106. $this->check_current_mark(0.5);
  107. $this->check_current_output(
  108. $this->get_contains_partcorrect_expectation());
  109. $autogradedstep = $this->get_step($this->get_step_count() - 2);
  110. $this->assertWithinMargin($autogradedstep->get_fraction(), -10/9, 0.0000001);
  111. }
  112. public function test_immediatecbm_feedback_multichoice_try_to_submit_blank() {
  113. // Create a true-false question with correct answer true.
  114. $mc = test_question_maker::make_a_multichoice_single_question();
  115. $this->start_attempt_at_question($mc, 'immediatecbm');
  116. // Check the initial state.
  117. $this->check_current_state(question_state::$todo);
  118. $this->check_current_mark(null);
  119. $this->check_current_output(
  120. $this->get_contains_question_text_expectation($mc),
  121. $this->get_contains_mc_radio_expectation(0, true, false),
  122. $this->get_contains_mc_radio_expectation(1, true, false),
  123. $this->get_contains_mc_radio_expectation(2, true, false),
  124. $this->get_contains_submit_button_expectation(true),
  125. $this->get_does_not_contain_feedback_expectation());
  126. // Submit nothing.
  127. $this->process_submission(array('-submit' => 1));
  128. // Verify.
  129. $this->check_current_state(question_state::$invalid);
  130. $this->check_current_mark(null);
  131. $this->check_current_output(
  132. $this->get_contains_mc_radio_expectation(0, true, false),
  133. $this->get_contains_mc_radio_expectation(1, true, false),
  134. $this->get_contains_mc_radio_expectation(2, true, false),
  135. $this->get_contains_submit_button_expectation(true),
  136. $this->get_does_not_contain_correctness_expectation(),
  137. $this->get_contains_validation_error_expectation());
  138. // Finish the attempt.
  139. $this->quba->finish_all_questions();
  140. // Verify.
  141. $this->check_current_state(question_state::$gaveup);
  142. $this->check_current_mark(null);
  143. $this->check_current_output(
  144. $this->get_contains_mc_radio_expectation(0, false, false),
  145. $this->get_contains_mc_radio_expectation(1, false, false),
  146. $this->get_contains_mc_radio_expectation(2, false, false));
  147. // Process a manual comment.
  148. $this->manual_grade('Not good enough!', 0.5);
  149. // Verify.
  150. $this->check_current_state(question_state::$mangrpartial);
  151. $this->check_current_mark(0.5);
  152. $this->check_current_output(
  153. $this->get_contains_partcorrect_expectation(),
  154. new PatternExpectation('/' . preg_quote('Not good enough!') . '/'));
  155. }
  156. public function test_immediatecbm_feedback_shortanswer_try_to_submit_no_certainty() {
  157. // Create a short answer question with correct answer true.
  158. $sa = test_question_maker::make_a_shortanswer_question();
  159. $this->start_attempt_at_question($sa, 'immediatecbm');
  160. // Check the initial state.
  161. $this->check_current_state(question_state::$todo);
  162. $this->check_current_mark(null);
  163. $this->check_current_output(
  164. $this->get_contains_submit_button_expectation(true),
  165. $this->get_does_not_contain_feedback_expectation());
  166. // Submit with certainty missing.
  167. $this->process_submission(array('-submit' => 1, 'answer' => 'frog'));
  168. // Verify.
  169. $this->check_current_state(question_state::$invalid);
  170. $this->check_current_mark(null);
  171. $this->check_current_output(
  172. $this->get_contains_submit_button_expectation(true),
  173. $this->get_does_not_contain_correctness_expectation(),
  174. $this->get_contains_validation_error_expectation());
  175. // Now get it right.
  176. $this->process_submission(array('-submit' => 1, 'answer' => 'frog', '-certainty' => 3));
  177. // Verify.
  178. $this->check_current_state(question_state::$gradedright);
  179. $this->check_current_mark(1);
  180. $this->check_current_output(
  181. $this->get_does_not_contain_validation_error_expectation());
  182. }
  183. public function test_immediatecbm_feedback_multichoice_wrong_on_finish() {
  184. // Create a true-false question with correct answer true.
  185. $mc = test_question_maker::make_a_multichoice_single_question();
  186. $this->start_attempt_at_question($mc, 'immediatecbm');
  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_question_text_expectation($mc),
  192. $this->get_contains_mc_radio_expectation(0, true, false),
  193. $this->get_contains_mc_radio_expectation(1, true, false),
  194. $this->get_contains_mc_radio_expectation(2, true, false),
  195. $this->get_contains_submit_button_expectation(true),
  196. $this->get_does_not_contain_feedback_expectation());
  197. $rightindex = $this->get_mc_right_answer_index($mc);
  198. $wrongindex = ($rightindex + 1) % 3;
  199. // Save the wrong answer.
  200. $this->process_submission(array('answer' => $wrongindex, '-certainty' => 3));
  201. // Verify.
  202. $this->check_current_state(question_state::$todo);
  203. $this->check_current_mark(null);
  204. $this->check_current_output(
  205. $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  206. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  207. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  208. $this->get_contains_submit_button_expectation(true),
  209. $this->get_does_not_contain_correctness_expectation());
  210. // Finish the attempt.
  211. $this->quba->finish_all_questions();
  212. // Verify.
  213. $this->check_current_state(question_state::$gradedwrong);
  214. $this->check_current_mark(-3);
  215. $this->check_current_output(
  216. $this->get_contains_mc_radio_expectation($wrongindex, false, true),
  217. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, false, false),
  218. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, false, false),
  219. $this->get_contains_incorrect_expectation());
  220. }
  221. public function test_immediatecbm_cbm_truefalse_no_certainty_feedback_when_not_answered() {
  222. // Create a true-false question with correct answer true.
  223. $tf = test_question_maker::make_a_truefalse_question();
  224. $this->start_attempt_at_question($tf, 'deferredcbm', 2);
  225. // Verify.
  226. $this->check_current_state(question_state::$todo);
  227. $this->check_current_mark(null);
  228. $this->check_current_output(
  229. $this->get_does_not_contain_correctness_expectation(),
  230. $this->get_contains_cbm_radio_expectation(1, true, false),
  231. $this->get_does_not_contain_feedback_expectation());
  232. // Finish without answering.
  233. $this->quba->finish_all_questions();
  234. // Verify.
  235. $this->check_current_state(question_state::$gaveup);
  236. $this->check_current_mark(null);
  237. $this->check_current_output(
  238. new NoPatternExpectation('/class=\"im-feedback/'));
  239. }
  240. }