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 * Data provider tests. 19 * 20 * @package tool_messageinbound 21 * @category test 22 * @copyright 2018 Frédéric Massart 23 * @author Frédéric Massart <fred@branchup.tech> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 namespace tool_messageinbound\privacy; 27 28 defined('MOODLE_INTERNAL') || die(); 29 global $CFG; 30 31 use core_privacy\tests\provider_testcase; 32 use core_privacy\local\request\approved_contextlist; 33 use core_privacy\local\request\approved_userlist; 34 use core_privacy\local\request\transform; 35 use core_privacy\local\request\userlist; 36 use core_privacy\local\request\writer; 37 use tool_messageinbound\privacy\provider; 38 39 /** 40 * Data provider testcase class. 41 * 42 * @package tool_messageinbound 43 * @category test 44 * @copyright 2018 Frédéric Massart 45 * @author Frédéric Massart <fred@branchup.tech> 46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 47 */ 48 class provider_test extends provider_testcase { 49 50 public function setUp(): void { 51 global $CFG; 52 $this->resetAfterTest(); 53 54 // Pretend the system is enabled. 55 $CFG->messageinbound_enabled = true; 56 $CFG->messageinbound_mailbox = 'mailbox'; 57 $CFG->messageinbound_domain = 'example.com'; 58 } 59 60 public function test_get_contexts_for_userid() { 61 $dg = $this->getDataGenerator(); 62 $u1 = $dg->create_user(); 63 $u2 = $dg->create_user(); 64 $u1ctx = \context_user::instance($u1->id); 65 $u2ctx = \context_user::instance($u2->id); 66 67 $contexts = provider::get_contexts_for_userid($u1->id)->get_contexts(); 68 $this->assertCount(1, $contexts); 69 $this->assertEquals($u1ctx->id, $contexts[0]->id); 70 71 $contexts = provider::get_contexts_for_userid($u2->id)->get_contexts(); 72 $this->assertCount(1, $contexts); 73 $this->assertEquals($u2ctx->id, $contexts[0]->id); 74 } 75 76 /** 77 * Test for provider::test_get_users_in_context(). 78 */ 79 public function test_get_users_in_context() { 80 $component = 'tool_messageinbound'; 81 $dg = $this->getDataGenerator(); 82 $u1 = $dg->create_user(); 83 $u2 = $dg->create_user(); 84 $u3 = $dg->create_user(); 85 $u1ctx = \context_user::instance($u1->id); 86 $u2ctx = \context_user::instance($u2->id); 87 $u3ctx = \context_user::instance($u3->id); 88 89 $addressmanager = new \core\message\inbound\address_manager(); 90 $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler'); 91 $addressmanager->set_data(123); 92 93 // Create a user key for user 1. 94 $addressmanager->generate($u1->id); 95 96 // Create a messagelist for user 2. 97 $this->create_messagelist(['userid' => $u2->id, 'address' => 'u2@example1.com']); 98 99 $userlist1 = new userlist($u1ctx, $component); 100 provider::get_users_in_context($userlist1); 101 $userlist2 = new userlist($u2ctx, $component); 102 provider::get_users_in_context($userlist2); 103 $userlist3 = new userlist($u3ctx, $component); 104 provider::get_users_in_context($userlist3); 105 106 // Ensure user 1 is found from userkey. 107 $userids = $userlist1->get_userids(); 108 $this->assertCount(1, $userids); 109 $this->assertEquals($u1->id, $userids[0]); 110 111 // Ensure user 2 is found from messagelist. 112 $userids = $userlist2->get_userids(); 113 $this->assertCount(1, $userids); 114 $this->assertEquals($u2->id, $userids[0]); 115 116 // User 3 has neither, so should not be found. 117 $userids = $userlist3->get_userids(); 118 $this->assertCount(0, $userids); 119 } 120 121 public function test_delete_data_for_user() { 122 global $DB; 123 $dg = $this->getDataGenerator(); 124 $u1 = $dg->create_user(); 125 $u2 = $dg->create_user(); 126 $u1ctx = \context_user::instance($u1->id); 127 $u2ctx = \context_user::instance($u2->id); 128 129 $addressmanager = new \core\message\inbound\address_manager(); 130 $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler'); 131 $addressmanager->set_data(123); 132 133 // Create a user key for both users. 134 $addressmanager->generate($u1->id); 135 $addressmanager->generate($u2->id); 136 137 // Create a messagelist for both users. 138 $this->create_messagelist(['userid' => $u1->id]); 139 $this->create_messagelist(['userid' => $u2->id]); 140 141 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 142 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 143 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 144 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 145 146 // Passing another user's context does not do anything. 147 provider::delete_data_for_user(new approved_contextlist($u1, 'tool_messageinbound', [$u2ctx->id])); 148 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 149 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 150 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 151 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 152 153 // Deleting user 1. 154 provider::delete_data_for_user(new approved_contextlist($u1, 'tool_messageinbound', [$u1ctx->id])); 155 $this->assertFalse($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 156 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 157 $this->assertFalse($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 158 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 159 } 160 161 /** 162 * Test for provider::test_delete_data_for_users(). 163 */ 164 public function test_delete_data_for_users() { 165 global $DB; 166 $component = 'tool_messageinbound'; 167 $dg = $this->getDataGenerator(); 168 $u1 = $dg->create_user(); 169 $u2 = $dg->create_user(); 170 $u1ctx = \context_user::instance($u1->id); 171 $u2ctx = \context_user::instance($u2->id); 172 173 $addressmanager = new \core\message\inbound\address_manager(); 174 $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler'); 175 $addressmanager->set_data(123); 176 177 // Create a user key for both users. 178 $addressmanager->generate($u1->id); 179 $addressmanager->generate($u2->id); 180 181 // Create a messagelist for both users. 182 $this->create_messagelist(['userid' => $u1->id]); 183 $this->create_messagelist(['userid' => $u2->id]); 184 185 // Ensure data exists for both users. 186 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 187 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 188 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 189 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 190 191 // Ensure passing another user's ID does not do anything. 192 $approveduserids = [$u2->id]; 193 $approvedlist = new approved_userlist($u1ctx, $component, $approveduserids); 194 provider::delete_data_for_users($approvedlist); 195 196 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 197 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 198 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 199 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 200 201 // Delete u1's data. 202 $approveduserids = [$u1->id]; 203 $approvedlist = new approved_userlist($u1ctx, $component, $approveduserids); 204 provider::delete_data_for_users($approvedlist); 205 206 // Confirm only u1's data is deleted. 207 $this->assertFalse($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 208 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 209 $this->assertFalse($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 210 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 211 } 212 213 public function test_delete_data_for_all_users_in_context() { 214 global $DB; 215 $dg = $this->getDataGenerator(); 216 $u1 = $dg->create_user(); 217 $u2 = $dg->create_user(); 218 $u1ctx = \context_user::instance($u1->id); 219 $u2ctx = \context_user::instance($u2->id); 220 221 $addressmanager = new \core\message\inbound\address_manager(); 222 $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler'); 223 $addressmanager->set_data(123); 224 225 // Create a user key for both users. 226 $addressmanager->generate($u1->id); 227 $addressmanager->generate($u2->id); 228 229 // Create a messagelist for both users. 230 $this->create_messagelist(['userid' => $u1->id]); 231 $this->create_messagelist(['userid' => $u2->id]); 232 233 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 234 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 235 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 236 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 237 238 // Deleting user 1. 239 provider::delete_data_for_all_users_in_context($u1ctx); 240 $this->assertFalse($DB->record_exists('user_private_key', ['userid' => $u1->id, 'script' => 'messageinbound_handler'])); 241 $this->assertTrue($DB->record_exists('user_private_key', ['userid' => $u2->id, 'script' => 'messageinbound_handler'])); 242 $this->assertFalse($DB->record_exists('messageinbound_messagelist', ['userid' => $u1->id])); 243 $this->assertTrue($DB->record_exists('messageinbound_messagelist', ['userid' => $u2->id])); 244 } 245 246 public function test_export_data_for_user() { 247 $dg = $this->getDataGenerator(); 248 $u1 = $dg->create_user(); 249 $u2 = $dg->create_user(); 250 $u1ctx = \context_user::instance($u1->id); 251 $u2ctx = \context_user::instance($u2->id); 252 253 $addressmanager = new \core\message\inbound\address_manager(); 254 $addressmanager->set_handler('\tool_messageinbound\message\inbound\invalid_recipient_handler'); 255 $addressmanager->set_data(123); 256 257 // Create a user key for both users. 258 $addressmanager->generate($u1->id); 259 $addressmanager->generate($u2->id); 260 261 // Create a messagelist for both users. 262 $this->create_messagelist(['userid' => $u1->id, 'address' => 'u1@example1.com']); 263 $this->create_messagelist(['userid' => $u1->id, 'address' => 'u1@example2.com']); 264 $this->create_messagelist(['userid' => $u2->id, 'address' => 'u2@example1.com']); 265 266 // Export for user. 267 $this->setUser($u1); 268 provider::export_user_data(new approved_contextlist($u1, 'tool_messageinbound', [$u1ctx->id, $u2ctx->id])); 269 $data = writer::with_context($u2ctx)->get_data([get_string('messageinbound', 'tool_messageinbound')]); 270 $this->assertEmpty($data); 271 $data = writer::with_context($u1ctx)->get_data([get_string('messageinbound', 'tool_messageinbound')]); 272 $this->assertCount(2, $data->messages_pending_validation); 273 $this->assertEquals('u1@example1.com', $data->messages_pending_validation[0]['received_at']); 274 $this->assertEquals('u1@example2.com', $data->messages_pending_validation[1]['received_at']); 275 276 $data = writer::with_context($u2ctx)->get_related_data([get_string('messageinbound', 'tool_messageinbound')], 'userkeys'); 277 $this->assertEmpty($data); 278 $data = writer::with_context($u1ctx)->get_related_data([get_string('messageinbound', 'tool_messageinbound')], 'userkeys'); 279 $this->assertCount(1, $data->keys); 280 $this->assertEquals('messageinbound_handler', $data->keys[0]->script); 281 } 282 283 /** 284 * Create a message to validate. 285 * 286 * @param array $params The params. 287 * @return stdClass 288 */ 289 protected function create_messagelist(array $params) { 290 global $DB, $USER; 291 $record = (object) array_merge([ 292 'messageid' => 'abc', 293 'userid' => $USER->id, 294 'address' => 'text@example.com', 295 'timecreated' => time(), 296 ], $params); 297 $record->id = $DB->insert_record('messageinbound_messagelist', $record); 298 return $record; 299 } 300 301 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body