Proyectos de Subversion Moodle

Rev

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