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 core_question;
18
 
19
/**
20
 * Test the conversion of legacy random question sets into the newer format.
21
 *
22
 * @package    core_question
23
 * @copyright  2025 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @author     Conn Warwicker <conn.warwicker@catalyst-eu.net>
26
 * @covers     \core_question\question_reference_manager
27
 */
28
final class legacy_question_set_conversion_test extends \advanced_testcase {
29
 
30
    /**
31
     * Test the conversion of the old formatted `filtercondition` value to the newer format.
32
     * @covers \core_question\question_reference_manager::convert_legacy_set_reference_filter_condition
33
     * @return void
34
     */
35
    public function test_legacy_question_set_conversion(): void {
36
 
37
        $this->resetAfterTest(false);
38
 
39
        // Test conversion without a valid question category.
40
        $old = [
41
            'questioncategoryid' => 123,
42
            'includingsubcategories' => 0,
43
        ];
44
 
45
        $new = question_reference_manager::convert_legacy_set_reference_filter_condition($old);
46
        $expected = [
47
            'filter' => [
48
                'category' => [
49
                    'jointype' => 1,
50
                    'values' => [123],
51
                    'filteroptions' => [
52
                        'includesubcategories' => 0,
53
                    ],
54
                ],
55
            ],
56
            'cat' => '',
57
            'tabname' => 'questions',
58
            'qpage' => 0,
59
            'qperpage' => 100,
60
            'jointype' => 2,
61
        ];
62
        $this->assertEquals($new, $expected);
63
 
64
        // Test conversion with a valid question category.
65
        // Generate a question category.
66
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
67
        $cat = $questiongenerator->create_question_category();
68
 
69
        // Add the valid category into the arrays.
70
        $old['questioncategoryid'] = $cat->id;
71
        $expected['cat'] = "{$cat->id},{$cat->contextid}";
72
        $expected['filter']['category']['values'] = [$cat->id];
73
        $new = question_reference_manager::convert_legacy_set_reference_filter_condition($old);
74
        $this->assertEquals($new, $expected);
75
 
76
    }
77
 
78
    /**
79
     * Verifies that a legacy tag filter re-uses an existing tag that
80
     * lives in the default collection and does not create a duplicate.
81
     *
82
     * @covers \core_question\question_reference_manager::convert_legacy_set_reference_filter_condition
83
     * @return void
84
     */
85
    public function test_tag_conversion_uses_existing_tag_in_default_collection(): void {
86
        $this->resetAfterTest();
87
 
88
        // Prepare a tag that already exists in the default collection.
89
        $defaultcollectionid = \core_tag_collection::get_default();
90
 
91
        // Create a tag inside that collection.
92
        $tag = \core_tag_tag::create_if_missing($defaultcollectionid, ['legacytag'])['legacytag'];
93
 
94
        // Legacy random-question filter – tag specified as "id,rawname".
95
        $legacyfilter = ['tags' => ["{$tag->id},legacytag"]];
96
        $converted = question_reference_manager::convert_legacy_set_reference_filter_condition($legacyfilter);
97
 
98
        $this->assertEquals(
99
            [$tag->id],
100
            $converted['filter']['qtagids']['values'],
101
            'Converter should preserve the existing tag ID and avoid duplicates.'
102
        );
103
    }
104
 
105
    /**
106
     * Verifies that a legacy tag filter respects a custom collection after
107
     * the *question* tag-area has been moved there.
108
     *
109
     * @covers \core_question\question_reference_manager::convert_legacy_set_reference_filter_condition
110
     * @return void
111
     */
112
    public function test_tag_conversion_respects_custom_collection(): void {
113
        $this->resetAfterTest();
114
 
115
        // Create a custom collection and move the question tag-area to it.
116
        $customcollection = \core_tag_collection::create((object) [
117
            'name' => 'Questions',
118
            'component' => 'core_question',
119
            'searchable' => 0,
120
        ]);
121
        $questionarea = \core_tag_area::get_areas()['question']['core_question'];
122
        \core_tag_area::update($questionarea, ['tagcollid' => $customcollection->id]);
123
 
124
        // Create a tag inside that collection.
125
        $tag = \core_tag_tag::create_if_missing($customcollection->id, ['legacytag'])['legacytag'];
126
 
127
        $legacyfilter = ['tags' => ["{$tag->id},legacytag"]];
128
        $converted = question_reference_manager::convert_legacy_set_reference_filter_condition($legacyfilter);
129
 
130
        $this->assertEquals(
131
            [$tag->id],
132
            $converted['filter']['qtagids']['values'],
133
            'Converter should use the tag ID from the custom collection.'
134
        );
135
    }
136
}