MDL-68526 core_contentbank: Delete content when a course is deleted
[moodle.git] / contentbank / tests / contenttype_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 content bank contenttype class.
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
26namespace core_contentbank;
27
bd4e0a76
AA
28use stdClass;
29use context_system;
30use contenttype_testable\contenttype as contenttype;
31/**
32 * Test for content bank contenttype class.
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\contenttype
39 *
40 */
41class core_contenttype_contenttype_testcase extends \advanced_testcase {
42
c0d615e8
SA
43 /** @var int Identifier for the manager role. */
44 protected $managerroleid;
45
46 /** @var stdClass Manager user. */
47 protected $manager1;
48
49 /** @var stdClass Manager user. */
50 protected $manager2;
51
52 /** @var stdClass User. */
53 protected $user;
54
55 /** @var array List of contents created (every user has a key with contents created by her). */
56 protected $contents = [];
57
58 /** @var contenttype The contenttype instance. */
59 protected $contenttype;
60
f3126f84
SA
61 /**
62 * Setup to ensure that fixtures are loaded.
63 */
64 public static function setupBeforeClass(): void {
65 global $CFG;
66
67 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
68 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
69 }
70
bd4e0a76
AA
71 /**
72 * Tests get_contenttype_name result.
73 *
74 * @covers ::get_contenttype_name
75 */
76 public function test_get_contenttype_name() {
77 $this->resetAfterTest();
78
79 $systemcontext = \context_system::instance();
80 $testable = new contenttype($systemcontext);
81
82 $this->assertEquals('contenttype_testable', $testable->get_contenttype_name());
83 }
84
85 /**
86 * Tests get_plugin_name result.
87 *
88 * @covers ::get_plugin_name
89 */
90 public function test_get_plugin_name() {
91 $this->resetAfterTest();
92
93 $systemcontext = \context_system::instance();
94 $testable = new contenttype($systemcontext);
95
96 $this->assertEquals('testable', $testable->get_plugin_name());
97 }
98
99 /**
100 * Tests get_icon result.
101 *
102 * @covers ::get_icon
103 */
104 public function test_get_icon() {
105 $this->resetAfterTest();
106
107 $systemcontext = \context_system::instance();
108 $testable = new contenttype($systemcontext);
109 $icon = $testable->get_icon('new content');
110 $this->assertContains('archive', $icon);
111 }
112
113 /**
114 * Tests is_feature_supported behavior .
115 *
116 * @covers ::is_feature_supported
117 */
118 public function test_is_feature_supported() {
119 $this->resetAfterTest();
120
121 $systemcontext = \context_system::instance();
122 $testable = new contenttype($systemcontext);
123
124 $this->assertTrue($testable->is_feature_supported(contenttype::CAN_TEST));
125 $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
126 }
127
128 /**
129 * Tests can_upload behavior with no implemented upload feature.
130 *
131 * @covers ::can_upload
132 */
133 public function test_no_upload_feature_supported() {
134 $this->resetAfterTest();
135
136 $systemcontext = \context_system::instance();
137 $testable = new contenttype($systemcontext);
138
139 $this->setAdminUser();
140 $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
141 $this->assertFalse($testable->can_upload());
142 }
143
144 /**
145 * Test create_content() with empty data.
146 *
147 * @covers ::create_content
148 */
149 public function test_create_empty_content() {
150 $this->resetAfterTest();
151
152 // Create empty content.
153 $record = new stdClass();
154
155 $contenttype = new contenttype(context_system::instance());
156 $content = $contenttype->create_content($record);
157
158 $this->assertEquals('contenttype_testable', $content->get_content_type());
159 $this->assertInstanceOf('\\contenttype_testable\\content', $content);
160 }
161
162 /**
163 * Tests for behaviour of create_content() with data.
164 *
165 * @covers ::create_content
166 */
167 public function test_create_content() {
168 $this->resetAfterTest();
169
170 // Create content.
171 $record = new stdClass();
172 $record->name = 'Test content';
173 $record->configdata = '';
174 $record->contenttype = '';
175
176 $contenttype = new contenttype(context_system::instance());
177 $content = $contenttype->create_content($record);
178
179 $this->assertEquals('contenttype_testable', $content->get_content_type());
180 $this->assertInstanceOf('\\contenttype_testable\\content', $content);
181 }
c0d615e8 182
c0d615e8
SA
183 /**
184 * Test the behaviour of can_delete().
185 */
186 public function test_can_delete() {
187 global $DB;
188
189 $this->resetAfterTest();
190 $this->contenttype_setup_scenario_data();
191
192 $managercontent = array_shift($this->contents[$this->manager1->id]);
193 $usercontent = array_shift($this->contents[$this->user->id]);
194
195 // Check the content has been created as expected.
196 $records = $DB->count_records('contentbank_content');
197 $this->assertEquals(4, $records);
198
199 // Check user can only delete records created by her.
200 $this->setUser($this->user);
201 $this->assertFalse($this->contenttype->can_delete($managercontent));
202 $this->assertTrue($this->contenttype->can_delete($usercontent));
203
204 // Check manager can delete records all the records created.
205 $this->setUser($this->manager1);
206 $this->assertTrue($this->contenttype->can_delete($managercontent));
207 $this->assertTrue($this->contenttype->can_delete($usercontent));
208
209 // Unassign capability to manager role and check not can only delete their own records.
210 unassign_capability('moodle/contentbank:deleteanycontent', $this->managerroleid);
211 $this->assertTrue($this->contenttype->can_delete($managercontent));
212 $this->assertFalse($this->contenttype->can_delete($usercontent));
213 $this->setUser($this->manager2);
214 $this->assertFalse($this->contenttype->can_delete($managercontent));
215 $this->assertFalse($this->contenttype->can_delete($usercontent));
216 }
217
218 /**
219 * Test the behaviour of delete_content().
220 */
221 public function test_delete_content() {
222 global $DB;
223
224 $this->resetAfterTest();
225 $this->contenttype_setup_scenario_data();
226
227 // Check the content has been created as expected.
228 $this->assertEquals(4, $DB->count_records('contentbank_content'));
229
230 // Check the content is deleted as expected.
231 $this->setUser($this->manager1);
232 $content = array_shift($this->contents[$this->manager1->id]);
233 $deleted = $this->contenttype->delete_content($content);
234 $this->assertTrue($deleted);
235 $this->assertEquals(3, $DB->count_records('contentbank_content'));
236 }
237
238 /**
239 * Helper function to setup 3 users (manager1, manager2 and user) and 4 contents (3 created by manager1 and 1 by user).
240 */
241 protected function contenttype_setup_scenario_data(): void {
242 global $DB;
243 $systemcontext = context_system::instance();
244
245 // Create users.
246 $this->manager1 = $this->getDataGenerator()->create_user();
247 $this->manager2 = $this->getDataGenerator()->create_user();
248 $this->managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
249 $this->getDataGenerator()->role_assign($this->managerroleid, $this->manager1->id);
250 $this->getDataGenerator()->role_assign($this->managerroleid, $this->manager2->id);
251 $this->user = $this->getDataGenerator()->create_user();
252
253 // Add some content to the content bank.
254 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
255 $this->contents[$this->manager1->id] = $generator->generate_contentbank_data(null, 3, $this->manager1->id);
256 $this->contents[$this->user->id] = $generator->generate_contentbank_data(null, 1, $this->user->id);
257
258 $this->contenttype = new \contenttype_testable\contenttype($systemcontext);
259 }
3a6ca392
AA
260
261 /**
262 * Data provider for test_rename_content.
263 *
264 * @return array
265 */
266 public function rename_content_provider() {
267 return [
268 'Standard name' => ['New name', 'New name'],
269 'Name with digits' => ['Today is 17/04/2017', 'Today is 17/04/2017'],
270 'Name with symbols' => ['Follow us: @moodle', 'Follow us: @moodle'],
271 'Name with tags' => ['This is <b>bold</b>', 'This is bold'],
272 'Long name' => [str_repeat('a', 100), str_repeat('a', 100)],
273 'Too long name' => [str_repeat('a', 300), str_repeat('a', 255)]
274 ];
275 }
276
277 /**
278 * Test the behaviour of rename_content().
279 *
280 * @dataProvider rename_content_provider
281 * @param string $newname The name to set
282 * @param string $expected The name result
283 *
284 * @covers ::rename_content
285 */
286 public function test_rename_content(string $newname, string $expected) {
287 global $DB;
288
289 $this->resetAfterTest();
290
291 // Create course and teacher user.
292 $course = $this->getDataGenerator()->create_course();
293 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
294 $coursecontext = \context_course::instance($course->id);
295 $contenttype = new contenttype($coursecontext);
296
297 // Add some content to the content bank as teacher.
298 $this->setUser($teacher);
299 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
300 $contents = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id);
301 $content = array_shift($contents);
302
303 $oldname = $content->get_name();
304
305 // Check the content is renamed as expected by a user with permission.
306 $renamed = $contenttype->rename_content($content, $newname);
307 $this->assertTrue($renamed);
308 $record = $DB->get_record('contentbank_content', ['id' => $content->get_id()]);
309 $this->assertNotEquals($oldname, $record->name);
310 $this->assertEquals($expected, $record->name);
311 }
312
313 /**
314 * Test the behaviour of can_manage().
315 *
316 * @covers ::can_manage
317 */
318 public function test_can_manage() {
319 global $DB, $USER;
320
321 $this->resetAfterTest();
322 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
323
324 // Create course and teacher user.
325 $teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
326 $course = $this->getDataGenerator()->create_course();
327 $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
328 $manager = $this->getDataGenerator()->create_and_enrol($course, 'manager');
329 $coursecontext = \context_course::instance($course->id);
330
331 $contenttype = new contenttype($coursecontext);
332
333 // Add some content to the content bank as admin.
334 $this->setAdminUser();
335 $contentsbyadmin = $generator->generate_contentbank_data('contenttype_testable', 1, $USER->id, $coursecontext);
336 $contentbyadmin = array_shift($contentsbyadmin);
337
338 // Add some content to the content bank as teacher.
339 $contentsbyteacher = $generator->generate_contentbank_data('contenttype_testable', 1, $teacher->id, $coursecontext);
340 $contentbyteacher = array_shift($contentsbyteacher);
341
342 // Check the content has been created as expected.
343 $records = $DB->count_records('contentbank_content');
344 $this->assertEquals(2, $records);
345
346 // Check manager can manage by default all the contents created.
347 $this->setUser($manager);
348 $this->assertTrue($contenttype->can_manage($contentbyteacher));
349 $this->assertTrue($contenttype->can_manage($contentbyadmin));
350
351 // Check teacher can only edit their own content.
352 $this->setUser($teacher);
353 $this->assertTrue($contenttype->can_manage($contentbyteacher));
354 $this->assertFalse($contenttype->can_manage($contentbyadmin));
355
356 // Unassign capability to teacher role and check they not can not edit any content.
357 unassign_capability('moodle/contentbank:manageowncontent', $teacherroleid);
358 $this->assertFalse($contenttype->can_manage($contentbyteacher));
359 $this->assertFalse($contenttype->can_manage($contentbyadmin));
360 }
bd4e0a76 361}