1 |
efrain |
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 |
namespace qtype_truefalse;
|
|
|
18 |
|
|
|
19 |
use question_state;
|
1441 |
ariadna |
20 |
use question_display_options;
|
1 |
efrain |
21 |
|
|
|
22 |
defined('MOODLE_INTERNAL') || die();
|
|
|
23 |
|
|
|
24 |
global $CFG;
|
|
|
25 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Walkthrough tests for the truefalse question type.
|
|
|
30 |
*
|
|
|
31 |
* @package qtype_truefalse
|
|
|
32 |
* @copyright 2011 The Open University
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @covers \qtype_truefalse_question
|
|
|
35 |
*/
|
1441 |
ariadna |
36 |
final class walkthrough_test extends \qbehaviour_walkthrough_test_base {
|
11 |
efrain |
37 |
public function test_false_right_does_not_show_feedback_when_not_answered(): void {
|
1 |
efrain |
38 |
|
|
|
39 |
// Create a true-false question with correct answer false.
|
|
|
40 |
$tf = \test_question_maker::make_question('truefalse', 'false');
|
|
|
41 |
$this->start_attempt_at_question($tf, 'deferredfeedback', 1);
|
|
|
42 |
|
|
|
43 |
// Check the initial state.
|
|
|
44 |
$this->check_current_state(question_state::$todo);
|
|
|
45 |
$this->check_current_mark(null);
|
|
|
46 |
$this->check_current_output(
|
|
|
47 |
$this->get_contains_question_text_expectation($tf),
|
|
|
48 |
$this->get_does_not_contain_feedback_expectation(),
|
|
|
49 |
new \question_contains_tag_with_contents('h4',
|
|
|
50 |
get_string('questiontext', 'question')));
|
|
|
51 |
$this->assertEquals(get_string('false', 'qtype_truefalse'),
|
|
|
52 |
$this->quba->get_right_answer_summary($this->slot));
|
|
|
53 |
$this->assertMatchesRegularExpression('/' . preg_quote($tf->questiontext, '/') . '/',
|
|
|
54 |
$this->quba->get_question_summary($this->slot));
|
|
|
55 |
$this->assertNull($this->quba->get_response_summary($this->slot));
|
|
|
56 |
|
|
|
57 |
// Finish the attempt without answering.
|
|
|
58 |
$this->quba->finish_all_questions();
|
|
|
59 |
|
|
|
60 |
// Verify.
|
|
|
61 |
$this->check_current_state(question_state::$gaveup);
|
|
|
62 |
$this->check_current_mark(null);
|
|
|
63 |
$this->check_current_output(
|
|
|
64 |
$this->get_contains_tf_true_radio_expectation(false, false),
|
|
|
65 |
$this->get_contains_tf_false_radio_expectation(false, false),
|
|
|
66 |
|
|
|
67 |
// In particular, check that the false feedback is not displayed.
|
|
|
68 |
new \question_no_pattern_expectation('/' . preg_quote($tf->falsefeedback, '/') . '/'));
|
|
|
69 |
|
|
|
70 |
}
|
|
|
71 |
|
11 |
efrain |
72 |
public function test_each_attempt_builds_on_last_and_regrade(): void {
|
1 |
efrain |
73 |
|
|
|
74 |
// Create a true-false question with correct answer false.
|
|
|
75 |
$tf = \test_question_maker::make_question('truefalse', 'false');
|
|
|
76 |
$this->start_attempt_at_question(clone($tf), 'deferredfeedback', 1);
|
|
|
77 |
|
|
|
78 |
// Submit the answer false (correct).
|
|
|
79 |
$this->process_submission(['answer' => 0]);
|
|
|
80 |
|
|
|
81 |
// Finish the attempt.
|
|
|
82 |
$this->quba->finish_all_questions();
|
|
|
83 |
|
|
|
84 |
// Check the state.
|
|
|
85 |
$this->check_current_state(question_state::$gradedright);
|
|
|
86 |
$this->check_current_mark(1);
|
|
|
87 |
$this->check_current_output(
|
|
|
88 |
$this->get_contains_tf_true_radio_expectation(false, false),
|
|
|
89 |
$this->get_contains_tf_false_radio_expectation(false, true));
|
|
|
90 |
|
|
|
91 |
// Start a new attempt based on the first one.
|
|
|
92 |
$firstattemptqa = $this->quba->get_question_attempt($this->slot);
|
|
|
93 |
$this->quba = \question_engine::make_questions_usage_by_activity('unit_test',
|
|
|
94 |
\context_system::instance());
|
|
|
95 |
$this->quba->set_preferred_behaviour('deferredfeedback');
|
|
|
96 |
$this->slot = $this->quba->add_question(clone($tf), 1);
|
|
|
97 |
$this->quba->start_question_based_on($this->slot, $firstattemptqa);
|
|
|
98 |
|
|
|
99 |
// Verify.
|
|
|
100 |
$this->check_current_state(question_state::$complete);
|
|
|
101 |
$this->check_current_mark(null);
|
|
|
102 |
$this->check_current_output(
|
|
|
103 |
$this->get_contains_tf_true_radio_expectation(true, false),
|
|
|
104 |
$this->get_contains_tf_false_radio_expectation(true, true));
|
|
|
105 |
|
|
|
106 |
// Finish the attempt without changing the answer.
|
|
|
107 |
$this->quba->finish_all_questions();
|
|
|
108 |
|
|
|
109 |
// Check the state.
|
|
|
110 |
$this->check_current_state(question_state::$gradedright);
|
|
|
111 |
$this->check_current_mark(1);
|
|
|
112 |
$this->check_current_output(
|
|
|
113 |
$this->get_contains_tf_true_radio_expectation(false, false),
|
|
|
114 |
$this->get_contains_tf_false_radio_expectation(false, true));
|
|
|
115 |
|
|
|
116 |
// Regrade the attempt - code based on question_usage_by_activity::regrade_question.
|
|
|
117 |
$oldqa = $this->quba->get_question_attempt($this->slot);
|
|
|
118 |
$newqa = new \question_attempt(clone($tf),
|
|
|
119 |
$oldqa->get_usage_id(), $this->quba->get_observer());
|
|
|
120 |
$newqa->set_database_id($oldqa->get_database_id());
|
|
|
121 |
$newqa->set_slot($oldqa->get_slot());
|
|
|
122 |
$newqa->regrade($oldqa, true);
|
|
|
123 |
|
|
|
124 |
// Check the state.
|
|
|
125 |
$this->assertEquals(question_state::$gradedright, $newqa->get_state());
|
|
|
126 |
$this->assertEquals(1, $newqa->get_mark());
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* @covers \qtype_truefalse_renderer::formulation_and_controls
|
|
|
131 |
*/
|
11 |
efrain |
132 |
public function test_deferredfeedback_feedback_multichoice_single_showstandardinstruction_yes(): void {
|
1 |
efrain |
133 |
|
|
|
134 |
// Create a true-false question with correct answer false.
|
|
|
135 |
$tf = \test_question_maker::make_question('truefalse', 'false');
|
|
|
136 |
$tf->showstandardinstruction = true;
|
|
|
137 |
|
|
|
138 |
$this->start_attempt_at_question($tf, 'deferredfeedback', 1);
|
|
|
139 |
$this->render();
|
|
|
140 |
|
|
|
141 |
// Check for 'Show standard instruction'.
|
|
|
142 |
$standardinstruction = get_string('selectone', 'qtype_truefalse');
|
|
|
143 |
$this->assertStringContainsString($standardinstruction, $this->currentoutput);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* @covers \qtype_truefalse_renderer::formulation_and_controls
|
|
|
148 |
*/
|
11 |
efrain |
149 |
public function test_deferredfeedback_feedback_multichoice_single_showstandardinstruction_no(): void {
|
1 |
efrain |
150 |
|
|
|
151 |
// Create a true-false question with correct answer false.
|
|
|
152 |
$tf = \test_question_maker::make_question('truefalse', 'false');
|
|
|
153 |
$tf->showstandardinstruction = false;
|
|
|
154 |
|
|
|
155 |
$this->start_attempt_at_question($tf, 'deferredfeedback', 1);
|
|
|
156 |
$this->render();
|
|
|
157 |
|
|
|
158 |
// Check for 'Show standard instruction'.
|
|
|
159 |
$standardinstruction = \html_writer::tag('legend', get_string('answer'), [
|
1441 |
ariadna |
160 |
'class' => 'prompt h6 fw-normal visually-hidden',
|
1 |
efrain |
161 |
]);
|
|
|
162 |
$this->assertStringContainsString($standardinstruction, $this->currentoutput);
|
|
|
163 |
}
|
1441 |
ariadna |
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Tests that the general feedback box is not displayed when it is empty for a false answer.
|
|
|
167 |
*
|
|
|
168 |
* @covers ::format_generalfeedback
|
|
|
169 |
*/
|
|
|
170 |
public function test_false_right_does_not_show_empty_general_feedback_when_answer(): void {
|
|
|
171 |
$tf = \test_question_maker::make_question('truefalse', 'false');
|
|
|
172 |
$this->start_attempt_at_question($tf, 'deferredfeedback', 1);
|
|
|
173 |
$this->process_submission(data: ['answer' => 0]);
|
|
|
174 |
$this->quba->finish_all_questions();
|
|
|
175 |
$displayoptions = new question_display_options();
|
|
|
176 |
$html = $this->quba->render_question(1, $displayoptions);
|
|
|
177 |
$pattern = "/<div class=\"generalfeedback\">\s*<div class=\"clearfix\">\s*{$tf->generalfeedback}\s*<\/div>\s*<\/div>/";
|
|
|
178 |
$this->assertMatchesRegularExpression($pattern, $html);
|
|
|
179 |
|
|
|
180 |
$tf->generalfeedback = "";
|
|
|
181 |
$this->start_attempt_at_question($tf, 'deferredfeedback', 1);
|
|
|
182 |
$this->process_submission(data: ['answer' => 0]);
|
|
|
183 |
$this->quba->finish_all_questions();
|
|
|
184 |
$html = $this->quba->render_question(1, $displayoptions);
|
|
|
185 |
$pattern = "/<div class=\"generalfeedback\">\s*<div class=\"clearfix\"><\/div>\s*<\/div>/";
|
|
|
186 |
$this->assertDoesNotMatchRegularExpression($pattern, $html);
|
|
|
187 |
}
|
1 |
efrain |
188 |
}
|