MDL-68483 contentbank: order contents by name
[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 */
d90ff839 188 public function test_search_contents(?string $search, string $where, int $expectedresult, array $contexts = []): void {
fb445c76
SA
189 global $DB;
190
191 $this->resetAfterTest();
192
193 // Create users.
194 $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
195 $manager = $this->getDataGenerator()->create_user();
196 $this->getDataGenerator()->role_assign($managerroleid, $manager->id);
197
d90ff839
AA
198 // Create a category and a course.
199 $coursecat = $this->getDataGenerator()->create_category();
200 $course = $this->getDataGenerator()->create_course();
201 $existingcontexts = [];
202 $existingcontexts['system'] = \context_system::instance();
203 $existingcontexts['category'] = \context_coursecat::instance($coursecat->id);
204 $existingcontexts['course'] = \context_course::instance($course->id);
205
206 if (empty($where)) {
207 $contextid = 0;
208 } else {
209 $contextid = $existingcontexts[$where]->id;
210 }
211
fb445c76
SA
212 // Add some content to the content bank.
213 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
214 foreach ($contexts as $context) {
d90ff839 215 $contextinstance = $existingcontexts[$context];
fb445c76 216 $records = $generator->generate_contentbank_data('contenttype_h5p', 3,
d90ff839 217 $manager->id, $contextinstance, false);
fb445c76
SA
218 }
219
220 // Search for some content.
221 $cb = new \core_contentbank\contentbank();
222 $contents = $cb->search_contents($search, $contextid);
223
224 $this->assertCount($expectedresult, $contents);
225 if (!empty($contents) && !empty($search)) {
226 foreach ($contents as $content) {
227 $this->assertContains($search, $content->get_name());
228 }
229 }
230 }
231
232 /**
233 * Data provider for test_search_contents().
234 *
235 * @return array
236 */
237 public function search_contents_provider(): array {
fb445c76
SA
238
239 return [
240 'Search all content in all contexts' => [
241 null,
d90ff839 242 '',
fb445c76 243 9,
d90ff839 244 ['system', 'category', 'course']
fb445c76
SA
245 ],
246 'Search in all contexts for existing string in all contents' => [
247 'content',
d90ff839 248 '',
fb445c76 249 9,
d90ff839 250 ['system', 'category', 'course']
fb445c76
SA
251 ],
252 'Search in all contexts for unexisting string in all contents' => [
253 'chocolate',
d90ff839 254 '',
fb445c76 255 0,
d90ff839 256 ['system', 'category', 'course']
fb445c76
SA
257 ],
258 'Search in all contexts for existing string in some contents' => [
259 '1',
d90ff839 260 '',
fb445c76 261 3,
d90ff839 262 ['system', 'category', 'course']
fb445c76
SA
263 ],
264 'Search in all contexts for existing string in some contents (create only 1 context)' => [
265 '1',
d90ff839 266 '',
fb445c76 267 1,
d90ff839 268 ['system']
fb445c76
SA
269 ],
270 'Search in system context for existing string in all contents' => [
271 'content',
d90ff839 272 'system',
fb445c76 273 3,
d90ff839 274 ['system', 'category', 'course']
fb445c76
SA
275 ],
276 'Search in category context for unexisting string in all contents' => [
277 'chocolate',
d90ff839 278 'category',
fb445c76 279 0,
d90ff839 280 ['system', 'category', 'course']
fb445c76
SA
281 ],
282 'Search in course context for existing string in some contents' => [
283 '1',
d90ff839 284 'course',
fb445c76 285 1,
d90ff839 286 ['system', 'category', 'course']
fb445c76
SA
287 ],
288 'Search in system context' => [
289 null,
d90ff839 290 'system',
fb445c76 291 3,
d90ff839 292 ['system', 'category', 'course']
fb445c76
SA
293 ],
294 'Search in course context with existing content' => [
295 null,
d90ff839 296 'course',
fb445c76 297 3,
d90ff839 298 ['system', 'category', 'course']
fb445c76
SA
299 ],
300 'Search in course context without existing content' => [
301 null,
d90ff839 302 'course',
fb445c76 303 0,
d90ff839 304 ['system', 'category']
fb445c76
SA
305 ],
306 'Search in an empty contentbank' => [
307 null,
d90ff839 308 '',
fb445c76
SA
309 0,
310 []
311 ],
312 'Search in a context in an empty contentbank' => [
313 null,
d90ff839 314 'system',
fb445c76
SA
315 0,
316 []
317 ],
318 'Search for a string in an empty contentbank' => [
319 'content',
d90ff839 320 '',
fb445c76
SA
321 0,
322 []
323 ],
324 ];
325 }
48bcb1f2
AA
326
327 /**
328 * Test create_content_from_file function.
329 *
330 * @covers ::create_content_from_file
331 */
332 public function test_create_content_from_file() {
333 global $USER;
334
335 $this->resetAfterTest();
336 $this->setAdminUser();
337 $systemcontext = \context_system::instance();
338 $name = 'dummy_h5p.h5p';
339
340 // Create a dummy H5P file.
341 $dummyh5p = array(
342 'contextid' => $systemcontext->id,
343 'component' => 'contentbank',
344 'filearea' => 'public',
345 'itemid' => 1,
346 'filepath' => '/',
347 'filename' => $name,
348 'userid' => $USER->id
349 );
350 $fs = get_file_storage();
351 $dummyh5pfile = $fs->create_file_from_string($dummyh5p, 'Dummy H5Pcontent');
352
353 $cb = new \core_contentbank\contentbank();
354 $content = $cb->create_content_from_file($systemcontext, $USER->id, $dummyh5pfile);
355
356 $this->assertEquals('contenttype_h5p', $content->get_content_type());
357 $this->assertInstanceOf('\\contenttype_h5p\\content', $content);
358 $this->assertEquals($name, $content->get_name());
359 }
bd4e0a76 360}