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