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_missingtype;
|
|
|
18 |
|
|
|
19 |
use qbehaviour_deferredfeedback;
|
|
|
20 |
use qtype_missingtype;
|
|
|
21 |
use qtype_missingtype_question;
|
|
|
22 |
use question_attempt_step;
|
|
|
23 |
use question_bank;
|
|
|
24 |
use question_contains_tag_with_attribute;
|
|
|
25 |
use question_display_options;
|
|
|
26 |
use question_state;
|
|
|
27 |
use testable_question_attempt;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
require_once(__DIR__ . '/../../../engine/tests/helpers.php');
|
|
|
33 |
require_once(__DIR__ . '/../../../behaviour/deferredfeedback/behaviour.php');
|
|
|
34 |
require_once(__DIR__ . '/../question.php');
|
|
|
35 |
require_once($CFG->dirroot . '/question/type/missingtype/questiontype.php');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Unit tests for the 'missing' question type.
|
|
|
39 |
*
|
|
|
40 |
* @package qtype_missingtype
|
|
|
41 |
* @copyright 2010 The Open University
|
|
|
42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
43 |
*/
|
1441 |
ariadna |
44 |
final class missingtype_test extends \question_testcase {
|
1 |
efrain |
45 |
|
|
|
46 |
protected function get_unknown_questiondata() {
|
|
|
47 |
$questiondata = new \stdClass();
|
|
|
48 |
$questiondata->id = 0;
|
|
|
49 |
$questiondata->category = 0;
|
|
|
50 |
$questiondata->contextid = 0;
|
|
|
51 |
$questiondata->parent = 0;
|
|
|
52 |
$questiondata->name = 'Test';
|
|
|
53 |
$questiondata->questiontext = 'This is the question text.';
|
|
|
54 |
$questiondata->questiontextformat = FORMAT_HTML;
|
|
|
55 |
$questiondata->generalfeedback = 'This is the general feedback.';
|
|
|
56 |
$questiondata->generalfeedbackformat = FORMAT_HTML;
|
|
|
57 |
$questiondata->defaultmark = 1;
|
|
|
58 |
$questiondata->penalty = 0.3333333;
|
|
|
59 |
$questiondata->qtype = 'strange_unknown';
|
|
|
60 |
$questiondata->length = 1;
|
|
|
61 |
$questiondata->stamp = make_unique_id_code();
|
|
|
62 |
$questiondata->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY;
|
|
|
63 |
$questiondata->version = 1;
|
|
|
64 |
$questiondata->versionid = 0;
|
|
|
65 |
$questiondata->questionbankentryid = 0;
|
|
|
66 |
$questiondata->idnumber = null;
|
|
|
67 |
$questiondata->timecreated = 0;
|
|
|
68 |
$questiondata->timemodified = 0;
|
|
|
69 |
$questiondata->createdby = 0;
|
|
|
70 |
$questiondata->modifiedby = 0;
|
|
|
71 |
|
|
|
72 |
return $questiondata;
|
|
|
73 |
}
|
|
|
74 |
|
11 |
efrain |
75 |
public function test_cannot_grade(): void {
|
1 |
efrain |
76 |
$q = new qtype_missingtype_question();
|
|
|
77 |
$this->expectException(\moodle_exception::class);
|
|
|
78 |
$q->grade_response(array());
|
|
|
79 |
}
|
|
|
80 |
|
11 |
efrain |
81 |
public function test_load_qtype_strict(): void {
|
1 |
efrain |
82 |
$this->expectException(\moodle_exception::class);
|
|
|
83 |
$qtype = question_bank::get_qtype('strange_unknown');
|
|
|
84 |
}
|
|
|
85 |
|
11 |
efrain |
86 |
public function test_load_qtype_lax(): void {
|
1 |
efrain |
87 |
$qtype = question_bank::get_qtype('strange_unknown', false);
|
|
|
88 |
$this->assertInstanceOf('qtype_missingtype', $qtype);
|
|
|
89 |
}
|
|
|
90 |
|
11 |
efrain |
91 |
public function test_make_question(): void {
|
1 |
efrain |
92 |
$questiondata = $this->get_unknown_questiondata();
|
|
|
93 |
$q = question_bank::make_question($questiondata);
|
|
|
94 |
$this->assertInstanceOf('qtype_missingtype_question', $q);
|
|
|
95 |
$this->assertEquals($q->questiontext, \html_writer::tag('div',
|
|
|
96 |
get_string('missingqtypewarning', 'qtype_missingtype'),
|
|
|
97 |
array('class' => 'warning missingqtypewarning')) .
|
|
|
98 |
$questiondata->questiontext);
|
|
|
99 |
}
|
|
|
100 |
|
11 |
efrain |
101 |
public function test_render_missing(): void {
|
1 |
efrain |
102 |
$questiondata = $this->get_unknown_questiondata();
|
|
|
103 |
$q = question_bank::make_question($questiondata);
|
|
|
104 |
$qa = new testable_question_attempt($q, 0);
|
|
|
105 |
|
|
|
106 |
$step = new question_attempt_step(array('answer' => 'frog'));
|
|
|
107 |
$step->set_state(question_state::$todo);
|
|
|
108 |
$qa->add_step($step);
|
|
|
109 |
$qa->set_behaviour(new qbehaviour_deferredfeedback($qa, 'deferredfeedback'));
|
|
|
110 |
|
|
|
111 |
$output = $qa->render(new question_display_options(), '1');
|
|
|
112 |
|
|
|
113 |
$this->assertMatchesRegularExpression('/' .
|
|
|
114 |
preg_quote($qa->get_question(false)->questiontext, '/') . '/', $output);
|
|
|
115 |
$this->assertMatchesRegularExpression('/' .
|
|
|
116 |
preg_quote(get_string('missingqtypewarning', 'qtype_missingtype'), '/') . '/', $output);
|
|
|
117 |
$this->assert(new question_contains_tag_with_attribute(
|
|
|
118 |
'div', 'class', 'warning missingqtypewarning'), $output);
|
|
|
119 |
}
|
|
|
120 |
|
11 |
efrain |
121 |
public function test_get_question_summary(): void {
|
1 |
efrain |
122 |
$q = new qtype_missingtype_question();
|
|
|
123 |
$q->questiontext = '<b>Test</b>';
|
|
|
124 |
$this->assertEquals('TEST', $q->get_question_summary());
|
|
|
125 |
}
|
|
|
126 |
|
11 |
efrain |
127 |
public function test_summarise_response(): void {
|
1 |
efrain |
128 |
$q = new qtype_missingtype_question();
|
|
|
129 |
$this->assertNull($q->summarise_response(array('a' => 'irrelevant')));
|
|
|
130 |
}
|
|
|
131 |
|
11 |
efrain |
132 |
public function test_can_analyse_responses(): void {
|
1 |
efrain |
133 |
$qtype = new qtype_missingtype();
|
|
|
134 |
$this->assertFalse($qtype->can_analyse_responses());
|
|
|
135 |
}
|
|
|
136 |
|
11 |
efrain |
137 |
public function test_get_random_guess_score(): void {
|
1 |
efrain |
138 |
$qtype = new qtype_missingtype();
|
|
|
139 |
$this->assertNull($qtype->get_random_guess_score(null));
|
|
|
140 |
}
|
|
|
141 |
|
11 |
efrain |
142 |
public function test_get_possible_responses(): void {
|
1 |
efrain |
143 |
$qtype = new qtype_missingtype();
|
|
|
144 |
$this->assertEquals(array(), $qtype->get_possible_responses(null));
|
|
|
145 |
}
|
1441 |
ariadna |
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Test moving a question category from one context to another when it contains questions of an invalid type
|
|
|
149 |
*
|
|
|
150 |
* @covers ::question_move_category_to_context
|
|
|
151 |
*/
|
|
|
152 |
public function test_move_question_category_with_missing_question_types(): void {
|
|
|
153 |
|
|
|
154 |
global $DB;
|
|
|
155 |
|
|
|
156 |
$this->resetAfterTest();
|
|
|
157 |
|
|
|
158 |
// Create a course we can move the category to.
|
|
|
159 |
$course = $this->getDataGenerator()->create_course();
|
|
|
160 |
|
|
|
161 |
// Create a quiz module to attach the question category to.
|
|
|
162 |
$module1 = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
163 |
|
|
|
164 |
// And another one to move things to.
|
|
|
165 |
$module2 = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
166 |
|
|
|
167 |
// Create a question category on the system context.
|
|
|
168 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
169 |
$category = $generator->create_question_category([
|
|
|
170 |
'contextid' => \core\context\module::instance($module1->cmid)->id,
|
|
|
171 |
]);
|
|
|
172 |
|
|
|
173 |
// Create a question of an invalid type and put it in the category.
|
|
|
174 |
$question = $generator->create_question('missingtype', null, ['category' => $category->id]);
|
|
|
175 |
|
|
|
176 |
// Update the question to set an invalid qtype, as "missingtype" is actually installed and won't fail.
|
|
|
177 |
$question->qtype = 'invalid';
|
|
|
178 |
$DB->update_record('question', $question);
|
|
|
179 |
|
|
|
180 |
// We just want to assert that no exception is thrown.
|
|
|
181 |
$this->expectNotToPerformAssertions();
|
|
|
182 |
|
|
|
183 |
// Try and move the categories.
|
|
|
184 |
question_move_category_to_context(
|
|
|
185 |
$category->id,
|
|
|
186 |
\core\context\module::instance($module1->cmid)->id,
|
|
|
187 |
\core\context\module::instance($module2->cmid)->id,
|
|
|
188 |
);
|
|
|
189 |
|
|
|
190 |
}
|
|
|
191 |
|
1 |
efrain |
192 |
}
|