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

/question/behaviour/immediatefeedback/tests/walkthrough_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 246 lines | 149 code | 40 blank | 57 comment | 1 complexity | d2b017c805155a188d60a802052c0ff9 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 feedback
  18. * behaviour.
  19. *
  20. * @package qbehaviour
  21. * @subpackage immediatefeedback
  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 immediate 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_immediatefeedback_walkthrough_test extends qbehaviour_walkthrough_test_base {
  36. public function test_immediatefeedback_feedback_multichoice_right() {
  37. // Create a true-false question with correct answer true.
  38. $mc = test_question_maker::make_a_multichoice_single_question();
  39. $this->start_attempt_at_question($mc, 'immediatefeedback');
  40. $rightindex = $this->get_mc_right_answer_index($mc);
  41. $wrongindex = ($rightindex + 1) % 3;
  42. // Check the initial state.
  43. $this->check_current_state(question_state::$todo);
  44. $this->check_current_mark(null);
  45. $this->check_current_output(
  46. $this->get_contains_question_text_expectation($mc),
  47. $this->get_contains_mc_radio_expectation(0, true, false),
  48. $this->get_contains_mc_radio_expectation(1, true, false),
  49. $this->get_contains_mc_radio_expectation(2, true, false),
  50. $this->get_contains_submit_button_expectation(true),
  51. $this->get_does_not_contain_feedback_expectation());
  52. // Save the wrong answer.
  53. $this->process_submission(array('answer' => $wrongindex));
  54. // Verify.
  55. $this->check_current_state(question_state::$todo);
  56. $this->check_current_mark(null);
  57. $this->check_current_output(
  58. $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  59. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  60. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  61. $this->get_contains_submit_button_expectation(true),
  62. $this->get_does_not_contain_correctness_expectation(),
  63. $this->get_does_not_contain_feedback_expectation());
  64. // Submit the right answer.
  65. $this->process_submission(array('answer' => $rightindex, '-submit' => 1));
  66. // Verify.
  67. $this->check_current_state(question_state::$gradedright);
  68. $this->check_current_mark(1);
  69. $this->check_current_output(
  70. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  71. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  72. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  73. $this->get_contains_correct_expectation());
  74. $this->assertEquals('A',
  75. $this->quba->get_response_summary($this->slot));
  76. $numsteps = $this->get_step_count();
  77. // Now try to save again - as if the user clicked next in the quiz.
  78. $this->process_submission(array('answer' => $rightindex));
  79. // Verify.
  80. $this->assertEquals($numsteps, $this->get_step_count());
  81. $this->check_current_state(question_state::$gradedright);
  82. $this->check_current_mark(1);
  83. $this->check_current_output(
  84. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  85. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  86. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  87. $this->get_contains_correct_expectation());
  88. // Finish the attempt - should not need to add a new state.
  89. $this->quba->finish_all_questions();
  90. // Verify.
  91. $this->assertEquals($numsteps, $this->get_step_count());
  92. $this->check_current_state(question_state::$gradedright);
  93. $this->check_current_mark(1);
  94. $this->check_current_output(
  95. $this->get_contains_mc_radio_expectation($rightindex, false, true),
  96. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  97. $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
  98. $this->get_contains_correct_expectation());
  99. // Process a manual comment.
  100. $this->manual_grade('Not good enough!', 0.5, FORMAT_HTML);
  101. // Verify.
  102. $this->check_current_state(question_state::$mangrpartial);
  103. $this->check_current_mark(0.5);
  104. $this->check_current_output(
  105. $this->get_contains_partcorrect_expectation(),
  106. new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
  107. // Now change the correct answer to the question, and regrade.
  108. $mc->answers[13]->fraction = -0.33333333;
  109. $mc->answers[15]->fraction = 1;
  110. $this->quba->regrade_all_questions();
  111. // Verify.
  112. $this->check_current_state(question_state::$mangrpartial);
  113. $this->check_current_mark(0.5);
  114. $this->check_current_output(
  115. $this->get_contains_partcorrect_expectation());
  116. $autogradedstep = $this->get_step($this->get_step_count() - 2);
  117. $this->assertEquals($autogradedstep->get_fraction(), -0.3333333, '', 0.0000001);
  118. }
  119. public function test_immediatefeedback_feedback_multichoice_try_to_submit_blank() {
  120. // Create a true-false question with correct answer true.
  121. $mc = test_question_maker::make_a_multichoice_single_question();
  122. $this->start_attempt_at_question($mc, 'immediatefeedback');
  123. // Check the initial state.
  124. $this->check_current_state(question_state::$todo);
  125. $this->check_current_mark(null);
  126. $this->check_current_output(
  127. $this->get_contains_question_text_expectation($mc),
  128. $this->get_contains_mc_radio_expectation(0, true, false),
  129. $this->get_contains_mc_radio_expectation(1, true, false),
  130. $this->get_contains_mc_radio_expectation(2, true, false),
  131. $this->get_contains_submit_button_expectation(true),
  132. $this->get_does_not_contain_feedback_expectation());
  133. // Submit nothing.
  134. $this->process_submission(array('-submit' => 1));
  135. // Verify.
  136. $this->check_current_state(question_state::$invalid);
  137. $this->check_current_mark(null);
  138. $this->check_current_output(
  139. $this->get_contains_mc_radio_expectation(0, true, false),
  140. $this->get_contains_mc_radio_expectation(1, true, false),
  141. $this->get_contains_mc_radio_expectation(2, true, false),
  142. $this->get_contains_submit_button_expectation(true),
  143. $this->get_does_not_contain_correctness_expectation(),
  144. $this->get_does_not_contain_feedback_expectation(),
  145. $this->get_contains_validation_error_expectation());
  146. $this->assertNull($this->quba->get_response_summary($this->slot));
  147. // Finish the attempt.
  148. $this->quba->finish_all_questions();
  149. // Verify.
  150. $this->check_current_state(question_state::$gaveup);
  151. $this->check_current_mark(null);
  152. $this->check_current_output(
  153. $this->get_contains_mc_radio_expectation(0, false, false),
  154. $this->get_contains_mc_radio_expectation(1, false, false),
  155. $this->get_contains_mc_radio_expectation(2, false, false));
  156. // Process a manual comment.
  157. $this->manual_grade('Not good enough!', 0.5, FORMAT_HTML);
  158. // Verify.
  159. $this->check_current_state(question_state::$mangrpartial);
  160. $this->check_current_mark(0.5);
  161. $this->check_current_output(
  162. $this->get_contains_partcorrect_expectation(),
  163. new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
  164. }
  165. public function test_immediatefeedback_feedback_multichoice_wrong_on_finish() {
  166. // Create a true-false question with correct answer true.
  167. $mc = test_question_maker::make_a_multichoice_single_question();
  168. $this->start_attempt_at_question($mc, 'immediatefeedback');
  169. // Check the initial state.
  170. $this->check_current_state(question_state::$todo);
  171. $this->check_current_mark(null);
  172. $this->check_current_output(
  173. $this->get_contains_question_text_expectation($mc),
  174. $this->get_contains_mc_radio_expectation(0, true, false),
  175. $this->get_contains_mc_radio_expectation(1, true, false),
  176. $this->get_contains_mc_radio_expectation(2, true, false),
  177. $this->get_contains_submit_button_expectation(true),
  178. $this->get_does_not_contain_feedback_expectation());
  179. $rightindex = $this->get_mc_right_answer_index($mc);
  180. $wrongindex = ($rightindex + 1) % 3;
  181. // Save the wrong answer.
  182. $this->process_submission(array('answer' => $wrongindex));
  183. // Verify.
  184. $this->check_current_state(question_state::$todo);
  185. $this->check_current_mark(null);
  186. $this->check_current_output(
  187. $this->get_contains_mc_radio_expectation($wrongindex, true, true),
  188. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  189. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
  190. $this->get_contains_submit_button_expectation(true),
  191. $this->get_does_not_contain_correctness_expectation(),
  192. $this->get_does_not_contain_feedback_expectation());
  193. // Finish the attempt.
  194. $this->quba->finish_all_questions();
  195. // Verify.
  196. $this->check_current_state(question_state::$gradedwrong);
  197. $this->check_current_mark(-0.3333333);
  198. $this->check_current_output(
  199. $this->get_contains_mc_radio_expectation($wrongindex, false, true),
  200. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, false, false),
  201. $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, false, false),
  202. $this->get_contains_incorrect_expectation());
  203. $this->assertRegExp('/B|C/',
  204. $this->quba->get_response_summary($this->slot));
  205. }
  206. }