/question/type/essay/tests/question_test.php

https://github.com/mnoorenberghe/moodle · PHP · 155 lines · 90 code · 35 blank · 30 comment · 1 complexity · 749e42f1fedc4bd6ebe7916393a3bdb0 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 essay question definition class.
  18. *
  19. * @package qtype
  20. * @subpackage essay
  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($CFG->dirroot . '/question/engine/tests/helpers.php');
  27. /**
  28. * Unit tests for the matching question definition class.
  29. *
  30. * @copyright 2009 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class qtype_essay_question_testcase extends advanced_testcase {
  34. public function test_get_question_summary() {
  35. $essay = test_question_maker::make_an_essay_question();
  36. $essay->questiontext = 'Hello <img src="http://example.com/globe.png" alt="world" />';
  37. $this->assertEquals('Hello [world]', $essay->get_question_summary());
  38. }
  39. public function test_summarise_response() {
  40. $longstring = str_repeat('0123456789', 50);
  41. $essay = test_question_maker::make_an_essay_question();
  42. $this->assertEquals($longstring, $essay->summarise_response(
  43. array('answer' => $longstring, 'answerformat' => FORMAT_HTML)));
  44. }
  45. public function test_is_same_response() {
  46. $essay = test_question_maker::make_an_essay_question();
  47. $essay->responsetemplate = '';
  48. $essay->start_attempt(new question_attempt_step(), 1);
  49. $this->assertTrue($essay->is_same_response(
  50. array(),
  51. array('answer' => '')));
  52. $this->assertTrue($essay->is_same_response(
  53. array('answer' => ''),
  54. array('answer' => '')));
  55. $this->assertTrue($essay->is_same_response(
  56. array('answer' => ''),
  57. array()));
  58. $this->assertFalse($essay->is_same_response(
  59. array('answer' => 'Hello'),
  60. array()));
  61. $this->assertFalse($essay->is_same_response(
  62. array('answer' => 'Hello'),
  63. array('answer' => '')));
  64. $this->assertFalse($essay->is_same_response(
  65. array('answer' => 0),
  66. array('answer' => '')));
  67. $this->assertFalse($essay->is_same_response(
  68. array('answer' => ''),
  69. array('answer' => 0)));
  70. $this->assertFalse($essay->is_same_response(
  71. array('answer' => '0'),
  72. array('answer' => '')));
  73. $this->assertFalse($essay->is_same_response(
  74. array('answer' => ''),
  75. array('answer' => '0')));
  76. }
  77. public function test_is_same_response_with_template() {
  78. $essay = test_question_maker::make_an_essay_question();
  79. $essay->responsetemplate = 'Once upon a time';
  80. $essay->start_attempt(new question_attempt_step(), 1);
  81. $this->assertTrue($essay->is_same_response(
  82. array(),
  83. array('answer' => 'Once upon a time')));
  84. $this->assertTrue($essay->is_same_response(
  85. array('answer' => ''),
  86. array('answer' => 'Once upon a time')));
  87. $this->assertTrue($essay->is_same_response(
  88. array('answer' => 'Once upon a time'),
  89. array('answer' => '')));
  90. $this->assertTrue($essay->is_same_response(
  91. array('answer' => ''),
  92. array()));
  93. $this->assertTrue($essay->is_same_response(
  94. array('answer' => 'Once upon a time'),
  95. array()));
  96. $this->assertFalse($essay->is_same_response(
  97. array('answer' => 0),
  98. array('answer' => '')));
  99. $this->assertFalse($essay->is_same_response(
  100. array('answer' => ''),
  101. array('answer' => 0)));
  102. $this->assertFalse($essay->is_same_response(
  103. array('answer' => '0'),
  104. array('answer' => '')));
  105. $this->assertFalse($essay->is_same_response(
  106. array('answer' => ''),
  107. array('answer' => '0')));
  108. }
  109. public function test_is_complete_response() {
  110. $essay = test_question_maker::make_an_essay_question();
  111. $essay->start_attempt(new question_attempt_step(), 1);
  112. // The empty string should be considered an empty response, as should a lack of a response.
  113. $this->assertFalse($essay->is_complete_response(array('answer' => '')));
  114. $this->assertFalse($essay->is_complete_response(array()));
  115. // Any nonempty string should be considered a complete response.
  116. $this->assertTrue($essay->is_complete_response(array('answer' => 'A student response.')));
  117. $this->assertTrue($essay->is_complete_response(array('answer' => '0 times.')));
  118. $this->assertTrue($essay->is_complete_response(array('answer' => '0')));
  119. }
  120. }