MDL-65788 tool_policy: Prevent two modals rendering for guests.
[moodle.git] / blocks / rss_client / 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/>.
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  */
24 defined('MOODLE_INTERNAL') || die();
26 use \core_privacy\tests\provider_testcase;
27 use \block_rss_client\privacy\provider;
28 use \core_privacy\local\request\approved_userlist;
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  */
36 class block_rss_client_testcase extends provider_testcase {
38     /**
39      * Basic setup for these tests.
40      */
41     public function setUp() {
42         $this->resetAfterTest(true);
43     }
45     /**
46      * Test getting the context for the user ID related to this plugin.
47      */
48     public function test_get_contexts_for_userid() {
50         $user = $this->getDataGenerator()->create_user();
51         $context = context_user::instance($user->id);
53         $this->add_rss_feed($user);
55         $contextlist = provider::get_contexts_for_userid($user->id);
57         $this->assertEquals($context, $contextlist->current());
58     }
60     /**
61      * Test that data is exported correctly for this plugin.
62      */
63     public function test_export_user_data() {
65         $user = $this->getDataGenerator()->create_user();
66         $context = context_user::instance($user->id);
68         $this->add_rss_feed($user);
69         $this->add_rss_feed($user);
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');
75         $data = $writer->get_data([get_string('pluginname', 'block_rss_client')]);
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);
83     }
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';
91         // Create a user.
92         $user = $this->getDataGenerator()->create_user();
93         $usercontext = context_user::instance($user->id);
95         $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
96         provider::get_users_in_context($userlist);
97         $this->assertCount(0, $userlist);
99         $this->add_rss_feed($user);
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);
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     }
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';
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);
126         $this->add_rss_feed($user1);
127         $this->add_rss_feed($user2);
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);
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);
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);
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);
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);
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     }
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;
178         $user = $this->getDataGenerator()->create_user();
179         $context = context_user::instance($user->id);
181         $this->add_rss_feed($user);
183         // Check that we have an entry.
184         $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
185         $this->assertCount(1, $rssfeeds);
187         provider::delete_data_for_all_users_in_context($context);
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     }
194     /**
195      * Test that user data is deleted for this user.
196      */
197     public function test_delete_data_for_user() {
198         global $DB;
200         $user = $this->getDataGenerator()->create_user();
201         $context = context_user::instance($user->id);
203         $this->add_rss_feed($user);
205         // Check that we have an entry.
206         $rssfeeds = $DB->get_records('block_rss_client', ['userid' => $user->id]);
207         $this->assertCount(1, $rssfeeds);
209         $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'block_rss_feed',
210                 [$context->id]);
211         provider::delete_data_for_user($approvedlist);
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     }
218     /**
219      * Add dummy rss feed.
220      *
221      * @param object $user User object
222      */
223     private function add_rss_feed($user) {
224         global $DB;
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         );
235         $DB->insert_record('block_rss_client', $rssfeeddata);
236     }