8c6327eeede997718cda078b0d0ec5d44ee4188e
[moodle.git] / blocks / tests / privacy_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  * Data provider tests.
19  *
20  * @package    core_block
21  * @category   test
22  * @copyright  2018 Frédéric Massart
23  * @author     Frédéric Massart <fred@branchup.tech>
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 use core_privacy\tests\provider_testcase;
31 use core_privacy\local\request\approved_contextlist;
32 use core_privacy\local\request\transform;
33 use core_privacy\local\request\writer;
34 use core_block\privacy\provider;
36 /**
37  * Data provider testcase class.
38  *
39  * @package    core_block
40  * @category   test
41  * @copyright  2018 Frédéric Massart
42  * @author     Frédéric Massart <fred@branchup.tech>
43  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class core_block_privacy_testcase extends provider_testcase {
47     public function setUp() {
48         $this->resetAfterTest();
49     }
51     public function test_get_contexts_for_userid() {
52         $dg = $this->getDataGenerator();
53         $c1 = $dg->create_course();
54         $c2 = $dg->create_course();
55         $u1 = $dg->create_user();
56         $u2 = $dg->create_user();
57         $c1ctx = context_course::instance($c1->id);
58         $c2ctx = context_course::instance($c2->id);
59         $u1ctx = context_user::instance($u1->id);
60         $u2ctx = context_user::instance($u2->id);
62         $manager = $this->get_block_manager(['region-a'], $c1ctx);
63         $manager->add_block('myprofile', 'region-a', 0, false);
64         $manager->load_blocks();
65         $blockmyprofile = $manager->get_blocks_for_region('region-a')[0];
67         $manager = $this->get_block_manager(['region-a'], $c2ctx);
68         $manager->add_block('login', 'region-a', 0, false);
69         $manager->add_block('mentees', 'region-a', 1, false);
70         $manager->load_blocks();
71         list($blocklogin, $blockmentees) = $manager->get_blocks_for_region('region-a');
73         $manager = $this->get_block_manager(['region-a'], $u1ctx);
74         $manager->add_block('private_files', 'region-a', 0, false);
75         $manager->load_blocks();
76         $blockprivatefiles = $manager->get_blocks_for_region('region-a')[0];
78         $this->set_hidden_pref($blocklogin, true, $u1->id);
79         $this->set_hidden_pref($blockprivatefiles, true, $u1->id);
80         $this->set_docked_pref($blockmyprofile, true, $u1->id);
81         $this->set_docked_pref($blockmentees, true, $u1->id);
82         $this->set_docked_pref($blockmentees, true, $u2->id);
84         $contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
85         $this->assertCount(4, $contextids);
86         $this->assertTrue(in_array($blocklogin->context->id, $contextids));
87         $this->assertTrue(in_array($blockprivatefiles->context->id, $contextids));
88         $this->assertTrue(in_array($blockmyprofile->context->id, $contextids));
89         $this->assertTrue(in_array($blockmentees->context->id, $contextids));
91         $contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
92         $this->assertCount(1, $contextids);
93         $this->assertTrue(in_array($blockmentees->context->id, $contextids));
94     }
96     public function test_delete_data_for_user() {
97         global $DB;
98         $dg = $this->getDataGenerator();
99         $c1 = $dg->create_course();
100         $c2 = $dg->create_course();
101         $u1 = $dg->create_user();
102         $u2 = $dg->create_user();
103         $c1ctx = context_course::instance($c1->id);
104         $c2ctx = context_course::instance($c2->id);
105         $u1ctx = context_user::instance($u1->id);
106         $u2ctx = context_user::instance($u2->id);
108         $manager = $this->get_block_manager(['region-a'], $c1ctx);
109         $manager->add_block('myprofile', 'region-a', 0, false);
110         $manager->load_blocks();
111         $blockmyprofile = $manager->get_blocks_for_region('region-a')[0];
113         $manager = $this->get_block_manager(['region-a'], $c2ctx);
114         $manager->add_block('login', 'region-a', 0, false);
115         $manager->add_block('mentees', 'region-a', 1, false);
116         $manager->load_blocks();
117         list($blocklogin, $blockmentees) = $manager->get_blocks_for_region('region-a');
119         $manager = $this->get_block_manager(['region-a'], $u1ctx);
120         $manager->add_block('private_files', 'region-a', 0, false);
121         $manager->load_blocks();
122         $blockprivatefiles = $manager->get_blocks_for_region('region-a')[0];
124         $this->set_hidden_pref($blocklogin, true, $u1->id);
125         $this->set_hidden_pref($blocklogin, true, $u2->id);
126         $this->set_hidden_pref($blockprivatefiles, true, $u1->id);
127         $this->set_hidden_pref($blockmyprofile, true, $u1->id);
128         $this->set_docked_pref($blockmyprofile, true, $u1->id);
129         $this->set_docked_pref($blockmentees, true, $u1->id);
130         $this->set_docked_pref($blockmentees, true, $u2->id);
132         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
133             'name' => "block{$blocklogin->instance->id}hidden"]));
134         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
135             'name' => "block{$blocklogin->instance->id}hidden"]));
136         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
137             'name' => "block{$blockprivatefiles->instance->id}hidden"]));
138         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
139             'name' => "block{$blockmyprofile->instance->id}hidden"]));
140         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
141             'name' => "docked_block_instance_{$blockmyprofile->instance->id}"]));
142         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
143             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
144         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
145             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
147         provider::delete_data_for_user(new approved_contextlist($u1, 'core_block', [$blocklogin->context->id,
148             $blockmyprofile->context->id, $blockmentees->context->id]));
150         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
151             'name' => "block{$blocklogin->instance->id}hidden"]));
152         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
153             'name' => "block{$blocklogin->instance->id}hidden"]));
154         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
155             'name' => "block{$blockprivatefiles->instance->id}hidden"]));
156         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
157             'name' => "block{$blockmyprofile->instance->id}hidden"]));
158         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
159             'name' => "docked_block_instance_{$blockmyprofile->instance->id}"]));
160         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
161             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
162         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
163             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
164     }
166     public function test_delete_data_for_all_users_in_context() {
167         global $DB;
168         $dg = $this->getDataGenerator();
169         $c1 = $dg->create_course();
170         $c2 = $dg->create_course();
171         $u1 = $dg->create_user();
172         $u2 = $dg->create_user();
173         $c1ctx = context_course::instance($c1->id);
174         $c2ctx = context_course::instance($c2->id);
175         $u1ctx = context_user::instance($u1->id);
176         $u2ctx = context_user::instance($u2->id);
178         $manager = $this->get_block_manager(['region-a'], $c1ctx);
179         $manager->add_block('myprofile', 'region-a', 0, false);
180         $manager->load_blocks();
181         $blockmyprofile = $manager->get_blocks_for_region('region-a')[0];
183         $manager = $this->get_block_manager(['region-a'], $c2ctx);
184         $manager->add_block('login', 'region-a', 0, false);
185         $manager->add_block('mentees', 'region-a', 1, false);
186         $manager->load_blocks();
187         list($blocklogin, $blockmentees) = $manager->get_blocks_for_region('region-a');
189         $manager = $this->get_block_manager(['region-a'], $u1ctx);
190         $manager->add_block('private_files', 'region-a', 0, false);
191         $manager->load_blocks();
192         $blockprivatefiles = $manager->get_blocks_for_region('region-a')[0];
194         $this->set_hidden_pref($blocklogin, true, $u1->id);
195         $this->set_hidden_pref($blocklogin, true, $u2->id);
196         $this->set_hidden_pref($blockprivatefiles, true, $u1->id);
197         $this->set_hidden_pref($blockmyprofile, true, $u1->id);
198         $this->set_docked_pref($blockmyprofile, true, $u1->id);
199         $this->set_docked_pref($blockmentees, true, $u1->id);
200         $this->set_docked_pref($blockmentees, true, $u2->id);
202         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
203             'name' => "block{$blocklogin->instance->id}hidden"]));
204         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
205             'name' => "block{$blocklogin->instance->id}hidden"]));
206         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
207             'name' => "block{$blockprivatefiles->instance->id}hidden"]));
208         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
209             'name' => "block{$blockmyprofile->instance->id}hidden"]));
210         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
211             'name' => "docked_block_instance_{$blockmyprofile->instance->id}"]));
212         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
213             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
214         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
215             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
217         // Nothing happens.
218         provider::delete_data_for_all_users_in_context($c1ctx);
219         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
220             'name' => "block{$blocklogin->instance->id}hidden"]));
221         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
222             'name' => "block{$blocklogin->instance->id}hidden"]));
223         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
224             'name' => "block{$blockprivatefiles->instance->id}hidden"]));
225         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
226             'name' => "block{$blockmyprofile->instance->id}hidden"]));
227         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
228             'name' => "docked_block_instance_{$blockmyprofile->instance->id}"]));
229         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
230             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
231         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
232             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
234         // Delete one block.
235         provider::delete_data_for_all_users_in_context($blocklogin->context);
236         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
237             'name' => "block{$blocklogin->instance->id}hidden"]));
238         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u2->id,
239             'name' => "block{$blocklogin->instance->id}hidden"]));
240         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
241             'name' => "block{$blockprivatefiles->instance->id}hidden"]));
242         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
243             'name' => "block{$blockmyprofile->instance->id}hidden"]));
244         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
245             'name' => "docked_block_instance_{$blockmyprofile->instance->id}"]));
246         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
247             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
248         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
249             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
251         // Delete another block.
252         provider::delete_data_for_all_users_in_context($blockmyprofile->context);
253         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
254             'name' => "block{$blocklogin->instance->id}hidden"]));
255         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u2->id,
256             'name' => "block{$blocklogin->instance->id}hidden"]));
257         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
258             'name' => "block{$blockprivatefiles->instance->id}hidden"]));
259         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
260             'name' => "block{$blockmyprofile->instance->id}hidden"]));
261         $this->assertFalse($DB->record_exists('user_preferences', ['userid' => $u1->id,
262             'name' => "docked_block_instance_{$blockmyprofile->instance->id}"]));
263         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u1->id,
264             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
265         $this->assertTrue($DB->record_exists('user_preferences', ['userid' => $u2->id,
266             'name' => "docked_block_instance_{$blockmentees->instance->id}"]));
267     }
269     public function test_export_data_for_user() {
270         global $DB;
271         $dg = $this->getDataGenerator();
272         $c1 = $dg->create_course();
273         $c2 = $dg->create_course();
274         $u1 = $dg->create_user();
275         $u2 = $dg->create_user();
276         $c1ctx = context_course::instance($c1->id);
277         $c2ctx = context_course::instance($c2->id);
278         $u1ctx = context_user::instance($u1->id);
279         $u2ctx = context_user::instance($u2->id);
280         $yes = transform::yesno(true);
281         $no = transform::yesno(false);
283         $manager = $this->get_block_manager(['region-a'], $c1ctx);
284         $manager->add_block('myprofile', 'region-a', 0, false);
285         $manager->add_block('login', 'region-a', 1, false);
286         $manager->add_block('mentees', 'region-a', 2, false);
287         $manager->add_block('private_files', 'region-a', 3, false);
288         $manager->load_blocks();
289         list($bmyprofile, $blogin, $bmentees, $bprivatefiles) = $manager->get_blocks_for_region('region-a');
291         // Set some user preferences.
292         $this->set_hidden_pref($blogin, true, $u1->id);
293         $this->set_docked_pref($blogin, false, $u1->id);
294         $this->set_docked_pref($blogin, true, $u2->id);
295         $this->set_hidden_pref($bprivatefiles, false, $u1->id);
296         $this->set_docked_pref($bprivatefiles, true, $u2->id);
297         $this->set_docked_pref($bmyprofile, true, $u1->id);
298         $this->set_docked_pref($bmentees, true, $u2->id);
300         // Export data.
301         provider::export_user_data(new approved_contextlist($u1, 'core_block', [$bmyprofile->context->id, $blogin->context->id,
302             $bmentees->context->id, $bprivatefiles->context->id]));
303         $prefs = writer::with_context($bmentees->context)->get_user_context_preferences('core_block');
304         $this->assertEmpty((array) $prefs);
306         $prefs = writer::with_context($blogin->context)->get_user_context_preferences('core_block');
307         $this->assertEquals($no, $prefs->block_is_docked->value);
308         $this->assertEquals($yes, $prefs->block_is_hidden->value);
310         $prefs = writer::with_context($bprivatefiles->context)->get_user_context_preferences('core_block');
311         $this->assertObjectNotHasAttribute('block_is_docked', $prefs);
312         $this->assertEquals($no, $prefs->block_is_hidden->value);
314         $prefs = writer::with_context($bmyprofile->context)->get_user_context_preferences('core_block');
315         $this->assertEquals($yes, $prefs->block_is_docked->value);
316         $this->assertObjectNotHasAttribute('block_is_hidden', $prefs);
317     }
319     /**
320      * Get the block manager.
321      *
322      * @param array $regions The regions.
323      * @param context $context The context.
324      * @param string $pagetype The page type.
325      * @param string $subpage The sub page.
326      * @return block_manager
327      */
328     protected function get_block_manager($regions, $context, $pagetype = 'page-type', $subpage = '') {
329         $page = new moodle_page();
330         $page->set_context($context);
331         $page->set_pagetype($pagetype);
332         $page->set_subpage($subpage);
333         $page->set_url(new moodle_url('/'));
335         $blockmanager = new block_manager($page);
336         $blockmanager->add_regions($regions, false);
337         $blockmanager->set_default_region($regions[0]);
339         return $blockmanager;
340     }
342     /**
343      * Set a docked preference.
344      *
345      * @param block_base $block The block.
346      * @param bool $value The value.
347      * @param int $userid The user ID.
348      */
349     protected function set_docked_pref($block, $value, $userid) {
350         set_user_preference("docked_block_instance_{$block->instance->id}", $value, $userid);
351     }
353     /**
354      * Set a hidden preference.
355      *
356      * @param block_base $block The block.
357      * @param bool $value The value.
358      * @param int $userid The user ID.
359      */
360     protected function set_hidden_pref($block, $value, $userid) {
361         set_user_preference("block{$block->instance->id}hidden", $value, $userid);
362     }