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 /** 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 /** @var \core_privacy\tests\request\content_writer $writer */ 74 $writer = writer::with_context($context); 75 76 $this->assertTrue($writer->has_any_data()); 77 $exported = $writer->get_related_data($subcontext, 'userkeys'); 78 79 $this->assertCount(1, $exported->keys); 80 81 $firstkey = reset($exported->keys); 82 $this->assertEquals('core_tests', $firstkey->script); 83 $this->assertEquals('', $firstkey->instance); 84 $this->assertEquals('', $firstkey->iprestriction); 85 $this->assertNotEmpty($firstkey->validuntil); 86 $this->assertNotEmpty($firstkey->timecreated); 87 88 provider::delete_userkeys('core_tests', $user->id); 89 90 $this->assertCount(0, $DB->get_records('user_private_key')); 91 } 92 93 /** 94 * Export for a user with a key against a script where additional data is specified. 95 */ 96 public function test_export_userkeys_complex_key() { 97 global $DB; 98 $this->resetAfterTest(); 99 100 $user = $this->getDataGenerator()->create_user(); 101 $this->setUser($user); 102 103 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 104 105 $context = \context_system::instance(); 106 $subcontext = []; 107 108 // Export all keys in core_tests. 109 provider::export_userkeys($context, $subcontext, 'core_tests'); 110 111 /** @var \core_privacy\tests\request\content_writer $writer */ 112 $writer = writer::with_context($context); 113 114 $this->assertTrue($writer->has_any_data()); 115 $exported = $writer->get_related_data($subcontext, 'userkeys'); 116 117 $this->assertCount(1, $exported->keys); 118 119 $firstkey = reset($exported->keys); 120 $this->assertEquals('core_tests', $firstkey->script); 121 $this->assertEquals(42, $firstkey->instance); 122 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 123 $this->assertNotEmpty($firstkey->validuntil); 124 $this->assertNotEmpty($firstkey->timecreated); 125 126 provider::delete_userkeys('core_tests', $user->id); 127 128 $this->assertCount(0, $DB->get_records('user_private_key')); 129 } 130 131 /** 132 * Export for a user with a key against a script where no instance is specified. 133 */ 134 public function test_export_userkeys_basic_key_without_filter() { 135 global $DB; 136 $this->resetAfterTest(); 137 138 $user = $this->getDataGenerator()->create_user(); 139 $this->setUser($user); 140 141 $key = get_user_key('core_tests', $user->id); 142 143 $context = \context_system::instance(); 144 $subcontext = []; 145 146 provider::export_userkeys($context, $subcontext, 'core_tests'); 147 148 /** @var \core_privacy\tests\request\content_writer $writer */ 149 $writer = writer::with_context($context); 150 151 $this->assertTrue($writer->has_any_data()); 152 $exported = $writer->get_related_data($subcontext, 'userkeys'); 153 154 $this->assertCount(1, $exported->keys); 155 156 $firstkey = reset($exported->keys); 157 $this->assertEquals('core_tests', $firstkey->script); 158 $this->assertEquals('', $firstkey->instance); 159 $this->assertEquals('', $firstkey->iprestriction); 160 $this->assertNotEmpty($firstkey->validuntil); 161 $this->assertNotEmpty($firstkey->timecreated); 162 163 provider::delete_userkeys('core_tests', $user->id); 164 165 $this->assertCount(0, $DB->get_records('user_private_key')); 166 } 167 168 /** 169 * Export for a user with a key against a script where additional data is specified. 170 */ 171 public function test_export_userkeys_complex_key_with_filter() { 172 global $DB; 173 $this->resetAfterTest(); 174 175 $user = $this->getDataGenerator()->create_user(); 176 $this->setUser($user); 177 178 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 179 180 $context = \context_system::instance(); 181 $subcontext = []; 182 183 // Export all keys in core_tests against instance 43 - no keys. 184 provider::export_userkeys($context, $subcontext, 'core_tests', 43); 185 /** @var \core_privacy\tests\request\content_writer $writer */ 186 $writer = writer::with_context($context); 187 $this->assertFalse($writer->has_any_data()); 188 189 // Export all keys in core_tests against instance 42. 190 provider::export_userkeys($context, $subcontext, 'core_tests', 42); 191 /** @var \core_privacy\tests\request\content_writer $writer */ 192 $writer = writer::with_context($context); 193 $this->assertTrue($writer->has_any_data()); 194 $exported = $writer->get_related_data($subcontext, 'userkeys'); 195 196 $this->assertCount(1, $exported->keys); 197 198 $firstkey = reset($exported->keys); 199 $this->assertEquals('core_tests', $firstkey->script); 200 $this->assertEquals(42, $firstkey->instance); 201 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 202 $this->assertNotEmpty($firstkey->validuntil); 203 $this->assertNotEmpty($firstkey->timecreated); 204 205 // Delete for instance 43 (no keys). 206 provider::delete_userkeys('core_tests', $user->id, 43); 207 $this->assertCount(1, $DB->get_records('user_private_key')); 208 209 // Delete for instance 42. 210 provider::delete_userkeys('core_tests', $user->id, 42); 211 $this->assertCount(0, $DB->get_records('user_private_key')); 212 } 213 214 /** 215 * Export for a user with keys against multiple scripts where additional data is specified. 216 */ 217 public function test_export_userkeys_multiple_complex_key_with_filter() { 218 global $DB; 219 $this->resetAfterTest(); 220 221 $user = $this->getDataGenerator()->create_user(); 222 $this->setUser($user); 223 224 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 225 $key = get_user_key('core_userkey', $user->id, 99, '240.0.0.1', 54321); 226 227 $context = \context_system::instance(); 228 $subcontext = []; 229 230 // Export all keys in core_tests against instance 43 - no keys. 231 provider::export_userkeys($context, $subcontext, 'core_tests', 43); 232 /** @var \core_privacy\tests\request\content_writer $writer */ 233 $writer = writer::with_context($context); 234 $this->assertFalse($writer->has_any_data()); 235 236 // Export all keys in core_tests against instance 42. 237 provider::export_userkeys($context, $subcontext, 'core_tests', 42); 238 /** @var \core_privacy\tests\request\content_writer $writer */ 239 $writer = writer::with_context($context); 240 $this->assertTrue($writer->has_any_data()); 241 $exported = $writer->get_related_data($subcontext, 'userkeys'); 242 243 $this->assertCount(1, $exported->keys); 244 245 $firstkey = reset($exported->keys); 246 $this->assertEquals('core_tests', $firstkey->script); 247 $this->assertEquals(42, $firstkey->instance); 248 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 249 $this->assertNotEmpty($firstkey->validuntil); 250 $this->assertNotEmpty($firstkey->timecreated); 251 252 // Delete for instance 43 (no keys). 253 provider::delete_userkeys('core_tests', $user->id, 43); 254 $this->assertCount(2, $DB->get_records('user_private_key')); 255 256 // Delete for instance 42. 257 provider::delete_userkeys('core_tests', $user->id, 42); 258 $this->assertCount(1, $DB->get_records('user_private_key')); 259 260 // Delete for instance 99. 261 provider::delete_userkeys('core_tests', $user->id, 99); 262 $this->assertCount(1, $DB->get_records('user_private_key')); 263 264 // Delete for instance 99 of core_userkey too. 265 provider::delete_userkeys('core_userkey', $user->id, 99); 266 $this->assertCount(0, $DB->get_records('user_private_key')); 267 } 268 269 /** 270 * Export for keys against multiple users. 271 */ 272 public function test_export_userkeys_multiple_users() { 273 global $DB; 274 $this->resetAfterTest(); 275 276 $user = $this->getDataGenerator()->create_user(); 277 $otheruser = $this->getDataGenerator()->create_user(); 278 $this->setUser($user); 279 280 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 281 $key = get_user_key('core_tests', $otheruser->id, 42, '127.0.0.1', 12345); 282 283 $context = \context_system::instance(); 284 $subcontext = []; 285 286 // Export all keys in core_tests against instance 43 - no keys. 287 provider::export_userkeys($context, $subcontext, 'core_tests', 43); 288 /** @var \core_privacy\tests\request\content_writer $writer */ 289 $writer = writer::with_context($context); 290 $this->assertFalse($writer->has_any_data()); 291 292 // Export all keys in core_tests against instance 42. 293 provider::export_userkeys($context, $subcontext, 'core_tests', 42); 294 /** @var \core_privacy\tests\request\content_writer $writer */ 295 $writer = writer::with_context($context); 296 $this->assertTrue($writer->has_any_data()); 297 $exported = $writer->get_related_data($subcontext, 'userkeys'); 298 299 $this->assertCount(1, $exported->keys); 300 301 $firstkey = reset($exported->keys); 302 $this->assertEquals('core_tests', $firstkey->script); 303 $this->assertEquals(42, $firstkey->instance); 304 $this->assertEquals('127.0.0.1', $firstkey->iprestriction); 305 $this->assertNotEmpty($firstkey->validuntil); 306 $this->assertNotEmpty($firstkey->timecreated); 307 308 // Delete for instance 43 (no keys). 309 provider::delete_userkeys('core_tests', $user->id, 43); 310 $this->assertCount(2, $DB->get_records('user_private_key')); 311 312 // Delete for instance 42. 313 provider::delete_userkeys('core_tests', $user->id, 42); 314 $this->assertCount(1, $DB->get_records('user_private_key')); 315 316 // Delete for instance 99. 317 provider::delete_userkeys('core_tests', $user->id, 99); 318 $this->assertCount(1, $DB->get_records('user_private_key')); 319 } 320 321 /** 322 * Delete for all users in a script. 323 */ 324 public function test_delete_all_userkeys_in_script() { 325 global $DB; 326 $this->resetAfterTest(); 327 328 $user = $this->getDataGenerator()->create_user(); 329 $otheruser = $this->getDataGenerator()->create_user(); 330 331 $key = get_user_key('core_tests', $user->id, 42, '127.0.0.1', 12345); 332 $key = get_user_key('core_tests', $user->id, 43, '127.0.0.1', 12345); 333 $key = get_user_key('core_userkey', $user->id, 42, '127.0.0.1', 12345); 334 $key = get_user_key('core_userkey', $user->id, 43, '127.0.0.1', 12345); 335 $key = get_user_key('core_tests', $otheruser->id, 42, '127.0.0.1', 12345); 336 $key = get_user_key('core_tests', $otheruser->id, 43, '127.0.0.1', 12345); 337 $key = get_user_key('core_userkey', $otheruser->id, 42, '127.0.0.1', 12345); 338 $key = get_user_key('core_userkey', $otheruser->id, 43, '127.0.0.1', 12345); 339 340 $context = \context_system::instance(); 341 $subcontext = []; 342 343 $this->assertCount(8, $DB->get_records('user_private_key')); 344 345 // Delete for all of core_tests. 346 provider::delete_userkeys('core_tests'); 347 $this->assertCount(4, $DB->get_records('user_private_key')); 348 349 // Delete for all of core_userkey where instanceid = 42. 350 provider::delete_userkeys('core_userkey', null, 42); 351 $this->assertCount(2, $DB->get_records('user_private_key')); 352 353 provider::delete_userkeys('core_userkey', $otheruser->id); 354 $this->assertCount(1, $DB->get_records('user_private_key')); 355 } 356 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body