MDL-67790 core_contentbank: Unit and behat tests for renaming content
[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.
184 * @param int $contextid Contextid to search.
185 * @param int $expectedresult Expected result.
186 * @param array $contexts List of contexts where to create content.
187 */
188 public function test_search_contents(?string $search, int $contextid, int $expectedresult, array $contexts = []): void {
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
198 // Add some content to the content bank.
199 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
200 foreach ($contexts as $context) {
201 $records = $generator->generate_contentbank_data('contenttype_h5p', 3,
202 $manager->id, $context, false);
203 }
204
205 // Search for some content.
206 $cb = new \core_contentbank\contentbank();
207 $contents = $cb->search_contents($search, $contextid);
208
209 $this->assertCount($expectedresult, $contents);
210 if (!empty($contents) && !empty($search)) {
211 foreach ($contents as $content) {
212 $this->assertContains($search, $content->get_name());
213 }
214 }
215 }
216
217 /**
218 * Data provider for test_search_contents().
219 *
220 * @return array
221 */
222 public function search_contents_provider(): array {
223 // Create a category and a course.
224 $systemcontext = \context_system::instance();
225 $coursecat = $this->getDataGenerator()->create_category();
226 $course = $this->getDataGenerator()->create_course();
227 $coursecatcontext = \context_coursecat::instance($coursecat->id);
228 $coursecontext = \context_course::instance($course->id);
229
230 return [
231 'Search all content in all contexts' => [
232 null,
233 0,
234 9,
235 [$systemcontext, $coursecatcontext, $coursecontext]
236 ],
237 'Search in all contexts for existing string in all contents' => [
238 'content',
239 0,
240 9,
241 [$systemcontext, $coursecatcontext, $coursecontext]
242 ],
243 'Search in all contexts for unexisting string in all contents' => [
244 'chocolate',
245 0,
246 0,
247 [$systemcontext, $coursecatcontext, $coursecontext]
248 ],
249 'Search in all contexts for existing string in some contents' => [
250 '1',
251 0,
252 3,
253 [$systemcontext, $coursecatcontext, $coursecontext]
254 ],
255 'Search in all contexts for existing string in some contents (create only 1 context)' => [
256 '1',
257 0,
258 1,
259 [$systemcontext]
260 ],
261 'Search in system context for existing string in all contents' => [
262 'content',
263 $systemcontext->id,
264 3,
265 [$systemcontext, $coursecatcontext, $coursecontext]
266 ],
267 'Search in category context for unexisting string in all contents' => [
268 'chocolate',
269 $coursecatcontext->id,
270 0,
271 [$systemcontext, $coursecatcontext, $coursecontext]
272 ],
273 'Search in course context for existing string in some contents' => [
274 '1',
275 $coursecontext->id,
276 1,
277 [$systemcontext, $coursecatcontext, $coursecontext]
278 ],
279 'Search in system context' => [
280 null,
281 $systemcontext->id,
282 3,
283 [$systemcontext, $coursecatcontext, $coursecontext]
284 ],
285 'Search in course context with existing content' => [
286 null,
287 $coursecontext->id,
288 3,
289 [$systemcontext, $coursecatcontext, $coursecontext]
290 ],
291 'Search in course context without existing content' => [
292 null,
293 $coursecontext->id,
294 0,
295 [$systemcontext, $coursecatcontext]
296 ],
297 'Search in an empty contentbank' => [
298 null,
299 0,
300 0,
301 []
302 ],
303 'Search in a context in an empty contentbank' => [
304 null,
305 $systemcontext->id,
306 0,
307 []
308 ],
309 'Search for a string in an empty contentbank' => [
310 'content',
311 0,
312 0,
313 []
314 ],
315 ];
316 }
bd4e0a76 317}