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