Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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;
18
 
19
use data_privacy_testcase;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
require_once('data_privacy_testcase.php');
24
 
25
/**
26
 * Expired data requests tests.
27
 *
28
 * @package    tool_dataprivacy
11 efrain 29
 * @covers     \tool_dataprivacy\data_request
1 efrain 30
 * @copyright  2018 Michael Hawkins
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
11 efrain 33
final class expired_data_requests_test extends data_privacy_testcase {
1 efrain 34
 
35
    /**
36
     * Test tearDown.
37
     */
38
    public function tearDown(): void {
39
        \core_privacy\local\request\writer::reset();
1441 ariadna 40
        parent::tearDown();
1 efrain 41
    }
42
 
43
    /**
44
     * Test finding and deleting expired data requests
45
     */
11 efrain 46
    public function test_data_request_expiry(): void {
1 efrain 47
        global $DB;
48
        $this->resetAfterTest();
49
        \core_privacy\local\request\writer::setup_real_writer_instance();
50
 
51
        // Set up test users.
52
        $this->setAdminUser();
53
        $studentuser = $this->getDataGenerator()->create_user();
54
        $studentusercontext = \context_user::instance($studentuser->id);
55
 
56
        $dpouser = $this->getDataGenerator()->create_user();
57
        $this->assign_site_dpo($dpouser);
58
 
59
        // Set site purpose.
60
        $this->create_system_purpose();
61
 
62
        // Set request expiry to 5 minutes.
63
        set_config('privacyrequestexpiry', 300, 'tool_dataprivacy');
64
 
65
        // Create and approve data request.
66
        $this->setUser($studentuser->id);
67
        $datarequest = api::create_data_request($studentuser->id, api::DATAREQUEST_TYPE_EXPORT);
68
        $requestid = $datarequest->get('id');
69
        $this->setAdminUser();
70
        api::approve_data_request($requestid);
71
        $this->setUser();
72
        ob_start();
73
        $this->runAdhocTasks('\tool_dataprivacy\task\process_data_request_task');
74
        ob_end_clean();
75
 
76
        // Confirm approved and exported.
77
        $request = new data_request($requestid);
78
        $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
79
        $fileconditions = array(
80
            'userid' => $studentuser->id,
81
            'component' => 'tool_dataprivacy',
82
            'filearea' => 'export',
83
            'itemid' => $requestid,
84
            'contextid' => $studentusercontext->id,
85
        );
86
        $this->assertEquals(2, $DB->count_records('files', $fileconditions));
87
 
88
        // Run expiry deletion - should not affect test export.
89
        $expiredrequests = data_request::get_expired_requests();
90
        $this->assertEquals(0, count($expiredrequests));
91
        data_request::expire($expiredrequests);
92
 
93
        // Confirm test export was not deleted.
94
        $request = new data_request($requestid);
95
        $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
96
        $this->assertEquals(2, $DB->count_records('files', $fileconditions));
97
 
98
        // Change request expiry to 1 second and allow it to elapse.
99
        set_config('privacyrequestexpiry', 1, 'tool_dataprivacy');
100
        $this->waitForSecond();
101
 
102
        // Re-run expiry deletion, confirm the request expires and export is deleted.
103
        $expiredrequests = data_request::get_expired_requests();
104
        $this->assertEquals(1, count($expiredrequests));
105
        data_request::expire($expiredrequests);
106
 
107
        $request = new data_request($requestid);
108
        $this->assertEquals(api::DATAREQUEST_STATUS_EXPIRED, $request->get('status'));
109
        $this->assertEquals(0, $DB->count_records('files', $fileconditions));
110
    }
111
 
112
    /**
11 efrain 113
     * Test that data requests are not expired when expiration is disabled (set to zero)
114
     */
