Differences Between: [Versions 311 and 402] [Versions 311 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 * Base class for unit tests for tool_mobile. 18 * 19 * @package tool_mobile 20 * @category test 21 * @copyright 2018 Carlos Escobedo <carlos@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace tool_mobile\privacy; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 use core_privacy\local\request\writer; 29 use core_privacy\local\request\transform; 30 use core_privacy\local\request\approved_contextlist; 31 use core_privacy\local\request\approved_userlist; 32 use tool_mobile\privacy\provider; 33 34 /** 35 * Unit tests for the tool_mobile implementation of the privacy API. 36 * 37 * @copyright 2018 Carlos Escobedo <carlos@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class provider_test extends \core_privacy\tests\provider_testcase { 41 42 /** 43 * Basic setup for these tests. 44 */ 45 public function setUp(): void { 46 $this->resetAfterTest(true); 47 } 48 49 /** 50 * Test to check export_user_preferences. 51 * returns user preferences data. 52 */ 53 public function test_export_user_preferences() { 54 $user = $this->getDataGenerator()->create_user(); 55 $expectedtime = time(); 56 set_user_preference('tool_mobile_autologin_request_last', time(), $user); 57 provider::export_user_preferences($user->id); 58 $writer = writer::with_context(\context_system::instance()); 59 $prefs = $writer->get_user_preferences('tool_mobile'); 60 $time = transform::datetime($expectedtime); 61 $this->assertEquals($time, $prefs->tool_mobile_autologin_request_last->value); 62 $this->assertEquals(get_string('privacy:metadata:preference:tool_mobile_autologin_request_last', 'tool_mobile'), 63 $prefs->tool_mobile_autologin_request_last->description); 64 } 65 66 /** 67 * Test getting the context for the user ID related to this plugin. 68 */ 69 public function test_get_contexts_for_userid() { 70 // Create user and Mobile user keys. 71 $user = $this->getDataGenerator()->create_user(); 72 $context = \context_user::instance($user->id); 73 $key = get_user_key('tool_mobile', $user->id); 74 $contextlist = provider::get_contexts_for_userid($user->id); 75 $this->assertEquals($context->id, $contextlist->current()->id); 76 } 77 78 /** 79 * Test getting the users for a context related to this plugin. 80 */ 81 public function test_get_users_in_context() { 82 $component = 'tool_mobile'; 83 84 // Create users and Mobile user keys. 85 $user1 = $this->getDataGenerator()->create_user(); 86 $user2 = $this->getDataGenerator()->create_user(); 87 $context1 = \context_user::instance($user1->id); 88 $context2 = \context_user::instance($user2->id); 89 $key1 = get_user_key('tool_mobile', $user1->id); 90 $key2 = get_user_key('tool_mobile', $user2->id); 91 92 // Ensure only user1 is found in context1. 93 $userlist = new \core_privacy\local\request\userlist($context1, $component); 94 provider::get_users_in_context($userlist); 95 $userids = $userlist->get_userids(); 96 $userid = reset($userids); 97 98 $this->assertCount(1, $userids); 99 $this->assertEquals($user1->id, $userid); 100 } 101 102 /** 103 * Test that data is exported correctly for this plugin. 104 */ 105 public function test_export_user_data() { 106 global $DB; 107 // Create user and Mobile user keys. 108 $user = $this->getDataGenerator()->create_user(); 109 $context = \context_user::instance($user->id); 110 $keyvalue = get_user_key('tool_mobile', $user->id); 111 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]); 112 // Validate exported data. 113 $this->setUser($user); 114 $writer = writer::with_context($context); 115 $this->assertFalse($writer->has_any_data()); 116 $this->export_context_data_for_user($user->id, $context, 'tool_mobile'); 117 $userkeydata = $writer->get_related_data([], 'userkeys'); 118 $this->assertCount(1, $userkeydata->keys); 119 $this->assertEquals($key->script, reset($userkeydata->keys)->script); 120 } 121 122 /** 123 * Test for provider::delete_data_for_all_users_in_context(). 124 */ 125 public function test_delete_data_for_all_users_in_context() { 126 global $DB; 127 // Create user and Mobile user keys. 128 $user = $this->getDataGenerator()->create_user(); 129 $context = \context_user::instance($user->id); 130 $keyvalue = get_user_key('tool_mobile', $user->id); 131 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]); 132 // Before deletion, we should have 1 user_private_key. 133 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 134 $this->assertEquals(1, $count); 135 // Delete data. 136 provider::delete_data_for_all_users_in_context($context); 137 // After deletion, the user_private_key entries should have been deleted. 138 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 139 $this->assertEquals(0, $count); 140 } 141 142 /** 143 * Test for provider::delete_data_for_user(). 144 */ 145 public function test_delete_data_for_user() { 146 global $DB; 147 // Create user and Mobile user keys. 148 $user = $this->getDataGenerator()->create_user(); 149 $context = \context_user::instance($user->id); 150 $keyvalue = get_user_key('tool_mobile', $user->id); 151 $key = $DB->get_record('user_private_key', ['value' => $keyvalue]); 152 // Before deletion, we should have 1 user_private_key. 153 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 154 $this->assertEquals(1, $count); 155 // Delete data. 156 $contextlist = provider::get_contexts_for_userid($user->id); 157 $approvedcontextlist = new approved_contextlist($user, 'tool_mobile', $contextlist->get_contextids()); 158 provider::delete_data_for_user($approvedcontextlist); 159 // After deletion, the user_private_key entries should have been deleted. 160 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 161 $this->assertEquals(0, $count); 162 } 163 164 /** 165 * Test for provider::test_delete_data_for_users(). 166 */ 167 public function test_delete_data_for_users() { 168 global $DB; 169 $component = 'tool_mobile'; 170 171 // Create users and Mobile user keys. 172 $user1 = $this->getDataGenerator()->create_user(); 173 $user2 = $this->getDataGenerator()->create_user(); 174 $context1 = \context_user::instance($user1->id); 175 $context2 = \context_user::instance($user2->id); 176 $keyvalue1 = get_user_key('tool_mobile', $user1->id); 177 $keyvalue2 = get_user_key('tool_mobile', $user2->id); 178 $key1 = $DB->get_record('user_private_key', ['value' => $keyvalue1]); 179 180 // Before deletion, we should have 2 user_private_keys. 181 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 182 $this->assertEquals(2, $count); 183 184 // Ensure deleting wrong user in the user context does nothing. 185 $approveduserids = [$user2->id]; 186 $approvedlist = new approved_userlist($context1, $component, $approveduserids); 187 provider::delete_data_for_users($approvedlist); 188 189 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 190 $this->assertEquals(2, $count); 191 192 // Delete for user1 in context1. 193 $approveduserids = [$user1->id]; 194 $approvedlist = new approved_userlist($context1, $component, $approveduserids); 195 provider::delete_data_for_users($approvedlist); 196 197 // Ensure only user1's data is deleted, user2's remains. 198 $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']); 199 $this->assertEquals(1, $count); 200 201 $params = ['script' => $component]; 202 $userid = $DB->get_field_select('user_private_key', 'userid', 'script = :script', $params); 203 $this->assertEquals($user2->id, $userid); 204 } 205 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body