MDL-37506 question code: incorrect preg_quote uses.
[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_test extends qbehaviour_walkthrough_test_base {
42     public function test_manual_graded_essay() {
44         // Create an essay question.
45         $essay = test_question_maker::make_an_essay_question();
46         $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
48         // Check the right model is being used.
49         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
50                 $this->slot)->get_behaviour_name());
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());
58         // Simulate some data submitted by the student.
59         $this->process_submission(array('answer' => 'This is my wonderful essay!'));
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());
69         // Process the same data again, check it does not create a new step.
70         $numsteps = $this->get_step_count();
71         $this->process_submission(array('answer' => 'This is my wonderful essay!'));
72         $this->check_step_count($numsteps);
74         // Process different data, check it creates a new step.
75         $this->process_submission(array('answer' => ''));
76         $this->check_step_count($numsteps + 1);
77         $this->check_current_state(question_state::$todo);
79         // Change back, check it creates a new step.
80         $this->process_submission(array('answer' => 'This is my wonderful essay!'));
81         $this->check_step_count($numsteps + 2);
83         // Finish the attempt.
84         $this->quba->finish_all_questions();
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));
92         // Process a manual comment.
93         $this->manual_grade('Not good enough!', 10);
95         // Verify.
96         $this->check_current_state(question_state::$mangrright);
97         $this->check_current_mark(10);
98         $this->check_current_output(
99                 new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
101         // Now change the max mark for the question and regrade.
102         $this->quba->regrade_question($this->slot, true, 1);
104         // Verify.
105         $this->check_current_state(question_state::$mangrright);
106         $this->check_current_mark(1);
107     }
109     public function test_manual_graded_truefalse() {
111         // Create a true-false question with correct answer true.
112         $tf = test_question_maker::make_question('truefalse', 'true');
113         $this->start_attempt_at_question($tf, 'manualgraded', 2);
115         // Check the initial state.
116         $this->check_current_state(question_state::$todo);
117         $this->check_current_mark(null);
118         $this->check_current_output(
119                 $this->get_contains_question_text_expectation($tf),
120                 $this->get_does_not_contain_feedback_expectation());
122         // Process a true answer and check the expected result.
123         $this->process_submission(array('answer' => 1));
125         $this->check_current_state(question_state::$complete);
126         $this->check_current_mark(null);
127         $this->check_current_output(
128                 $this->get_contains_tf_true_radio_expectation(true, true),
129                 $this->get_does_not_contain_correctness_expectation(),
130                 $this->get_does_not_contain_feedback_expectation());
132         // Finish the attempt.
133         $this->quba->finish_all_questions();
135         // Verify.
136         $this->check_current_state(question_state::$needsgrading);
137         $this->check_current_mark(null);
138         $this->check_current_output(
139                 $this->get_does_not_contain_correctness_expectation(),
140                 $this->get_does_not_contain_specific_feedback_expectation());
142         // Process a manual comment.
143         $this->manual_grade('Not good enough!', 1);
145         $this->check_current_state(question_state::$mangrpartial);
146         $this->check_current_mark(1);
147         $this->check_current_output(
148             $this->get_does_not_contain_correctness_expectation(),
149             $this->get_does_not_contain_specific_feedback_expectation(),
150             new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
151     }
153     public function test_manual_graded_ignore_repeat_sumbission() {
154         // Create an essay question.
155         $essay = test_question_maker::make_an_essay_question();
156         $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
158         // Check the right model is being used.
159         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
160                 $this->slot)->get_behaviour_name());
162         // Check the initial state.
163         $this->check_current_state(question_state::$todo);
164         $this->check_current_mark(null);
166         // Simulate some data submitted by the student.
167         $this->process_submission(array('answer' => 'This is my wonderful essay!'));
169         // Verify.
170         $this->check_current_state(question_state::$complete);
171         $this->check_current_mark(null);
173         // Finish the attempt.
174         $this->quba->finish_all_questions();
176         // Verify.
177         $this->check_current_state(question_state::$needsgrading);
178         $this->check_current_mark(null);
179         $this->assertEquals('This is my wonderful essay!',
180                 $this->quba->get_response_summary($this->slot));
182         // Process a blank manual comment. Ensure it does not change the state.
183         $numsteps = $this->get_step_count();
184         $this->manual_grade('', '');
185         $this->check_step_count($numsteps);
186         $this->check_current_state(question_state::$needsgrading);
187         $this->check_current_mark(null);
189         // Process a comment, but with the mark blank. Should be recorded, but
190         // not change the mark.
191         $this->manual_grade('I am not sure what grade to award.', '');
192         $this->check_step_count($numsteps + 1);
193         $this->check_current_state(question_state::$needsgrading);
194         $this->check_current_mark(null);
195         $this->check_current_output(
196                 new question_pattern_expectation('/' .
197                         preg_quote('I am not sure what grade to award.', '/') . '/'));
199         // Now grade it.
200         $this->manual_grade('Pretty good!', '9.00000');
201         $this->check_step_count($numsteps + 2);
202         $this->check_current_state(question_state::$mangrpartial);
203         $this->check_current_mark(9);
204         $this->check_current_output(
205                 new question_pattern_expectation('/' . preg_quote('Pretty good!', '/') . '/'));
207         // Process the same data again, and make sure it does not add a step.
208         $this->manual_grade('Pretty good!', '9.00000');
209         $this->check_step_count($numsteps + 2);
210         $this->check_current_state(question_state::$mangrpartial);
211         $this->check_current_mark(9);
213         // Now set the mark back to blank.
214         $this->manual_grade('Actually, I am not sure any more.', '');
215         $this->check_step_count($numsteps + 3);
216         $this->check_current_state(question_state::$needsgrading);
217         $this->check_current_mark(null);
218         $this->check_current_output(
219                 new question_pattern_expectation('/' .
220                         preg_quote('Actually, I am not sure any more.', '/') . '/'));
222         $qa = $this->quba->get_question_attempt($this->slot);
223         $this->assertEquals('Commented: Actually, I am not sure any more.',
224                 $qa->summarise_action($qa->get_last_step()));
225     }
227     public function test_manual_graded_essay_can_grade_0() {
229         // Create an essay question.
230         $essay = test_question_maker::make_an_essay_question();
231         $this->start_attempt_at_question($essay, 'deferredfeedback', 10);
233         // Check the right model is being used.
234         $this->assertEquals('manualgraded', $this->quba->get_question_attempt(
235                 $this->slot)->get_behaviour_name());
237         // Check the initial state.
238         $this->check_current_state(question_state::$todo);
239         $this->check_current_mark(null);
240         $this->check_current_output($this->get_contains_question_text_expectation($essay),
241                 $this->get_does_not_contain_feedback_expectation());
243         // Simulate some data submitted by the student.
244         $this->process_submission(array('answer' => 'This is my wonderful essay!'));
246         // Verify.
247         $this->check_current_state(question_state::$complete);
248         $this->check_current_mark(null);
249         $this->check_current_output(
250                 new question_contains_tag_with_attribute('textarea', 'name',
251                 $this->quba->get_question_attempt($this->slot)->get_qt_field_name('answer')),
252                 $this->get_does_not_contain_feedback_expectation());
254         // Finish the attempt.
255         $this->quba->finish_all_questions();
257         // Verify.
258         $this->check_current_state(question_state::$needsgrading);
259         $this->check_current_mark(null);
260         $this->assertEquals('This is my wonderful essay!',
261                 $this->quba->get_response_summary($this->slot));
263         // Process a blank comment and a grade of 0.
264         $this->manual_grade('', 0);
266         // Verify.
267         $this->check_current_state(question_state::$mangrwrong);
268         $this->check_current_mark(0);
269     }