Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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 /** 18 * Tests for the event observer. 19 * 20 * @package tool_dataprivacy 21 * @copyright 2018 Mihail Geshoski <mihail@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use \tool_dataprivacy\event\user_deleted_observer; 28 use \tool_dataprivacy\api; 29 30 /** 31 * Event observer test. 32 * 33 * @package tool_dataprivacy 34 * @copyright 2018 Mihail Geshoski <mihail@moodle.com> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class tool_dataprivacy_user_deleted_observer_testcase extends advanced_testcase { 38 39 /** 40 * Ensure that a delete data request is created upon user deletion. 41 */ 42 public function test_create_delete_data_request() { 43 $this->resetAfterTest(); 44 45 // Enable automatic creation of delete data requests. 46 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 47 48 // Create another user who is not a DPO. 49 $user = $this->getDataGenerator()->create_user(); 50 51 $event = $this->trigger_delete_user_event($user); 52 53 user_deleted_observer::create_delete_data_request($event); 54 // Validate that delete data request has been created. 55 $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 56 } 57 58 /** 59 * Ensure that a delete data request is not created upon user deletion if automatic creation of 60 * delete data requests is disabled. 61 */ 62 public function test_create_delete_data_request_automatic_creation_disabled() { 63 $this->resetAfterTest(); 64 65 // Disable automatic creation of delete data requests. 66 set_config('automaticdeletionrequests', 0, 'tool_dataprivacy'); 67 68 // Create another user who is not a DPO. 69 $user = $this->getDataGenerator()->create_user(); 70 71 $event = $this->trigger_delete_user_event($user); 72 73 user_deleted_observer::create_delete_data_request($event); 74 // Validate that delete data request has been created. 75 $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 76 } 77 78 /** 79 * Ensure that a delete data request is being created upon user deletion 80 * if an ongoing export data request (or any other except delete data request) for that user already exists. 81 */ 82 public function test_create_delete_data_request_export_data_request_preexists() { 83 $this->resetAfterTest(); 84 $this->setAdminUser(); 85 86 // Enable automatic creation of delete data requests. 87 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 88 89 // Create another user who is not a DPO. 90 $user = $this->getDataGenerator()->create_user(); 91 // Create a delete data request for $user. 92 api::create_data_request($user->id, api::DATAREQUEST_TYPE_EXPORT); 93 // Validate that delete data request has been created. 94 $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_EXPORT)); 95 $this->assertEquals(0, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 96 97 $event = $this->trigger_delete_user_event($user); 98 99 user_deleted_observer::create_delete_data_request($event); 100 // Validate that delete data request has been created. 101 $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 102 } 103 104 /** 105 * Ensure that a delete data request is not being created upon user deletion 106 * if an ongoing delete data request for that user already exists. 107 */ 108 public function test_create_delete_data_request_ongoing_delete_data_request_preexists() { 109 $this->resetAfterTest(); 110 $this->setAdminUser(); 111 112 // Enable automatic creation of delete data requests. 113 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 114 115 // Create another user who is not a DPO. 116 $user = $this->getDataGenerator()->create_user(); 117 // Create a delete data request for $user. 118 api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE); 119 // Validate that delete data request has been created. 120 $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 121 122 $event = $this->trigger_delete_user_event($user); 123 124 user_deleted_observer::create_delete_data_request($event); 125 // Validate that additional delete data request has not been created. 126 $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 127 } 128 129 /** 130 * Ensure that a delete data request is being created upon user deletion 131 * if a finished delete data request (excluding complete) for that user already exists. 132 */ 133 public function test_create_delete_data_request_canceled_delete_data_request_preexists() { 134 $this->resetAfterTest(); 135 $this->setAdminUser(); 136 137 // Enable automatic creation of delete data requests. 138 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 139 140 // Create another user who is not a DPO. 141 $user = $this->getDataGenerator()->create_user(); 142 // Create a delete data request for $user. 143 $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE); 144 $requestid = $datarequest->get('id'); 145 api::update_request_status($requestid, api::DATAREQUEST_STATUS_CANCELLED); 146 147 // Validate that delete data request has been created and the status has been updated to 'Canceled'. 148 $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 149 $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 150 151 $event = $this->trigger_delete_user_event($user); 152 153 user_deleted_observer::create_delete_data_request($event); 154 // Validate that additional delete data request has been created. 155 $this->assertEquals(2, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 156 $this->assertTrue(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 157 } 158 159 /** 160 * Ensure that a delete data request is being created upon user deletion 161 * if a completed delete data request for that user already exists. 162 */ 163 public function test_create_delete_data_request_completed_delete_data_request_preexists() { 164 $this->resetAfterTest(); 165 $this->setAdminUser(); 166 167 // Enable automatic creation of delete data requests. 168 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 169 170 // Create another user who is not a DPO. 171 $user = $this->getDataGenerator()->create_user(); 172 // Create a delete data request for $user. 173 $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE); 174 $requestid = $datarequest->get('id'); 175 api::update_request_status($requestid, api::DATAREQUEST_STATUS_COMPLETE); 176 177 // Validate that delete data request has been created and the status has been updated to 'Completed'. 178 $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 179 $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 180 181 $event = $this->trigger_delete_user_event($user); 182 183 user_deleted_observer::create_delete_data_request($event); 184 // Validate that additional delete data request has not been created. 185 $this->assertEquals(1, api::get_data_requests_count($user->id, [], [api::DATAREQUEST_TYPE_DELETE])); 186 $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 187 } 188 189 /** 190 * Helper to trigger and capture the delete user event. 191 * 192 * @param object $user The user object. 193 * @return \core\event\user_deleted $event The returned event. 194 */ 195 private function trigger_delete_user_event($user) { 196 197 $sink = $this->redirectEvents(); 198 delete_user($user); 199 $events = $sink->get_events(); 200 $sink->close(); 201 $event = reset($events); 202 // Validate event data. 203 $this->assertInstanceOf('\core\event\user_deleted', $event); 204 205 return $event; 206 } 207 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body