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