MDL-43646 "disapprove" changed to "undo approval"
[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 */
afb1b3d0 39class qtype_essay_question_testcase 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() {
142
143 $essay = test_question_maker::make_an_essay_question();
144 $essay->start_attempt(new question_attempt_step(), 1);
145
146 // The empty string should be considered an empty response, as should a lack of a response.
147 $this->assertFalse($essay->is_complete_response(array('answer' => '')));
148 $this->assertFalse($essay->is_complete_response(array()));
149
150 // Any nonempty string should be considered a complete response.
151 $this->assertTrue($essay->is_complete_response(array('answer' => 'A student response.')));
152 $this->assertTrue($essay->is_complete_response(array('answer' => '0 times.')));
153 $this->assertTrue($essay->is_complete_response(array('answer' => '0')));
154 }
603bd001 155}