/question/type/randomsamatch/tests/question_test.php

https://bitbucket.org/moodle/moodle · PHP · 150 lines · 100 code · 29 blank · 21 comment · 1 complexity · da5223155da9f0afb092b75e3b6f80a0 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. namespace qtype_randomsamatch;
  17. use question_attempt_step;
  18. use question_state;
  19. defined('MOODLE_INTERNAL') || die();
  20. global $CFG;
  21. require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  22. /**
  23. * Unit tests for the random shortanswer matching question definition class.
  24. *
  25. * @package qtype_randomsamatch
  26. * @copyright 2013 Jean-Michel Vedrine
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. */
  29. class question_test extends \advanced_testcase {
  30. public function test_get_expected_data() {
  31. $question = \test_question_maker::make_question('randomsamatch');
  32. $question->start_attempt(new question_attempt_step(), 1);
  33. $this->assertEquals(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT,
  34. 'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data());
  35. }
  36. public function test_is_complete_response() {
  37. $question = \test_question_maker::make_question('randomsamatch');
  38. $question->start_attempt(new question_attempt_step(), 1);
  39. $this->assertFalse($question->is_complete_response(array()));
  40. $this->assertFalse($question->is_complete_response(
  41. array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0')));
  42. $this->assertFalse($question->is_complete_response(array('sub1' => '1')));
  43. $this->assertTrue($question->is_complete_response(
  44. array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1')));
  45. }
  46. public function test_is_gradable_response() {
  47. $question = \test_question_maker::make_question('randomsamatch');
  48. $question->start_attempt(new question_attempt_step(), 1);
  49. $this->assertFalse($question->is_gradable_response(array()));
  50. $this->assertFalse($question->is_gradable_response(
  51. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  52. $this->assertTrue($question->is_gradable_response(
  53. array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  54. $this->assertTrue($question->is_gradable_response(array('sub1' => '1')));
  55. $this->assertTrue($question->is_gradable_response(
  56. array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1')));
  57. }
  58. public function test_is_same_response() {
  59. $question = \test_question_maker::make_question('randomsamatch');
  60. $question->start_attempt(new question_attempt_step(), 1);
  61. $this->assertTrue($question->is_same_response(
  62. array(),
  63. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  64. $this->assertTrue($question->is_same_response(
  65. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  66. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
  67. $this->assertFalse($question->is_same_response(
  68. array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
  69. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  70. $this->assertTrue($question->is_same_response(
  71. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  72. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  73. $this->assertFalse($question->is_same_response(
  74. array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
  75. array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
  76. }
  77. public function test_grading() {
  78. $question = \test_question_maker::make_question('randomsamatch');
  79. $question->start_attempt(new question_attempt_step(), 1);
  80. $choiceorder = $question->get_choice_order();
  81. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  82. $this->assertEquals(array(1, question_state::$gradedright),
  83. $question->grade_response(array('sub0' => $orderforchoice[13],
  84. 'sub1' => $orderforchoice[16], 'sub2' => $orderforchoice[16],
  85. 'sub3' => $orderforchoice[13])));
  86. $this->assertEquals(array(0.25, question_state::$gradedpartial),
  87. $question->grade_response(array('sub0' => $orderforchoice[13])));
  88. $this->assertEquals(array(0, question_state::$gradedwrong),
  89. $question->grade_response(array('sub0' => $orderforchoice[16],
  90. 'sub1' => $orderforchoice[13], 'sub2' => $orderforchoice[13],
  91. 'sub3' => $orderforchoice[16])));
  92. }
  93. public function test_get_correct_response() {
  94. $question = \test_question_maker::make_question('randomsamatch');
  95. $question->start_attempt(new question_attempt_step(), 1);
  96. $choiceorder = $question->get_choice_order();
  97. $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  98. $this->assertEquals(array('sub0' => $orderforchoice[13], 'sub1' => $orderforchoice[16],
  99. 'sub2' => $orderforchoice[16], 'sub3' => $orderforchoice[13]),
  100. $question->get_correct_response());
  101. }
  102. public function test_get_question_summary() {
  103. $question = \test_question_maker::make_question('randomsamatch');
  104. $question->start_attempt(new question_attempt_step(), 1);
  105. $qsummary = $question->get_question_summary();
  106. $this->assertMatchesRegularExpression('/' . preg_quote($question->questiontext, '/') . '/', $qsummary);
  107. foreach ($question->stems as $stem) {
  108. $this->assertMatchesRegularExpression('/' . preg_quote($stem, '/') . '/', $qsummary);
  109. }
  110. foreach ($question->choices as $choice) {
  111. $this->assertMatchesRegularExpression('/' . preg_quote($choice, '/') . '/', $qsummary);
  112. }
  113. }
  114. public function test_summarise_response() {
  115. $question = \test_question_maker::make_question('randomsamatch');
  116. $question->shufflestems = false;
  117. $question->start_attempt(new question_attempt_step(), 1);
  118. $summary = $question->summarise_response(array('sub0' => 2, 'sub1' => 1));
  119. $this->assertMatchesRegularExpression('/Dog -> \w+; Frog -> \w+/', $summary);
  120. }
  121. }