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