/question/type/essay/tests/walkthrough_test.php

https://github.com/andreev-artem/moodle · PHP · 156 lines · 92 code · 25 blank · 39 comment · 2 complexity · 29f4426dae2c9e733764dad2aee11b9b 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 essay questions through some attempts.
  18. *
  19. * @package qtype_essay
  20. * @copyright 2013 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. global $CFG;
  25. require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26. /**
  27. * Unit tests for the essay question type.
  28. *
  29. * @copyright 2013 The Open University
  30. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31. */
  32. class qtype_essay_walkthrough_test extends qbehaviour_walkthrough_test_base {
  33. protected function check_contains_textarea($name, $content = '', $height = 10) {
  34. $fieldname = $this->quba->get_field_prefix($this->slot) . $name;
  35. $this->assertTag(array('tag' => 'textarea',
  36. 'attributes' => array('cols' => '60', 'rows' => $height,
  37. 'name' => $fieldname)),
  38. $this->currentoutput);
  39. if ($content) {
  40. $this->assertRegExp('/' . preg_quote(s($content), '/') . '/', $this->currentoutput);
  41. }
  42. }
  43. public function test_deferred_feedback_html_editor() {
  44. // Create a matching question.
  45. $q = test_question_maker::make_question('essay', 'editor');
  46. $this->start_attempt_at_question($q, 'deferredfeedback', 1);
  47. $prefix = $this->quba->get_field_prefix($this->slot);
  48. $fieldname = $prefix . 'answer';
  49. $response = '<p>The <b>cat</b> sat on the mat. Then it ate a <b>frog</b>.</p>';
  50. // Check the initial state.
  51. $this->check_current_state(question_state::$todo);
  52. $this->check_current_mark(null);
  53. $this->render();
  54. $this->check_contains_textarea('answer', '');
  55. $this->check_current_output(
  56. $this->get_contains_question_text_expectation($q),
  57. $this->get_does_not_contain_feedback_expectation());
  58. $this->check_step_count(1);
  59. // Save a response.
  60. $this->quba->process_all_actions(null, array(
  61. 'slots' => $this->slot,
  62. $fieldname => $response,
  63. $fieldname . 'format' => FORMAT_HTML,
  64. $prefix . ':sequencecheck' => '1',
  65. ));
  66. // Verify.
  67. $this->check_current_state(question_state::$complete);
  68. $this->check_current_mark(null);
  69. $this->check_step_count(2);
  70. $this->render();
  71. $this->check_contains_textarea('answer', $response);
  72. $this->check_current_output(
  73. $this->get_contains_question_text_expectation($q),
  74. $this->get_does_not_contain_feedback_expectation());
  75. $this->check_step_count(2);
  76. // Finish the attempt.
  77. $this->quba->finish_all_questions();
  78. // Verify.
  79. $this->check_current_state(question_state::$needsgrading);
  80. $this->check_current_mark(null);
  81. $this->render();
  82. $this->assertRegExp('/' . preg_quote($response, '/') . '/', $this->currentoutput);
  83. $this->check_current_output(
  84. $this->get_contains_question_text_expectation($q),
  85. $this->get_contains_general_feedback_expectation($q));
  86. }
  87. public function test_deferred_feedback_plain_text() {
  88. // Create a matching question.
  89. $q = test_question_maker::make_question('essay', 'plain');
  90. $this->start_attempt_at_question($q, 'deferredfeedback', 1);
  91. $prefix = $this->quba->get_field_prefix($this->slot);
  92. $fieldname = $prefix . 'answer';
  93. $response = "x < 1\nx > 0\nFrog & Toad were friends.";
  94. // Check the initial state.
  95. $this->check_current_state(question_state::$todo);
  96. $this->check_current_mark(null);
  97. $this->render();
  98. $this->check_contains_textarea('answer', '');
  99. $this->check_current_output(
  100. $this->get_contains_question_text_expectation($q),
  101. $this->get_does_not_contain_feedback_expectation());
  102. $this->check_step_count(1);
  103. // Save a response.
  104. $this->quba->process_all_actions(null, array(
  105. 'slots' => $this->slot,
  106. $fieldname => $response,
  107. $fieldname . 'format' => FORMAT_HTML,
  108. $prefix . ':sequencecheck' => '1',
  109. ));
  110. // Verify.
  111. $this->check_current_state(question_state::$complete);
  112. $this->check_current_mark(null);
  113. $this->check_step_count(2);
  114. $this->render();
  115. $this->check_contains_textarea('answer', $response);
  116. $this->check_current_output(
  117. $this->get_contains_question_text_expectation($q),
  118. $this->get_does_not_contain_feedback_expectation());
  119. $this->check_step_count(2);
  120. // Finish the attempt.
  121. $this->quba->finish_all_questions();
  122. // Verify.
  123. $this->check_current_state(question_state::$needsgrading);
  124. $this->check_current_mark(null);
  125. $this->render();
  126. $this->assertRegExp('/' . preg_quote(s($response), '/') . '/', $this->currentoutput);
  127. $this->check_current_output(
  128. $this->get_contains_question_text_expectation($q),
  129. $this->get_contains_general_feedback_expectation($q));
  130. }
  131. }