MDL-67790 core_contentbank: Behat contentbank content generator
[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
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
32require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
33
34use stdClass;
35use context_system;
36use 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 */
47class core_contenttype_contenttype_testcase extends \advanced_testcase {
48
c0d615e8
SA
49 /** @var int Identifier for the manager role. */
50 protected $managerroleid;
51
52 /** @var stdClass Manager user. */
53 protected $manager1;
54
55 /** @var stdClass Manager user. */
56 protected $manager2;
57
58 /** @var stdClass User. */
59 protected $user;
60
61 /** @var array List of contents created (every user has a key with contents created by her). */
62 protected $contents = [];
63
64 /** @var contenttype The contenttype instance. */
65 protected $contenttype;
66
bd4e0a76
AA
67 /**
68 * Tests get_contenttype_name result.
69 *
70 * @covers ::get_contenttype_name
71 */
72 public function test_get_contenttype_name() {
73 $this->resetAfterTest();
74
75 $systemcontext = \context_system::instance();
76 $testable = new contenttype($systemcontext);
77
78 $this->assertEquals('contenttype_testable', $testable->get_contenttype_name());
79 }
80
81 /**
82 * Tests get_plugin_name result.
83 *
84 * @covers ::get_plugin_name
85 */
86 public function test_get_plugin_name() {
87 $this->resetAfterTest();
88
89 $systemcontext = \context_system::instance();
90 $testable = new contenttype($systemcontext);
91
92 $this->assertEquals('testable', $testable->get_plugin_name());
93 }
94
95 /**
96 * Tests get_icon result.
97 *
98 * @covers ::get_icon
99 */
100 public function test_get_icon() {
101 $this->resetAfterTest();
102
103 $systemcontext = \context_system::instance();
104 $testable = new contenttype($systemcontext);
105 $icon = $testable->get_icon('new content');
106 $this->assertContains('archive', $icon);
107 }
108
109 /**
110 * Tests is_feature_supported behavior .
111 *
112 * @covers ::is_feature_supported
113 */
114 public function test_is_feature_supported() {
115 $this->resetAfterTest();
116
117 $systemcontext = \context_system::instance();
118 $testable = new contenttype($systemcontext);
119
120 $this->assertTrue($testable->is_feature_supported(contenttype::CAN_TEST));
121 $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
122 }
123
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();
131
132 $systemcontext = \context_system::instance();
133 $testable = new contenttype($systemcontext);
134
135 $this->setAdminUser();
136 $this->assertFalse($testable->is_feature_supported(contenttype::CAN_UPLOAD));
137 $this->assertFalse($testable->can_upload());
138 }
139
140 /**
141 * Test create_content() with empty data.
142 *
143 * @covers ::create_content
144 */
145 public function test_create_empty_content() {
146 $this->resetAfterTest();
147
148 // Create empty content.
149 $record = new stdClass();
150
151 $contenttype = new contenttype(context_system::instance());
152 $content = $contenttype->create_content($record);
153
154 $this->assertEquals('contenttype_testable', $content->get_content_type());
155 $this->assertInstanceOf('\\contenttype_testable\\content', $content);
156 }
157
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();
165
166 // Create content.
167 $record = new stdClass();
168 $record->name = 'Test content';
169 $record->configdata = '';
170 $record->contenttype = '';
171
172 $contenttype = new contenttype(context_system::instance());
173 $content = $contenttype->create_content($record);
174
175 $this->assertEquals('contenttype_testable', $content->get_content_type());
176 $this->assertInstanceOf('\\contenttype_testable\\content', $content);
177 }
c0d615e8
SA
178
179
180 /**
181 * Test the behaviour of can_delete().
182 */
183 public function test_can_delete() {
184 global $DB;
185
186 $this->resetAfterTest();
187 $this->contenttype_setup_scenario_data();
188
189 $managercontent = array_shift($this->contents[$this->manager1->id]);
190 $usercontent = array_shift($this->contents[$this->user->id]);
191
192 // Check the content has been created as expected.
193 $records = $DB->count_records('contentbank_content');
194 $this->assertEquals(4, $records);
195
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));
200
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));
205
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 }
214
215 /**
216 * Test the behaviour of delete_content().
217 */
218 public function test_delete_content() {
219 global $DB;
220
221 $this->resetAfterTest();
222 $this->contenttype_setup_scenario_data();
223
224 // Check the content has been created as expected.
225 $this->assertEquals(4, $DB->count_records('contentbank_content'));
226
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 }
234
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();
241
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();
249
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);
254
255 $this->contenttype = new \contenttype_testable\contenttype($systemcontext);
256 }
bd4e0a76 257}