| Línea 29... |
Línea 29... |
| 29 |
*
|
29 |
*
|
| 30 |
* @package mod_quiz
|
30 |
* @package mod_quiz
|
| 31 |
* @category test
|
31 |
* @category test
|
| 32 |
* @copyright 2018 the Open University
|
32 |
* @copyright 2018 the Open University
|
| 33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| - |
|
34 |
* @covers \mod_quiz\question\bank\custom_view
|
| 34 |
*/
|
35 |
*/
|
| 35 |
class quiz_question_bank_view_test extends \advanced_testcase {
|
36 |
final class quiz_question_bank_view_test extends \advanced_testcase {
|
| Línea 36... |
Línea 37... |
| 36 |
|
37 |
|
| 37 |
public function test_viewing_question_bank_should_not_load_individual_questions(): void {
|
38 |
public function test_viewing_question_bank_should_not_load_individual_questions(): void {
|
| 38 |
$this->resetAfterTest();
|
39 |
$this->resetAfterTest();
|
| 39 |
$this->setAdminUser();
|
40 |
$this->setAdminUser();
|
| Línea 47... |
Línea 48... |
| 47 |
$context = \context_module::instance($quiz->cmid);
|
48 |
$context = \context_module::instance($quiz->cmid);
|
| 48 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
49 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
| Línea 49... |
Línea 50... |
| 49 |
|
50 |
|
| 50 |
// Create a question in the default category.
|
51 |
// Create a question in the default category.
|
| 51 |
$contexts = new question_edit_contexts($context);
|
- |
|
| 52 |
question_make_default_categories($contexts->all());
|
52 |
$contexts = new question_edit_contexts($context);
|
| 53 |
$cat = question_get_default_category($context->id);
|
53 |
$cat = question_get_default_category($context->id, true);
|
| 54 |
$questiondata = $questiongenerator->create_question('numerical', null,
|
54 |
$questiondata = $questiongenerator->create_question('numerical', null,
|
| Línea 55... |
Línea 55... |
| 55 |
['name' => 'Example question', 'category' => $cat->id]);
|
55 |
['name' => 'Example question', 'category' => $cat->id]);
|
| 56 |
|
56 |
|
| Línea 66... |
Línea 66... |
| 66 |
'recurse' => false,
|
66 |
'recurse' => false,
|
| 67 |
'showhidden' => false,
|
67 |
'showhidden' => false,
|
| 68 |
'qbshowtext' => false,
|
68 |
'qbshowtext' => false,
|
| 69 |
'tabname' => 'editq'
|
69 |
'tabname' => 'editq'
|
| 70 |
];
|
70 |
];
|
| 71 |
$extraparams = ['cmid' => $cm->id];
|
71 |
$extraparams = ['cmid' => $cm->id, 'quizcmid' => $cm->id];
|
| 72 |
$view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, $extraparams);
|
72 |
$view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, $extraparams);
|
| 73 |
ob_start();
|
73 |
ob_start();
|
| 74 |
$view->display();
|
74 |
$view->display();
|
| 75 |
$html = ob_get_clean();
|
75 |
$html = ob_get_clean();
|
| Línea 78... |
Línea 78... |
| 78 |
$this->assertStringContainsString('Example question', $html);
|
78 |
$this->assertStringContainsString('Example question', $html);
|
| Línea 79... |
Línea 79... |
| 79 |
|
79 |
|
| 80 |
// Verify the question has not been loaded into the cache.
|
80 |
// Verify the question has not been loaded into the cache.
|
| 81 |
$this->assertFalse($cache->has($questiondata->id));
|
81 |
$this->assertFalse($cache->has($questiondata->id));
|
| - |
|
82 |
}
|
| - |
|
83 |
|
| - |
|
84 |
public function test_viewing_question_bank_should_not_load_hidden_question(): void {
|
| - |
|
85 |
$this->resetAfterTest();
|
| - |
|
86 |
$this->setAdminUser();
|
| - |
|
87 |
$generator = $this->getDataGenerator();
|
| - |
|
88 |
/** @var core_question_generator $questiongenerator */
|
| - |
|
89 |
$questiongenerator = $generator->get_plugin_generator('core_question');
|
| - |
|
90 |
|
| - |
|
91 |
// Create a course and a quiz.
|
| - |
|
92 |
$course = $generator->create_course();
|
| - |
|
93 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
| - |
|
94 |
$context = \context_module::instance($quiz->cmid);
|
| - |
|
95 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
| - |
|
96 |
|
| - |
|
97 |
// Create a question in the default category.
|
| - |
|
98 |
$contexts = new question_edit_contexts($context);
|
| - |
|
99 |
$cat = question_get_default_category($context->id, true);
|
| - |
|
100 |
$question = $questiongenerator->create_question('numerical', null,
|
| - |
|
101 |
['name' => 'Example question', 'category' => $cat->id]);
|
| - |
|
102 |
|
| - |
|
103 |
// Create another version.
|
| - |
|
104 |
$newversion = $questiongenerator->update_question($question, null, ['name' => 'This is the latest version']);
|
| - |
|
105 |
|
| - |
|
106 |
// Add them to the quiz.
|
| - |
|
107 |
quiz_add_quiz_question($newversion->id, $quiz);
|
| - |
|
108 |
// Generate the view.
|
| - |
|
109 |
$params = [
|
| - |
|
110 |
'qpage' => 0,
|
| - |
|
111 |
'qperpage' => 20,
|
| - |
|
112 |
'cat' => $cat->id . ',' . $context->id,
|
| - |
|
113 |
'recurse' => false,
|
| - |
|
114 |
'showhidden' => false,
|
| - |
|
115 |
'qbshowtext' => false,
|
| - |
|
116 |
'tabname' => 'editq',
|
| - |
|
117 |
];
|
| - |
|
118 |
$extraparams = ['quizcmid' => $cm->id];
|
| - |
|
119 |
$view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, $extraparams);
|
| - |
|
120 |
ob_start();
|
| - |
|
121 |
$view->display();
|
| - |
|
122 |
$html = ob_get_clean();
|
| - |
|
123 |
// Verify the output should included the latest version.
|
| - |
|
124 |
$this->assertStringContainsString('This is the latest version', $html);
|
| - |
|
125 |
$this->assertStringNotContainsString('Example question', $html);
|
| - |
|
126 |
// Delete the latest version.
|
| - |
|
127 |
question_delete_question($newversion->id);
|
| - |
|
128 |
// Verify the output should display the old version with status ready.
|
| - |
|
129 |
ob_start();
|
| - |
|
130 |
$view->display();
|
| - |
|
131 |
$html = ob_get_clean();
|
| - |
|
132 |
$this->assertStringContainsString('Example question', $html);
|
| - |
|
133 |
$this->assertStringNotContainsString('This is the latest version', $html);
|
| - |
|
134 |
}
|
| - |
|
135 |
|
| - |
|
136 |
public function test_viewing_question_bank_when_paging_out_of_limit(): void {
|
| - |
|
137 |
$this->resetAfterTest();
|
| - |
|
138 |
$this->setAdminUser();
|
| - |
|
139 |
|
| - |
|
140 |
$generator = $this->getDataGenerator();
|
| - |
|
141 |
/** @var core_question_generator $questiongenerator */
|
| - |
|
142 |
$questiongenerator = $generator->get_plugin_generator('core_question');
|
| - |
|
143 |
|
| - |
|
144 |
// Create a course and a quiz.
|
| - |
|
145 |
$course = $generator->create_course();
|
| - |
|
146 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
| - |
|
147 |
$context = \context_module::instance($quiz->cmid);
|
| - |
|
148 |
|
| - |
|
149 |
// Create a question in the default category.
|
| - |
|
150 |
$contexts = new question_edit_contexts($context);
|
| - |
|
151 |
$cm = get_coursemodule_from_instance('quiz', $quiz->id);
|
| - |
|
152 |
$cat = question_get_default_category($context->id, true);
|
| - |
|
153 |
|
| - |
|
154 |
// Create three questions.
|
| - |
|
155 |
$questiongenerator->create_question('numerical', null,
|
| - |
|
156 |
['name' => 'Example question 1', 'category' => $cat->id]);
|
| - |
|
157 |
$questiongenerator->create_question('numerical', null,
|
| - |
|
158 |
['name' => 'Example question 2', 'category' => $cat->id]);
|
| - |
|
159 |
$question3 = $questiongenerator->create_question('numerical', null,
|
| - |
|
160 |
['name' => 'Example question 3', 'category' => $cat->id]);
|
| - |
|
161 |
|
| - |
|
162 |
// Retrieve the question bank view on page 3 with 1 questions per page.
|
| - |
|
163 |
$params = [
|
| - |
|
164 |
'qpage' => 3,
|
| - |
|
165 |
'qperpage' => 1,
|
| - |
|
166 |
'cat' => $cat->id . ',' . $context->id,
|
| - |
|
167 |
'recurse' => false,
|
| - |
|
168 |
'showhidden' => false,
|
| - |
|
169 |
'qbshowtext' => false,
|
| - |
|
170 |
'tabname' => 'editq',
|
| - |
|
171 |
];
|
| - |
|
172 |
|
| - |
|
173 |
// Load the question bank view.
|
| - |
|
174 |
$view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, ['quizcmid' => $cm->id]);
|
| - |
|
175 |
ob_start();
|
| - |
|
176 |
$view->display();
|
| - |
|
177 |
$html = ob_get_clean();
|
| - |
|
178 |
|
| - |
|
179 |
// Verify that questions exist in the view.
|
| - |
|
180 |
$this->assertStringNotContainsString('Example question 1', $html);
|
| - |
|
181 |
$this->assertStringNotContainsString('Example question 2', $html);
|
| - |
|
182 |
$this->assertStringContainsString('Example question 3', $html);
|
| - |
|
183 |
|
| - |
|
184 |
// Set the param per page is 2.
|
| - |
|
185 |
// The view only has 2 pages.
|
| - |
|
186 |
$params['qperpage'] = 2;
|
| - |
|
187 |
|
| - |
|
188 |
// Reload the question bank view on page 3.
|
| - |
|
189 |
$view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, ['quizcmid' => $cm->id]);
|
| - |
|
190 |
ob_start();
|
| - |
|
191 |
$view->display();
|
| - |
|
192 |
$html = ob_get_clean();
|
| - |
|
193 |
|
| - |
|
194 |
// Since the view has only 2 pages and the requested page is out of range,
|
| - |
|
195 |
// the view will move to the nearest available page.
|
| - |
|
196 |
// Verify that the view is in the page 2.
|
| - |
|
197 |
$this->assertEquals(1, $view->get_pagevars('qpage'));
|
| - |
|
198 |
// Verify that questions exist in the view.
|
| - |
|
199 |
$this->assertStringNotContainsString('Example question 1', $html);
|
| - |
|
200 |
$this->assertStringNotContainsString('Example question 2', $html);
|
| - |
|
201 |
$this->assertStringContainsString('Example question 3', $html);
|
| - |
|
202 |
|
| - |
|
203 |
// Create a new category.
|
| - |
|
204 |
$newcategory = $generator->create_category();
|
| - |
|
205 |
$newcontext = \context_coursecat::instance($newcategory->id);
|
| - |
|
206 |
$newquestioncat = $questiongenerator->create_question_category([
|
| - |
|
207 |
'contextid' => $newcontext->id,
|
| - |
|
208 |
]);
|
| - |
|
209 |
// Move question 3 to a new category.
|
| - |
|
210 |
question_move_questions_to_category([$question3->id], $newquestioncat->id);
|
| - |
|
211 |
// Load the question bank view from the new category.
|
| - |
|
212 |
$params['cat'] = $newquestioncat->id . ',' . $newquestioncat->contextid;
|
| - |
|
213 |
$view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, ['quizcmid' => $cm->id]);
|
| - |
|
214 |
ob_start();
|
| - |
|
215 |
$view->display();
|
| - |
|
216 |
$html = ob_get_clean();
|
| - |
|
217 |
// Verify that the view is in the page 1 and exist only one question.
|
| - |
|
218 |
$this->assertEquals(0, $view->get_pagevars('qpage'));
|
| - |
|
219 |
$this->assertStringContainsString('Example question 3', $html);
|
| 82 |
}
|
220 |
}
|