Merge branch 'MDL-40493-master-fix1' of git://github.com/damyon/moodle
[moodle.git] / question / behaviour / manualgraded / tests / walkthrough_test.php
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/>.
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  */
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once(dirname(__FILE__) . '/../../../engine/lib.php');
32 require_once(dirname(__FILE__) . '/../../../engine/tests/helpers.php');
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  */
41 class qbehaviour_manualgraded_walkthrough_testcase extends qbehaviour_walkthrough_test_base {
42     public function test_manual_graded_essay() {
44         // The current text editor depends on the users profile setting - so it needs a valid user.
45         $this->setAdminUser();
47         // Create an essay question.
48         $essay = test_question_maker::make_an_essay_question();
49         $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
51         // Check the right model is being used.
52         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
53                 $this->slot)->get_behaviour_name());
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());
61         // Simulate some data submitted by the student.
62         $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
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());
72         // Process the same data again, check it does not create a new step.
73         $numsteps = $this->get_step_count();
74         $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
75         $this->check_step_count($numsteps);
77         // Process different data, check it creates a new step.
78         $this->process_submission(array('answer' => '', 'answerformat' => FORMAT_HTML));
79         $this->check_step_count($numsteps + 1);
80         $this->check_current_state(question_state::$todo);
82         // Change back, check it creates a new step.
83         $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
84         $this->check_step_count($numsteps + 2);
86         // Finish the attempt.
87         $this->quba->finish_all_questions();
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));
95         // Process a manual comment.
96         $this->manual_grade('Not good enough!', 10, FORMAT_HTML);
98         // Verify.
99         $this->check_current_state(question_state::$mangrright);
100         $this->check_current_mark(10);
101         $this->check_current_output(
102                 new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
104         // Now change the max mark for the question and regrade.
105         $this->quba->regrade_question($this->slot, true, 1);
107         // Verify.
108         $this->check_current_state(question_state::$mangrright);
109         $this->check_current_mark(1);
110     }
112     public function test_manual_graded_essay_not_answered() {
114         // The current text editor depends on the users profile setting - so it needs a valid user.
115         $this->setAdminUser();
117         // Create an essay question.
118         $essay = test_question_maker::make_an_essay_question();
119         $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
121         // Check the right model is being used.
122         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
123                 $this->slot)->get_behaviour_name());
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());
131         // Finish the attempt.
132         $this->quba->finish_all_questions();
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));
140         // Process a manual comment.
141         $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
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!') . '/'));
149         // Now change the max mark for the question and regrade.
150         $this->quba->regrade_question($this->slot, true, 1);
152         // Verify.
153         $this->check_current_state(question_state::$mangrpartial);
154         $this->check_current_mark(0.1);
155     }
157     public function test_manual_graded_truefalse() {
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);
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());
170         // Process a true answer and check the expected result.
171         $this->process_submission(array('answer' => 1));
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());
180         // Finish the attempt.
181         $this->quba->finish_all_questions();
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());
190         // Process a manual comment.
191         $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
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(),
198             new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
199     }
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);
206         // Check the right model is being used.
207         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
208                 $this->slot)->get_behaviour_name());
210         // Check the initial state.
211         $this->check_current_state(question_state::$todo);
212         $this->check_current_mark(null);
214         // Simulate some data submitted by the student.
215         $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
217         // Verify.
218         $this->check_current_state(question_state::$complete);
219         $this->check_current_mark(null);
221         // Finish the attempt.
222         $this->quba->finish_all_questions();
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));
230         // Process a blank manual comment. Ensure it does not change the state.
231         $numsteps = $this->get_step_count();
232         $this->manual_grade('', '', FORMAT_HTML);
233         $this->check_step_count($numsteps);
234         $this->check_current_state(question_state::$needsgrading);
235         $this->check_current_mark(null);
237         // Process a comment, but with the mark blank. Should be recorded, but
238         // not change the mark.
239         $this->manual_grade('I am not sure what grade to award.', '', FORMAT_HTML);
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('/' .
245                         preg_quote('I am not sure what grade to award.', '/') . '/'));
247         // Now grade it.
248         $this->manual_grade('Pretty good!', '9.00000', FORMAT_HTML);
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(
253                 new question_pattern_expectation('/' . preg_quote('Pretty good!', '/') . '/'));
255         // Process the same data again, and make sure it does not add a step.
256         $this->manual_grade('Pretty good!', '9.00000', FORMAT_HTML);
257         $this->check_step_count($numsteps + 2);
258         $this->check_current_state(question_state::$mangrpartial);
259         $this->check_current_mark(9);
261         // Now set the mark back to blank.
262         $this->manual_grade('Actually, I am not sure any more.', '', FORMAT_HTML);
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('/' .
268                         preg_quote('Actually, I am not sure any more.', '/') . '/'));
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     }
275     public function test_manual_graded_essay_can_grade_0() {
277         // The current text editor depends on the users profile setting - so it needs a valid user.
278         $this->setAdminUser();
280         // Create an essay question.
281         $essay = test_question_maker::make_an_essay_question();
282         $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
284         // Check the right model is being used.
285         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
286                 $this->slot)->get_behaviour_name());
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());
294         // Simulate some data submitted by the student.
295         $this->process_submission(array('answer' => 'This is my wonderful essay!', 'answerformat' => FORMAT_HTML));
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());
305         // Finish the attempt.
306         $this->quba->finish_all_questions();
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));
314         // Process a blank comment and a grade of 0.
315         $this->manual_grade('', 0, FORMAT_HTML);
317         // Verify.
318         $this->check_current_state(question_state::$mangrwrong);
319         $this->check_current_mark(0);
320     }