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 qbank_managecategories;
18
 
1441 ariadna 19
use core\exception\moodle_exception;
20
use core_question\category_manager;
1 efrain 21
 
1441 ariadna 22
defined('MOODLE_INTERNAL') || die;
1 efrain 23
 
24
global $CFG;
1441 ariadna 25
require_once($CFG->dirroot . '/question/bank/managecategories/tests/manage_category_test_base.php');
1 efrain 26
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
27
 
28
/**
29
 * Unit tests for helper class.
30
 *
31
 * @package    qbank_managecategories
32
 * @copyright  2006 The Open University
33
 * @author     2021, Guillermo Gomez Arias <guillermogomez@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @coversDefaultClass \qbank_managecategories\helper
36
 */
1441 ariadna 37
final class helper_test extends manage_category_test_base {
1 efrain 38
    use \quiz_question_helper_test_trait;
39
 
40
    /**
41
     * @var \context_module module context.
42
     */
43
    protected $context;
44
 
45
    /**
46
     * @var \stdClass course object.
47
     */
48
    protected $course;
49
 
50
    /**
51
     * @var \component_generator_base question generator.
52
     */
53
    protected $qgenerator;
54
 
55
    /**
56
     * @var \stdClass quiz object.
57
     */
58
    protected $quiz;
59
 
60
    /**
61
     * Tests initial setup.
62
     */
63
    protected function setUp(): void {
64
        parent::setUp();
65
        self::setAdminUser();
66
        $this->resetAfterTest();
67
 
68
        $datagenerator = $this->getDataGenerator();
69
        $this->course = $datagenerator->create_course();
1441 ariadna 70
        $this->quiz = $datagenerator->create_module(
71
            'quiz',
72
            ['course' => $this->course->id, 'name' => 'Quiz 1'],
73
        );
1 efrain 74
        $this->qgenerator = $datagenerator->get_plugin_generator('core_question');
75
        $this->context = \context_module::instance($this->quiz->cmid);
76
    }
77
 
78
    /**
79
     * Test question_remove_stale_questions_from_category function.
80
     *
81
     * @covers ::question_remove_stale_questions_from_category
82
     */
11 efrain 83
    public function test_question_remove_stale_questions_from_category(): void {
1 efrain 84
        global $DB;
85
 
1441 ariadna 86
        $this->setAdminUser();
87
        $this->resetAfterTest();
88
 
89
        // Quiz and its context.
90
        $quiz = $this->create_quiz();
91
 
92
        // Create category 1 and one question.
93
        $qcat1 = $this->create_question_category_for_a_quiz($quiz);
94
        $q1a = $this->create_question_in_a_category('shortanswer', $qcat1->id);
1 efrain 95
        $DB->set_field('question_versions', 'status', 'hidden', ['questionid' => $q1a->id]);
96
 
1441 ariadna 97
        // Create category 2 and two questions.
98
        $qcat2 = $this->create_question_category_for_a_quiz($quiz);
99
        $q2a = $this->create_question_in_a_category('shortanswer', $qcat2->id);
100
        $q2b = $this->create_question_in_a_category('shortanswer', $qcat2->id);
1 efrain 101
        $DB->set_field('question_versions', 'status', 'hidden', ['questionid' => $q2a->id]);
102
        $DB->set_field('question_versions', 'status', 'hidden', ['questionid' => $q2b->id]);
103
 
1441 ariadna 104
        // Add question to the quiz.
105
        quiz_add_quiz_question($q2b->id, $quiz);
106
 
1 efrain 107
        // Adding a new random question does not add a new question, adds a question_set_references record.
1441 ariadna 108
        $this->add_random_questions($quiz->id, 0, $qcat2->id, 1);
1 efrain 109
 
110
        // We added one random question to the quiz and we expect the quiz to have only one random question.
1441 ariadna 111
        $q2d = $DB->get_record_sql(
112
            "SELECT qsr.*
113
               FROM {quiz_slots} qs
114
               JOIN {question_set_references} qsr ON qsr.itemid = qs.id
115
              WHERE qs.quizid = ?
116
                AND qsr.component = ?
117
                AND qsr.questionarea = ?",
118
            [$quiz->id, 'mod_quiz', 'slot'],
119
            MUST_EXIST
120
        );
1 efrain 121
 
122
        // The following 2 lines have to be after the quiz_add_random_questions() call above.
123
        // Otherwise, quiz_add_random_questions() will to be "smart" and use them instead of creating a new "random" question.
1441 ariadna 124
        $q1b = $this->create_question_in_a_category('random', $qcat1->id);
125
        $q2c = $this->create_question_in_a_category('random', $qcat2->id);
1 efrain 126
 
1441 ariadna 127
        $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid));
