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\external;
18
 
19
use core\context\module;
20
 
21
/**
22
 * Unit tests for core_question\external\search_shared_banks
23
 *
24
 * @package   core_question
25
 * @copyright 2025 onwards Catalyst IT EU {@link https://catalyst-eu.net}
26
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core_question\external\search_shared_banks
29
 */
30
final class search_shared_banks_test extends \advanced_testcase {
31
 
32
    /**
33
     * Create a set of question banks across 3 courses.
34
     *
35
     * One bank belongs to a quiz, which is not searchable as it is not shared.
36
     * One bank does not include the word "test" in its name, so will not match that search.
37
     * One bank is on a course where the user has different permissions.
38
     * One bank is on the same course as the quiz, so will not be returned unless banks on the same course are included.
39
     *
40
     * @return array
41
     */
42
    protected function create_banks(): array {
43
        $generator = $this->getDataGenerator();
44
        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
45
        $qbankgenerator = $this->getDataGenerator()->get_plugin_generator('mod_qbank');
46
 
47
        $course1 = $generator->create_course();
48
        $course2 = $generator->create_course();
49
        $course3 = $generator->create_course();
50
        $teacher = $generator->create_user();
51
        $generator->enrol_user($teacher->id, $course1->id, 'teacher');
52
        $generator->enrol_user($teacher->id, $course2->id, 'teacher');
53
        $generator->enrol_user($teacher->id, $course3->id, 'editingteacher');
54
 
55
        $quiz = $quizgenerator->create_instance(['name' => 'Test quiz', 'course' => $course1->id]);
56
        $qbank1 = $qbankgenerator->create_instance(['name' => 'Test qbank 1', 'course' => $course2->id]);
57
        $qbank2 = $qbankgenerator->create_instance(['name' => 'Test qbank 2', 'course' => $course2->id]);
58
        $qbank3 = $qbankgenerator->create_instance(['name' => 'Different name', 'course' => $course2->id]);
59
        $qbank4 = $qbankgenerator->create_instance(['name' => 'Test qbank with different permissions', 'course' => $course3->id]);
60
 
61
        return [
62
            $course1,
63
            $course2,
64
            $course3,
65
            $teacher,
66
            module::instance($quiz->cmid),
67
            $qbank1,
68
            $qbank2,
69
            $qbank3,
70
            $qbank4,
71
        ];
72
    }
73
 
74
    /**
75
     * Call the function with no search string. All banks the user has permission to should be returned.
76
     */
77
    public function test_empty_search(): void {
78
        $this->resetAfterTest();
79
        [
80
            ,
81
            $course2,
82
            $course3,
83
            $teacher,
84
            $quizcontext,
85
            $qbank1,
86
            $qbank2,
87
            $qbank3,
88
            $qbank4,
89
        ] = $this->create_banks();
90
 
91
        $this->setUser($teacher);
92
        $result = search_shared_banks::execute($quizcontext->id);
93
 
94
        $this->assertEquals(
95
            [
96
                [
97
                    'label' => "{$course2->shortname} - {$qbank1->name}",
98
                    'value' => $qbank1->cmid,
99
                ],
100
                [
101
                    'label' => "{$course2->shortname} - {$qbank2->name}",
102
                    'value' => $qbank2->cmid,
103
                ],
104
                [
105
                    'label' => "{$course2->shortname} - {$qbank3->name}",
106
                    'value' => $qbank3->cmid,
107
                ],
108
                [
109
                    'label' => "{$course3->shortname} - {$qbank4->name}",
110
                    'value' => $qbank4->cmid,
111
                ],
112
            ],
113
            $result['sharedbanks'],
114
        );
115
    }
116
 
117
    /**
118
     * Call the function with a search string matching a subset of available banks. Only those matching should be returned.
119
     */
120
    public function test_search(): void {
121
        $this->resetAfterTest();
122
        [
123
            ,
124
            $course2,
125
            $course3,
126
            $teacher,
127
            $quizcontext,
128
            $qbank1,
129
            $qbank2,
130
            ,
131
            $qbank4,
132
        ] = $this->create_banks();
133
 
134
        $this->setUser($teacher);
135
        $result = search_shared_banks::execute($quizcontext->id, 'Test');
136
 
137
        $this->assertEquals(
138
            [
139
                [
140
                    'label' => "{$course2->shortname} - {$qbank1->name}",
141
                    'value' => $qbank1->cmid,
142
                ],
143
                [
144
                    'label' => "{$course2->shortname} - {$qbank2->name}",
145
                    'value' => $qbank2->cmid,
146
                ],
147
                [
148
                    'label' => "{$course3->shortname} - {$qbank4->name}",
149
                    'value' => $qbank4->cmid,
150
                ],
151
            ],
152
            $result['sharedbanks'],
153
        );
154
    }
155
 
156
    /**
157
     * Call the function with a different capability. Only results where the user has that capability should be returned.
158
     */
159
    public function test_search_different_capability(): void {
160
        $this->resetAfterTest();
161
        [
162
            ,
163
            ,
164
            $course3,
165
            $teacher,
166
            $quizcontext,
167
            ,
168
            ,
169
            ,
170
            $qbank4,
171
        ] = $this->create_banks();
172
 
173
        $this->setUser($teacher);
174
        $result = search_shared_banks::execute($quizcontext->id, 'Test', ['edit']);
175
 
176
        $this->assertEquals(
177
            [
178
                [
179
                    'label' => "{$course3->shortname} - {$qbank4->name}",
180
                    'value' => $qbank4->cmid,
181
                ],
182
            ],
183
            $result['sharedbanks'],
184
        );
185
    }
186
 
187
    /**
188
     * If there are more than the max number of results, a placeholder is returned at the end.
189
     */
190
    public function test_search_max_results(): void {
191
        $this->resetAfterTest();
192
        [
193
            ,
194
            $course2,
195
            ,
196
            $teacher,
197
            $quizcontext,
198
        ] = $this->create_banks();
199
 
200
        $qbankgenerator = $this->getDataGenerator()->get_plugin_generator('mod_qbank');
201
        for ($i = 1; $i <= search_shared_banks::MAX_RESULTS + 2; $i++) {
202
            $qbankgenerator->create_instance(['name' => "Extra qbank {$i}", 'course' => $course2->id]);
203
        }
204
 
205
        $this->setUser($teacher);
206
        $result = search_shared_banks::execute($quizcontext->id, 'Extra');
207
        $this->assertCount(search_shared_banks::MAX_RESULTS + 1, $result['sharedbanks']);
208
        $lastresult = end($result['sharedbanks']);
209
        $this->assertEquals(
210
            [
211
                'label' => get_string('otherquestionbankstoomany', 'question', search_shared_banks::MAX_RESULTS),
212
                'value' => 0,
213
            ],
214
            $lastresult
215
        );
216
    }
217
}