MDL-68483 contentbank: improve search API
[moodle.git] / contentbank / tests / contentbank_test.php
CommitLineData
bd4e0a76
AA
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/**
18 * Test for extensions manager.
19 *
20 * @package core_contentbank
21 * @category test
22 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
30
31/**
32 * Test for extensions manager.
33 *
34 * @package core_contentbank
35 * @category test
36 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @coversDefaultClass \core_contentbank\contentbank
39 */
40class core_contentbank_testcase extends advanced_testcase {
41 /**
42 * Data provider for test_get_extension_supporter.
43 *
44 * @return array
45 */
46 public function get_extension_provider() {
47 return [
48 'H5P file' => ['something.h5p', '.h5p'],
49 'PDF file' => ['something.pdf', '.pdf']
50 ];
51 }
52
53 /**
54 * Tests for get_extension() function.
55 *
56 * @dataProvider get_extension_provider
57 * @param string $filename The filename given
58 * @param string $expected The extension of the file
59 *
60 * @covers ::get_extension
61 */
62 public function test_get_extension(string $filename, string $expected) {
63 $this->resetAfterTest();
64
65 $cb = new \core_contentbank\contentbank();
66
67 $extension = $cb->get_extension($filename);
68 $this->assertEquals($expected, $extension);
69 }
70
71 /**
72 * Data provider for test_load_context_supported_extensions.
73 *
74 * @return array
75 */
76 public function get_extension_supporters_provider() {
77 return [
78 'H5P first' => [['.h5p' => ['h5p', 'testable']], '.h5p', 'h5p'],
79 'Testable first (but upload not implemented)' => [['.h5p' => ['testable', 'h5p']], '.h5p', 'h5p'],
80 ];
81 }
82
83 /**
84 * Tests for get_extension_supporter() function with admin permissions.
85 *
86 * @dataProvider get_extension_supporters_provider
87 * @param array $supporters The content type plugin supporters for each extension
88 * @param string $extension The extension of the file given
89 * @param string $expected The supporter contenttype of the file
90 *
91 * @covers ::load_context_supported_extensions
92 */
93 public function test_get_extension_supporter_for_admins(array $supporters, string $extension, string $expected) {
94 $this->resetAfterTest();
95
96 $cb = new \core_contentbank\contentbank();
97 $expectedsupporters = [$extension => $expected];
98
99 $systemcontext = context_system::instance();
100
101 // All contexts allowed for admins.
102 $this->setAdminUser();
103 $contextsupporters = $cb->load_context_supported_extensions($systemcontext);
104 $this->assertEquals($expectedsupporters, $contextsupporters);
105 }
106
107 /**
108 * Tests for get_extension_supporter() function with user default permissions.
109 *
110 * @dataProvider get_extension_supporters_provider
111 * @param array $supporters The content type plugin supporters for each extension
112 * @param string $extension The extension of the file given
113 * @param string $expected The supporter contenttype of the file
114 *
115 * @covers ::load_context_supported_extensions
116 */
117 public function test_get_extension_supporter_for_users(array $supporters, string $extension, string $expected) {
118 $this->resetAfterTest();
119
120 $cb = new \core_contentbank\contentbank();
121 $systemcontext = context_system::instance();
122
123 // Set a user with no permissions.
124 $user = $this->getDataGenerator()->create_user();
125 $this->setUser($user);
126
127 // Users with no capabilities can't upload content.
128 $contextsupporters = $cb->load_context_supported_extensions($systemcontext);
129 $this->assertEquals([], $contextsupporters);
130 }
131
132 /**
133 * Tests for get_extension_supporter() function with teacher defaul permissions.
134 *
135 * @dataProvider get_extension_supporters_provider
136 * @param array $supporters The content type plugin supporters for each extension
137 * @param string $extension The extension of the file given
138 * @param string $expected The supporter contenttype of the file
139 *
140 * @covers ::load_context_supported_extensions
141 */
142 public function test_get_extension_supporter_for_teachers(array $supporters, string $extension, string $expected) {
143 $this->resetAfterTest();
144
145 $cb = new \core_contentbank\contentbank();
146 $expectedsupporters = [$extension => $expected];
147
148 $course = $this->getDataGenerator()->create_course();
149 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
150 $this->setUser($teacher);
151 $coursecontext = context_course::instance($course->id);
152
153 // Teachers has permission in their context to upload supported by H5P content type.
154 $contextsupporters = $cb->load_context_supported_extensions($coursecontext);
155 $this->assertEquals($expectedsupporters, $contextsupporters);
156 }
157
158 /**
159 * Tests for get_extension_supporter() function.
160 *
161 * @dataProvider get_extension_supporters_provider
162 * @param array $supporters The content type plugin supporters for each extension
163 * @param string $extension The extension of the file given
164 * @param string $expected The supporter contenttype of the file
165 *
166 * @covers ::get_extension_supporter
167 */
168 public function test_get_extension_supporter(array $supporters, string $extension, string $expected) {
169 $this->resetAfterTest();
170
171 $cb = new \core_contentbank\contentbank();
172 $systemcontext = context_system::instance();
173 $this->setAdminUser();
174
175 $supporter = $cb->get_extension_supporter($extension, $systemcontext);
176 $this->assertEquals($expected, $supporter);
177 }
fb445c76
SA
178
179 /**
180 * Test the behaviour of search_contents().
181 *
182 * @dataProvider search_contents_provider
183 * @param string $search String to search.
d90ff839 184 * @param string $where Context where to search.
fb445c76
SA
185 * @param int $expectedresult Expected result.
186 * @param array $contexts List of contexts where to create content.
187 */
54ce66bd
SA
188 public function test_search_contents(?string $search, string $where, int $expectedresult, array $contexts = [],
189 array $contenttypes = null): void {
fb445c76
SA
190 global $DB;
191
192 $this->resetAfterTest();
193
194 // Create users.
195 $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
196 $manager = $this->getDataGenerator()->create_user();
197 $this->getDataGenerator()->role_assign($managerroleid, $manager->id);
198
d90ff839
AA
199 // Create a category and a course.
200 $coursecat = $this->getDataGenerator()->create_category();
201 $course = $this->getDataGenerator()->create_course();
202 $existingcontexts = [];
203 $existingcontexts['system'] = \context_system::instance();
204 $existingcontexts['category'] = \context_coursecat::instance($coursecat->id);
205 $existingcontexts['course'] = \context_course::instance($course->id);
206
207 if (empty($where)) {
208 $contextid = 0;
209 } else {
210 $contextid = $existingcontexts[$where]->id;
211 }
212
fb445c76
SA
213 // Add some content to the content bank.
214 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
215 foreach ($contexts as $context) {
d90ff839 216 $contextinstance = $existingcontexts[$context];
fb445c76 217 $records = $generator->generate_contentbank_data('contenttype_h5p', 3,
d90ff839 218 $manager->id, $contextinstance, false);
fb445c76
SA
219 }
220
221 // Search for some content.
222 $cb = new \core_contentbank\contentbank();
54ce66bd 223 $contents = $cb->search_contents($search, $contextid, $contenttypes);
fb445c76
SA
224
225 $this->assertCount($expectedresult, $contents);
226 if (!empty($contents) && !empty($search)) {
227 foreach ($contents as $content) {
228 $this->assertContains($search, $content->get_name());
229 }
230 }
231 }
232
233 /**
234 * Data provider for test_search_contents().
235 *
236 * @return array
237 */
238 public function search_contents_provider(): array {
fb445c76
SA
239
240 return [
241 'Search all content in all contexts' => [
242 null,
d90ff839 243 '',
fb445c76 244 9,
d90ff839 245 ['system', 'category', 'course']
fb445c76
SA
246 ],
247 'Search in all contexts for existing string in all contents' => [
248 'content',
d90ff839 249 '',
fb445c76 250 9,
d90ff839 251 ['system', 'category', 'course']
fb445c76
SA
252 ],
253 'Search in all contexts for unexisting string in all contents' => [
254 'chocolate',
d90ff839 255 '',
fb445c76 256 0,
d90ff839 257 ['system', 'category', 'course']
fb445c76
SA
258 ],
259 'Search in all contexts for existing string in some contents' => [
260 '1',
d90ff839 261 '',
fb445c76 262 3,
d90ff839 263 ['system', 'category', 'course']
fb445c76
SA
264 ],
265 'Search in all contexts for existing string in some contents (create only 1 context)' => [
266 '1',
d90ff839 267 '',
fb445c76 268 1,
d90ff839 269 ['system']
fb445c76
SA
270 ],
271 'Search in system context for existing string in all contents' => [
272 'content',
d90ff839 273 'system',
fb445c76 274 3,
d90ff839 275 ['system', 'category', 'course']
fb445c76
SA
276 ],
277 'Search in category context for unexisting string in all contents' => [
278 'chocolate',
d90ff839 279 'category',
fb445c76 280 0,
d90ff839 281 ['system', 'category', 'course']
fb445c76
SA
282 ],
283 'Search in course context for existing string in some contents' => [
284 '1',
d90ff839 285 'course',
fb445c76 286 1,
d90ff839 287 ['system', 'category', 'course']
fb445c76
SA
288 ],
289 'Search in system context' => [
290 null,
d90ff839 291 'system',
fb445c76 292 3,
d90ff839 293 ['system', 'category', 'course']
fb445c76
SA
294 ],
295 'Search in course context with existing content' => [
296 null,
d90ff839 297 'course',
fb445c76 298 3,
d90ff839 299 ['system', 'category', 'course']
fb445c76
SA
300 ],
301 'Search in course context without existing content' => [
302 null,
d90ff839 303 'course',
fb445c76 304 0,
d90ff839 305 ['system', 'category']
fb445c76
SA
306 ],
307 'Search in an empty contentbank' => [
308 null,
d90ff839 309 '',
fb445c76
SA
310 0,
311 []
312 ],
313 'Search in a context in an empty contentbank' => [
314 null,
d90ff839 315 'system',
fb445c76
SA
316 0,
317 []
318 ],
319 'Search for a string in an empty contentbank' => [
320 'content',
d90ff839 321 '',
fb445c76
SA
322 0,
323 []
324 ],
54ce66bd
SA
325 'Search with unexisting content-type' => [
326 null,
327 'course',
328 0,
329 ['system', 'category', 'course'],
330 ['contenttype_unexisting'],
331 ],
fb445c76
SA
332 ];
333 }
48bcb1f2
AA
334
335 /**
336 * Test create_content_from_file function.
337 *
338 * @covers ::create_content_from_file
339 */
340 public function test_create_content_from_file() {
341 global $USER;
342
343 $this->resetAfterTest();
344 $this->setAdminUser();
345 $systemcontext = \context_system::instance();
346 $name = 'dummy_h5p.h5p';
347
348 // Create a dummy H5P file.
349 $dummyh5p = array(
350 'contextid' => $systemcontext->id,
351 'component' => 'contentbank',
352 'filearea' => 'public',
353 'itemid' => 1,
354 'filepath' => '/',
355 'filename' => $name,
356 'userid' => $USER->id
357 );
358 $fs = get_file_storage();
359 $dummyh5pfile = $fs->create_file_from_string($dummyh5p, 'Dummy H5Pcontent');
360
361 $cb = new \core_contentbank\contentbank();
362 $content = $cb->create_content_from_file($systemcontext, $USER->id, $dummyh5pfile);
363
364 $this->assertEquals('contenttype_h5p', $content->get_content_type());
365 $this->assertInstanceOf('\\contenttype_h5p\\content', $content);
366 $this->assertEquals($name, $content->get_name());
367 }
bd4e0a76 368}