weekly release 4.0dev
[moodle.git] / question / type / essay / tests / question_test.php
CommitLineData
603bd001
PS
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 * Unit tests for the essay question definition class.
19 *
20 * @package qtype
21 * @subpackage essay
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31
32
33/**
34 * Unit tests for the matching question definition class.
35 *
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
a4f765eb 39class qtype_essay_question_test extends advanced_testcase {
603bd001
PS
40 public function test_get_question_summary() {
41 $essay = test_question_maker::make_an_essay_question();
42 $essay->questiontext = 'Hello <img src="http://example.com/globe.png" alt="world" />';
43 $this->assertEquals('Hello [world]', $essay->get_question_summary());
44 }
45
46 public function test_summarise_response() {
47 $longstring = str_repeat('0123456789', 50);
48 $essay = test_question_maker::make_an_essay_question();
afb1b3d0
TH
49 $this->assertEquals($longstring, $essay->summarise_response(
50 array('answer' => $longstring, 'answerformat' => FORMAT_HTML)));
603bd001 51 }
60527d0c
JMV
52
53 public function test_is_same_response() {
54 $essay = test_question_maker::make_an_essay_question();
55
56 $essay->responsetemplate = '';
57
58 $essay->start_attempt(new question_attempt_step(), 1);
59
60 $this->assertTrue($essay->is_same_response(
61 array(),
62 array('answer' => '')));
63
64 $this->assertTrue($essay->is_same_response(
65 array('answer' => ''),
66 array('answer' => '')));
67
68 $this->assertTrue($essay->is_same_response(
69 array('answer' => ''),
70 array()));
71
72 $this->assertFalse($essay->is_same_response(
73 array('answer' => 'Hello'),
74 array()));
75
76 $this->assertFalse($essay->is_same_response(
77 array('answer' => 'Hello'),
78 array('answer' => '')));
79
80 $this->assertFalse($essay->is_same_response(
81 array('answer' => 0),
82 array('answer' => '')));
83
84 $this->assertFalse($essay->is_same_response(
85 array('answer' => ''),
86 array('answer' => 0)));
87
88 $this->assertFalse($essay->is_same_response(
89 array('answer' => '0'),
90 array('answer' => '')));
91
92 $this->assertFalse($essay->is_same_response(
93 array('answer' => ''),
94 array('answer' => '0')));
95 }
96
97 public function test_is_same_response_with_template() {
98 $essay = test_question_maker::make_an_essay_question();
99
100 $essay->responsetemplate = 'Once upon a time';
101
102 $essay->start_attempt(new question_attempt_step(), 1);
103
104 $this->assertTrue($essay->is_same_response(
105 array(),
106 array('answer' => 'Once upon a time')));
107
108 $this->assertTrue($essay->is_same_response(
109 array('answer' => ''),
110 array('answer' => 'Once upon a time')));
111
112 $this->assertTrue($essay->is_same_response(
113 array('answer' => 'Once upon a time'),
114 array('answer' => '')));
115
116 $this->assertTrue($essay->is_same_response(
117 array('answer' => ''),
118 array()));
119
120 $this->assertTrue($essay->is_same_response(
121 array('answer' => 'Once upon a time'),
122 array()));
123
124 $this->assertFalse($essay->is_same_response(
125 array('answer' => 0),
126 array('answer' => '')));
127
128 $this->assertFalse($essay->is_same_response(
129 array('answer' => ''),
130 array('answer' => 0)));
131
132 $this->assertFalse($essay->is_same_response(
133 array('answer' => '0'),
134 array('answer' => '')));
135
136 $this->assertFalse($essay->is_same_response(
137 array('answer' => ''),
138 array('answer' => '0')));
139 }
efe67797
KT
140
141 public function test_is_complete_response() {
a4f765eb 142 $this->resetAfterTest(true);
efe67797 143
a4f765eb
KT
144 // Create a new logged-in user, so we can test responses with attachments.
145 $user = $this->getDataGenerator()->create_user();
146 $this->setUser($user);
147
148 // Create sample attachments to use in testing.
149 $helper = test_question_maker::get_test_helper('essay');
150 $attachments = array();
151 for ($i = 0; $i < 4; ++$i) {
152 $attachments[$i] = $helper->make_attachments_saver($i);
153 }
154
155 // Create the essay question under test.
efe67797
KT
156 $essay = test_question_maker::make_an_essay_question();
157 $essay->start_attempt(new question_attempt_step(), 1);
158
a4f765eb
KT
159 // Test the "traditional" case, where we must recieve a response from the user.
160 $essay->responserequired = 1;
161 $essay->attachmentsrequired = 0;
162 $essay->responseformat = 'editor';
163
164 // The empty string should be considered an incomplete response, as should a lack of a response.
efe67797
KT
165 $this->assertFalse($essay->is_complete_response(array('answer' => '')));
166 $this->assertFalse($essay->is_complete_response(array()));
167
168 // Any nonempty string should be considered a complete response.
169 $this->assertTrue($essay->is_complete_response(array('answer' => 'A student response.')));
170 $this->assertTrue($essay->is_complete_response(array('answer' => '0 times.')));
171 $this->assertTrue($essay->is_complete_response(array('answer' => '0')));
a4f765eb 172
997a407b
MK
173 // Test case for minimum and/or maximum word limit.
174 $response = [];
175 $response['answer'] = 'In this essay, I will be testing a function called check_input_word_count().';
176
177 $essay->minwordlimit = 50; // The answer is shorter than the required minimum word limit.
178 $this->assertFalse($essay->is_complete_response($response));
179
180 $essay->minwordlimit = 10; // The word count meets the required minimum word limit.
181 $this->assertTrue($essay->is_complete_response($response));
182
183 // The word count meets the required minimum and maximum word limit.
184 $essay->minwordlimit = 10;
185 $essay->maxwordlimit = 15;
186 $this->assertTrue($essay->is_complete_response($response));
187
188 // Unset the minwordlimit/maxwordlimit variables to avoid the extra check in is_complete_response() for further tests.
c4e2b67c
TH
189 $essay->minwordlimit = null;
190 $essay->maxwordlimit = null;
997a407b 191
a4f765eb
KT
192 // Test the case where two files are required.
193 $essay->attachmentsrequired = 2;
194
195 // Attaching less than two files should result in an incomplete response.
196 $this->assertFalse($essay->is_complete_response(array('answer' => 'A')));
197 $this->assertFalse($essay->is_complete_response(
198 array('answer' => 'A', 'attachments' => $attachments[0])));
199 $this->assertFalse($essay->is_complete_response(
200 array('answer' => 'A', 'attachments' => $attachments[1])));
201
202 // Anything without response text should result in an incomplete response.
203 $this->assertFalse($essay->is_complete_response(
204 array('answer' => '', 'attachments' => $attachments[2])));
205
206 // Attaching two or more files should result in a complete response.
207 $this->assertTrue($essay->is_complete_response(
208 array('answer' => 'A', 'attachments' => $attachments[2])));
209 $this->assertTrue($essay->is_complete_response(
210 array('answer' => 'A', 'attachments' => $attachments[3])));
211
212 // Test the case in which two files are required, but the inline
213 // response is optional.
214 $essay->responserequired = 0;
215
216 $this->assertFalse($essay->is_complete_response(
217 array('answer' => '', 'attachments' => $attachments[1])));
218
219 $this->assertTrue($essay->is_complete_response(
220 array('answer' => '', 'attachments' => $attachments[2])));
221
ee9e7ee3 222 // Test the case in which both the response and online text are optional.
a4f765eb
KT
223 $essay->attachmentsrequired = 0;
224
225 // Providing no answer and no attachment should result in an incomplete
226 // response.
227 $this->assertFalse($essay->is_complete_response(
228 array('answer' => '')));
229 $this->assertFalse($essay->is_complete_response(
230 array('answer' => '', 'attachments' => $attachments[0])));
231
232 // Providing an answer _or_ an attachment should result in a complete
233 // response.
234 $this->assertTrue($essay->is_complete_response(
235 array('answer' => '', 'attachments' => $attachments[1])));
236 $this->assertTrue($essay->is_complete_response(
237 array('answer' => 'Answer text.', 'attachments' => $attachments[0])));
238
239 // Test the case in which we're in "no inline response" mode,
240 // in which the response is not required (as it's not provided).
241 $essay->reponserequired = 0;
242 $essay->responseformat = 'noinline';
243 $essay->attachmensrequired = 1;
244
245 $this->assertFalse($essay->is_complete_response(
246 array()));
247 $this->assertFalse($essay->is_complete_response(
248 array('attachments' => $attachments[0])));
249
250 // Providing an attachment should result in a complete response.
251 $this->assertTrue($essay->is_complete_response(
252 array('attachments' => $attachments[1])));
253
254 // Ensure that responserequired is ignored when we're in inline response mode.
255 $essay->reponserequired = 1;
256 $this->assertTrue($essay->is_complete_response(
257 array('attachments' => $attachments[1])));
efe67797 258 }
a4f765eb 259
440aaccb
JL
260 /**
261 * test_get_question_definition_for_external_rendering
262 */
263 public function test_get_question_definition_for_external_rendering() {
264 $this->resetAfterTest();
265
266 $essay = test_question_maker::make_an_essay_question();
267 $essay->start_attempt(new question_attempt_step(), 1);
268 $qa = test_question_maker::get_a_qa($essay);
269 $displayoptions = new question_display_options();
270
271 $options = $essay->get_question_definition_for_external_rendering($qa, $displayoptions);
272 $this->assertNotEmpty($options);
273 $this->assertEquals('editor', $options['responseformat']);
274 $this->assertEquals(1, $options['responserequired']);
275 $this->assertEquals(15, $options['responsefieldlines']);
276 $this->assertEquals(0, $options['attachments']);
277 $this->assertEquals(0, $options['attachmentsrequired']);
278 $this->assertNull($options['maxbytes']);
279 $this->assertNull($options['filetypeslist']);
280 $this->assertEquals('', $options['responsetemplate']);
281 $this->assertEquals(FORMAT_MOODLE, $options['responsetemplateformat']);
282 }
997a407b
MK
283
284 /**
285 * Test get_validation_error when users submit their input text.
286 *
c4e2b67c
TH
287 * (The tests are done with a fixed 14-word response.)
288 *
997a407b 289 * @dataProvider get_min_max_wordlimit_test_cases()
c4e2b67c 290 * @param int $responserequired whether response required (yes = 1, no = 0)
997a407b
MK
291 * @param int $minwordlimit minimum word limit
292 * @param int $maxwordlimit maximum word limit
293 * @param string $expected error message | null
294 */
295 public function test_get_validation_error(int $responserequired,
296 int $minwordlimit, int $maxwordlimit, string $expected): void {
297 $question = test_question_maker::make_an_essay_question();
298 $response = ['answer' => 'In this essay, I will be testing a function called check_input_word_count().'];
299 $question->responserequired = $responserequired;
300 $question->minwordlimit = $minwordlimit;
301 $question->maxwordlimit = $maxwordlimit;
302 $actual = $question->get_validation_error($response);
303 $this->assertEquals($expected, $actual);
304 }
305
306 /**
307 * Data provider for get_validation_error test.
c4e2b67c
TH
308 *
309 * @return array the test cases.
997a407b 310 */
c4e2b67c 311 public function get_min_max_wordlimit_test_cases(): array {
997a407b
MK
312 return [
313 'text input required, min/max word limit not set' => [1, 0, 0, ''],
314 'text input required, min/max word limit valid (within the boundaries)' => [1, 10, 25, ''],
d9b0da85
TH
315 'text input required, min word limit not reached' => [1, 15, 25,
316 get_string('minwordlimitboundary', 'qtype_essay', ['count' => 14, 'limit' => 15])],
997a407b 317 'text input required, max word limit is exceeded' => [1, 5, 12,
d9b0da85 318 get_string('maxwordlimitboundary', 'qtype_essay', ['count' => 14, 'limit' => 12])],
997a407b
MK
319 'text input not required, min/max word limit not set' => [0, 5, 12, ''],
320 ];
321 }
c4e2b67c
TH
322
323 /**
324 * Test get_word_count_message_for_review when users submit their input text.
325 *
326 * (The tests are done with a fixed 14-word response.)
327 *
328 * @dataProvider get_word_count_message_for_review_test_cases()
329 * @param int|null $minwordlimit minimum word limit
330 * @param int|null $maxwordlimit maximum word limit
331 * @param string $expected error message | null
332 */
333 public function test_get_word_count_message_for_review(?int $minwordlimit, ?int $maxwordlimit, string $expected): void {
334 $question = test_question_maker::make_an_essay_question();
335 $question->minwordlimit = $minwordlimit;
336 $question->maxwordlimit = $maxwordlimit;
337
338 $response = ['answer' => 'One two three four five six seven eight nine ten eleven twelve thirteen fourteen.'];
339 $this->assertEquals($expected, $question->get_word_count_message_for_review($response));
340 }
341
342 /**
343 * Data provider for test_get_word_count_message_for_review.
344 *
345 * @return array the test cases.
346 */
347 public function get_word_count_message_for_review_test_cases() {
348 return [
349 'No limit' =>
350 [null, null, ''],
351 'min and max, answer within range' =>
352 [10, 25, get_string('wordcount', 'qtype_essay', 14)],
353 'min and max, answer too short' =>
354 [15, 25, get_string('wordcounttoofew', 'qtype_essay', ['count' => 14, 'limit' => 15])],
355 'min and max, answer too long' =>
356 [5, 12, get_string('wordcounttoomuch', 'qtype_essay', ['count' => 14, 'limit' => 12])],
357 'min only, answer within range' =>
358 [14, null, get_string('wordcount', 'qtype_essay', 14)],
359 'min only, answer too short' =>
360 [15, null, get_string('wordcounttoofew', 'qtype_essay', ['count' => 14, 'limit' => 15])],
361 'max only, answer within range' =>
362 [null, 14, get_string('wordcount', 'qtype_essay', 14)],
363 'max only, answer too short' =>
364 [null, 13, get_string('wordcounttoomuch', 'qtype_essay', ['count' => 14, 'limit' => 13])],
365 ];
366 }
603bd001 367}