1bc806709fdd91698bb2665b2813263f55c9cdce
[moodle.git] / question / behaviour / deferredfeedback / 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  * behaviour.
20  *
21  * @package    qbehaviour
22  * @subpackage deferredfeedback
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 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_deferredfeedback_walkthrough_test extends qbehaviour_walkthrough_test_base {
42     public function test_deferredfeedback_feedback_truefalse() {
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, 'deferredfeedback', 2);
48         // Check the initial state.
49         $this->check_current_state(question_state::$todo);
50         $this->check_current_mark(null);
51         $this->check_current_output($this->get_contains_question_text_expectation($tf),
52                 $this->get_does_not_contain_feedback_expectation());
53         $this->assertEquals(get_string('true', 'qtype_truefalse'),
54                 $this->quba->get_right_answer_summary($this->slot));
55         $this->assertRegExp('/' . preg_quote($tf->questiontext, '/') . '/',
56                 $this->quba->get_question_summary($this->slot));
57         $this->assertNull($this->quba->get_response_summary($this->slot));
59         // Process a true answer and check the expected result.
60         $this->process_submission(array('answer' => 1));
62         $this->check_current_state(question_state::$complete);
63         $this->check_current_mark(null);
64         $this->check_current_output($this->get_contains_tf_true_radio_expectation(true, true),
65                 $this->get_does_not_contain_correctness_expectation(),
66                 $this->get_does_not_contain_feedback_expectation());
68         // Process the same data again, check it does not create a new step.
69         $numsteps = $this->get_step_count();
70         $this->process_submission(array('answer' => 1));
71         $this->check_step_count($numsteps);
73         // Process different data, check it creates a new step.
74         $this->process_submission(array('answer' => 0));
75         $this->check_step_count($numsteps + 1);
76         $this->check_current_state(question_state::$complete);
78         // Change back, check it creates a new step.
79         $this->process_submission(array('answer' => 1));
80         $this->check_step_count($numsteps + 2);
82         // Finish the attempt.
83         $this->quba->finish_all_questions();
85         // Verify.
86         $this->check_current_state(question_state::$gradedright);
87         $this->check_current_mark(2);
88         $this->check_current_output($this->get_contains_correct_expectation(),
89                 $this->get_contains_tf_true_radio_expectation(false, true),
90                 new question_pattern_expectation('/class="r0 correct"/'));
91         $this->assertEquals(get_string('true', 'qtype_truefalse'),
92                 $this->quba->get_response_summary($this->slot));
94         // Process a manual comment.
95         $this->manual_grade('Not good enough!', 1, FORMAT_HTML);
97         $this->check_current_state(question_state::$mangrpartial);
98         $this->check_current_mark(1);
99         $this->check_current_output(
100                 new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
102         // Now change the correct answer to the question, and regrade.
103         $tf->rightanswer = false;
104         $this->quba->regrade_all_questions();
106         // Verify.
107         $this->check_current_state(question_state::$mangrpartial);
108         $this->check_current_mark(1);
110         $autogradedstep = $this->get_step($this->get_step_count() - 2);
111         $this->assertEquals($autogradedstep->get_fraction(), 0, '', 0.0000001);
112     }
114     public function test_deferredfeedback_feedback_multichoice_single() {
116         // Create a true-false question with correct answer true.
117         $mc = test_question_maker::make_a_multichoice_single_question();
118         $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
120         // Start a deferred feedback attempt and add the question to it.
121         $rightindex = $this->get_mc_right_answer_index($mc);
123         $this->check_current_state(question_state::$todo);
124         $this->check_current_mark(null);
125         $this->check_current_output(
126                 $this->get_contains_question_text_expectation($mc),
127                 $this->get_contains_mc_radio_expectation(0, true, false),
128                 $this->get_contains_mc_radio_expectation(1, true, false),
129                 $this->get_contains_mc_radio_expectation(2, true, false),
130                 $this->get_does_not_contain_feedback_expectation());
132         // Process the data extracted for this question.
133         $this->process_submission(array('answer' => $rightindex));
135         // Verify.
136         $this->check_current_state(question_state::$complete);
137         $this->check_current_mark(null);
138         $this->check_current_output(
139                 $this->get_contains_mc_radio_expectation($rightindex, true, true),
140                 $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
141                 $this->get_contains_mc_radio_expectation(($rightindex + 1) % 3, true, false),
142                 $this->get_does_not_contain_correctness_expectation(),
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(3);
151         $this->check_current_output(
152                 $this->get_contains_mc_radio_expectation($rightindex, false, true),
153                 $this->get_contains_correct_expectation());
155         // Now change the correct answer to the question, and regrade.
156         $mc->answers[13]->fraction = -0.33333333;
157         $mc->answers[14]->fraction = 1;
158         $this->quba->regrade_all_questions();
160         // Verify.
161         $this->check_current_state(question_state::$gradedwrong);
162         $this->check_current_mark(-1);
163         $this->check_current_output(
164                 $this->get_contains_incorrect_expectation());
165     }
167     public function test_deferredfeedback_resume_multichoice_single() {
169         // Create a multiple-choice question.
170         $mc = test_question_maker::make_a_multichoice_single_question();
172         // Attempt it getting it wrong.
173         $this->start_attempt_at_question($mc, 'deferredfeedback', 3);
174         $rightindex = $this->get_mc_right_answer_index($mc);
175         $wrongindex = ($rightindex + 1) % 3;
176         $this->process_submission(array('answer' => $wrongindex));
177         $this->quba->finish_all_questions();
179         // Verify.
180         $this->check_current_state(question_state::$gradedwrong);
181         $this->check_current_mark(-1);
182         $this->check_current_output(
183                 $this->get_contains_mc_radio_expectation($wrongindex, false, true),
184                 $this->get_contains_incorrect_expectation());
186         // Save the old attempt.
187         $oldqa = $this->quba->get_question_attempt($this->slot);
189         // Reinitialise.
190         $this->setUp();
191         $this->quba->set_preferred_behaviour('deferredfeedback');
192         $this->slot = $this->quba->add_question($mc, 3);
193         $this->quba->start_question_based_on($this->slot, $oldqa);
195         // Verify.
196         $this->check_current_state(question_state::$todo);
197         $this->check_current_mark(null);
198         $this->check_current_output(
199                 $this->get_contains_mc_radio_expectation($wrongindex, true, true),
200                 $this->get_does_not_contain_feedback_expectation(),
201                 $this->get_does_not_contain_correctness_expectation());
203         // Now get it right.
204         $this->process_submission(array('answer' => $rightindex));
205         $this->quba->finish_all_questions();
207         // Verify.
208         $this->check_current_state(question_state::$gradedright);
209         $this->check_current_mark(3);
210         $this->check_current_output(
211                 $this->get_contains_mc_radio_expectation($rightindex, false, true),
212                 $this->get_contains_correct_expectation());
213     }