MDL-61899 tool_dataprivacy: Subject access requests tool
[moodle.git] / admin / tool / dataprivacy / tests / expired_contexts_test.php
CommitLineData
5efc1f9e
DM
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 * Expired contexts tests.
19 *
20 * @package tool_dataprivacy
21 * @copyright 2018 David Monllao
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use tool_dataprivacy\api;
26use tool_dataprivacy\data_registry;
27use tool_dataprivacy\expired_context;
28
29defined('MOODLE_INTERNAL') || die();
30global $CFG;
31
32/**
33 * Expired contexts tests.
34 *
35 * @package tool_dataprivacy
36 * @copyright 2018 David Monllao
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class tool_dataprivacy_expired_contexts_testcase extends advanced_testcase {
40
41 /**
42 * setUp.
43 */
44 public function setUp() {
45 $this->resetAfterTest();
46 $this->setAdminUser();
47 }
48
49 /**
50 * Test expired users flagging and deletion.
51 *
52 * @return null
53 */
54 public function test_expired_users() {
55 global $DB;
56
57 $purpose = api::create_purpose((object)['name' => 'p1', 'retentionperiod' => 'PT1H']);
58 $cat = api::create_category((object)['name' => 'a']);
59
60 $record = (object)[
61 'purposeid' => $purpose->get('id'),
62 'categoryid' => $cat->get('id'),
63 'contextlevel' => CONTEXT_SYSTEM,
64 ];
65 api::set_contextlevel($record);
66 $record->contextlevel = CONTEXT_USER;
67 api::set_contextlevel($record);
68
69 $userdata = ['lastaccess' => '123'];
70 $user1 = $this->getDataGenerator()->create_user($userdata);
71 $user2 = $this->getDataGenerator()->create_user($userdata);
72 $user3 = $this->getDataGenerator()->create_user($userdata);
73 $user4 = $this->getDataGenerator()->create_user($userdata);
74 $user5 = $this->getDataGenerator()->create_user();
75
76 $course1 = $this->getDataGenerator()->create_course();
77 // Old course.
78 $course2 = $this->getDataGenerator()->create_course(['startdate' => '1', 'enddate' => '2']);
79 // Ongoing course.
80 $course3 = $this->getDataGenerator()->create_course(['startdate' => '1', 'enddate' => time()]);
81
82 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
83 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
84 $this->getDataGenerator()->enrol_user($user3->id, $course2->id, 'student');
85 $this->getDataGenerator()->enrol_user($user4->id, $course3->id, 'student');
86
87 // Users without lastaccess are skipped as well as users enroled in courses with no end date.
88 $expired = new \tool_dataprivacy\expired_user_contexts();
89 $numexpired = $expired->flag_expired();
90 $this->assertEquals(2, $numexpired);
91 $this->assertEquals(2, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_EXPIRED]));
92
93 // Approve user1 to be deleted.
94 $user2ctx = \context_user::instance($user2->id);
95 $expiredctx = expired_context::get_record(['contextid' => $user2ctx->id]);
96 api::set_expired_context_status($expiredctx, expired_context::STATUS_APPROVED);
97 $this->assertEquals(1, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_APPROVED]));
98
99 // Delete expired contexts.
100 $deleted = $expired->delete();
101 $this->assertEquals(1, $deleted);
102 $this->assertEquals(1, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_EXPIRED]));
103 $this->assertEquals(1, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_CLEANED]));
104
105 // No new records are generated.
106 $numexpired = $expired->flag_expired();
107 $this->assertEquals(0, $numexpired);
108 $this->assertEquals(2, $DB->count_records('tool_dataprivacy_ctxexpired'));
109 $deleted = $expired->delete();
110 $this->assertEquals(0, $deleted);
111 }
112
113 /**
114 * Test expired course and course stuff flagging and deletion.
115 *
116 * @return null
117 */
118 public function test_expired_course_related_contexts() {
119 global $DB;
120
121 $purpose1 = api::create_purpose((object)['name' => 'p1', 'retentionperiod' => 'PT1H']);
122 $purpose2 = api::create_purpose((object)['name' => 'p1', 'retentionperiod' => 'P1000Y']);
123 $cat = api::create_category((object)['name' => 'a']);
124
125 $record = (object)[
126 'purposeid' => $purpose1->get('id'),
127 'categoryid' => $cat->get('id'),
128 'contextlevel' => CONTEXT_SYSTEM,
129 ];
130 api::set_contextlevel($record);
131
132 list($purposevar, $categoryvar) = data_registry::var_names_from_context(
133 \context_helper::get_class_for_level(CONTEXT_COURSE)
134 );
135 set_config($purposevar, $purpose1->get('id'), 'tool_dataprivacy');
136
137 // A lot more time for modules
138 list($purposevar, $categoryvar) = data_registry::var_names_from_context(
139 \context_helper::get_class_for_level(CONTEXT_MODULE)
140 );
141 set_config($purposevar, $purpose2->get('id'), 'tool_dataprivacy');
142
143 $course1 = $this->getDataGenerator()->create_course();
144
145 // Old course.
146 $course2 = $this->getDataGenerator()->create_course(['startdate' => '1', 'enddate' => '2']);
147 $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id));
148 $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id));
149
150 // We want to override this last module instance purpose so we can test that modules are also
151 // returned as expired.
152 $forum2ctx = \context_module::instance($forum2->cmid);
153 $record = (object)[
154 'purposeid' => $purpose1->get('id'),
155 'categoryid' => $cat->get('id'),
156 'contextid' => $forum2ctx->id,
157 ];
158 api::set_context_instance($record);
159
160 // Ongoing course.
161 $course3 = $this->getDataGenerator()->create_course(['startdate' => '1', 'enddate' => time()]);
162 $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course3->id));
163
164 $expired = new \tool_dataprivacy\expired_course_related_contexts();
165 $numexpired = $expired->flag_expired();
166
167 // Only 1 module has expired.
168 $this->assertEquals(1, $numexpired);
169 $this->assertEquals(1, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_EXPIRED]));
170
171 // Add a forum1 override to 1h retention period so both forum1 and course2 are also expired.
172 $forum1ctx = \context_module::instance($forum1->cmid);
173 $record->purposeid = $purpose1->get('id');
174 $record->contextid = $forum1ctx->id;
175 api::set_context_instance($record);
176 $numexpired = $expired->flag_expired();
177 $this->assertEquals(2, $numexpired);
178 $this->assertEquals(3, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_EXPIRED]));
179
180 // Approve forum1 to be deleted.
181 $expiredctx = expired_context::get_record(['contextid' => $forum1ctx->id]);
182 api::set_expired_context_status($expiredctx, expired_context::STATUS_APPROVED);
183
184 // Delete expired contexts.
185 $deleted = $expired->delete();
186 $this->assertEquals(1, $deleted);
187 $this->assertEquals(1, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_CLEANED]));
188
189 $expiredctx = expired_context::get_record(['contextid' => $forum2ctx->id]);
190 api::set_expired_context_status($expiredctx, expired_context::STATUS_APPROVED);
191
192 $course2ctx = \context_course::instance($course2->id);
193 $expiredctx = expired_context::get_record(['contextid' => $course2ctx->id]);
194 api::set_expired_context_status($expiredctx, expired_context::STATUS_APPROVED);
195
196 // Delete expired contexts.
197 $deleted = $expired->delete();
198 $this->assertEquals(2, $deleted);
199 $this->assertEquals(3, $DB->count_records('tool_dataprivacy_ctxexpired', ['status' => expired_context::STATUS_CLEANED]));
200
201 // No new records are generated.
202 $numexpired = $expired->flag_expired();
203 $this->assertEquals(0, $numexpired);
204 $this->assertEquals(3, $DB->count_records('tool_dataprivacy_ctxexpired'));
205 $deleted = $expired->delete();
206 $this->assertEquals(0, $deleted);
207
208 }
209}