MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / rss_client / tests / privacy_test.php
CommitLineData
9b8822d2
MG
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 block_rss_client.
18 *
19 * @package block_rss_client
20 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26use \core_privacy\tests\provider_testcase;
5038a863
MG
27use \block_rss_client\privacy\provider;
28use \core_privacy\local\request\approved_userlist;
9b8822d2
MG
29
30/**
31 * Unit tests for blocks\rss_client\classes\privacy\provider.php
32 *
33 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class block_rss_client_testcase extends provider_testcase {
37
38 /**
39 * Basic setup for these tests.
40 */
41 public function setUp() {
42 $this->resetAfterTest(true);
43 }
44
45 /**
46 * Test getting the context for the user ID related to this plugin.
47 */
48 public function test_get_contexts_for_userid() {
49
50 $user = $this->getDataGenerator()->create_user();
51 $context = context_user::instance($user->id);
52
53 $this->add_rss_feed($user);
54
5038a863 55 $contextlist = provider::get_contexts_for_userid($user->id);
9b8822d2
MG
56
57 $this->assertEquals($context, $contextlist->current());
58 }
59
60 /**
61 * Test that data is exported correctly for this plugin.
62 */
63 public function test_export_user_data() {
64
65 $user = $this->getDataGenerator()->create_user();
66 $context = context_user::instance($user->id);
67
9d594541 68 $this->add_rss_feed($user);
9b8822d2
MG
69 $this->add_rss_feed($user);
70
71 $writer = \core_privacy\local\request\writer::with_context($context);
72 $this->assertFalse($writer->has_any_data());
73 $this->export_context_data_for_user($user->id, $context, 'block_rss_client');
74
75 $data = $writer->get_data([get_string('pluginname', 'block_rss_client')]);
9d594541
SA
76 $this->assertCount(2, $data->feeds);
77 $feed1 = reset($data->feeds);
78 $this->assertEquals('BBC News - World', $feed1->title);
79 $this->assertEquals('World News', $feed1->preferredtitle);
80 $this->assertEquals('Description: BBC News - World', $feed1->description);
81 $this->assertEquals(get_string('no'), $feed1->shared);
82 $this->assertEquals('http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk', $feed1->url);
9b8822d2
MG
83 }
84
5038a863
MG
85 /**
86 * Test that only users within a course context are fetched.
87 */
88 public function test_get_users_in_context() {
89 $component = 'block_rss_client';
90
91 // Create a user.
92 $user = $this->getDataGenerator()->create_user();
93 $usercontext = context_user::instance($user->id);
94
95 $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
96 provider::get_users_in_context($userlist);
97 $this->assertCount(0, $userlist);
98
99 $this->add_rss_feed($user);
100
101 // The list of users within the user context should contain user.
102 provider::get_users_in_context($userlist);
103 $this->assertCount(1, $userlist);
104 $expected = [$user->id];
105 $actual = $userlist->get_userids();
106 $this->assertEquals($expected, $actual);
107
108 // The list of users within the system context should be empty.
109 $systemcontext = context_system::instance();
110 $userlist2 = new \core_privacy\local\request\userlist($systemcontext, $component);
111 provider::get_users_in_context($userlist2);
112 $this->assertCount(0, $userlist2);
113 }
114
115 /**
116 * Test that data for users in approved userlist is deleted.
117 */
118 public function test_delete_data_for_users() {
119 $component = 'block_rss_client';
120
121 $user1 = $this->getDataGenerator()->create_user();
122 $usercontext1 = context_user::instance($user1->id);
123 $user2 = $this->getDataGenerator()->create_user();
124 $usercontext2 = context_user::instance($user2->id);
125
126 $this->add_rss_feed($user1);
127 $this->add_rss_feed($user2);
128
129 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
130 provider::get_users_in_context($userlist1);
131 $this->assertCount(1, $userlist1);
132 $expected = [$user1->id];
133 $actual = $userlist1->get_userids();
134 $this->assertEquals($expected, $actual);
135
136 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
137 provider::get_users_in_context($userlist2);
138 $this->assertCount(1, $userlist2);
139 $expected = [$user2->id];
140 $actual = $userlist2->get_userids();
141 $this->assertEquals($expected, $actual);
142
143 // Convert $userlist1 into an approved_contextlist.
144 $approvedlist1 = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
145 // Delete using delete_data_for_user.
146 provider::delete_data_for_users($approvedlist1);
147
148 // Re-fetch users in usercontext1.
149 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
150 provider::get_users_in_context($userlist1);
151 // The user data in usercontext1 should be deleted.
152 $this->assertCount(0, $userlist1);
153
154 // Re-fetch users in usercontext2.
155 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
156 provider::get_users_in_context($userlist2);
157 // The user data in usercontext2 should be still present.
158 $this->assertCount(1, $userlist2);
159
160 // Convert $userlist2 into an approved_contextlist in the system context.
161 $systemcontext = context_system::instance();
162 $approvedlist2 = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
163 // Delete using delete_data_for_user.
164 provider::delete_data_for_users($approvedlist2);
165 // Re-fetch users in usercontext2.
166 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
167 provider::get_users_in_context($userlist2);
168 // The user data in systemcontext should not be deleted.
169 $this->assertCount(1, $userlist2);
170 }
171
9b8822d2
MG
172 /**
173 * Test that user data is deleted using the context.
174 */
175 public function test_delete_data_for_all_users_in_context() {
176 global $DB;
177
178 $user = $this->getDataGenerator()->create_user();
179 $context = context_user::instance($user->id);
180
181 $this->add_rss_feed($user);
182
183 // Check that we have an entry.
184 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
185 $this->assertCount(1, $rssfeeds);
186
5038a863 187 provider::delete_data_for_all_users_in_context($context);
9b8822d2
MG
188
189 // Check that it has now been deleted.
190 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
191 $this->assertCount(0, $rssfeeds);
192 }
193
194 /**
195 * Test that user data is deleted for this user.
196 */
197 public function test_delete_data_for_user() {
198 global $DB;
199
200 $user = $this->getDataGenerator()->create_user();
201 $context = context_user::instance($user->id);
202
203 $this->add_rss_feed($user);
204
205 // Check that we have an entry.
206 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
207 $this->assertCount(1, $rssfeeds);
208
209 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'block_rss_feed',
210 [$context->id]);
5038a863 211 provider::delete_data_for_user($approvedlist);
9b8822d2
MG
212
213 // Check that it has now been deleted.
214 $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
215 $this->assertCount(0, $rssfeeds);
216 }
217
218 /**
219 * Add dummy rss feed.
220 *
221 * @param object $user User object
222 */
223 private function add_rss_feed($user) {
224 global $DB;
225
226 $rssfeeddata = array(
227 'userid' => $user->id,
228 'title' => 'BBC News - World',
229 'preferredtitle' => 'World News',
230 'description' => 'Description: BBC News - World',
231 'shared' => 0,
232 'url' => 'http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk',
233 );
234
235 $DB->insert_record('block_rss_client', $rssfeeddata);
236 }
237}