/question/type/match/simpletest/testquestion.php

https://github.com/colchambers/Moodle-Question-Engine-2 · PHP · 177 lines · 119 code · 31 blank · 27 comment · 0 complexity · ad5be1d70f69aa1f74124331fcb16664 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. * Unit tests for the matching question definition classes.
  18. *
  19. * @package qtype_match
  20. * @copyright 2009 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
  24. /**
  25. * Unit tests for the matching question definition class.
  26. *
  27. * @copyright 2009 The Open University
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class qtype_match_question_test extends UnitTestCase {
  31. public function test_get_expected_data() {
  32. $question = test_question_maker::make_a_matching_question();
  33. $question->init_first_step(new question_attempt_step());
  34. $this->assertEqual(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT,
  35. 'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data());
  36. }
  37. public function test_is_complete_response() {
  38. $question = test_question_maker::make_a_matching_question();
  39. $question->init_first_step(new question_attempt_step());
  40. $this->assertFalse($question->is_complete_response(array()));
  41. $this->assertFalse($question->is_complete_response(
  42. array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0')));
  43. $this->assertFalse($question->is_complete_response(array('sub1' => '1')));
  44. $this->assertTrue($question->is_complete_response(
  45. array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1')));
  46. }
  47. public function test_is_gradable_response() {
  48. $question = test_question_maker::make_a_matching_question();
  49. $question->init_first_step(new question_attempt_step());
  50. $this->assertFalse($question->is_gradable_response(array()));
  51. $this->assertFalse($question->is_gradable_response(
  52. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  53. $this->assertTrue($question->is_gradable_response(
  54. array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  55. $this->assertTrue($question->is_gradable_response(array('sub1' => '1')));
  56. $this->assertTrue($question->is_gradable_response(
  57. array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1')));
  58. }
  59. public function test_is_same_response() {
  60. $question = test_question_maker::make_a_matching_question();
  61. $question->init_first_step(new question_attempt_step());
  62. $this->assertTrue($question->is_same_response(
  63. array(),
  64. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  65. $this->assertTrue($question->is_same_response(
  66. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  67. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  68. $this->assertFalse($question->is_same_response(
  69. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  70. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  71. $this->assertTrue($question->is_same_response(
  72. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  73. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  74. $this->assertFalse($question->is_same_response(
  75. array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  76. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  77. }
  78. public function test_grading() {
  79. $question = test_question_maker::make_a_matching_question();
  80. $question->shufflestems = false;
  81. $question->init_first_step(new question_attempt_step());
  82. $choiceorder = $question->get_choice_order();
  83. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  84. $this->assertEqual(array(1, question_state::$gradedright),
  85. $question->grade_response(array('sub0' => $orderforchoice[1],
  86. 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2], 'sub3' => $orderforchoice[1])));
  87. $this->assertEqual(array(0.25, question_state::$gradedpartial),
  88. $question->grade_response(array('sub0' => $orderforchoice[1])));
  89. $this->assertEqual(array(0, question_state::$gradedwrong),
  90. $question->grade_response(array('sub0' => $orderforchoice[2], 'sub1' => $orderforchoice[3], 'sub2' => $orderforchoice[1], 'sub3' => $orderforchoice[2])));
  91. }
  92. public function test_get_correct_response() {
  93. $question = test_question_maker::make_a_matching_question();
  94. $question->shufflestems = false;
  95. $question->init_first_step(new question_attempt_step());
  96. $choiceorder = $question->get_choice_order();
  97. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  98. $this->assertEqual(array('sub0' => $orderforchoice[1], 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2], 'sub3' => $orderforchoice[1]),
  99. $question->get_correct_response());
  100. }
  101. public function test_get_question_summary() {
  102. $match = test_question_maker::make_a_matching_question();
  103. $match->init_first_step(new question_attempt_step());
  104. $qsummary = $match->get_question_summary();
  105. $this->assertPattern('/' . preg_quote($match->questiontext) . '/', $qsummary);
  106. foreach ($match->stems as $stem) {
  107. $this->assertPattern('/' . preg_quote($stem) . '/', $qsummary);
  108. }
  109. foreach ($match->choices as $choice) {
  110. $this->assertPattern('/' . preg_quote($choice) . '/', $qsummary);
  111. }
  112. }
  113. public function test_summarise_response() {
  114. $match = test_question_maker::make_a_matching_question();
  115. $match->shufflestems = false;
  116. $match->init_first_step(new question_attempt_step());
  117. $summary = $match->summarise_response(array('sub0' => 2, 'sub1' => 1));
  118. $this->assertPattern('/Dog -> \w+; Frog -> \w+/', $summary);
  119. }
  120. public function test_classify_response() {
  121. $match = test_question_maker::make_a_matching_question();
  122. $match->shufflestems = false;
  123. $match->init_first_step(new question_attempt_step());
  124. $choiceorder = $match->get_choice_order();
  125. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  126. $choices = array(0 => get_string('choose') . '...');
  127. foreach ($choiceorder as $key => $choice) {
  128. $choices[$key] = $match->choices[$choice];
  129. }
  130. $this->assertEqual(array(
  131. 1 => new question_classified_response(2, 'Amphibian', 0),
  132. 2 => new question_classified_response(3, 'Insect', 0),
  133. 3 => question_classified_response::no_response(),
  134. 4 => question_classified_response::no_response(),
  135. ), $match->classify_response(array('sub0' => $orderforchoice[2],
  136. 'sub1' => $orderforchoice[3], 'sub2' => 0, 'sub3' => 0)));
  137. $this->assertEqual(array(
  138. 1 => new question_classified_response(1, 'Mammal', 0.25),
  139. 2 => new question_classified_response(2, 'Amphibian', 0.25),
  140. 3 => new question_classified_response(2, 'Amphibian', 0.25),
  141. 4 => new question_classified_response(1, 'Mammal', 0.25),
  142. ), $match->classify_response(array('sub0' => $orderforchoice[1],
  143. 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
  144. 'sub3' => $orderforchoice[1])));
  145. }
  146. }