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