MDL-62660 tool_dataprivacy: Add and update PHPUnit tests
[moodle.git] / admin / tool / dataprivacy / tests / expired_data_requests_test.php
blob00c6259cc9247e43c2830d9400fbe44b6db61616
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.
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/>.
17 /**
18 * Expired data requests tests.
20 * @package tool_dataprivacy
21 * @copyright 2018 Michael Hawkins
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use tool_dataprivacy\api;
26 use tool_dataprivacy\data_request;
28 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once('data_privacy_testcase.php');
33 /**
34 * Expired data requests tests.
36 * @package tool_dataprivacy
37 * @copyright 2018 Michael Hawkins
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class tool_dataprivacy_expired_data_requests_testcase extends data_privacy_testcase {
42 /**
43 * Test tearDown.
45 public function tearDown() {
46 \core_privacy\local\request\writer::reset();
49 /**
50 * Test finding and deleting expired data requests
52 public function test_data_request_expiry() {
53 global $DB;
54 $this->resetAfterTest();
55 \core_privacy\local\request\writer::setup_real_writer_instance();
57 // Set up test users.
58 $this->setAdminUser();
59 $studentuser = $this->getDataGenerator()->create_user();
60 $studentusercontext = context_user::instance($studentuser->id);
62 $dpouser = $this->getDataGenerator()->create_user();
63 $this->assign_site_dpo($dpouser);
65 // Set request expiry to 5 minutes.
66 set_config('privacyrequestexpiry', 300, 'tool_dataprivacy');
68 // Create and approve data request.
69 $this->setUser($studentuser->id);
70 $datarequest = api::create_data_request($studentuser->id, api::DATAREQUEST_TYPE_EXPORT);
71 $this->setAdminUser();
72 ob_start();
73 $this->runAdhocTasks('\tool_dataprivacy\task\initiate_data_request_task');
74 $requestid = $datarequest->get('id');
75 $this->setUser($dpouser->id);
76 api::approve_data_request($requestid);
77 $this->setAdminUser();
78 $this->runAdhocTasks('\tool_dataprivacy\task\process_data_request_task');
79 ob_end_clean();
81 // Confirm approved and exported.
82 $request = new data_request($requestid);
83 $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
84 $fileconditions = array(
85 'userid' => $studentuser->id,
86 'component' => 'tool_dataprivacy',
87 'filearea' => 'export',
88 'itemid' => $requestid,
89 'contextid' => $studentusercontext->id,
91 $this->assertEquals(2, $DB->count_records('files', $fileconditions));
93 // Run expiry deletion - should not affect test export.
94 $expiredrequests = data_request::get_expired_requests();
95 $this->assertEquals(0, count($expiredrequests));
96 data_request::expire($expiredrequests);
98 // Confirm test export was not deleted.
99 $request = new data_request($requestid);
100 $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
101 $this->assertEquals(2, $DB->count_records('files', $fileconditions));
103 // Change request expiry to 1 second and allow it to elapse.
104 set_config('privacyrequestexpiry', 1, 'tool_dataprivacy');
105 $this->waitForSecond();
107 // Re-run expiry deletion, confirm the request expires and export is deleted.
108 $expiredrequests = data_request::get_expired_requests();
109 $this->assertEquals(1, count($expiredrequests));
110 data_request::expire($expiredrequests);
112 $request = new data_request($requestid);
113 $this->assertEquals(api::DATAREQUEST_STATUS_EXPIRED, $request->get('status'));
114 $this->assertEquals(0, $DB->count_records('files', $fileconditions));
119 * Test for \tool_dataprivacy\data_request::is_expired()
120 * Tests for the expected request status to protect from false positive/negative,
121 * then tests is_expired() is returning the expected response.
123 public function test_is_expired() {
124 $this->resetAfterTest();
125 \core_privacy\local\request\writer::setup_real_writer_instance();
127 // Set request expiry beyond this test.
128 set_config('privacyrequestexpiry', 20, 'tool_dataprivacy');
130 $admin = get_admin();
131 $this->setAdminUser();
133 // Create export request.
134 $datarequest = api::create_data_request($admin->id, api::DATAREQUEST_TYPE_EXPORT);
135 $requestid = $datarequest->get('id');
137 // Approve the request.
138 ob_start();
139 $this->runAdhocTasks('\tool_dataprivacy\task\initiate_data_request_task');
140 $this->setAdminUser();
141 api::approve_data_request($requestid);
142 $this->runAdhocTasks('\tool_dataprivacy\task\process_data_request_task');
143 ob_end_clean();
145 // Test Download ready (not expired) response.
146 $request = new data_request($requestid);
147 $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
148 $result = data_request::is_expired($request);
149 $this->assertFalse($result);
151 // Let request expiry time lapse.
152 set_config('privacyrequestexpiry', 1, 'tool_dataprivacy');
153 $this->waitForSecond();
155 // Test Download ready (time expired) response.
156 $request = new data_request($requestid);
157 $this->assertEquals(api::DATAREQUEST_STATUS_DOWNLOAD_READY, $request->get('status'));
158 $result = data_request::is_expired($request);
159 $this->assertTrue($result);
161 // Run the expiry task to properly expire the request.
162 ob_start();
163 $task = \core\task\manager::get_scheduled_task('\tool_dataprivacy\task\delete_expired_requests');
164 $task->execute();
165 ob_end_clean();
167 // Test Expired response status response.
168 $request = new data_request($requestid);
169 $this->assertEquals(api::DATAREQUEST_STATUS_EXPIRED, $request->get('status'));
170 $result = data_request::is_expired($request);
171 $this->assertTrue($result);