128
        $manager = new category_manager();
129
        $this->assertEquals(2, count($manager->get_real_question_ids_in_category($qcat1->id, $contexts)));
130
        $this->assertEquals(3, count($manager->get_real_question_ids_in_category($qcat2->id, $contexts)));
1 efrain 131
 
132
        // Non-existing category, nothing will happen.
133
        helper::question_remove_stale_questions_from_category(0);
1441 ariadna 134
        $this->assertEquals(2, count($manager->get_real_question_ids_in_category($qcat1->id, $contexts)));
135
        $this->assertEquals(3, count($manager->get_real_question_ids_in_category($qcat2->id, $contexts)));
1 efrain 136
 
137
        // First category, should be empty afterwards.
138
        helper::question_remove_stale_questions_from_category($qcat1->id);
1441 ariadna 139
        $this->assertEquals(0, count($manager->get_real_question_ids_in_category($qcat1->id, $contexts)));
140
        $this->assertEquals(3, count($manager->get_real_question_ids_in_category($qcat2->id, $contexts)));
1 efrain 141
        $this->assertFalse($DB->record_exists('question', ['id' => $q1a->id]));
142
        $this->assertFalse($DB->record_exists('question', ['id' => $q1b->id]));
143
 
144
        // Second category, used questions should be left untouched.
145
        helper::question_remove_stale_questions_from_category($qcat2->id);
1441 ariadna 146
        $this->assertEquals(0, count($manager->get_real_question_ids_in_category($qcat1->id, $contexts)));
147
        $this->assertEquals(1, count($manager->get_real_question_ids_in_category($qcat2->id, $contexts)));
1 efrain 148
        $this->assertFalse($DB->record_exists('question', ['id' => $q2a->id]));
149
        $this->assertTrue($DB->record_exists('question', ['id' => $q2b->id]));
150
        $this->assertFalse($DB->record_exists('question', ['id' => $q2c->id]));
1441 ariadna 151
        $this->assertTrue($DB->record_exists(
152
            'question_set_references',
153
            ['id' => $q2d->id, 'component' => 'mod_quiz', 'questionarea' => 'slot'],
154
        ));
1 efrain 155
    }
156
 
1441 ariadna 157
 
1 efrain 158
    /**
159
     * Test delete top category in function question_can_delete_cat.
160
     *
161
     * @covers ::question_can_delete_cat
162
     * @covers ::question_is_top_category
163
     */
11 efrain 164
    public function test_question_can_delete_cat_top_category(): void {
1 efrain 165
 
166
        $qcategory1 = $this->qgenerator->create_question_category(['contextid' => $this->context->id]);
167
 
168
        // Try to delete a top category.
1441 ariadna 169
        $categorytop = question_get_top_category($qcategory1->contextid, true)->id;
170
        try {
171
            helper::question_can_delete_cat($categorytop);
172
        } catch (moodle_exception $e) {
173
            $this->assertEquals(get_string('cannotdeletetopcat', 'question'), $e->getMessage());
174
        }
175
        $this->assertDebuggingCalled(
176
            'Deprecation: qbank_managecategories\helper::question_can_delete_cat has been deprecated since 4.5. ' .
177
                'Moved to core namespace. ' .
178
                'Use core_question\category_manager::can_delete_category instead. ' .
179
                'See MDL-72397 for more information.',
180
        );
1 efrain 181
    }
182
 
183
    /**
184
     * Test delete only child category in function question_can_delete_cat.
185
     *
186
     * @covers ::question_can_delete_cat
187
     * @covers ::question_is_only_child_of_top_category_in_context
188
     */
