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