Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/question/bank/managecategories/tests/manage_category_test_base.php');
23
 
24
/**
25
 * Unit tests for question_categories
26
 *
27
 * @package   qbank_managecategories
28
 * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
29
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
30
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @covers \qbank_managecategories\question_categories
32
 */
33
final class question_categories_test extends manage_category_test_base {
34
    /**
35
     * Test creation of an ordered tree of categories in the constructor.
36
     */
37
    public function test_create_order_tree(): void {
38
        $this->setAdminUser();
39
        $this->resetAfterTest();
40
 
41
        // Create question categories for a course.
42
        $qbank = $this->create_qbank($this->create_course());
43
        $context = \context_module::instance($qbank->cmid);
44
        $qcat1 = question_get_default_category($context->id);
45
        $qcat2 = $this->create_question_category_for_a_qbank($qbank, ['parent' => $qcat1->id]);
46
        $qcat3 = $this->create_question_category_for_a_qbank($qbank);
47
        $qcat4 = $this->create_question_category_for_a_qbank($qbank, ['parent' => $qcat2->id]);
48
 
49
        // Create ordered tree.
50
        $questioncategories = new question_categories(
51
            new \moodle_url('/'),
52
            [$context],
53
        );
54
        $items = $questioncategories->editlists[$context->id]->items;
55
 
56
        // Two top categories (1 and 3) in the course.
57
        $this->assertCount(2, $items);
58
        $this->assertArrayHasKey($qcat1->id, $items);
59
        $this->assertArrayHasKey($qcat3->id, $items);
60
 
61
        // Category 2 is the only child of Category 1.
62
        $children = $items[$qcat1->id]->children;
63
        $this->assertCount(1, $children);
64
        $this->assertArrayHasKey($qcat2->id, $children);
65
 
66
        // Category 4 is the only child of Category 2.
67
        $children = $children[$qcat2->id]->children;
68
        $this->assertCount(1, $children);
69
        $this->assertArrayHasKey($qcat4->id, $children);
70
    }
71
}