11 efrain 189
    public function test_question_can_delete_cat_child_category(): void {
1 efrain 190
 
191
        $qcategory1 = $this->qgenerator->create_question_category(['contextid' => $this->context->id]);
192
 
193
        // Try to delete an only child of top category having also at least one child.
1441 ariadna 194
        try {
195
            helper::question_can_delete_cat($qcategory1->id);
196
        } catch (moodle_exception $e) {
197
            $this->assertEquals(get_string('cannotdeletecate', 'question'), $e->getMessage());
198
        }
199
        $this->assertDebuggingCalled(
200
            'Deprecation: qbank_managecategories\helper::question_can_delete_cat has been deprecated since 4.5. ' .
201
                'Moved to core namespace. ' .
202
                'Use core_question\category_manager::can_delete_category instead. ' .
203
                'See MDL-72397 for more information.',
204
        );
1 efrain 205
    }
206
 
207
    /**
208
     * Test delete category in function question_can_delete_cat without capabilities.
209
     *
210
     * @covers ::question_can_delete_cat
211
     */
11 efrain 212
    public function test_question_can_delete_cat_capability(): void {
1 efrain 213
 
214
        $qcategory1 = $this->qgenerator->create_question_category(['contextid' => $this->context->id]);
215
        $qcategory2 = $this->qgenerator->create_question_category(['contextid' => $this->context->id, 'parent' => $qcategory1->id]);
216
 
217
        // This call should not throw an exception as admin user has the capabilities moodle/question:managecategory.
218
        helper::question_can_delete_cat($qcategory2->id);
219
 
220
        // Try to delete a category with and user without the capability.
221
        $user = $this->getDataGenerator()->create_user();
222
        $this->setUser($user);
223
 
1441 ariadna 224
        try {
225
            helper::question_can_delete_cat($qcategory2->id);
226
        } catch (\required_capability_exception $e) {
227
            $this->assertEquals(
228
                get_string('nopermissions', 'error', get_string('question:managecategory', 'role')),
229
                $e->getMessage(),
230
            );
231
        }
232
        $message = 'Deprecation: qbank_managecategories\helper::question_can_delete_cat has been deprecated since 4.5. ' .
233
            'Moved to core namespace. ' .
234
            'Use core_question\category_manager::can_delete_category instead. ' .
235
            'See MDL-72397 for more information.';
236
        $this->assertdebuggingcalledcount(2, [$message, $message]);
1 efrain 237
    }
238
 
239
    /**
240
     * Test question_category_select_menu function.
241
     *
242
     * @covers ::question_category_select_menu
243
     * @covers ::question_category_options
244
     */
11 efrain 245
    public function test_question_category_select_menu(): void {
1441 ariadna 246
        $this->setAdminUser();
247
        $this->resetAfterTest();
1 efrain 248
 
1441 ariadna 249
        // Create category.
250
        $quiz = $this->create_quiz();
251
        $this->create_question_category_for_a_quiz($quiz, ['name' => 'Test this question category']);
252
        $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid));
1 efrain 253
 
254
        ob_start();
255
        helper::question_category_select_menu($contexts->having_cap('moodle/question:add'));
256
        $output = ob_get_clean();
257
 
258
        // Test the select menu of question categories output.
259
        $this->assertStringContainsString('Question category', $output);
260
        $this->assertStringContainsString('Test this question category', $output);
261
    }
262
 
263
    /**
264
     * Test that question_category_options function returns the correct category tree.
265
     *
266
     * @covers ::question_category_options
267
     * @covers ::get_categories_for_contexts
268
     * @covers ::question_fix_top_names
269
     * @covers ::question_add_context_in_key
270
     * @covers ::add_indented_names
271
     */
11 efrain 272
    public function test_question_category_options(): void {
1441 ariadna 273
        $this->setAdminUser();
274
        $this->resetAfterTest();
1 efrain 275
 
1441 ariadna 276
        // Create categories.
277
        $quiz = $this->create_quiz();
278
        $context = \context_module::instance($quiz->cmid);
279
        $qcategory1 = question_get_default_category($context->id);
280
        $this->create_question_category_for_a_quiz($quiz, ['parent' => $qcategory1->id]);
281
        $this->create_question_category_for_a_quiz($quiz);
1 efrain 282
 
1441 ariadna 283
        $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid));
