See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
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 /** 18 * Privacy tests for core_userkey. 19 * 20 * @package core_userkey 21 * @category test 22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 namespace core_userkey\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 use core_privacy\tests\provider_testcase; 30 use core_privacy\local\request\writer; 31 use core_userkey\privacy\provider; 32 33 /** 34 * Privacy tests for core_userkey. 35 * 36 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class provider_test extends provider_testcase { 40 /** 41 * Export for a user with no keys in the specified instance will not have any data exported. 42 */ 43 public function test_export_userkeys_no_keys() { 44 $this->resetAfterTest(); 45 46 $user = $this->getDataGenerator()->create_user(); 47 $this->setUser($user); 48 49 $context = \context_system::instance(); 50 51 provider::export_userkeys($context, [], 'core_tests'); 52 53 $this->assertFalse(writer::with_context($context)->has_any_data()); 54 } 55 56 /** 57 * Export for a user with a key against a script where no instance is specified. 58 */ 59 public function test_export_userkeys_basic_key() { 60 global $DB; 61 $this->resetAfterTest(); 62 63 $user = $this->getDataGenerator()->create_user(); 64 $this->setUser($user); 65 66 $key = get_user_key('core_tests', $user->id); 67 68 $context = \context_system::instance(); 69 $subcontext = []; 70 71 provider::export_userkeys($context, $subcontext, 'core_tests'); 72 73 $writer = writer::with_context($context); 74 75 $this->assertTrue($writer->has_any_data()); 76 $exported = $writer->get_related_data($subcontext, 'userkeys'); 77 78 $this->assertCount(1, $exported->keys); 79 80 $firstkey = reset($exported->keys); 81 $this->assertEquals('core_tests', $firstkey->script); 82 $this->assertEquals('', $firstkey->instance); 83 $this->assertEquals('', $firstkey->iprestriction); 84 $this->assertNotEmpty($firstkey->validuntil); 85 $this->assertNotEmpty($firstkey->timecreated); 86 87 provider::delete_userkeys('core_tests', $user->id); 88 89 $this->assertCount(0, $DB->get_records('user_private_key')); 90 } 91 92 /** 93 * Export for a user with a key against a script where additional data is specified. 94 */ 95 public function test_export_userkeys_complex_key() { 96 global $DB; 97 $this->resetAfterTest(); 98 99 $user = $this->getDataGenerator()->create_user(); 100 $this->setUser($user); 101 102 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 103 104 $context = \context_system::instance(); 105 $subcontext = []; 106 107 // Export all keys in core_tests. 108 provider::export_userkeys($context, $subcontext, 'core_tests'); 109 110 $writer = writer::with_context($context); 111 112 $this->assertTrue($writer->has_any_data()); 113 $exported = $writer->get_related_data($subcontext, 'userkeys'); 114 115 $this->assertCount(1, $exported->keys); 116 117 $firstkey = reset($exported->keys); 118 $this->assertEquals('core_tests', $firstkey->script); 119 $this->assertEquals(42, $firstkey->instance); 120 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 121 $this->assertNotEmpty($firstkey->validuntil); 122 $this->assertNotEmpty($firstkey->timecreated); 123 124 provider::delete_userkeys('core_tests', $user->id); 125 126 $this->assertCount(0, $DB->get_records('user_private_key')); 127 } 128 129 /** 130 * Export for a user with a key against a script where no instance is specified. 131 */ 132 public function test_export_userkeys_basic_key_without_filter() { 133 global $DB; 134 $this->resetAfterTest(); 135 136 $user = $this->getDataGenerator()->create_user(); 137 $this->setUser($user); 138 139 $key = get_user_key('core_tests', $user->id); 140 141 $context = \context_system::instance(); 142 $subcontext = []; 143 144 provider::export_userkeys($context, $subcontext, 'core_tests'); 145 146 $writer = writer::with_context($context); 147 148 $this->assertTrue($writer->has_any_data()); 149 $exported = $writer->get_related_data($subcontext, 'userkeys'); 150 151 $this->assertCount(1, $exported->keys); 152 153 $firstkey = reset($exported->keys); 154 $this->assertEquals('core_tests', $firstkey->script); 155 $this->assertEquals('', $firstkey->instance); 156 $this->assertEquals('', $firstkey->iprestriction); 157 $this->assertNotEmpty($firstkey->validuntil); 158 $this->assertNotEmpty($firstkey->timecreated); 159 160 provider::delete_userkeys('core_tests', $user->id); 161 162 $this->assertCount(0, $DB->get_records('user_private_key')); 163 } 164 165 /** 166 * Export for a user with a key against a script where additional data is specified. 167 */ 168 public function test_export_userkeys_complex_key_with_filter() { 169 global $DB; 170 $this->resetAfterTest(); 171 172 $user = $this->getDataGenerator()->create_user(); 173 $this->setUser($user); 174 175 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 176 177 $context = \context_system::instance(); 178 $subcontext = []; 179 180 // Export all keys in core_tests against instance 43 - no keys. 181 provider::export_userkeys($context, $subcontext, 'core_tests', 43); 182 $writer = writer::with_context($context); 183 $this->assertFalse($writer->has_any_data()); 184 185 // Export all keys in core_tests against instance 42. 186 provider::export_userkeys($context, $subcontext, 'core_tests', 42); 187 $writer = writer::with_context($context); 188 $this->assertTrue($writer->has_any_data()); 189 $exported = $writer->get_related_data($subcontext, 'userkeys'); 190 191 $this->assertCount(1, $exported->keys); 192 193 $firstkey = reset($exported->keys); 194 $this->assertEquals('core_tests', $firstkey->script); 195 $this->assertEquals(42, $firstkey->instance); 196 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 197 $this->assertNotEmpty($firstkey->validuntil); 198 $this->assertNotEmpty($firstkey->timecreated); 199 200 // Delete for instance 43 (no keys). 201 provider::delete_userkeys('core_tests', $user->id, 43); 202 $this->assertCount(1, $DB->get_records('user_private_key')); 203 204 // Delete for instance 42. 205 provider::delete_userkeys('core_tests', $user->id, 42); 206 $this->assertCount(0, $DB->get_records('user_private_key')); 207 } 208 209 /** 210 * Export for a user with keys against multiple scripts where additional data is specified. 211 */ 212 public function test_export_userkeys_multiple_complex_key_with_filter() { 213 global $DB; 214 $this->resetAfterTest(); 215 216 $user = $this->getDataGenerator()->create_user(); 217 $this->setUser($user); 218 219 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 220 $key = get_user_key('core_userkey', $user->id, 99, '240.0.0.1', 54321); 221 222 $context = \context_system::instance(); 223 $subcontext = []; 224 225 // Export all keys in core_tests against instance 43 - no keys. 226 provider::export_userkeys($context, $subcontext, 'core_tests', 43); 227 $writer = writer::with_context($context); 228 $this->assertFalse($writer->has_any_data()); 229 230 // Export all keys in core_tests against instance 42. 231 provider::export_userkeys($context, $subcontext, 'core_tests', 42); 232 $writer = writer::with_context($context); 233 $this->assertTrue($writer->has_any_data()); 234 $exported = $writer->get_related_data($subcontext, 'userkeys'); 235 236 $this->assertCount(1, $exported->keys); 237 238 $firstkey = reset($exported->keys); 239 $this->assertEquals('core_tests', $firstkey->script); 240 $this->assertEquals(42, $firstkey->instance); 241 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 242 $this->assertNotEmpty($firstkey->validuntil); 243 $this->assertNotEmpty($firstkey->timecreated); 244 245 // Delete for instance 43 (no keys). 246 provider::delete_userkeys('core_tests', $user->id, 43); 247 $this->assertCount(2, $DB->get_records('user_private_key')); 248 249 // Delete for instance 42. 250 provider::delete_userkeys('core_tests', $user->id, 42); 251 $this->assertCount(1, $DB->get_records('user_private_key')); 252 253 // Delete for instance 99. 254 provider::delete_userkeys('core_tests', $user->id, 99); 255 $this->assertCount(1, $DB->get_records('user_private_key')); 256 257 // Delete for instance 99 of core_userkey too. 258 provider::delete_userkeys('core_userkey', $user->id, 99); 259 $this->assertCount(0, $DB->get_records('user_private_key')); 260 } 261 262 /** 263 * Export for keys against multiple users. 264 */ 265 public function test_export_userkeys_multiple_users() { 266 global $DB; 267 $this->resetAfterTest(); 268 269 $user = $this->getDataGenerator()->create_user(); 270 $otheruser = $this->getDataGenerator()->create_user(); 271 $this->setUser($user); 272 273 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 274 $key = get_user_key('core_tests', $otheruser->id, 42, '127.0.0.1', 12345); 275 276 $context = \context_system::instance(); 277 $subcontext = []; 278 279 // Export all keys in core_tests against instance 43 - no keys. 280 provider::export_userkeys($context, $subcontext, 'core_tests', 43); 281 $writer = writer::with_context($context); 282 $this->assertFalse($writer->has_any_data()); 283 284 // Export all keys in core_tests against instance 42. 285 provider::export_userkeys($context, $subcontext, 'core_tests', 42); 286 $writer = writer::with_context($context); 287 $this->assertTrue($writer->has_any_data()); 288 $exported = $writer->get_related_data($subcontext, 'userkeys'); 289 290 $this->assertCount(1, $exported->keys); 291 292 $firstkey = reset($exported->keys); 293 $this->assertEquals('core_tests', $firstkey->script); 294 $this->assertEquals(42, $firstkey->instance); 295 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 296 $this->assertNotEmpty($firstkey->validuntil); 297 $this->assertNotEmpty($firstkey->timecreated); 298 299 // Delete for instance 43 (no keys). 300 provider::delete_userkeys('core_tests', $user->id, 43); 301 $this->assertCount(2, $DB->get_records('user_private_key')); 302 303 // Delete for instance 42. 304 provider::delete_userkeys('core_tests', $user->id, 42); 305 $this->assertCount(1, $DB->get_records('user_private_key')); 306 307 // Delete for instance 99. 308 provider::delete_userkeys('core_tests', $user->id, 99); 309 $this->assertCount(1, $DB->get_records('user_private_key')); 310 } 311 312 /** 313 * Delete for all users in a script. 314 */ 315 public function test_delete_all_userkeys_in_script() { 316 global $DB; 317 $this->resetAfterTest(); 318 319 $user = $this->getDataGenerator()->create_user(); 320 $otheruser = $this->getDataGenerator()->create_user(); 321 322 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 323 $key = get_user_key('core_tests', $user->id, 43, '127.0.0.1', 12345); 324 $key = get_user_key('core_userkey', $user->id, 42, '127.0.0.1', 12345); 325 $key = get_user_key('core_userkey', $user->id, 43, '127.0.0.1', 12345); 326 $key = get_user_key('core_tests', $otheruser->id, 42, '127.0.0.1', 12345); 327 $key = get_user_key('core_tests', $otheruser->id, 43, '127.0.0.1', 12345); 328 $key = get_user_key('core_userkey', $otheruser->id, 42, '127.0.0.1', 12345); 329 $key = get_user_key('core_userkey', $otheruser->id, 43, '127.0.0.1', 12345); 330 331 $context = \context_system::instance(); 332 $subcontext = []; 333 334 $this->assertCount(8, $DB->get_records('user_private_key')); 335 336 // Delete for all of core_tests. 337 provider::delete_userkeys('core_tests'); 338 $this->assertCount(4, $DB->get_records('user_private_key')); 339 340 // Delete for all of core_userkey where instanceid = 42. 341 provider::delete_userkeys('core_userkey', null, 42); 342 $this->assertCount(2, $DB->get_records('user_private_key')); 343 344 provider::delete_userkeys('core_userkey', $otheruser->id); 345 $this->assertCount(1, $DB->get_records('user_private_key')); 346 } 347 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body