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