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