MDL-31226: quiz: Fix message for attempts built on previous.
[moodle.git] / question / behaviour / deferredcbm / 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 deferred feedback
19  * with certainty base marking behaviour.
20  *
21  * @package    qbehaviour
22  * @subpackage deferredcbm
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 deferred feedback with certainty base marking 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_deferredcbm_walkthrough_test extends qbehaviour_walkthrough_test_base {
42     public function test_deferred_cbm_truefalse_high_certainty() {
44         // Create a true-false question with correct answer true.
45         $tf = test_question_maker::make_question('truefalse', 'true');
46         $this->start_attempt_at_question($tf, 'deferredcbm', 2);
48         // Verify.
49         $this->check_current_state(question_state::$todo);
50         $this->check_output_contains_lang_string('notyetanswered', 'question');
51         $this->check_current_mark(null);
52         $this->check_current_output(
53                 $this->get_contains_question_text_expectation($tf),
54                 $this->get_contains_tf_true_radio_expectation(true, false),
55                 $this->get_contains_tf_false_radio_expectation(true, false),
56                 $this->get_contains_cbm_radio_expectation(1, true, false),
57                 $this->get_contains_cbm_radio_expectation(2, true, false),
58                 $this->get_contains_cbm_radio_expectation(3, true, false),
59                 $this->get_does_not_contain_feedback_expectation());
61         // Process the data extracted for this question.
62         $this->process_submission(array('answer' => 1, '-certainty' => 3));
64         // Verify.
65         $this->check_current_state(question_state::$complete);
66         $this->check_output_contains_lang_string('answersaved', 'question');
67         $this->check_current_mark(null);
68         $this->check_current_output(
69                 $this->get_contains_tf_true_radio_expectation(true, true),
70                 $this->get_contains_cbm_radio_expectation(3, true, true),
71                 $this->get_does_not_contain_correctness_expectation(),
72                 $this->get_does_not_contain_feedback_expectation());
74         // Process the same data again, check it does not create a new step.
75         $numsteps = $this->get_step_count();
76         $this->process_submission(array('answer' => 1, '-certainty' => 3));
77         $this->check_step_count($numsteps);
79         // Process different data, check it creates a new step.
80         $this->process_submission(array('answer' => 1, '-certainty' => 1));
81         $this->check_step_count($numsteps + 1);
82         $this->check_current_state(question_state::$complete);
84         // Change back, check it creates a new step.
85         $this->process_submission(array('answer' => 1, '-certainty' => 3));
86         $this->check_step_count($numsteps + 2);
88         // Finish the attempt.
89         $this->quba->finish_all_questions();
91         // Verify.
92         $this->check_current_state(question_state::$gradedright);
93         $this->check_current_mark(2);
94         $this->check_current_output(
95                 $this->get_contains_tf_true_radio_expectation(false, true),
96                 $this->get_contains_cbm_radio_expectation(3, false, true),
97                 $this->get_contains_correct_expectation());
99         // Process a manual comment.
100         $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
102         // Verify.
103         $this->check_current_state(question_state::$mangrpartial);
104         $this->check_current_mark(1);
105         $this->check_current_output(new question_pattern_expectation('/' .
106                 preg_quote('Not good enough!', '/') . '/'));
108         // Now change the correct answer to the question, and regrade.
109         $tf->rightanswer = false;
110         $this->quba->regrade_all_questions();
112         // Verify.
113         $this->check_current_state(question_state::$mangrpartial);
114         $this->check_current_mark(1);
115         $autogradedstep = $this->get_step($this->get_step_count() - 2);
116         $this->assertEquals($autogradedstep->get_fraction(), -2, '', 0.0000001);
117     }
119     public function test_deferred_cbm_truefalse_low_certainty() {
121         // Create a true-false question with correct answer true.
122         $tf = test_question_maker::make_question('truefalse', 'true');
123         $this->start_attempt_at_question($tf, 'deferredcbm', 2);
125         // Verify.
126         $this->check_current_state(question_state::$todo);
127         $this->check_output_contains_lang_string('notyetanswered', 'question');
128         $this->check_current_mark(null);
129         $this->check_current_output(
130                 $this->get_does_not_contain_correctness_expectation(),
131                 $this->get_contains_cbm_radio_expectation(1, true, false),
132                 $this->get_does_not_contain_feedback_expectation());
134         // Submit ansewer with low certainty.
135         $this->process_submission(array('answer' => 1, '-certainty' => 1));
137         // Verify.
138         $this->check_current_state(question_state::$complete);
139         $this->check_output_contains_lang_string('answersaved', 'question');
140         $this->check_current_mark(null);
141         $this->check_current_output($this->get_does_not_contain_correctness_expectation(),
142                 $this->get_contains_cbm_radio_expectation(1, true, true),
143                 $this->get_does_not_contain_feedback_expectation());
145         // Finish the attempt.
146         $this->quba->finish_all_questions();
148         // Verify.
149         $this->check_current_state(question_state::$gradedright);
150         $this->check_current_mark(0.6666667);
151         $this->check_current_output($this->get_contains_correct_expectation(),
152                 $this->get_contains_cbm_radio_expectation(1, false, true));
153         $this->assertEquals(get_string('true', 'qtype_truefalse') . ' [' .
154                 question_cbm::get_string(1) . ']',
155                 $this->quba->get_response_summary($this->slot));
156     }
158     public function test_deferred_cbm_truefalse_default_certainty() {
160         // Create a true-false question with correct answer true.
161         $tf = test_question_maker::make_question('truefalse', 'true');
162         $this->start_attempt_at_question($tf, 'deferredcbm', 2);
164         // Verify.
165         $this->check_current_state(question_state::$todo);
166         $this->check_output_contains_lang_string('notyetanswered', 'question');
167         $this->check_current_mark(null);
168         $this->check_current_output(
169                 $this->get_does_not_contain_correctness_expectation(),
170                 $this->get_contains_cbm_radio_expectation(1, true, false),
171                 $this->get_does_not_contain_feedback_expectation());
173         // Submit ansewer with low certainty and finish the attempt.
174         $this->process_submission(array('answer' => 1));
175         $this->quba->finish_all_questions();
177         // Verify.
178         $qa = $this->quba->get_question_attempt($this->slot);
179         $this->check_current_state(question_state::$gradedright);
180         $this->check_current_mark(0.6666667);
181         $this->check_current_output($this->get_contains_correct_expectation(),
182                 $this->get_contains_cbm_radio_expectation(1, false, false),
183                 new question_pattern_expectation('/' . preg_quote(
184                         get_string('assumingcertainty', 'qbehaviour_deferredcbm',
185                         question_cbm::get_string(
186                             $qa->get_last_behaviour_var('_assumedcertainty'))), '/') . '/'));
187         $this->assertEquals(get_string('true', 'qtype_truefalse'),
188                 $this->quba->get_response_summary($this->slot));
189     }
191     public function test_deferredcbm_resume_multichoice_single() {
193         // Create a multiple-choice question.
194         $mc = test_question_maker::make_a_multichoice_single_question();
196         // Attempt it getting it wrong.
197         $this->start_attempt_at_question($mc, 'deferredcbm', 3);
198         $rightindex = $this->get_mc_right_answer_index($mc);
199         $wrongindex = ($rightindex + 1) % 3;
200         $this->process_submission(array('answer' => $wrongindex, '-certainty' => 2));
201         $this->quba->finish_all_questions();
203         // Verify.
204         $this->check_current_state(question_state::$gradedwrong);
205         $this->check_current_mark(-3.3333333);
206         $this->check_current_output(
207                 $this->get_contains_mc_radio_expectation($wrongindex, false, true),
208                 $this->get_contains_cbm_radio_expectation(2, false, true),
209                 $this->get_contains_incorrect_expectation());
210         $this->assertEquals('A [' . question_cbm::get_string(question_cbm::HIGH) . ']',
211                 $this->quba->get_right_answer_summary($this->slot));
212         $this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/',
213                 $this->quba->get_question_summary($this->slot));
214         $this->assertRegExp('/(B|C) \[' . preg_quote(question_cbm::get_string(2), '/') . '\]/',
215                 $this->quba->get_response_summary($this->slot));
217         // Save the old attempt.
218         $oldqa = $this->quba->get_question_attempt($this->slot);
220         // Reinitialise.
221         $this->setUp();
222         $this->quba->set_preferred_behaviour('deferredcbm');
223         $this->slot = $this->quba->add_question($mc, 3);
224         $this->quba->start_question_based_on($this->slot, $oldqa);
226         // Verify.
227         $this->check_current_state(question_state::$complete);
228         $this->check_output_contains_lang_string('notchanged', 'question');
229         $this->check_current_mark(null);
230         $this->check_current_output(
231                 $this->get_contains_mc_radio_expectation($wrongindex, true, true),
232                 $this->get_contains_cbm_radio_expectation(2, true, true),
233                 $this->get_does_not_contain_feedback_expectation(),
234                 $this->get_does_not_contain_correctness_expectation());
235         $this->assertEquals('A [' . question_cbm::get_string(question_cbm::HIGH) . ']',
236                 $this->quba->get_right_answer_summary($this->slot));
237         $this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/',
238                 $this->quba->get_question_summary($this->slot));
239         $this->assertNull($this->quba->get_response_summary($this->slot));
241         // Now get it right.
242         $this->process_submission(array('answer' => $rightindex, '-certainty' => 3));
243         $this->quba->finish_all_questions();
245         // Verify.
246         $this->check_current_state(question_state::$gradedright);
247         $this->check_current_mark(3);
248         $this->check_current_output(
249                 $this->get_contains_mc_radio_expectation($rightindex, false, true),
250                 $this->get_contains_cbm_radio_expectation(3, false, true),
251                 $this->get_contains_correct_expectation());
252         $this->assertRegExp('/(A) \[' . preg_quote(question_cbm::get_string(3), '/') . '\]/',
253                 $this->quba->get_response_summary($this->slot));
254     }
256     public function test_deferred_cbm_truefalse_no_certainty_feedback_when_not_answered() {
258         // Create a true-false question with correct answer true.
259         $tf = test_question_maker::make_question('truefalse', 'true');
260         $this->start_attempt_at_question($tf, 'deferredcbm', 2);
262         // Verify.
263         $this->check_current_state(question_state::$todo);
264         $this->check_output_contains_lang_string('notyetanswered', 'question');
265         $this->check_current_mark(null);
266         $this->check_current_output(
267                 $this->get_does_not_contain_correctness_expectation(),
268                 $this->get_contains_cbm_radio_expectation(1, true, false),
269                 $this->get_does_not_contain_feedback_expectation());
271         // Finish without answering.
272         $this->quba->finish_all_questions();
274         // Verify.
275         $this->check_current_state(question_state::$gaveup);
276         $this->check_current_mark(null);
277         $this->check_current_output(
278                 new question_no_pattern_expectation('/class=\"im-feedback/'));
279     }