See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310]
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 message_airnotifier. 18 * 19 * @package message_airnotifier 20 * @copyright 2018 Adrian Greeve <adrian@moodle.com> 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 */ 23 24 defined('MOODLE_INTERNAL') || die(); 25 26 use \core_privacy\tests\provider_testcase; 27 use \message_airnotifier\privacy\provider; 28 use \core_privacy\local\request\approved_userlist; 29 30 /** 31 * Unit tests for message\output\airnotifier\classes\privacy\provider.php 32 * 33 * @copyright 2018 Adrian Greeve <adrian@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class message_airnotifier_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 * / 47 * @param object $user User object 48 * @param string $pushid unique string 49 */ 50 protected function add_device($user, $pushid) { 51 global $DB; 52 53 // Add fake core device. 54 $device = array( 55 'appid' => 'com.moodle.moodlemobile', 56 'name' => 'occam', 57 'model' => 'Nexus 4', 58 'platform' => 'Android', 59 'version' => '4.2.2', 60 'pushid' => $pushid, 61 'uuid' => 'asdnfl348qlksfaasef859', 62 'userid' => $user->id, 63 'timecreated' => time(), 64 'timemodified' => time(), 65 ); 66 $coredeviceid = $DB->insert_record('user_devices', (object) $device); 67 68 $airnotifierdev = array( 69 'userdeviceid' => $coredeviceid, 70 'enable' => 1 71 ); 72 $airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev); 73 } 74 75 /** 76 * Test returning metadata. 77 */ 78 public function test_get_metadata() { 79 $collection = new \core_privacy\local\metadata\collection('message_airnotifier'); 80 $collection = \message_airnotifier\privacy\provider::get_metadata($collection); 81 $this->assertNotEmpty($collection); 82 } 83 84 /** 85 * Test getting the context for the user ID related to this plugin. 86 */ 87 public function test_get_contexts_for_userid() { 88 89 $user = $this->getDataGenerator()->create_user(); 90 $context = context_user::instance($user->id); 91 92 $this->add_device($user, 'apuJih874kj'); 93 $this->add_device($user, 'bdu09Ikjjsu'); 94 95 $contextlist = \message_airnotifier\privacy\provider::get_contexts_for_userid($user->id); 96 $this->assertEquals($context->id, $contextlist->current()->id); 97 } 98 99 /** 100 * Test that data is exported correctly for this plugin. 101 */ 102 public function test_export_user_data() { 103 $user = $this->getDataGenerator()->create_user(); 104 $context = context_user::instance($user->id); 105 106 $this->add_device($user, 'apuJih874kj'); 107 $this->add_device($user, 'bdu09Ikjjsu'); 108 109 $writer = \core_privacy\local\request\writer::with_context($context); 110 $this->assertFalse($writer->has_any_data()); 111 $this->export_context_data_for_user($user->id, $context, 'message_airnotifier'); 112 113 // First device. 114 $data = $writer->get_data([get_string('privacy:subcontext', 'message_airnotifier'), 'Nexus 4_apuJih874kj']); 115 $this->assertEquals('com.moodle.moodlemobile', $data->appid); 116 117 // Second device. 118 $data = $writer->get_data([get_string('privacy:subcontext', 'message_airnotifier'), 'Nexus 4_bdu09Ikjjsu']); 119 $this->assertEquals('bdu09Ikjjsu', $data->pushid); 120 } 121 122 /** 123 * Test that user data is deleted using the context. 124 */ 125 public function test_delete_data_for_all_users_in_context() { 126 global $DB; 127 128 $user = $this->getDataGenerator()->create_user(); 129 $context = context_user::instance($user->id); 130 131 $this->add_device($user, 'apuJih874kj'); 132 133 // Check that we have an entry. 134 $devices = $DB->get_records('message_airnotifier_devices'); 135 $this->assertCount(1, $devices); 136 137 \message_airnotifier\privacy\provider::delete_data_for_all_users_in_context($context); 138 139 // Check that it has now been deleted. 140 $devices = $DB->get_records('message_airnotifier_devices'); 141 $this->assertCount(0, $devices); 142 } 143 144 /** 145 * Test that user data is deleted for this user. 146 */ 147 public function test_delete_data_for_user() { 148 global $DB; 149 150 $user = $this->getDataGenerator()->create_user(); 151 $context = context_user::instance($user->id); 152 153 $this->add_device($user, 'apuJih874kj'); 154 155 // Check that we have an entry. 156 $devices = $DB->get_records('message_airnotifier_devices'); 157 $this->assertCount(1, $devices); 158 159 $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'message_airnotifier', [$context->id]); 160 \message_airnotifier\privacy\provider::delete_data_for_user($approvedlist); 161 162 // Check that it has now been deleted. 163 $devices = $DB->get_records('message_airnotifier_devices'); 164 $this->assertCount(0, $devices); 165 } 166 167 /** 168 * Test that only users with a user context are fetched. 169 */ 170 public function test_get_users_in_context() { 171 $component = 'message_airnotifier'; 172 173 // Create user. 174 $user = $this->getDataGenerator()->create_user(); 175 $usercontext = context_user::instance($user->id); 176 177 // The lists of users for the user context should be empty. 178 // Related user data have not been created yet. 179 $userlist = new \core_privacy\local\request\userlist($usercontext, $component); 180 provider::get_users_in_context($userlist); 181 $this->assertCount(0, $userlist); 182 183 $this->add_device($user, 'apuJih874kj'); 184 $this->add_device($user, 'bdu09Ikjjsu'); 185 186 // The list of users for userlist should return one user (user). 187 provider::get_users_in_context($userlist); 188 $this->assertCount(1, $userlist); 189 $expected = [$user->id]; 190 $actual = $userlist->get_userids(); 191 $this->assertEquals($expected, $actual); 192 193 // The list of users should only return users in the user context. 194 $systemcontext = context_system::instance(); 195 $userlist1 = new \core_privacy\local\request\userlist($systemcontext, $component); 196 provider::get_users_in_context($userlist1); 197 $this->assertCount(0, $userlist1); 198 } 199 200 /** 201 * Test that data for users in approved userlist is deleted. 202 */ 203 public function test_delete_data_for_users() { 204 $component = 'message_airnotifier'; 205 206 // Create user1. 207 $user1 = $this->getDataGenerator()->create_user(); 208 $usercontext1 = context_user::instance($user1->id); 209 // Create user2. 210 $user2 = $this->getDataGenerator()->create_user(); 211 $usercontext2 = context_user::instance($user2->id); 212 213 $this->add_device($user1, 'apuJih874kj'); 214 $this->add_device($user1, 'cpuJih874kp'); 215 $this->add_device($user2, 'bdu09Ikjjsu'); 216 217 // The list of users for usercontext1 should return one user (user1). 218 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 219 provider::get_users_in_context($userlist1); 220 $this->assertCount(1, $userlist1); 221 222 // The list of users for usercontext2 should return one user (user2). 223 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 224 provider::get_users_in_context($userlist2); 225 $this->assertCount(1, $userlist2); 226 227 $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids()); 228 // Delete using delete_data_for_user. 229 provider::delete_data_for_users($approvedlist); 230 231 // Re-fetch users in usercontext1 - the user data should now be empty. 232 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 233 provider::get_users_in_context($userlist1); 234 $this->assertCount(0, $userlist1); 235 236 // The list of users for usercontext2 should still return one user (user2). 237 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 238 provider::get_users_in_context($userlist2); 239 $this->assertCount(1, $userlist2); 240 241 // User data should only be removed in the user context. 242 $systemcontext = context_system::instance(); 243 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids()); 244 // Delete using delete_data_for_user. 245 provider::delete_data_for_users($approvedlist); 246 // Re-fetch users in usercontext2 - the user data should still be present. 247 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 248 provider::get_users_in_context($userlist2); 249 $this->assertCount(1, $userlist2); 250 } 251 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body