PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/question/engine/tests/questionusagebyactivity_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 284 lines | 181 code | 45 blank | 58 comment | 1 complexity | fef27a01fbb3625e5a049242a21ba8b1 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 for the question_usage_by_activity class.
  18. *
  19. * @package moodlecore
  20. * @subpackage questionengine
  21. * @copyright 2009 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once(dirname(__FILE__) . '/../lib.php');
  27. require_once(dirname(__FILE__) . '/helpers.php');
  28. /**
  29. * Unit tests for the question_usage_by_activity class.
  30. *
  31. * @copyright 2009 The Open University
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. class question_usage_by_activity_test extends advanced_testcase {
  35. public function test_set_get_preferred_model() {
  36. // Set up
  37. $quba = question_engine::make_questions_usage_by_activity('unit_test',
  38. context_system::instance());
  39. // Exercise SUT and verify.
  40. $quba->set_preferred_behaviour('deferredfeedback');
  41. $this->assertEquals('deferredfeedback', $quba->get_preferred_behaviour());
  42. }
  43. public function test_set_get_id() {
  44. // Set up
  45. $quba = question_engine::make_questions_usage_by_activity('unit_test',
  46. context_system::instance());
  47. // Exercise SUT and verify
  48. $quba->set_id_from_database(123);
  49. $this->assertEquals(123, $quba->get_id());
  50. }
  51. public function test_fake_id() {
  52. // Set up
  53. $quba = question_engine::make_questions_usage_by_activity('unit_test',
  54. context_system::instance());
  55. // Exercise SUT and verify
  56. $this->assertNotEmpty($quba->get_id());
  57. }
  58. public function test_create_usage_and_add_question() {
  59. // Exercise SUT
  60. $context = context_system::instance();
  61. $quba = question_engine::make_questions_usage_by_activity('unit_test', $context);
  62. $quba->set_preferred_behaviour('deferredfeedback');
  63. $tf = test_question_maker::make_question('truefalse', 'true');
  64. $slot = $quba->add_question($tf);
  65. // Verify.
  66. $this->assertEquals($slot, 1);
  67. $this->assertEquals('unit_test', $quba->get_owning_component());
  68. $this->assertSame($context, $quba->get_owning_context());
  69. $this->assertEquals($quba->question_count(), 1);
  70. $this->assertEquals($quba->get_question_state($slot), question_state::$notstarted);
  71. }
  72. public function test_get_question() {
  73. // Set up.
  74. $quba = question_engine::make_questions_usage_by_activity('unit_test',
  75. context_system::instance());
  76. $quba->set_preferred_behaviour('deferredfeedback');
  77. $tf = test_question_maker::make_question('truefalse', 'true');
  78. $slot = $quba->add_question($tf);
  79. // Exercise SUT and verify.
  80. $this->assertSame($tf, $quba->get_question($slot));
  81. $this->setExpectedException('moodle_exception');
  82. $quba->get_question($slot + 1);
  83. }
  84. public function test_extract_responses() {
  85. // Start a deferred feedback attempt with CBM and add the question to it.
  86. $tf = test_question_maker::make_question('truefalse', 'true');
  87. $quba = question_engine::make_questions_usage_by_activity('unit_test',
  88. context_system::instance());
  89. $quba->set_preferred_behaviour('deferredcbm');
  90. $slot = $quba->add_question($tf);
  91. $quba->start_all_questions();
  92. // Prepare data to be submitted
  93. $prefix = $quba->get_field_prefix($slot);
  94. $answername = $prefix . 'answer';
  95. $certaintyname = $prefix . '-certainty';
  96. $getdata = array(
  97. $answername => 1,
  98. $certaintyname => 3,
  99. 'irrelevant' => 'should be ignored',
  100. );
  101. // Exercise SUT
  102. $submitteddata = $quba->extract_responses($slot, $getdata);
  103. // Verify.
  104. $this->assertEquals(array('answer' => 1, '-certainty' => 3), $submitteddata);
  105. }
  106. public function test_access_out_of_sequence_throws_exception() {
  107. // Start a deferred feedback attempt with CBM and add the question to it.
  108. $tf = test_question_maker::make_question('truefalse', 'true');
  109. $quba = question_engine::make_questions_usage_by_activity('unit_test',
  110. context_system::instance());
  111. $quba->set_preferred_behaviour('deferredcbm');
  112. $slot = $quba->add_question($tf);
  113. $quba->start_all_questions();
  114. // Prepare data to be submitted
  115. $prefix = $quba->get_field_prefix($slot);
  116. $answername = $prefix . 'answer';
  117. $certaintyname = $prefix . '-certainty';
  118. $postdata = array(
  119. $answername => 1,
  120. $certaintyname => 3,
  121. $prefix . ':sequencecheck' => 1,
  122. 'irrelevant' => 'should be ignored',
  123. );
  124. // Exercise SUT - no exception yet.
  125. $quba->process_all_actions($slot, $postdata);
  126. $postdata = array(
  127. $answername => 1,
  128. $certaintyname => 3,
  129. $prefix . ':sequencecheck' => 3,
  130. 'irrelevant' => 'should be ignored',
  131. );
  132. // Exercise SUT - now it should fail.
  133. $this->setExpectedException('question_out_of_sequence_exception');
  134. $quba->process_all_actions($slot, $postdata);
  135. }
  136. }
  137. /**
  138. * Unit tests for loading data into the {@link question_usage_by_activity} class.
  139. *
  140. * @copyright 2012 The Open University
  141. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  142. */
  143. class question_usage_db_test extends data_loading_method_test_base {
  144. public function test_load() {
  145. $scid = context_system::instance()->id;
  146. $records = new question_test_recordset(array(
  147. array('qubaid', 'contextid', 'component', 'preferredbehaviour',
  148. 'questionattemptid', 'questionusageid', 'slot',
  149. 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
  150. 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
  151. 'attemptstepid', 'sequencenumber', 'state', 'fraction',
  152. 'timecreated', 'userid', 'name', 'value'),
  153. array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo', null, 1256233700, 1, null, null),
  154. array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo', null, 1256233705, 1, 'answer', '1'),
  155. array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 5, 2, 'gradedright', 1.0000000, 1256233720, 1, '-finish', '1'),
  156. ));
  157. $question = test_question_maker::make_question('truefalse', 'true');
  158. $question->id = -1;
  159. question_bank::start_unit_test();
  160. question_bank::load_test_question_data($question);
  161. $quba = question_usage_by_activity::load_from_records($records, 1);
  162. question_bank::end_unit_test();
  163. $this->assertEquals('unit_test', $quba->get_owning_component());
  164. $this->assertEquals(1, $quba->get_id());
  165. $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
  166. $this->assertEquals('interactive', $quba->get_preferred_behaviour());
  167. $qa = $quba->get_question_attempt(1);
  168. $this->assertEquals($question->questiontext, $qa->get_question()->questiontext);
  169. $this->assertEquals(3, $qa->get_num_steps());
  170. $step = $qa->get_step(0);
  171. $this->assertEquals(question_state::$todo, $step->get_state());
  172. $this->assertNull($step->get_fraction());
  173. $this->assertEquals(1256233700, $step->get_timecreated());
  174. $this->assertEquals(1, $step->get_user_id());
  175. $this->assertEquals(array(), $step->get_all_data());
  176. $step = $qa->get_step(1);
  177. $this->assertEquals(question_state::$todo, $step->get_state());
  178. $this->assertNull($step->get_fraction());
  179. $this->assertEquals(1256233705, $step->get_timecreated());
  180. $this->assertEquals(1, $step->get_user_id());
  181. $this->assertEquals(array('answer' => '1'), $step->get_all_data());
  182. $step = $qa->get_step(2);
  183. $this->assertEquals(question_state::$gradedright, $step->get_state());
  184. $this->assertEquals(1, $step->get_fraction());
  185. $this->assertEquals(1256233720, $step->get_timecreated());
  186. $this->assertEquals(1, $step->get_user_id());
  187. $this->assertEquals(array('-finish' => '1'), $step->get_all_data());
  188. }
  189. public function test_load_data_no_steps() {
  190. // The code had a bug where if one question_attempt had no steps,
  191. // load_from_records got stuck in an infinite loop. This test is to
  192. // verify that no longer happens.
  193. $scid = context_system::instance()->id;
  194. $records = new question_test_recordset(array(
  195. array('qubaid', 'contextid', 'component', 'preferredbehaviour',
  196. 'questionattemptid', 'questionusageid', 'slot',
  197. 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
  198. 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
  199. 'attemptstepid', 'sequencenumber', 'state', 'fraction',
  200. 'timecreated', 'userid', 'name', 'value'),
  201. array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', 0, 1, 1.0000000, 0.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
  202. array(1, $scid, 'unit_test', 'interactive', 2, 1, 2, 'interactive', 0, 1, 1.0000000, 0.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
  203. array(1, $scid, 'unit_test', 'interactive', 3, 1, 3, 'interactive', 0, 1, 1.0000000, 0.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
  204. ));
  205. question_bank::start_unit_test();
  206. $quba = question_usage_by_activity::load_from_records($records, 1);
  207. question_bank::end_unit_test();
  208. $this->assertEquals('unit_test', $quba->get_owning_component());
  209. $this->assertEquals(1, $quba->get_id());
  210. $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
  211. $this->assertEquals('interactive', $quba->get_preferred_behaviour());
  212. $this->assertEquals(array(1, 2, 3), $quba->get_slots());
  213. $qa = $quba->get_question_attempt(1);
  214. $this->assertEquals(0, $qa->get_num_steps());
  215. }
  216. public function test_load_data_no_qas() {
  217. // The code had a bug where if a question_usage had no question_attempts,
  218. // load_from_records got stuck in an infinite loop. This test is to
  219. // verify that no longer happens.
  220. $scid = context_system::instance()->id;
  221. $records = new question_test_recordset(array(
  222. array('qubaid', 'contextid', 'component', 'preferredbehaviour',
  223. 'questionattemptid', 'questionusageid', 'slot',
  224. 'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
  225. 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
  226. 'attemptstepid', 'sequencenumber', 'state', 'fraction',
  227. 'timecreated', 'userid', 'name', 'value'),
  228. array(1, $scid, 'unit_test', 'interactive', null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null),
  229. ));
  230. question_bank::start_unit_test();
  231. $quba = question_usage_by_activity::load_from_records($records, 1);
  232. question_bank::end_unit_test();
  233. $this->assertEquals('unit_test', $quba->get_owning_component());
  234. $this->assertEquals(1, $quba->get_id());
  235. $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
  236. $this->assertEquals('interactive', $quba->get_preferred_behaviour());
  237. $this->assertEquals(array(), $quba->get_slots());
  238. }
  239. }