MDL-67795 contentbank: delete content API
[moodle.git] / contentbank / tests / contenttype_test.php
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/>.
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  */
26 namespace core_contentbank;
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
32 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
34 use stdClass;
35 use context_system;
36 use contenttype_testable\contenttype as contenttype;
37 /**
38  * Test for content bank contenttype class.
39  *
40  * @package    core_contentbank
41  * @category   test
42  * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
43  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  * @coversDefaultClass \core_contentbank\contenttype
45  *
46  */
47 class core_contenttype_contenttype_testcase extends \advanced_testcase {
49     /** @var int Identifier for the manager role. */
50     protected $managerroleid;
52     /** @var stdClass Manager user. */
53     protected $manager1;
55     /** @var stdClass Manager user. */
56     protected $manager2;
58     /** @var stdClass User. */
59     protected $user;
61     /** @var array List of contents created (every user has a key with contents created by her). */
62     protected $contents = [];
64     /** @var contenttype The contenttype instance. */
65     protected $contenttype;
67     /**
68      * Tests get_contenttype_name result.
69      *
70      * @covers ::get_contenttype_name
71      */
72     public function test_get_contenttype_name() {
73         $this->resetAfterTest();
75         $systemcontext = \context_system::instance();
76         $testable = new contenttype($systemcontext);
78         $this->assertEquals('contenttype_testable', $testable->get_contenttype_name());
79     }
81     /**
82      * Tests get_plugin_name result.
83      *
84      * @covers ::get_plugin_name
85      */
86     public function test_get_plugin_name() {
87         $this->resetAfterTest();
89         $systemcontext = \context_system::instance();
90         $testable = new contenttype($systemcontext);
92         $this->assertEquals('testable', $testable->get_plugin_name());
93     }
95     /**
96      * Tests get_icon result.
97      *
98      * @covers ::get_icon
99      */
100     public function test_get_icon() {
101         $this->resetAfterTest();
103         $systemcontext = \context_system::instance();
104         $testable = new contenttype($systemcontext);
105         $icon = $testable->get_icon('new content');
106         $this->assertContains('archive', $icon);
107     }
109     /**
110      * Tests is_feature_supported behavior .
111      *
112      * @covers ::is_feature_supported
113      */
114     public function test_is_feature_supported() {
115         $this->resetAfterTest();
117         $systemcontext = \context_system::instance();
118         $testable = new contenttype($systemcontext);
120         $this->assertTrue($testable->is_feature_supported(contenttype::CAN_TEST));
121         $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
122     }
124     /**
125      * Tests can_upload behavior with no implemented upload feature.
126      *
127      * @covers ::can_upload
128      */
129     public function test_no_upload_feature_supported() {
130         $this->resetAfterTest();
132         $systemcontext = \context_system::instance();
133         $testable = new contenttype($systemcontext);
135         $this->setAdminUser();
136         $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
137         $this->assertFalse($testable->can_upload());
138     }
140     /**
141      * Test create_content() with empty data.
142      *
143      * @covers ::create_content
144      */
145     public function test_create_empty_content() {
146         $this->resetAfterTest();
148         // Create empty content.
149         $record = new stdClass();
151         $contenttype = new contenttype(context_system::instance());
152         $content = $contenttype->create_content($record);
154         $this->assertEquals('contenttype_testable', $content->get_content_type());
155         $this->assertInstanceOf('\\contenttype_testable\\content', $content);
156     }
158     /**
159      * Tests for behaviour of create_content() with data.
160      *
161      * @covers ::create_content
162      */
163     public function test_create_content() {
164         $this->resetAfterTest();
166         // Create content.
167         $record = new stdClass();
168         $record->name = 'Test content';
169         $record->configdata = '';
170         $record->contenttype = '';
172         $contenttype = new contenttype(context_system::instance());
173         $content = $contenttype->create_content($record);
175         $this->assertEquals('contenttype_testable', $content->get_content_type());
176         $this->assertInstanceOf('\\contenttype_testable\\content', $content);
177     }
180     /**
181      * Test the behaviour of can_delete().
182      */
183     public function test_can_delete() {
184         global $DB;
186         $this->resetAfterTest();
187         $this->contenttype_setup_scenario_data();
189         $managercontent = array_shift($this->contents[$this->manager1->id]);
190         $usercontent = array_shift($this->contents[$this->user->id]);
192         // Check the content has been created as expected.
193         $records = $DB->count_records('contentbank_content');
194         $this->assertEquals(4, $records);
196         // Check user can only delete records created by her.
197         $this->setUser($this->user);
198         $this->assertFalse($this->contenttype->can_delete($managercontent));
199         $this->assertTrue($this->contenttype->can_delete($usercontent));
201         // Check manager can delete records all the records created.
202         $this->setUser($this->manager1);
203         $this->assertTrue($this->contenttype->can_delete($managercontent));
204         $this->assertTrue($this->contenttype->can_delete($usercontent));
206         // Unassign capability to manager role and check not can only delete their own records.
207         unassign_capability('moodle/contentbank:deleteanycontent', $this->managerroleid);
208         $this->assertTrue($this->contenttype->can_delete($managercontent));
209         $this->assertFalse($this->contenttype->can_delete($usercontent));
210         $this->setUser($this->manager2);
211         $this->assertFalse($this->contenttype->can_delete($managercontent));
212         $this->assertFalse($this->contenttype->can_delete($usercontent));
213     }
215     /**
216      * Test the behaviour of delete_content().
217      */
218     public function test_delete_content() {
219         global $DB;
221         $this->resetAfterTest();
222         $this->contenttype_setup_scenario_data();
224         // Check the content has been created as expected.
225         $this->assertEquals(4, $DB->count_records('contentbank_content'));
227         // Check the content is deleted as expected.
228         $this->setUser($this->manager1);
229         $content = array_shift($this->contents[$this->manager1->id]);
230         $deleted = $this->contenttype->delete_content($content);
231         $this->assertTrue($deleted);
232         $this->assertEquals(3, $DB->count_records('contentbank_content'));
233     }
235     /**
236      * Helper function to setup 3 users (manager1, manager2 and user) and 4 contents (3 created by manager1 and 1 by user).
237      */
238     protected function contenttype_setup_scenario_data(): void {
239         global $DB;
240         $systemcontext = context_system::instance();
242         // Create users.
243         $this->manager1 = $this->getDataGenerator()->create_user();
244         $this->manager2 = $this->getDataGenerator()->create_user();
245         $this->managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
246         $this->getDataGenerator()->role_assign($this->managerroleid, $this->manager1->id);
247         $this->getDataGenerator()->role_assign($this->managerroleid, $this->manager2->id);
248         $this->user = $this->getDataGenerator()->create_user();
250         // Add some content to the content bank.
251         $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
252         $this->contents[$this->manager1->id] = $generator->generate_contentbank_data(null, 3, $this->manager1->id);
253         $this->contents[$this->user->id] = $generator->generate_contentbank_data(null, 1, $this->user->id);
255         $this->contenttype = new \contenttype_testable\contenttype($systemcontext);
256     }