MDL-63535 core_rss: Coding style fix
[moodle.git] / rss / tests / privacy_test.php
CommitLineData
8d3238d8
DM
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 * Base class for unit tests for core_rss.
18 *
19 * @package core_rss
20 * @copyright 2018 Sara Arjona <sara@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
6a91cc80
AN
26use core_privacy\tests\provider_testcase;
27use core_rss\privacy\provider;
28use core_privacy\local\request\writer;
29use core_privacy\local\request\approved_contextlist;
30use core_privacy\local\request\approved_userlist;
8d3238d8
DM
31
32/**
33 * Unit tests for rss\classes\privacy\provider.php
34 *
35 * @copyright 2018 Sara Arjona <sara@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class core_rss_testcase extends provider_testcase {
39
40 /**
41 * Basic setup for these tests.
42 */
43 public function setUp() {
44 $this->resetAfterTest(true);
45 }
46
47 /**
48 * Test getting the context for the user ID related to this plugin.
49 */
50 public function test_get_contexts_for_userid() {
b720d9da 51 // Create user and RSS user keys.
8d3238d8
DM
52 $user = $this->getDataGenerator()->create_user();
53 $context = \context_user::instance($user->id);
54 $key = get_user_key('rss', $user->id);
55
56 $contextlist = provider::get_contexts_for_userid($user->id);
57 $this->assertEquals($context->id, $contextlist->current()->id);
58 }
59
60 /**
61 * Test that data is exported correctly for this plugin.
62 */
63 public function test_export_user_data() {
64 global $DB;
65
b720d9da 66 // Create user and RSS user keys.
8d3238d8
DM
67 $user = $this->getDataGenerator()->create_user();
68 $context = \context_user::instance($user->id);
69 $keyvalue = get_user_key('rss', $user->id);
70 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
71
b720d9da 72 // Validate exported data.
73 $this->setUser($user);
8d3238d8
DM
74 $writer = writer::with_context($context);
75 $this->assertFalse($writer->has_any_data());
76 $this->export_context_data_for_user($user->id, $context, 'core_rss');
b720d9da 77 $userkeydata = $writer->get_related_data([], 'userkeys');
78 $this->assertCount(1, $userkeydata->keys);
79 $this->assertEquals($key->script, reset($userkeydata->keys)->script);
80 }
81
82 /**
83 * Test for provider::delete_data_for_all_users_in_context().
84 */
85 public function test_delete_data_for_all_users_in_context() {
86 global $DB;
87
88 // Create user and RSS user keys.
89 $user = $this->getDataGenerator()->create_user();
90 $context = \context_user::instance($user->id);
91 $keyvalue = get_user_key('rss', $user->id);
92 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
93
94 // Before deletion, we should have 1 user_private_key.
95 $count = $DB->count_records('user_private_key', ['script' => 'rss']);
96 $this->assertEquals(1, $count);
97
98 // Delete data.
99 provider::delete_data_for_all_users_in_context($context);
100
101 // After deletion, the user_private_key entries should have been deleted.
102 $count = $DB->count_records('user_private_key', ['script' => 'rss']);
103 $this->assertEquals(0, $count);
104 }
105
106 /**
107 * Test for provider::delete_data_for_user().
108 */
109 public function test_delete_data_for_user() {
110 global $DB;
111
112 // Create user and RSS user keys.
113 $user = $this->getDataGenerator()->create_user();
114 $context = \context_user::instance($user->id);
115 $keyvalue = get_user_key('rss', $user->id);
116 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
117
118 // Before deletion, we should have 1 user_private_key.
119 $count = $DB->count_records('user_private_key', ['script' => 'rss']);
120 $this->assertEquals(1, $count);
121
122 // Delete data.
123 $contextlist = provider::get_contexts_for_userid($user->id);
124 $approvedcontextlist = new approved_contextlist($user, 'rss', $contextlist->get_contextids());
125 provider::delete_data_for_user($approvedcontextlist);
126
127 // After deletion, the user_private_key entries should have been deleted.
128 $count = $DB->count_records('user_private_key', ['script' => 'rss']);
129 $this->assertEquals(0, $count);
8d3238d8 130 }
cee33e02
MG
131
132 /**
133 * Test that only users with a user context are fetched.
134 */
135 public function test_get_users_in_context() {
136 $component = 'core_rss';
137 // Create a user.
138 $user = $this->getDataGenerator()->create_user();
139
140 $usercontext = \context_user::instance($user->id);
141 $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
142 // The list of users should not return anything yet (related data still haven't been created).
143 provider::get_users_in_context($userlist);
144 $this->assertCount(0, $userlist);
145 // Create private access key for user.
146 get_user_key('rss', $user->id);
147
148 // The list of users for user context should return the user.
149 provider::get_users_in_context($userlist);
150 $this->assertCount(1, $userlist);
151 $expected = [$user->id];
152 $actual = $userlist->get_userids();
153 $this->assertEquals($expected, $actual);
154
155 // The list of users for system context should not return any users.
156 $systemcontext = context_system::instance();
157 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
158 provider::get_users_in_context($userlist);
159 $this->assertCount(0, $userlist);
160 }
161
162 /**
163 * Test that data for users in approved userlist is deleted.
164 */
165 public function test_delete_data_for_users() {
166 $component = 'core_rss';
167 // Create a user1.
168 $user1 = $this->getDataGenerator()->create_user();
169 $usercontext1 = \context_user::instance($user1->id);
170 // Create list of users with a related user data in usercontext1.
171 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
172
173 // Create a user1.
174 $user2 = $this->getDataGenerator()->create_user();
175 $usercontext2 = \context_user::instance($user2->id);
176 // Create list of users with a related user data in usercontext2.
177 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
178
179 // Create private access key for user1.
180 get_user_key('rss', $user1->id);
181 // Create private access key for user2.
182 get_user_key('rss', $user2->id);
183
184 // Ensure the user list for usercontext1 contains user1.
185 provider::get_users_in_context($userlist1);
186 $this->assertCount(1, $userlist1);
187 // Ensure the user list for usercontext2 contains user2.
188 provider::get_users_in_context($userlist2);
189 $this->assertCount(1, $userlist2);
190
191 // Convert $userlist1 into an approved_contextlist.
192 $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
193
194 // Delete using delete_data_for_user.
195 provider::delete_data_for_users($approvedlist);
196
197 // Re-fetch users in usercontext1 - The user list should now be empty.
198 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
199 provider::get_users_in_context($userlist1);
200 $this->assertCount(0, $userlist1);
201 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
202 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
203 provider::get_users_in_context($userlist2);
204 $this->assertCount(1, $userlist2);
205
206 // User data should be only removed in the user context.
207 $systemcontext = context_system::instance();
208 // Add userlist2 to the approved user list in the system context.
209 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
210 // Delete user1 data using delete_data_for_user.
211 provider::delete_data_for_users($approvedlist);
212 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
213 $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
214 provider::get_users_in_context($userlist1);
215 $this->assertCount(1, $userlist1);
216 }
8d3238d8 217}