weekly on-sync release 2.5dev
[moodle.git] / question / type / multianswer / tests / walkthrough_test.php
CommitLineData
603bd001
PS
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/>.
16
17/**
18 * This file contains overall tests of multianswer questions.
19 *
20 * @package qtype
21 * @subpackage multianswer
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31
32
33/**
34 * Unit tests for the multianswer question type.
35 *
36 * @copyright 2011 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_multianswer_walkthrough_test extends qbehaviour_walkthrough_test_base {
b2a79cc1
TH
40
41 protected function get_contains_subq_status(question_state $state) {
42 return new question_pattern_expectation('~' .
43 preg_quote($state->default_string(true), '~') . '<br />~');
44 }
45
be1d8ed1 46 public function test_deferred_feedback() {
603bd001 47
b2a79cc1 48 // Create a multianswer question.
be1d8ed1
TH
49 $q = test_question_maker::make_question('multianswer', 'fourmc');
50 $this->start_attempt_at_question($q, 'deferredfeedback', 4);
603bd001
PS
51
52 // Check the initial state.
53 $this->check_current_state(question_state::$todo);
54 $this->check_current_mark(null);
55 $this->check_current_output(
56 $this->get_contains_marked_out_of_summary(),
603bd001 57 $this->get_does_not_contain_feedback_expectation(),
be1d8ed1 58 $this->get_does_not_contain_validation_error_expectation());
603bd001 59
be1d8ed1
TH
60 // Save in incomplete answer.
61 $this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '',
62 'sub3_answer' => '', 'sub4_answer' => ''));
603bd001
PS
63
64 // Verify.
65 $this->check_current_state(question_state::$invalid);
66 $this->check_current_mark(null);
67 $this->check_current_output(
68 $this->get_contains_marked_out_of_summary(),
603bd001 69 $this->get_does_not_contain_feedback_expectation(),
be1d8ed1 70 $this->get_does_not_contain_validation_error_expectation()); // TODO, really, it should. See MDL-32049.
603bd001 71
be1d8ed1
TH
72 // Save a partially correct answer.
73 $this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '1',
74 'sub3_answer' => '1', 'sub4_answer' => '1'));
603bd001
PS
75
76 // Verify.
be1d8ed1 77 $this->check_current_state(question_state::$complete);
603bd001
PS
78 $this->check_current_mark(null);
79 $this->check_current_output(
80 $this->get_contains_marked_out_of_summary(),
603bd001 81 $this->get_does_not_contain_feedback_expectation(),
be1d8ed1 82 $this->get_does_not_contain_validation_error_expectation());
603bd001 83
be1d8ed1
TH
84 // Now submit all and finish.
85 $this->process_submission(array('-finish' => 1));
603bd001
PS
86
87 // Verify.
be1d8ed1
TH
88 $this->check_current_state(question_state::$gradedpartial);
89 $this->check_current_mark(2);
603bd001 90 $this->check_current_output(
be1d8ed1
TH
91 $this->get_contains_mark_summary(2),
92 $this->get_contains_partcorrect_expectation(),
93 $this->get_does_not_contain_validation_error_expectation());
603bd001 94 }
b2a79cc1
TH
95
96 public function test_deferred_feedback_numericalzero_not_answered() {
97 // Tests the situation found in MDL-35370.
98
99 // Create a multianswer question with one numerical subquestion, right answer zero.
100 $q = test_question_maker::make_question('multianswer', 'numericalzero');
101 $this->start_attempt_at_question($q, 'deferredfeedback', 1);
102
103 // Check the initial state.
104 $this->check_current_state(question_state::$todo);
105 $this->check_current_mark(null);
106 $this->check_current_output(
107 $this->get_contains_marked_out_of_summary(),
108 $this->get_does_not_contain_feedback_expectation(),
109 $this->get_does_not_contain_validation_error_expectation());
110
111 // Now submit all and finish.
112 $this->process_submission(array('-finish' => 1));
113
114 // Verify.
115 $this->check_current_state(question_state::$gaveup);
116 $this->check_current_mark(null);
117 $this->check_current_output(
118 $this->get_contains_marked_out_of_summary(),
119 new question_pattern_expectation('~<input[^>]* class="incorrect" [^>]*/>~'),
120 $this->get_contains_subq_status(question_state::$gaveup),
121 $this->get_does_not_contain_validation_error_expectation());
122 }
123
124 public function test_deferred_feedback_numericalzero_0_answer() {
125 // Tests the situation found in MDL-35370.
126
127 // Create a multianswer question with one numerical subquestion, right answer zero.
128 $q = test_question_maker::make_question('multianswer', 'numericalzero');
129 $this->start_attempt_at_question($q, 'deferredfeedback', 1);
130
131 // Check the initial state.
132 $this->check_current_state(question_state::$todo);
133 $this->check_current_mark(null);
134 $this->check_current_output(
135 $this->get_contains_marked_out_of_summary(),
136 $this->get_does_not_contain_feedback_expectation(),
137 $this->get_does_not_contain_validation_error_expectation());
138
139 // Save a the correct answer.
140 $this->process_submission(array('sub1_answer' => '0'));
141
142 // Verify.
143 $this->check_current_state(question_state::$complete);
144 $this->check_current_mark(null);
145 $this->check_current_output(
146 $this->get_contains_marked_out_of_summary(),
147 $this->get_does_not_contain_feedback_expectation(),
148 $this->get_does_not_contain_validation_error_expectation());
149
150 // Now submit all and finish.
151 $this->process_submission(array('-finish' => 1));
152
153 // Verify.
154 $this->check_current_state(question_state::$gradedright);
155 $this->check_current_mark(1);
156 $this->check_current_output(
157 $this->get_contains_mark_summary(1),
158 $this->get_contains_correct_expectation(),
159 $this->get_contains_subq_status(question_state::$gradedright),
160 $this->get_does_not_contain_validation_error_expectation());
161 }
162
163 public function test_deferred_feedback_numericalzero_0_wrong() {
164 // Tests the situation found in MDL-35370.
165
166 // Create a multianswer question with one numerical subquestion, right answer zero.
167 $q = test_question_maker::make_question('multianswer', 'numericalzero');
168 $this->start_attempt_at_question($q, 'deferredfeedback', 1);
169
170 // Check the initial state.
171 $this->check_current_state(question_state::$todo);
172 $this->check_current_mark(null);
173 $this->check_current_output(
174 $this->get_contains_marked_out_of_summary(),
175 $this->get_does_not_contain_feedback_expectation(),
176 $this->get_does_not_contain_validation_error_expectation());
177
178 // Save a the correct answer.
179 $this->process_submission(array('sub1_answer' => '42'));
180
181 // Verify.
182 $this->check_current_state(question_state::$complete);
183 $this->check_current_mark(null);
184 $this->check_current_output(
185 $this->get_contains_marked_out_of_summary(),
186 $this->get_does_not_contain_feedback_expectation(),
187 $this->get_does_not_contain_validation_error_expectation());
188
189 // Now submit all and finish.
190 $this->process_submission(array('-finish' => 1));
191
192 // Verify.
193 $this->check_current_state(question_state::$gradedwrong);
194 $this->check_current_mark(0);
195 $this->check_current_output(
196 $this->get_contains_mark_summary(0),
197 $this->get_contains_incorrect_expectation(),
198 $this->get_contains_subq_status(question_state::$gradedwrong),
199 $this->get_does_not_contain_validation_error_expectation());
200 }
603bd001 201}