115
    public function test_data_request_expiry_never(): void {
116
        global $DB;
117
 
118
        $this->resetAfterTest();
119
 
120
        \core_privacy\local\request\writer::setup_real_writer_instance();
121
 
122
        // Disable request expiry.
123
        set_config('privacyrequestexpiry', 0, 'tool_dataprivacy');
124
 
125
        // Create and approve data request.
126
        $user = $this->getDataGenerator()->create_user();
127
        $usercontext = \context_user::instance($user->id);
128
 
129
        $this->setUser($user->id);
130
        $datarequest = api::create_data_request($user->id, api::DATAREQUEST_TYPE_EXPORT);
131
        $requestid = $datarequest->get('id');
132
 
133
        $this->setAdminUser();
134
        api::approve_data_request($requestid);
135
 
136
        ob_start();
137
        $this->runAdhocTasks('\tool_dataprivacy\task\process_data_request_task');
138
        ob_end_clean();
139
 
140
        // Run expiry deletion - should not affect test export.
141
        $expiredrequests = data_request::get_expired_requests();
142
        $this->assertEmpty($expiredrequests);
143
        data_request::expire($expiredrequests);
144
 
145
        // Confirm approved and exported.
146
        $request = new data_request($requestid);
147
        $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
148
        $fileconditions = [
149
            'userid' => $user->id,
150
            'component' => 'tool_dataprivacy',
151
            'filearea' => 'export',
152
            'itemid' => $requestid,
153
            'contextid' => $usercontext->id,
154
        ];
155
        $this->assertEquals(2, $DB->count_records('files', $fileconditions));
156
    }
157
 
158
    /**
1 efrain 159
     * Test for \tool_dataprivacy\data_request::is_expired()
160
     * Tests for the expected request status to protect from false positive/negative,
161
     * then tests is_expired() is returning the expected response.
162
     */
11 efrain 163
    public function test_is_expired(): void {
1 efrain 164
        $this->resetAfterTest();
165
        \core_privacy\local\request\writer::setup_real_writer_instance();
166
 
167
        // Set request expiry beyond this test.
168
        set_config('privacyrequestexpiry', 20, 'tool_dataprivacy');
169
 
170
        $admin = get_admin();
171
        $this->setAdminUser();
172
 
173
        // Set site purpose.
174
        $this->create_system_purpose();
175
 
176
        // Create export request.
177
        $datarequest = api::create_data_request($admin->id, api::DATAREQUEST_TYPE_EXPORT);
178
        $requestid = $datarequest->get('id');
179
 
180
        // Approve the request.
181
        ob_start();
182
        $this->setAdminUser();
183
        api::approve_data_request($requestid);
184
        $this->runAdhocTasks('\tool_dataprivacy\task\process_data_request_task');
185
        ob_end_clean();
186
 
187
        // Test Download ready (not expired) response.
188
        $request = new data_request($requestid);
189
        $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
190
        $result = data_request::is_expired($request);
191
        $this->assertFalse($result);
192
 
193
        // Let request expiry time lapse.
194
        set_config('privacyrequestexpiry', 1, 'tool_dataprivacy');
195
        $this->waitForSecond();
196
 
197
        // Test Download ready (time expired) response.
198
        $request = new data_request($requestid);
199
        $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
200
        $result = data_request::is_expired($request);
201
        $this->assertTrue($result);
202
 
203
        // Run the expiry task to properly expire the request.
204
        ob_start();
205
        $task = \core\task\manager::get_scheduled_task('\tool_dataprivacy\task\delete_expired_requests');
206
        $task->execute();
207
        ob_end_clean();
208
 
209
        // Test Expired response status response.
210
        $request = new data_request($requestid);
211
        $this->assertEquals(api::DATAREQUEST_STATUS_EXPIRED, $request->get('status'));
212
        $result = data_request::is_expired($request);
213
        $this->assertTrue($result);
214
    }
215
 
216
    /**
217
     * Create a site (system context) purpose and category.
218
     *
219
     * @return  void
220
     */
221
    protected function create_system_purpose() {
222
        $purpose = new purpose(0, (object) [
223
            'name' => 'Test purpose ' . rand(1, 1000),
224
            'retentionperiod' => 'P1D',
225
            'lawfulbases' => 'gdpr_art_6_1_a',
226
        ]);
227
        $purpose->create();
228
 
229
        $cat = new category(0, (object) ['name' => 'Test category']);
230
        $cat->create();
231
 
232
        $record = (object) [
233
            'purposeid'     => $purpose->get('id'),
234
            'categoryid'    => $cat->get('id'),
235
            'contextlevel'  => CONTEXT_SYSTEM,
236
        ];
237
        api::set_contextlevel($record);
238
    }
239
}