MDL-41792 fix calendar test typos
[moodle.git] / question / behaviour / manualgraded / tests / walkthrough_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 * This file contains tests that walks a question through the manual graded
19 * behaviour.
20 *
21 * @package qbehaviour
22 * @subpackage manualgraded
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once(dirname(__FILE__) . '/../../../engine/lib.php');
32require_once(dirname(__FILE__) . '/../../../engine/tests/helpers.php');
33
34
35/**
36 * Unit tests for the manual graded behaviour.
37 *
38 * @copyright 2009 The Open University
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
afb1b3d0 41class qbehaviour_manualgraded_walkthrough_testcase extends qbehaviour_walkthrough_test_base {
603bd001
PS
42 public function test_manual_graded_essay() {
43
44 // Create an essay question.
45 $essay = test_question_maker::make_an_essay_question();
46 $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
47
48 // Check the right model is being used.
49 $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
50 $this->slot)->get_behaviour_name());
51
52 // Check the initial state.
53 $this->check_current_state(question_state::$todo);
54 $this->check_current_mark(null);
55 $this->check_current_output($this->get_contains_question_text_expectation($essay),
56 $this->get_does_not_contain_feedback_expectation());
57
58 // Simulate some data submitted by the student.
afb1b3d0 59 $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
603bd001
PS
60
61 // Verify.
62 $this->check_current_state(question_state::$complete);
63 $this->check_current_mark(null);
64 $this->check_current_output(
65 new question_contains_tag_with_attribute('textarea', 'name',
66 $this->quba->get_question_attempt($this->slot)->get_qt_field_name('answer')),
67 $this->get_does_not_contain_feedback_expectation());
68
69 // Process the same data again, check it does not create a new step.
70 $numsteps = $this->get_step_count();
afb1b3d0 71 $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
603bd001
PS
72 $this->check_step_count($numsteps);
73
74 // Process different data, check it creates a new step.
afb1b3d0 75 $this->process_submission(array('answer' => '', 'answerformat' => FORMAT_HTML));
603bd001
PS
76 $this->check_step_count($numsteps + 1);
77 $this->check_current_state(question_state::$todo);
78
79 // Change back, check it creates a new step.
afb1b3d0 80 $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
603bd001
PS
81 $this->check_step_count($numsteps + 2);
82
83 // Finish the attempt.
84 $this->quba->finish_all_questions();
85
86 // Verify.
87 $this->check_current_state(question_state::$needsgrading);
88 $this->check_current_mark(null);
89 $this->assertEquals('This is my wonderful essay!',
90 $this->quba->get_response_summary($this->slot));
91
92 // Process a manual comment.
53b8e256 93 $this->manual_grade('Not good enough!', 10, FORMAT_HTML);
603bd001
PS
94
95 // Verify.
96 $this->check_current_state(question_state::$mangrright);
97 $this->check_current_mark(10);
98 $this->check_current_output(
03cefcc9 99 new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
603bd001
PS
100
101 // Now change the max mark for the question and regrade.
102 $this->quba->regrade_question($this->slot, true, 1);
103
104 // Verify.
105 $this->check_current_state(question_state::$mangrright);
106 $this->check_current_mark(1);
107 }
108
8407a549
TH
109 public function test_manual_graded_essay_not_answered() {
110
111 // Create an essay question.
112 $essay = test_question_maker::make_an_essay_question();
113 $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
114
115 // Check the right model is being used.
116 $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
117 $this->slot)->get_behaviour_name());
118
119 // Check the initial state.
120 $this->check_current_state(question_state::$todo);
121 $this->check_current_mark(null);
122 $this->check_current_output($this->get_contains_question_text_expectation($essay),
123 $this->get_does_not_contain_feedback_expectation());
124
125 // Finish the attempt.
126 $this->quba->finish_all_questions();
127
128 // Verify.
129 $this->check_current_state(question_state::$gaveup);
130 $this->check_current_mark(null);
131 $this->assertEquals('',
132 $this->quba->get_response_summary($this->slot));
133
134 // Process a manual comment.
6026f235 135 $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
8407a549
TH
136
137 // Verify.
138 $this->check_current_state(question_state::$mangrpartial);
139 $this->check_current_mark(1);
140 $this->check_current_output(
141 new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
142
143 // Now change the max mark for the question and regrade.
144 $this->quba->regrade_question($this->slot, true, 1);
145
146 // Verify.
147 $this->check_current_state(question_state::$mangrpartial);
148 $this->check_current_mark(0.1);
149 }
150
603bd001
PS
151 public function test_manual_graded_truefalse() {
152
153 // Create a true-false question with correct answer true.
154 $tf = test_question_maker::make_question('truefalse', 'true');
155 $this->start_attempt_at_question($tf, 'manualgraded', 2);
156
157 // Check the initial state.
158 $this->check_current_state(question_state::$todo);
159 $this->check_current_mark(null);
160 $this->check_current_output(
161 $this->get_contains_question_text_expectation($tf),
162 $this->get_does_not_contain_feedback_expectation());
163
164 // Process a true answer and check the expected result.
165 $this->process_submission(array('answer' => 1));
166
167 $this->check_current_state(question_state::$complete);
168 $this->check_current_mark(null);
169 $this->check_current_output(
170 $this->get_contains_tf_true_radio_expectation(true, true),
171 $this->get_does_not_contain_correctness_expectation(),
172 $this->get_does_not_contain_feedback_expectation());
173
174 // Finish the attempt.
175 $this->quba->finish_all_questions();
176
177 // Verify.
178 $this->check_current_state(question_state::$needsgrading);
179 $this->check_current_mark(null);
180 $this->check_current_output(
181 $this->get_does_not_contain_correctness_expectation(),
182 $this->get_does_not_contain_specific_feedback_expectation());
183
184 // Process a manual comment.
53b8e256 185 $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
603bd001
PS
186
187 $this->check_current_state(question_state::$mangrpartial);
188 $this->check_current_mark(1);
189 $this->check_current_output(
190 $this->get_does_not_contain_correctness_expectation(),
191 $this->get_does_not_contain_specific_feedback_expectation(),
03cefcc9 192 new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
603bd001
PS
193 }
194
195 public function test_manual_graded_ignore_repeat_sumbission() {
196 // Create an essay question.
197 $essay = test_question_maker::make_an_essay_question();
198 $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
199
200 // Check the right model is being used.
201 $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
202 $this->slot)->get_behaviour_name());
203
204 // Check the initial state.
205 $this->check_current_state(question_state::$todo);
206 $this->check_current_mark(null);
207
208 // Simulate some data submitted by the student.
afb1b3d0 209 $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
603bd001
PS
210
211 // Verify.
212 $this->check_current_state(question_state::$complete);
213 $this->check_current_mark(null);
214
215 // Finish the attempt.
216 $this->quba->finish_all_questions();
217
218 // Verify.
219 $this->check_current_state(question_state::$needsgrading);
220 $this->check_current_mark(null);
221 $this->assertEquals('This is my wonderful essay!',
222 $this->quba->get_response_summary($this->slot));
223
224 // Process a blank manual comment. Ensure it does not change the state.
225 $numsteps = $this->get_step_count();
53b8e256 226 $this->manual_grade('', '', FORMAT_HTML);
603bd001
PS
227 $this->check_step_count($numsteps);
228 $this->check_current_state(question_state::$needsgrading);
229 $this->check_current_mark(null);
230
231 // Process a comment, but with the mark blank. Should be recorded, but
232 // not change the mark.
53b8e256 233 $this->manual_grade('I am not sure what grade to award.', '', FORMAT_HTML);
603bd001
PS
234 $this->check_step_count($numsteps + 1);
235 $this->check_current_state(question_state::$needsgrading);
236 $this->check_current_mark(null);
237 $this->check_current_output(
238 new question_pattern_expectation('/' .
03cefcc9 239 preg_quote('I am not sure what grade to award.', '/') . '/'));
603bd001
PS
240
241 // Now grade it.
53b8e256 242 $this->manual_grade('Pretty good!', '9.00000', FORMAT_HTML);
603bd001
PS
243 $this->check_step_count($numsteps + 2);
244 $this->check_current_state(question_state::$mangrpartial);
245 $this->check_current_mark(9);
246 $this->check_current_output(
03cefcc9 247 new question_pattern_expectation('/' . preg_quote('Pretty good!', '/') . '/'));
603bd001
PS
248
249 // Process the same data again, and make sure it does not add a step.
53b8e256 250 $this->manual_grade('Pretty good!', '9.00000', FORMAT_HTML);
603bd001
PS
251 $this->check_step_count($numsteps + 2);
252 $this->check_current_state(question_state::$mangrpartial);
253 $this->check_current_mark(9);
254
255 // Now set the mark back to blank.
53b8e256 256 $this->manual_grade('Actually, I am not sure any more.', '', FORMAT_HTML);
603bd001
PS
257 $this->check_step_count($numsteps + 3);
258 $this->check_current_state(question_state::$needsgrading);
259 $this->check_current_mark(null);
260 $this->check_current_output(
261 new question_pattern_expectation('/' .
03cefcc9 262 preg_quote('Actually, I am not sure any more.', '/') . '/'));
603bd001
PS
263
264 $qa = $this->quba->get_question_attempt($this->slot);
265 $this->assertEquals('Commented: Actually, I am not sure any more.',
266 $qa->summarise_action($qa->get_last_step()));
267 }
268
269 public function test_manual_graded_essay_can_grade_0() {
270
271 // Create an essay question.
272 $essay = test_question_maker::make_an_essay_question();
273 $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
274
275 // Check the right model is being used.
276 $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
277 $this->slot)->get_behaviour_name());
278
279 // Check the initial state.
280 $this->check_current_state(question_state::$todo);
281 $this->check_current_mark(null);
282 $this->check_current_output($this->get_contains_question_text_expectation($essay),
283 $this->get_does_not_contain_feedback_expectation());
284
285 // Simulate some data submitted by the student.
afb1b3d0 286 $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
603bd001
PS
287
288 // Verify.
289 $this->check_current_state(question_state::$complete);
290 $this->check_current_mark(null);
291 $this->check_current_output(
292 new question_contains_tag_with_attribute('textarea', 'name',
293 $this->quba->get_question_attempt($this->slot)->get_qt_field_name('answer')),
294 $this->get_does_not_contain_feedback_expectation());
295
296 // Finish the attempt.
297 $this->quba->finish_all_questions();
298
299 // Verify.
300 $this->check_current_state(question_state::$needsgrading);
301 $this->check_current_mark(null);
302 $this->assertEquals('This is my wonderful essay!',
303 $this->quba->get_response_summary($this->slot));
304
305 // Process a blank comment and a grade of 0.
53b8e256 306 $this->manual_grade('', 0, FORMAT_HTML);
603bd001
PS
307
308 // Verify.
309 $this->check_current_state(question_state::$mangrwrong);
310 $this->check_current_mark(0);
311 }
312}