1 efrain 284
 
285
        // Validate that we have the array with the categories tree.
286
        $categorycontexts = helper::question_category_options($contexts->having_cap('moodle/question:add'));
287
        // The quiz name 'Quiz 1' is set in setUp function.
1441 ariadna 288
        $categorycontext = $categorycontexts['Quiz: ' . $quiz->name];
1 efrain 289
        $this->assertCount(3, $categorycontext);
290
 
291
        // Validate that we have the array with the categories tree and that top category is there.
292
        $newcategorycontexts = helper::question_category_options($contexts->having_cap('moodle/question:add'), true);
293
        foreach ($newcategorycontexts as $key => $categorycontext) {
294
            $oldcategorycontext = $categorycontexts[$key];
295
            $count = count($oldcategorycontext);
296
            $this->assertCount($count + 1, $categorycontext);
297
        }
298
    }
1441 ariadna 299
 
300
    /**
301
     * Test that question_category_options function does not include the current category.
302
     *
303
     * @covers ::question_category_options
304
     */
305
    public function test_question_category_options_exclude_current(): void {
306
        $this->setAdminUser();
307
        $this->resetAfterTest();
308
 
309
        // Create categories.
310
        $quiz = $this->create_quiz();
311
        $context = \context_module::instance($quiz->cmid);
312
        $qcategory1 = question_get_default_category($context->id);
313
        $qcategory2 = $this->create_question_category_for_a_quiz($quiz, ['parent' => $qcategory1->id]);
314
        $qcategory3 = $this->create_question_category_for_a_quiz($quiz);
315
 
316
        $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid));
317
 
318
        $categorycontexts = helper::question_category_options($contexts->having_cap('moodle/question:add'));
319
        // We get all categories without the currentcat parameter.
320
        $categorycontext = $categorycontexts['Quiz: ' . $quiz->name];
321
        $this->assertCount(3, $categorycontext);
322
 
323
        // The currentcat category is excluded.
324
        $newcategorycontexts = helper::question_category_options(
325
            $contexts->having_cap('moodle/question:add'),
326
            currentcat: $qcategory2->id,
327
        );
328
        $newcategorycontext = $newcategorycontexts['Quiz: ' . $quiz->name];
329
        $this->assertCount(2, $newcategorycontext);
330
        $this->assertContains($qcategory1->name, $newcategorycontext);
331
        $this->assertNotContains($qcategory2->name, $newcategorycontext);
332
        $this->assertContains($qcategory3->name, $newcategorycontext);
333
    }
334
 
335
    /**
336
     * Test that get_categories_for_contexts function returns the correct question count number.
337
     *
338
     * @covers ::get_categories_for_contexts
339
     */
340
    public function test_question_category_question_count(): void {
341
        global $DB;
342
        // Create quiz.
343
        $quiz = $this->quiz;
344
        $context = \context_module::instance($quiz->cmid);
345
        // Get the question category and create one hidden question.
346
        $qcat = question_get_default_category($context->id);
347
        $q1 = $this->create_question_in_a_category('shortanswer', $qcat->id);
348
        $DB->set_field('question_versions', 'status', 'hidden', ['questionid' => $q1->id]);
349
 
350
        $contexts = new \core_question\local\bank\question_edit_contexts($context);
351
        $contexts = $contexts->having_cap('moodle/question:add');
352
        foreach ($contexts as $context) {
353
            $contextslist[] = $context->id;
354
        }
355
        $contextslist = join(', ', $contextslist);
356
        // Verify we have 0 question in category since it is hidden.
357
        $categorycontexts = helper::get_categories_for_contexts($contextslist);
358
        $this->assertEquals(0, reset($categorycontexts)->questioncount);
359
        // Add an extra question.
360
        $this->create_question_in_a_category('shortanswer', $qcat->id);
361
        $categorycontexts = helper::get_categories_for_contexts($contextslist);
362
        // Verify we have 1 question in category.
363
        $this->assertEquals(1, reset($categorycontexts)->questioncount);
364
    }
1 efrain 365
}