Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 mod_quiz;
18
 
19
use core_question\local\bank\question_edit_contexts;
20
use mod_quiz\question\bank\custom_view;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
require_once($CFG->dirroot . '/question/editlib.php');
26
 
27
/**
28
 * Unit tests for the quiz's own question bank view class.
29
 *
30
 * @package    mod_quiz
31
 * @category   test
32
 * @copyright  2018 the Open University
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 34
 * @covers \mod_quiz\question\bank\custom_view
1 efrain 35
 */
1441 ariadna 36
final class quiz_question_bank_view_test extends \advanced_testcase {
1 efrain 37
 
11 efrain 38
    public function test_viewing_question_bank_should_not_load_individual_questions(): void {
1 efrain 39
        $this->resetAfterTest();
40
        $this->setAdminUser();
41
        $generator = $this->getDataGenerator();
42
        /** @var core_question_generator $questiongenerator */
43
        $questiongenerator = $generator->get_plugin_generator('core_question');
44
 
45
        // Create a course and a quiz.
46
        $course = $generator->create_course();
47
        $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
48
        $context = \context_module::instance($quiz->cmid);
49
        $cm = get_coursemodule_from_instance('quiz', $quiz->id);
50
 
51
        // Create a question in the default category.
52
        $contexts = new question_edit_contexts($context);
1441 ariadna 53
        $cat = question_get_default_category($context->id, true);
1 efrain 54
        $questiondata = $questiongenerator->create_question('numerical', null,
55
                ['name' => 'Example question', 'category' => $cat->id]);
56
 
57
        // Ensure the question is not in the cache.
58
        $cache = \cache::make('core', 'questiondata');
59
        $cache->delete($questiondata->id);
60
 
61
        // Generate the view.
62
        $params = [
63
            'qpage' => 0,
64
            'qperpage' => 20,
65
            'cat' => $cat->id . ',' . $context->id,
66
            'recurse' => false,
67
            'showhidden' => false,
68
            'qbshowtext' => false,
69
            'tabname' => 'editq'
70
        ];
1441 ariadna 71
        $extraparams = ['cmid' => $cm->id, 'quizcmid' => $cm->id];
1 efrain 72
        $view = new custom_view($contexts, new \moodle_url('/'), $course, $cm, $params, $extraparams);
73
        ob_start();
74
        $view->display();
75
        $html = ob_get_clean();
76
 
77
        // Verify the output includes the expected question.
78
        $this->assertStringContainsString('Example question', $html);
79
 
80
        // Verify the question has not been loaded into the cache.
81
        $this->assertFalse($cache->has($questiondata->id));
82
    }
1441 ariadna 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);
220
    }
1 efrain 221
}