MDL-37506 question code: incorrect preg_quote uses.
[moodle.git] / question / behaviour / adaptivenopenalty / 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 adaptive (no penalties)k
19  * behaviour.
20  *
21  * @package    qbehaviour
22  * @subpackage adaptivenopenalty
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 adaptive (no penalties) 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_adaptivenopenalty_walkthrough_test extends qbehaviour_walkthrough_test_base {
43     protected function get_does_not_contain_gradingdetails_expectation() {
44         return new question_no_pattern_expectation('/class="gradingdetails"/');
45     }
47     public function test_multichoice() {
49         // Create a multiple choice, single response question.
50         $mc = test_question_maker::make_a_multichoice_single_question();
51         $mc->penalty = 0.3333333;
52         $this->start_attempt_at_question($mc, 'adaptivenopenalty', 3);
54         $rightindex = $this->get_mc_right_answer_index($mc);
55         $wrongindex = ($rightindex + 1) % 3;
57         // Check the initial state.
58         $this->check_current_state(question_state::$todo);
59         $this->check_current_mark(null);
60         $this->check_current_output(
61                 $this->get_contains_marked_out_of_summary(),
62                 $this->get_contains_question_text_expectation($mc),
63                 $this->get_contains_mc_radio_expectation(0, true, false),
64                 $this->get_contains_mc_radio_expectation(1, true, false),
65                 $this->get_contains_mc_radio_expectation(2, true, false),
66                 $this->get_contains_submit_button_expectation(true),
67                 $this->get_does_not_contain_feedback_expectation());
69         // Process a submit.
70         $this->process_submission(array('answer' => $wrongindex, '-submit' => 1));
72         // Verify.
73         $this->check_current_state(question_state::$todo);
74         $this->check_current_mark(0);
75         $this->check_current_output(
76                 $this->get_contains_mark_summary(0),
77                 $this->get_contains_mc_radio_expectation($wrongindex, true, true),
78                 $this->get_contains_mc_radio_expectation(($wrongindex + 1) % 3, true, false),
79                 $this->get_contains_mc_radio_expectation(($wrongindex + 2) % 3, true, false),
80                 $this->get_contains_incorrect_expectation());
81         $this->assertRegExp('/B|C/',
82                 $this->quba->get_response_summary($this->slot));
84         // Process a change of answer to the right one, but not sumbitted.
85         $this->process_submission(array('answer' => $rightindex));
87         // Verify.
88         $this->check_current_state(question_state::$todo);
89         $this->check_current_mark(0);
90         $this->check_current_output(
91                 $this->get_contains_mark_summary(0),
92                 $this->get_contains_mc_radio_expectation($rightindex, true, true),
93                 $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
94                 $this->get_contains_mc_radio_expectation(($rightindex + 2) % 3, true, false));
95         $this->assertRegExp('/B|C/',
96                 $this->quba->get_response_summary($this->slot));
98         // Now submit the right answer.
99         $this->process_submission(array('answer' => $rightindex, '-submit' => 1));
101         // Verify.
102         $this->check_current_state(question_state::$complete);
103         $this->check_current_mark(3);
104         $this->check_current_output(
105                 $this->get_contains_mark_summary(3),
106                 $this->get_contains_mc_radio_expectation($rightindex, true, true),
107                 $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
108                 $this->get_contains_mc_radio_expectation(($rightindex + 2) % 3, true, false),
109                 $this->get_contains_correct_expectation());
110         $this->assertEquals('A',
111                 $this->quba->get_response_summary($this->slot));
113         // Finish the attempt.
114         $this->quba->finish_all_questions();
116         // Verify.
117         $this->check_current_state(question_state::$gradedright);
118         $this->check_current_mark(3);
119         $this->check_current_output(
120                 $this->get_contains_mark_summary(3),
121                 $this->get_contains_mc_radio_expectation($rightindex, false, true),
122                 $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, false, false),
123                 $this->get_contains_mc_radio_expectation(($rightindex + 2) % 3, false, false),
124                 $this->get_contains_correct_expectation());
126         // Process a manual comment.
127         $this->manual_grade('Not good enough!', 1);
129         // Verify.
130         $this->check_current_state(question_state::$mangrpartial);
131         $this->check_current_mark(1);
132         $this->check_current_output(
133                 $this->get_contains_mark_summary(1),
134                 new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
136         // Now change the correct answer to the question, and regrade.
137         $mc->answers[13]->fraction = -0.33333333;
138         $mc->answers[14]->fraction = 1; // We don't know which "wrong" index we chose above!
139         $mc->answers[15]->fraction = 1; // Therefore, treat answers B and C with the same score.
140         $this->quba->regrade_all_questions();
142         // Verify.
143         $this->check_current_state(question_state::$mangrpartial);
144         $this->check_current_mark(1);
145         $this->check_current_output(
146                 $this->get_contains_mark_summary(1),
147                 $this->get_contains_partcorrect_expectation());
149         $autogradedstep = $this->get_step($this->get_step_count() - 3);
150         $this->assertEquals($autogradedstep->get_fraction(), 1, '', 0.0000001);
151     }
153     public function test_multichoice2() {
155         // Create a multiple choice, multiple response question.
156         $mc = test_question_maker::make_a_multichoice_multi_question();
157         $mc->penalty = 0.3333333;
158         $mc->shuffleanswers = 0;
159         $this->start_attempt_at_question($mc, 'adaptivenopenalty', 2);
161         // Check the initial state.
162         $this->check_current_state(question_state::$todo);
163         $this->check_current_mark(null);
164         $this->check_current_output(
165                 $this->get_contains_marked_out_of_summary(),
166                 $this->get_contains_question_text_expectation($mc),
167                 $this->get_contains_submit_button_expectation(true),
168                 $this->get_does_not_contain_feedback_expectation());
170         // Process a submit.
171         $this->process_submission(array('choice0' => 1, 'choice2' => 1, '-submit' => 1));
173         // Verify.
174         $this->check_current_state(question_state::$complete);
175         $this->check_current_mark(2);
176         $this->check_current_output(
177                 $this->get_contains_mark_summary(2),
178                 $this->get_contains_submit_button_expectation(true),
179                 $this->get_contains_correct_expectation());
181         // Save the same correct answer again. Should no do anything.
182         $numsteps = $this->get_step_count();
183         $this->process_submission(array('choice0' => 1, 'choice2' => 1));
185         // Verify.
186         $this->check_step_count($numsteps);
187         $this->check_current_state(question_state::$complete);
189         // Finish the attempt.
190         $this->quba->finish_all_questions();
192         // Verify.
193         $this->check_step_count($numsteps + 1);
194         $this->check_current_state(question_state::$gradedright);
195         $this->check_current_mark(2);
196         $this->check_current_output(
197                 $this->get_contains_mark_summary(2),
198                 $this->get_contains_submit_button_expectation(false),
199                 $this->get_contains_correct_expectation());
200     }
202     public function test_numerical_invalid() {
204         // Create a numerical question
205         $numq = test_question_maker::make_question('numerical', 'pi');
206         $numq->penalty = 0.1;
207         $this->start_attempt_at_question($numq, 'adaptivenopenalty');
209         // Check the initial state.
210         $this->check_current_state(question_state::$todo);
211         $this->check_current_mark(null);
212         $this->check_current_output(
213                 $this->get_contains_marked_out_of_summary(),
214                 $this->get_contains_submit_button_expectation(true),
215                 $this->get_does_not_contain_feedback_expectation());
217         // Submit a non-numerical answer.
218         $this->process_submission(array('-submit' => 1, 'answer' => 'Pi'));
220         // Verify.
221         $this->check_current_state(question_state::$invalid);
222         $this->check_current_mark(null);
223         $this->check_current_output(
224                 $this->get_contains_marked_out_of_summary(1),
225                 $this->get_contains_submit_button_expectation(true),
226                 $this->get_does_not_contain_correctness_expectation(),
227                 $this->get_contains_validation_error_expectation(),
228                 $this->get_does_not_contain_feedback_expectation());
230         // Submit an incorrect answer.
231         $this->process_submission(array('-submit' => 1, 'answer' => '-5'));
233         // Verify.
234         $this->check_current_state(question_state::$todo);
235         $this->check_current_mark(0);
236         $this->check_current_output(
237                 $this->get_contains_mark_summary(0),
238                 $this->get_contains_submit_button_expectation(true),
239                 $this->get_contains_incorrect_expectation(),
240                 $this->get_does_not_contain_validation_error_expectation());
242         // Submit another non-numerical answer.
243         $this->process_submission(array('-submit' => 1, 'answer' => 'Pi*2'));
245         // Verify.
246         $this->check_current_state(question_state::$invalid);
247         $this->check_current_mark(0);
248         $this->check_current_output(
249                 $this->get_contains_mark_summary(0),
250                 $this->get_contains_submit_button_expectation(true),
251                 $this->get_does_not_contain_correctness_expectation(),
252                 $this->get_contains_validation_error_expectation(),
253                 $this->get_does_not_contain_gradingdetails_expectation());
255         // Submit the correct answer.
256         $this->process_submission(array('-submit' => 1, 'answer' => '3.14'));
258         // Verify.
259         $this->check_current_state(question_state::$complete);
260         $this->check_current_mark(1.0);
261         $this->check_current_output(
262                 $this->get_contains_mark_summary(1.0),
263                 $this->get_contains_submit_button_expectation(true),
264                 $this->get_contains_correct_expectation(),
265                 $this->get_does_not_contain_validation_error_expectation());
267         // Submit another non-numerical answer.
268         $this->process_submission(array('-submit' => 1, 'answer' => 'Pi/3'));
270         // Verify.
271         $this->check_current_state(question_state::$invalid);
272         $this->check_current_mark(1.0);
273         $this->check_current_output(
274                 $this->get_contains_mark_summary(1.0),
275                 $this->get_contains_submit_button_expectation(true),
276                 $this->get_does_not_contain_correctness_expectation(),
277                 $this->get_contains_validation_error_expectation(),
278                 $this->get_does_not_contain_gradingdetails_expectation());
280         // Finish the attempt.
281         $this->quba->finish_all_questions();
283         // Verify.
284         $this->check_current_state(question_state::$gradedwrong);
285         $this->check_current_mark(1.0);
286         $this->check_current_output(
287                 $this->get_contains_mark_summary(1.0),
288                 $this->get_contains_submit_button_expectation(false),
289                 $this->get_contains_incorrect_expectation(),
290                 $this->get_does_not_contain_validation_error_expectation());
291     }