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