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 namespace tool_dataprivacy\task; 18 19 use tool_dataprivacy\api; 20 21 defined('MOODLE_INTERNAL') || die(); 22 require_once (__DIR__ . '/../data_privacy_testcase.php'); 23 24 /** 25 * Tests for scheduled tasks. 26 * 27 * @package tool_dataprivacy 28 * @copyright 2018 Mihail Geshoski <mihail@moodle.com> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class task_test extends \data_privacy_testcase { 32 33 /** 34 * Test tearDown. 35 */ 36 public function tearDown(): void { 37 \core_privacy\local\request\writer::reset(); 38 } 39 40 /** 41 * Ensure that a delete data request for pre-existing deleted users 42 * is created when there are not any existing data requests 43 * for that particular user. 44 */ 45 public function test_delete_existing_deleted_users_task_no_previous_requests() { 46 global $DB; 47 48 $this->resetAfterTest(); 49 $this->setAdminUser(); 50 51 // Enable automatic creation of delete data requests. 52 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 53 54 // Create a user. 55 $user = $this->getDataGenerator()->create_user(); 56 // Mark the user as deleted. 57 $user->deleted = 1; 58 $DB->update_record('user', $user); 59 60 // The user should not have a delete data request. 61 $this->assertCount(0, api::get_data_requests($user->id, [], 62 [api::DATAREQUEST_TYPE_DELETE])); 63 64 $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users'); 65 // After running the scheduled task, the deleted user should have a delete data request. 66 $this->assertCount(1, api::get_data_requests($user->id, [], 67 [api::DATAREQUEST_TYPE_DELETE])); 68 } 69 70 /** 71 * Ensure that a delete data request for pre-existing deleted users 72 * is not being created when automatic creation of delete data requests is disabled. 73 */ 74 public function test_delete_existing_deleted_users_task_automatic_creation_disabled() { 75 global $DB; 76 77 $this->resetAfterTest(); 78 $this->setAdminUser(); 79 80 // Disable automatic creation of delete data requests. 81 set_config('automaticdeletionrequests', 0, 'tool_dataprivacy'); 82 83 // Create a user. 84 $user = $this->getDataGenerator()->create_user(); 85 // Mark the user as deleted. 86 $user->deleted = 1; 87 $DB->update_record('user', $user); 88 89 // The user should not have a delete data request. 90 $this->assertCount(0, api::get_data_requests($user->id, [], 91 [api::DATAREQUEST_TYPE_DELETE])); 92 93 $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users'); 94 // After running the scheduled task, the deleted user should still not have a delete data request. 95 $this->assertCount(0, api::get_data_requests($user->id, [], 96 [api::DATAREQUEST_TYPE_DELETE])); 97 } 98 99 /** 100 * Ensure that a delete data request for pre-existing deleted users 101 * is created when there are existing non-delete data requests 102 * for that particular user. 103 */ 104 public function test_delete_existing_deleted_users_task_existing_export_data_requests() { 105 global $DB; 106 107 $this->resetAfterTest(); 108 $this->setAdminUser(); 109 110 // Enable automatic creation of delete data requests. 111 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 112 113 // Create a user. 114 $user = $this->getDataGenerator()->create_user(); 115 // Create export data request for the user. 116 api::create_data_request($user->id, api::DATAREQUEST_TYPE_EXPORT); 117 // Mark the user as deleted. 118 $user->deleted = 1; 119 $DB->update_record('user', $user); 120 121 // The user should have a export data request. 122 $this->assertCount(1, api::get_data_requests($user->id, [], 123 [api::DATAREQUEST_TYPE_EXPORT])); 124 // The user should not have a delete data request. 125 $this->assertCount(0, api::get_data_requests($user->id, [], 126 [api::DATAREQUEST_TYPE_DELETE])); 127 128 $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users'); 129 // After running the scheduled task, the deleted user should have a delete data request. 130 $this->assertCount(1, api::get_data_requests($user->id, [], 131 [api::DATAREQUEST_TYPE_DELETE])); 132 } 133 134 /** 135 * Ensure that a delete data request for pre-existing deleted users 136 * is not created when there are existing ongoing delete data requests 137 * for that particular user. 138 */ 139 public function test_delete_existing_deleted_users_task_existing_ongoing_delete_data_requests() { 140 global $DB; 141 142 $this->resetAfterTest(); 143 $this->setAdminUser(); 144 145 // Enable automatic creation of delete data requests. 146 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 147 148 // Create a user. 149 $user = $this->getDataGenerator()->create_user(); 150 $this->setUser($user); 151 // Create delete data request for the user. 152 $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE); 153 $requestid = $datarequest->get('id'); 154 api::update_request_status($requestid, api::DATAREQUEST_STATUS_AWAITING_APPROVAL); 155 156 // The user should have an ongoing delete data request. 157 $this->assertCount(1, api::get_data_requests($user->id, 158 [api::DATAREQUEST_STATUS_AWAITING_APPROVAL], [api::DATAREQUEST_TYPE_DELETE])); 159 160 // Mark the user as deleted. 161 $user->deleted = 1; 162 $DB->update_record('user', $user); 163 164 // The user should still have the existing ongoing delete data request. 165 $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id, 166 [api::DATAREQUEST_STATUS_AWAITING_APPROVAL], [api::DATAREQUEST_TYPE_DELETE])); 167 168 $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users'); 169 // After running the scheduled task, the user should have only one delete data request. 170 $this->assertCount(1, api::get_data_requests($user->id, [], 171 [api::DATAREQUEST_TYPE_DELETE])); 172 } 173 174 /** 175 * Ensure that a delete data request for pre-existing deleted users 176 * is not created when there are existing finished delete data requests 177 * for that particular user. 178 */ 179 public function test_delete_existing_deleted_users_task_existing_finished_delete_data_requests() { 180 global $DB; 181 182 $this->resetAfterTest(); 183 $this->setAdminUser(); 184 185 // Enable automatic creation of delete data requests. 186 set_config('automaticdeletionrequests', 1, 'tool_dataprivacy'); 187 188 // Create a user. 189 $user = $this->getDataGenerator()->create_user(); 190 $this->setUser($user); 191 // Create delete data request for the user. 192 $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_DELETE); 193 $requestid = $datarequest->get('id'); 194 api::update_request_status($requestid, api::DATAREQUEST_STATUS_CANCELLED); 195 196 // The user should have a delete data request. 197 $this->assertCount(1, api::get_data_requests($user->id, [], 198 [api::DATAREQUEST_TYPE_DELETE])); 199 // The user should not have an ongoing data requests. 200 $this->assertFalse(api::has_ongoing_request($user->id, api::DATAREQUEST_TYPE_DELETE)); 201 202 // Mark the user as deleted. 203 $user->deleted = 1; 204 $DB->update_record('user', $user); 205 206 // The user should still have the existing cancelled delete data request. 207 $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id, 208 [api::DATAREQUEST_STATUS_CANCELLED], [api::DATAREQUEST_TYPE_DELETE])); 209 210 $this->execute_task('tool_dataprivacy\task\delete_existing_deleted_users'); 211 // After running the scheduled task, the user should still have one delete data requests. 212 $this->assertCount(1, api::get_data_requests($user->id, [], 213 [api::DATAREQUEST_TYPE_DELETE])); 214 // The user should only have the existing cancelled delete data request. 215 $this->assertCount(1, \tool_dataprivacy\api::get_data_requests($user->id, 216 [api::DATAREQUEST_STATUS_CANCELLED], [api::DATAREQUEST_TYPE_DELETE])); 217 } 218 219 /** 220 * Helper to execute a particular task. 221 * 222 * @param string $task The task. 223 */ 224 private function execute_task($task) { 225 // Run the scheduled task. 226 ob_start(); 227 $task = \core\task\manager::get_scheduled_task($task); 228 $task->execute(); 229 ob_end_clean(); 230 } 231 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body