Merge branch 'MDL-68492-master' of https://github.com/sammarshallou/moodle
[moodle.git] / contentbank / tests / privacy_test.php
CommitLineData
aeacd6c8 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 * Base class for unit tests for core_contentbank.
19 *
20 * @package core_contentbank
21 * @category test
22 * @copyright 2020 Carlos Escobedo <carlos@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
30use stdClass;
31use context_system;
32use context_coursecat;
33use context_course;
34use core_contentbank\privacy\provider;
35use core_privacy\local\request\approved_contextlist;
36use core_privacy\local\request\writer;
37use core_privacy\tests\provider_testcase;
38use core_privacy\local\request\userlist;
39use core_privacy\local\request\approved_userlist;
40
41/**
42 * Unit tests for contentbank\classes\privacy\provider.php
43 *
44 * @copyright 2020 Carlos Escobedo <carlos@moodle.com>
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 */
47class core_contentbank_privacy_testcase extends provider_testcase {
48
49 /**
50 * Setup to ensure that fixtures are loaded.
51 */
52 public static function setupBeforeClass(): void {
53 global $CFG;
54 require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
55 }
56
57 /**
58 * Test for provider::get_contexts_for_userid().
59 */
60 public function test_get_contexts_for_userid() {
61
62 $this->resetAfterTest();
63 // Setup scenario.
64 $scenario = $this->setup_scenario();
65
66 // Testing againts Manager who has content in the three contexts.
67 $contextlist = provider::get_contexts_for_userid($scenario->manager->id);
68 // There are three contexts in the list.
69 $contextlistids = $contextlist->get_contextids();
70 $this->assertCount(3, $contextlistids);
71 // Check the list against the expected list of contexts.
72 $this->assertContains($scenario->systemcontext->id, $contextlistids);
73 $this->assertContains($scenario->coursecategorycontext->id,
74 $contextlistids);
75 $this->assertContains($scenario->coursecontext->id, $contextlistids);
76
77 // Testing againts Teacher who has content in the one context.
78 $contextlist = provider::get_contexts_for_userid($scenario->teacher->id);
79 // There are only one context in the list.
80 $contextlistids = $contextlist->get_contextids();
81 $this->assertCount(1, $contextlistids);
82 // Check the againts Course Context.
83 $this->assertContains($scenario->coursecontext->id, $contextlistids);
84 // And there is not a System and Course Category Context.
85 $this->assertNotContains($scenario->systemcontext->id, $contextlistids);
86 $this->assertNotContains($scenario->coursecategorycontext->id, $contextlistids);
87 }
88
89 /**
90 * Test for provider::get_users_in_context().
91 */
92 public function test_get_users_in_context() {
93
94 $this->resetAfterTest();
95 // Setup scenario.
96 $scenario = $this->setup_scenario();
97
98 // Get the userlist to Context System, only Manager will be there.
99 $userlist = new userlist($scenario->systemcontext, 'core_contentbank');
100 provider::get_users_in_context($userlist);
101 $this->assertEquals([$scenario->manager->id], $userlist->get_userids());
102 // Teacher will not be there.
103 $this->assertNotEquals([$scenario->teacher->id], $userlist->get_userids());
104
105 // Get the userlist to Context Course, Manager and Teacher will be there.
106 $userlist = new userlist($scenario->coursecontext, 'core_contentbank');
107 provider::get_users_in_context($userlist);
108 $this->assertEquals([$scenario->manager->id, $scenario->teacher->id],
109 $userlist->get_userids());
110 }
111
112 /**
113 * Test for provider::test_export_user_data().
114 */
115 public function test_export_user_data() {
116
117 $this->resetAfterTest();
118 // Setup scenario.
119 $scenario = $this->setup_scenario();
120
121 $subcontexts = [
122 get_string('name', 'core_contentbank')
123 ];
124 // Get the data for the System Context.
125 $writer = writer::with_context($scenario->systemcontext);
126 $this->assertFalse($writer->has_any_data());
127 // Export data for Manager.
128 $this->export_context_data_for_user($scenario->manager->id,
129 $scenario->systemcontext, 'core_contentbank');
130 $data = $writer->get_data($subcontexts);
131 $this->assertCount(3, (array) $data);
132 $this->assertCount(3, $writer->get_files($subcontexts));
133
134 // Get the data for the Course Categoy Context.
135 $writer = writer::with_context($scenario->coursecategorycontext);
136 // Export data for Manager.
137 $this->export_context_data_for_user($scenario->manager->id,
138 $scenario->coursecategorycontext, 'core_contentbank');
139 $data = $writer->get_data($subcontexts);
140 $this->assertCount(2, (array) $data);
141 $this->assertCount(2, $writer->get_files($subcontexts));
142
143 // Get the data for the Course Context.
144 $writer = writer::with_context($scenario->coursecontext);
145 // Export data for Manager.
146 $this->export_context_data_for_user($scenario->manager->id,
147 $scenario->coursecontext, 'core_contentbank');
148 $data = $writer->get_data($subcontexts);
149 $this->assertCount(2, (array) $data);
150 $this->assertCount(2, $writer->get_files($subcontexts));
151
152 // Export data for Teacher.
153 $writer = writer::reset();
154 $writer = writer::with_context($scenario->coursecontext);
155 $this->export_context_data_for_user($scenario->teacher->id,
156 $scenario->coursecontext, 'core_contentbank');
157 $data = $writer->get_data($subcontexts);
158 $this->assertCount(3, (array) $data);
159 $this->assertCount(3, $writer->get_files($subcontexts));
160 }
161
162 /**
163 * Test for provider::delete_data_for_all_users_in_context().
164 */
165 public function test_delete_data_for_all_users_in_context() {
166 global $DB;
167
168 $this->resetAfterTest();
169 // Setup scenario.
170 $scenario = $this->setup_scenario();
171
172 // Before delete data, we have 4 contents.
173 // - 3 in a system context.
174 // - 2 in a course category context.
175 // - 5 in a course context (2 by manager and 3 by teacher).
176
177 // Delete data based on system context.
178 provider::delete_data_for_all_users_in_context($scenario->systemcontext);
179 $count = $DB->count_records('contentbank_content');
180 // 3 content should be deleted.
181 // 7 contents should be remain.
182 $this->assertEquals(7, $count);
183
184 // Delete data based on course category context.
185 provider::delete_data_for_all_users_in_context($scenario->coursecategorycontext);
186 $count = $DB->count_records('contentbank_content');
187 // 2 contents should be deleted.
188 // 5 content should be remain.
189 $this->assertEquals(5, $count);
190
191 // Delete data based on course context.
192 provider::delete_data_for_all_users_in_context($scenario->coursecontext);
193 $count = $DB->count_records('contentbank_content');
194 // 5 content should be deleted.
195 // 0 content should be remain.
196 $this->assertEquals(0, $count);
197 }
198
199 /**
200 * Test for provider::test_delete_data_for_users().
201 */
202 public function test_delete_data_for_users() {
203 global $DB;
204
205 $this->resetAfterTest();
206 // Setup scenario.
207 $scenario = $this->setup_scenario();
208
209 // Before delete data, we have 4 contents.
210 // - 3 in a system context.
211 // - 2 in a course category context.
212 // - 5 in a course context (2 by manager and 3 by teacher).
213
214 // A list of users who has created content in Course Category Context.
215 $userlist1 = new userlist($scenario->coursecategorycontext,
216 'core_contentbank');
217 provider::get_users_in_context($userlist1);
218 $this->assertCount(1, $userlist1);
219 // Only Manager should be.
220 $this->assertEquals([$scenario->manager->id], $userlist1->get_userids());
221
222 // A list of users who has created content in Course Context.
223 $userlist2 = new userlist($scenario->coursecontext, 'core_contentbank');
224 provider::get_users_in_context($userlist2);
225 $this->assertCount(2, $userlist2);
226 // Manager and Teacher should be.
227 $this->assertEquals([$scenario->manager->id, $scenario->teacher->id],
228 $userlist2->get_userids());
229
230 // Convert $userlist1 into an approved_contextlist.
231 $approvedlist1 = new approved_userlist($scenario->coursecategorycontext, 'core_contentbank', $userlist1->get_userids());
232 // Delete data for users in course category context.
233 provider::delete_data_for_users($approvedlist1);
234
235 // Re-fetch users in course category context.
236 $userlist1 = new userlist($scenario->coursecategorycontext,
237 'core_contentbank');
238 provider::get_users_in_context($userlist1);
239 // The user data in course category context should be deleted.
240 $this->assertCount(0, $userlist1);
241 // Re-fetch users in course category context.
242 $userlist2 = new userlist($scenario->coursecontext, 'core_contentbank');
243 provider::get_users_in_context($userlist2);
244 // The user data in course context should be still present.
245 $this->assertCount(2, $userlist2);
246
247 // Convert $userlist2 into an approved_contextlist.
248 $approvedlist2 = new approved_userlist($scenario->coursecontext,
249 'core_contentbank', $userlist2->get_userids());
250 // Delete data for users in course context.
251 provider::delete_data_for_users($approvedlist2);
252 $userlist2 = new userlist($scenario->coursecontext, 'core_contentbank');
253 provider::get_users_in_context($userlist2);
254 // The user data in course context should be deleted.
255 $this->assertCount(0, $userlist2);
256 }
257
258 /**
259 * Test for provider::delete_data_for_user().
260 */
261 public function test_delete_data_for_user() {
262 global $DB;
263
264 $this->resetAfterTest();
265 // Setup scenario.
266 $scenario = $this->setup_scenario();
267
268 // Before delete data, we have 4 contents.
269 // - 3 in a system context.
270 // - 2 in a course category context.
271 // - 5 in a course context (2 by manager and 3 by teacher).
272
273 // Get all the context for Manager.
274 $contextlist = provider::get_contexts_for_userid($scenario->manager->id);
275 $approvedcontextlist = new approved_contextlist($scenario->manager,
276 'core_contentbank', $contextlist->get_contextids());
277 // Delete all the data created by the Manager in all the contexts.
278 provider::delete_data_for_user($approvedcontextlist);
279
280 // After deletion, only 3 content for teacher should be present.
281 $count = $DB->count_records('contentbank_content');
282 $this->assertEquals(3, $count);
283
284 // Confirm that the remaining content was created by the teacher.
285 $count = $DB->count_records('contentbank_content',
286 ['usercreated' => $scenario->teacher->id]);
287 $this->assertEquals(3, $count);
288
289 // Get all the context for Teacher.
290 $contextlist = provider::get_contexts_for_userid($scenario->teacher->id);
291 $approvedcontextlist = new approved_contextlist($scenario->teacher,
292 'core_contentbank', $contextlist->get_contextids());
293 // Delete all the data created by the Teacher in all the contexts.
294 provider::delete_data_for_user($approvedcontextlist);
295
296 // After deletion, no content should be present.
297 $count = $DB->count_records('contentbank_content');
298 $this->assertEquals(0, $count);
299 }
300
301 /**
302 * Create a complex scenario to use into the tests.
303 *
304 * @return stdClass $scenario
305 */
306 protected function setup_scenario() {
307 global $DB;
308
309 $systemcontext = context_system::instance();
310 $manager = $this->getDataGenerator()->create_user();
311 $managerroleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
312 $this->getDataGenerator()->role_assign($managerroleid, $manager->id);
313
314 $coursecategory = $this->getDataGenerator()->create_category();
315 $coursecategorycontext = context_coursecat::instance($coursecategory->id);
316
317 $course = $this->getDataGenerator()->create_course();
318 $coursecontext = context_course::instance($course->id);
319 $teacher = $this->getDataGenerator()->create_and_enrol($course,
320 'editingteacher');
321
322 // Add some content to the content bank.
323 $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
324 // Add contents by Manager in Context System.
325 $records = $generator->generate_contentbank_data('contenttype_testable',
326 1, $manager->id, $systemcontext, false, 'systemtestfile1.h5p');
327 $records = $generator->generate_contentbank_data('contenttype_testable',
328 1, $manager->id, $systemcontext, false, 'systemtestfile2.h5p');
329 $records = $generator->generate_contentbank_data('contenttype_testable',
330 1, $manager->id, $systemcontext, false, 'systemtestfile3.h5p');
331 // Add contents by Manager in Context Course Category.
332 $records = $generator->generate_contentbank_data('contenttype_testable',
333 1, $manager->id, $coursecategorycontext, false, 'coursecattestfile1.h5p');
334 $records = $generator->generate_contentbank_data('contenttype_testable',
335 1, $manager->id, $coursecategorycontext, false, 'coursecattestfile2.h5p');
336 // Add contents by Manager in Context Course.
337 $records = $generator->generate_contentbank_data('contenttype_testable',
338 1, $manager->id, $coursecontext, false, 'coursetestfile1.h5p');
339 $records = $generator->generate_contentbank_data('contenttype_testable',
340 1, $manager->id, $coursecontext, false, 'coursetestfile2.h5p');
341 // Add contents by Teacher.
342 $records = $generator->generate_contentbank_data('contenttype_testable',
343 1, $teacher->id, $coursecontext, false, 'courseteacherfile1.h5p');
344 $records = $generator->generate_contentbank_data('contenttype_testable',
345 1, $teacher->id, $coursecontext, false, 'courseteacherfile2.h5p');
346 $records = $generator->generate_contentbank_data('contenttype_testable',
347 1, $teacher->id, $coursecontext, false, 'courseteacherfile3.h5p');
348
349 $scenario = new stdClass();
350 $scenario->systemcontext = $systemcontext;
351 $scenario->coursecategorycontext = $coursecategorycontext;
352 $scenario->coursecontext = $coursecontext;
353 $scenario->manager = $manager;
354 $scenario->teacher = $teacher;
355
356 return $scenario;
357 }
358}