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 |
/**
|
|
|
18 |
* Unit tests for the core_notes implementation of the privacy API.
|
|
|
19 |
*
|
|
|
20 |
* @package core_notes
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2018 Zig Tan <zig@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
namespace core_notes\privacy;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
global $CFG;
|
|
|
29 |
|
|
|
30 |
require_once($CFG->dirroot . "/notes/lib.php");
|
|
|
31 |
|
|
|
32 |
use core_notes\privacy\provider;
|
|
|
33 |
use core_privacy\local\request\writer;
|
|
|
34 |
use core_privacy\local\request\approved_contextlist;
|
|
|
35 |
use core_privacy\local\request\approved_userlist;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Unit tests for the core_notes implementation of the privacy API.
|
|
|
39 |
*
|
|
|
40 |
* @copyright 2018 Zig Tan <zig@moodle.com>
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
* @covers \core_notes\privacy\provider
|
|
|
43 |
*/
|
|
|
44 |
class provider_test extends \core_privacy\tests\provider_testcase {
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Test for provider::get_contexts_for_userid().
|
|
|
48 |
*/
|
11 |
efrain |
49 |
public function test_get_contexts_for_userid(): void {
|
1 |
efrain |
50 |
global $DB;
|
|
|
51 |
|
|
|
52 |
// Test setup.
|
|
|
53 |
$this->resetAfterTest(true);
|
|
|
54 |
$this->setAdminUser();
|
|
|
55 |
set_config('enablenotes', true);
|
|
|
56 |
|
|
|
57 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
58 |
$this->setUser($teacher1);
|
|
|
59 |
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
|
|
60 |
|
|
|
61 |
$student = $this->getDataGenerator()->create_user();
|
|
|
62 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
63 |
|
|
|
64 |
// Create Courses, then enrol a teacher and a student.
|
|
|
65 |
$nocourses = 5;
|
|
|
66 |
$courses = [];
|
|
|
67 |
$coursecontextids = [];
|
|
|
68 |
for ($c = 1; $c <= $nocourses; $c++) {
|
|
|
69 |
$course = $this->getDataGenerator()->create_course();
|
|
|
70 |
$coursecontext = \context_course::instance($course->id);
|
|
|
71 |
|
|
|
72 |
role_assign($teacherrole->id, $teacher1->id, $coursecontext->id);
|
|
|
73 |
role_assign($studentrole->id, $student->id, $coursecontext->id);
|
|
|
74 |
|
|
|
75 |
// Only create private user notes (i.e. NOTES_STATE_DRAFT) for student in Course 1, 2, 3 written by the teacher.
|
|
|
76 |
if ($c <= 3) {
|
|
|
77 |
$this->help_create_user_note(
|
|
|
78 |
$student->id,
|
|
|
79 |
NOTES_STATE_DRAFT,
|
|
|
80 |
$course->id,
|
|
|
81 |
"Test private user note about the student in Course $c by the teacher"
|
|
|
82 |
);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
$courses[$c] = $course;
|
|
|
86 |
$coursecontextids[] = $coursecontext->id;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
// Test Teacher 1's contexts equals 3 because only 3 user notes were added for Course 1, 2, and 3.
|
|
|
90 |
// Course 4 and 5 does not have any notes associated with it, so the contexts should not be returned.
|
|
|
91 |
$contexts = provider::get_contexts_for_userid($teacher1->id);
|
|
|
92 |
$this->assertCount(3, $contexts->get_contextids());
|
|
|
93 |
|
|
|
94 |
// Test the Student's contexts is 0 because the notes written by the teacher are private.
|
|
|
95 |
$contexts = provider::get_contexts_for_userid($student->id);
|
|
|
96 |
$this->assertCount(0, $contexts->get_contextids());
|
|
|
97 |
|
|
|
98 |
// Add a public user note (i.e. NOTES_STATE_PUBLIC) written by the Teacher about the Student in Course 4.
|
|
|
99 |
$course = $courses[4];
|
|
|
100 |
$this->help_create_user_note(
|
|
|
101 |
$student->id,
|
|
|
102 |
NOTES_STATE_PUBLIC,
|
|
|
103 |
$course->id,
|
|
|
104 |
"Test public user note about the student in Course 4 by the teacher"
|
|
|
105 |
);
|
|
|
106 |
|
|
|
107 |
// Test Teacher 1's contexts equals 4 after adding a public note about a student in Course 4.
|
|
|
108 |
$contexts = provider::get_contexts_for_userid($teacher1->id);
|
|
|
109 |
$this->assertCount(4, $contexts->get_contextids());
|
|
|
110 |
|
|
|
111 |
// Test the Student's contexts is 1 for Course 4 because there is a public note written by the teacher.
|
|
|
112 |
$contexts = provider::get_contexts_for_userid($student->id);
|
|
|
113 |
$this->assertCount(1, $contexts->get_contextids());
|
|
|
114 |
|
|
|
115 |
// Add a site-wide user note (i.e. NOTES_STATE_SITE) written by the Teacher 1 about the Student in Course 3.
|
|
|
116 |
$course = $courses[3];
|
|
|
117 |
$this->help_create_user_note(
|
|
|
118 |
$student->id,
|
|
|
119 |
NOTES_STATE_SITE,
|
|
|
120 |
$course->id,
|
|
|
121 |
"Test site-wide user note about the student in Course 3 by the teacher"
|
|
|
122 |
);
|
|
|
123 |
|
|
|
124 |
// Test the Student's contexts is 2 for Courses 3, 4 because there is a public and site-wide note written by the Teacher.
|
|
|
125 |
$contexts = provider::get_contexts_for_userid($student->id);
|
|
|
126 |
$this->assertCount(2, $contexts->get_contextids());
|
|
|
127 |
|
|
|
128 |
// Add a site-wide user note for the Teacher 1 by another Teacher 2 in Course 5.
|
|
|
129 |
$teacher2 = $this->getDataGenerator()->create_user();
|
|
|
130 |
$this->setUser($teacher2);
|
|
|
131 |
|
|
|
132 |
$course = $courses[5];
|
|
|
133 |
$this->help_create_user_note(
|
|
|
134 |
$teacher1->id,
|
|
|
135 |
NOTES_STATE_SITE,
|
|
|
136 |
$course->id,
|
|
|
137 |
"Test site-wide user note about the teacher in Course 5 by another teacher"
|
|
|
138 |
);
|
|
|
139 |
|
|
|
140 |
// Test Teacher 1's contexts equals 5 after adding the note from another teacher.
|
|
|
141 |
$contextlist = provider::get_contexts_for_userid($teacher1->id);
|
|
|
142 |
$this->assertCount(5, $contextlist->get_contextids());
|
|
|
143 |
|
|
|
144 |
// Test Teacher 1's contexts match the contexts of the Courses associated with notes created.
|
|
|
145 |
$this->assertEmpty(array_diff($coursecontextids, $contextlist->get_contextids()));
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Test for provider::export_user_data().
|
|
|
150 |
*/
|
11 |
efrain |
151 |
public function test_export_user_data(): void {
|
1 |
efrain |
152 |
global $DB;
|
|
|
153 |
|
|
|
154 |
// Test setup.
|
|
|
155 |
$this->resetAfterTest(true);
|
|
|
156 |
$this->setAdminUser();
|
|
|
157 |
set_config('enablenotes', true);
|
|
|
158 |
|
|
|
159 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
160 |
$this->setUser($teacher1);
|
|
|
161 |
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
|
|
162 |
|
|
|
163 |
$nocourses = 5;
|
|
|
164 |
$nostudents = 2;
|
|
|
165 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
166 |
|
|
|
167 |
$courses = [];
|
|
|
168 |
$coursecontextids = [];
|
|
|
169 |
for ($c = 1; $c <= $nocourses; $c++) {
|
|
|
170 |
// Create a Course, then enrol a teacher and enrol 2 students.
|
|
|
171 |
$course = $this->getDataGenerator()->create_course();
|
|
|
172 |
$coursecontext = \context_course::instance($course->id);
|
|
|
173 |
|
|
|
174 |
role_assign($teacherrole->id, $teacher1->id, $coursecontext->id);
|
|
|
175 |
|
|
|
176 |
// Only create public user notes (i.e. NOTES_STATE_PUBLIC) for students in Course 1, 2, 3 written by the teacher.
|
|
|
177 |
if ($c <= 3) {
|
|
|
178 |
for ($s = 0; $s < $nostudents; $s++) {
|
|
|
179 |
$student = $this->getDataGenerator()->create_user();
|
|
|
180 |
role_assign($studentrole->id, $student->id, $coursecontext->id);
|
|
|
181 |
|
|
|
182 |
// Create test public user note data written for students by the teacher.
|
|
|
183 |
$this->help_create_user_note(
|
|
|
184 |
$student->id,
|
|
|
185 |
NOTES_STATE_PUBLIC,
|
|
|
186 |
$course->id,
|
|
|
187 |
"Test public user note for student $s in Course $c by the teacher"
|
|
|
188 |
);
|
|
|
189 |
}
|
|
|
190 |
// Store the Course context for those which have test notes added for verification.
|
|
|
191 |
$coursecontextids[] = $coursecontext->id;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
$courses[$c] = $course;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
// Add a site-wide user note for Teacher 1 by another Teacher 2 in Course 4.
|
|
|
198 |
$teacher2 = $this->getDataGenerator()->create_user();
|
|
|
199 |
$this->setUser($teacher2);
|
|
|
200 |
|
|
|
201 |
$course = $courses[4];
|
|
|
202 |
$this->help_create_user_note(
|
|
|
203 |
$teacher1->id,
|
|
|
204 |
NOTES_STATE_SITE,
|
|
|
205 |
$course->id,
|
|
|
206 |
"Test site-wide user note about the teacher in Course 4 by another teacher"
|
|
|
207 |
);
|
|
|
208 |
// Store the Course context for those which have test notes added for verification.
|
|
|
209 |
$coursecontextids[] = \context_course::instance($course->id)->id;
|
|
|
210 |
|
|
|
211 |
// Add a private user note for Teacher 1 by another Teacher 2 in Course 5.
|
|
|
212 |
$course = $courses[5];
|
|
|
213 |
$this->help_create_user_note(
|
|
|
214 |
$teacher1->id,
|
|
|
215 |
NOTES_STATE_DRAFT,
|
|
|
216 |
$course->id,
|
|
|
217 |
"Test private user note about the teacher in Course 5 by another teacher"
|
|
|
218 |
);
|
|
|
219 |
|
|
|
220 |
// Test the number of contexts returned matches the Course contexts created with notes.
|
|
|
221 |
$contextlist = provider::get_contexts_for_userid($teacher1->id);
|
|
|
222 |
$this->assertEmpty(array_diff($coursecontextids, $contextlist->get_contextids()));
|
|
|
223 |
|
|
|
224 |
$approvedcontextlist = new approved_contextlist($teacher1, 'core_notes', $contextlist->get_contextids());
|
|
|
225 |
|
|
|
226 |
// Retrieve User notes created by the teacher.
|
|
|
227 |
provider::export_user_data($approvedcontextlist);
|
|
|
228 |
|
|
|
229 |
// Test the core_notes data is exported at the Course context level and has content.
|
|
|
230 |
foreach ($contextlist as $context) {
|
|
|
231 |
$this->assertEquals(CONTEXT_COURSE, $context->contextlevel);
|
|
|
232 |
|
|
|
233 |
/** @var \core_privacy\tests\request\content_writer $writer */
|
|
|
234 |
$writer = writer::with_context($context);
|
|
|
235 |
$this->assertTrue($writer->has_any_data());
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Test for provider::delete_data_for_all_users_in_context().
|
|
|
241 |
*/
|
11 |
efrain |
242 |
public function test_delete_data_for_all_users_in_context(): void {
|
1 |
efrain |
243 |
global $DB;
|
|
|
244 |
|
|
|
245 |
// Test setup.
|
|
|
246 |
$this->resetAfterTest(true);
|
|
|
247 |
$this->setAdminUser();
|
|
|
248 |
set_config('enablenotes', true);
|
|
|
249 |
|
|
|
250 |
$teacher = $this->getDataGenerator()->create_user();
|
|
|
251 |
$this->setUser($teacher);
|
|
|
252 |
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
|
|
253 |
|
|
|
254 |
$nocourses = 2;
|
|
|
255 |
$nostudents = 5;
|
|
|
256 |
$nonotes = 7;
|
|
|
257 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
258 |
|
|
|
259 |
$n = 0;
|
|
|
260 |
for ($c = 0; $c < $nocourses; $c++) {
|
|
|
261 |
// Create a Course, then enrol a teacher and enrol 2 students.
|
|
|
262 |
$course = $this->getDataGenerator()->create_course();
|
|
|
263 |
$coursecontext = \context_course::instance($course->id);
|
|
|
264 |
|
|
|
265 |
role_assign($teacherrole->id, $teacher->id, $coursecontext->id);
|
|
|
266 |
|
|
|
267 |
for ($s = 0; $s < $nostudents; $s++) {
|
|
|
268 |
if ($n < $nonotes) {
|
|
|
269 |
$student = $this->getDataGenerator()->create_user();
|
|
|
270 |
role_assign($studentrole->id, $student->id, $coursecontext->id);
|
|
|
271 |
|
|
|
272 |
// Create test note data.
|
|
|
273 |
$this->help_create_user_note(
|
|
|
274 |
$student->id,
|
|
|
275 |
NOTES_STATE_PUBLIC,
|
|
|
276 |
$course->id,
|
|
|
277 |
"Test user note for student $s in Course $c"
|
|
|
278 |
);
|
|
|
279 |
}
|
|
|
280 |
$n++;
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
// Test the number of contexts returned equals the number of Courses created with user notes for its students.
|
|
|
285 |
$contextlist = provider::get_contexts_for_userid($teacher->id);
|
|
|
286 |
$this->assertCount($nocourses, $contextlist->get_contextids());
|
|
|
287 |
|
|
|
288 |
// Test the created user note records in mdl_post table matches the test number of user notes specified.
|
|
|
289 |
$notes = $DB->get_records('post', ['module' => 'notes', 'usermodified' => $teacher->id]);
|
|
|
290 |
$this->assertCount($nonotes, $notes);
|
|
|
291 |
|
|
|
292 |
// Delete all user note records in mdl_post table by the specified Course context.
|
|
|
293 |
foreach ($contextlist->get_contexts() as $context) {
|
|
|
294 |
provider::delete_data_for_all_users_in_context($context);
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
// Test the core_note records in mdl_post table is equals zero.
|
|
|
298 |
$notes = $DB->get_records('post', ['module' => 'notes', 'usermodified' => $teacher->id]);
|
|
|
299 |
$this->assertCount(0, $notes);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Test for provider::delete_data_for_user().
|
|
|
304 |
*/
|
11 |
efrain |
305 |
public function test_delete_data_for_user(): void {
|
1 |
efrain |
306 |
global $DB;
|
|
|
307 |
|
|
|
308 |
// Test setup.
|
|
|
309 |
$this->resetAfterTest(true);
|
|
|
310 |
$this->setAdminUser();
|
|
|
311 |
set_config('enablenotes', true);
|
|
|
312 |
|
|
|
313 |
$teacher = $this->getDataGenerator()->create_user();
|
|
|
314 |
$this->setUser($teacher);
|
|
|
315 |
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
|
|
316 |
|
|
|
317 |
$nocourses = 2;
|
|
|
318 |
$nostudents = 5;
|
|
|
319 |
$nonotes = 7;
|
|
|
320 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
321 |
|
|
|
322 |
$n = 0;
|
|
|
323 |
for ($c = 0; $c < $nocourses; $c++) {
|
|
|
324 |
// Create a Course, then enrol a teacher and enrol 2 students.
|
|
|
325 |
$course = $this->getDataGenerator()->create_course();
|
|
|
326 |
$coursecontext = \context_course::instance($course->id);
|
|
|
327 |
|
|
|
328 |
role_assign($teacherrole->id, $teacher->id, $coursecontext->id);
|
|
|
329 |
|
|
|
330 |
for ($s = 0; $s < $nostudents; $s++) {
|
|
|
331 |
if ($n < $nonotes) {
|
|
|
332 |
$student = $this->getDataGenerator()->create_user();
|
|
|
333 |
role_assign($studentrole->id, $student->id, $coursecontext->id);
|
|
|
334 |
|
|
|
335 |
// Create test note data.
|
|
|
336 |
$this->help_create_user_note(
|
|
|
337 |
$student->id,
|
|
|
338 |
NOTES_STATE_PUBLIC,
|
|
|
339 |
$course->id,
|
|
|
340 |
"Test user note for student $s in Course $c"
|
|
|
341 |
);
|
|
|
342 |
}
|
|
|
343 |
$n++;
|
|
|
344 |
}
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
// Test the number of contexts returned equals the number of Courses created with user notes for its students.
|
|
|
348 |
$contextlist = provider::get_contexts_for_userid($teacher->id);
|
|
|
349 |
$this->assertCount($nocourses, $contextlist->get_contextids());
|
|
|
350 |
|
|
|
351 |
// Test the created user note records in mdl_post table matches the test number of user notes specified.
|
|
|
352 |
$notes = $DB->get_records('post', ['module' => 'notes', 'usermodified' => $teacher->id]);
|
|
|
353 |
$this->assertCount($nonotes, $notes);
|
|
|
354 |
|
|
|
355 |
// Delete all user note records in mdl_post table created by the specified teacher.
|
|
|
356 |
$approvedcontextlist = new approved_contextlist($teacher, 'core_notes', $contextlist->get_contextids());
|
|
|
357 |
provider::delete_data_for_user($approvedcontextlist);
|
|
|
358 |
|
|
|
359 |
// Test the core_note records in mdl_post table is equals zero.
|
|
|
360 |
$notes = $DB->get_records('post', ['module' => 'notes', 'usermodified' => $teacher->id]);
|
|
|
361 |
$this->assertCount(0, $notes);
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
/**
|
|
|
365 |
* Test that only users within a course context are fetched.
|
|
|
366 |
*/
|
11 |
efrain |
367 |
public function test_get_users_in_context(): void {
|
1 |
efrain |
368 |
global $DB;
|
|
|
369 |
|
|
|
370 |
$this->resetAfterTest(true);
|
|
|
371 |
|
|
|
372 |
$component = 'core_notes';
|
|
|
373 |
// Test setup.
|
|
|
374 |
$this->setAdminUser();
|
|
|
375 |
set_config('enablenotes', true);
|
|
|
376 |
// Create a teacher.
|
|
|
377 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
378 |
$this->setUser($teacher1);
|
|
|
379 |
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
|
|
380 |
// Create a student.
|
|
|
381 |
$student = $this->getDataGenerator()->create_user();
|
|
|
382 |
// Create student2.
|
|
|
383 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
384 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
385 |
|
|
|
386 |
// Create courses, then enrol a teacher and a student.
|
|
|
387 |
$nocourses = 3;
|
|
|
388 |
for ($c = 1; $c <= $nocourses; $c++) {
|
|
|
389 |
${'course' . $c} = $this->getDataGenerator()->create_course();
|
|
|
390 |
${'coursecontext' . $c} = \context_course::instance(${'course' . $c}->id);
|
|
|
391 |
|
|
|
392 |
role_assign($teacherrole->id, $teacher1->id, ${'coursecontext' . $c}->id);
|
|
|
393 |
role_assign($studentrole->id, $student->id, ${'coursecontext' . $c}->id);
|
|
|
394 |
role_assign($studentrole->id, $student2->id, ${'coursecontext' . $c}->id);
|
|
|
395 |
}
|
|
|
396 |
// The list of users in coursecontext1 should be empty (related data still have not been created).
|
|
|
397 |
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
|
|
398 |
provider::get_users_in_context($userlist1);
|
|
|
399 |
$this->assertCount(0, $userlist1);
|
|
|
400 |
// The list of users in coursecontext2 should be empty (related data still have not been created).
|
|
|
401 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
|
|
402 |
provider::get_users_in_context($userlist2);
|
|
|
403 |
$this->assertCount(0, $userlist2);
|
|
|
404 |
// The list of users in coursecontext3 should be empty (related data still have not been created).
|
|
|
405 |
$userlist3 = new \core_privacy\local\request\userlist($coursecontext3, $component);
|
|
|
406 |
provider::get_users_in_context($userlist3);
|
|
|
407 |
$this->assertCount(0, $userlist3);
|
|
|
408 |
|
|
|
409 |
// Create private user notes (i.e. NOTES_STATE_DRAFT) for student in course1 and course2 written by the teacher.
|
|
|
410 |
$this->help_create_user_note($student->id, NOTES_STATE_DRAFT, $course1->id,
|
|
|
411 |
"Test private user note about the student in Course 1 by the teacher");
|
|
|
412 |
$this->help_create_user_note($student->id, NOTES_STATE_DRAFT, $course2->id,
|
|
|
413 |
"Test private user note about the student in Course 2 by the teacher");
|
|
|
414 |
|
|
|
415 |
// The list of users in coursecontext1 should return one user (teacher1).
|
|
|
416 |
provider::get_users_in_context($userlist1);
|
|
|
417 |
$this->assertCount(1, $userlist1);
|
|
|
418 |
$this->assertTrue(in_array($teacher1->id, $userlist1->get_userids()));
|
|
|
419 |
// The list of users in coursecontext2 should return one user (teacher1).
|
|
|
420 |
provider::get_users_in_context($userlist2);
|
|
|
421 |
$this->assertCount(1, $userlist2);
|
|
|
422 |
$this->assertTrue(in_array($teacher1->id, $userlist2->get_userids()));
|
|
|
423 |
// The list of users in coursecontext3 should not return any users.
|
|
|
424 |
provider::get_users_in_context($userlist3);
|
|
|
425 |
$this->assertCount(0, $userlist3);
|
|
|
426 |
|
|
|
427 |
// Create public user note (i.e. NOTES_STATE_PUBLIC) for student in course3 written by the teacher.
|
|
|
428 |
$this->help_create_user_note($student->id, NOTES_STATE_PUBLIC, $course3->id,
|
|
|
429 |
"Test public user note about the student in Course 3 by the teacher");
|
|
|
430 |
|
|
|
431 |
// The list of users in coursecontext3 should return 2 users (teacher and student).
|
|
|
432 |
provider::get_users_in_context($userlist3);
|
|
|
433 |
$this->assertCount(2, $userlist3);
|
|
|
434 |
$this->assertTrue(in_array($teacher1->id, $userlist3->get_userids()));
|
|
|
435 |
$this->assertTrue(in_array($student->id, $userlist3->get_userids()));
|
|
|
436 |
|
|
|
437 |
// Create site user note (i.e. NOTES_STATE_SITE) for student2 in course3 written by the teacher.
|
|
|
438 |
$this->help_create_user_note($student2->id, NOTES_STATE_SITE, $course3->id,
|
|
|
439 |
"Test site-wide user note about student2 in Course 3 by the teacher"
|
|
|
440 |
);
|
|
|
441 |
|
|
|
442 |
// The list of users in coursecontext3 should return 3 users (teacher, student and student2).
|
|
|
443 |
provider::get_users_in_context($userlist3);
|
|
|
444 |
$this->assertCount(3, $userlist3);
|
|
|
445 |
$this->assertTrue(in_array($teacher1->id, $userlist3->get_userids()));
|
|
|
446 |
$this->assertTrue(in_array($student->id, $userlist3->get_userids()));
|
|
|
447 |
$this->assertTrue(in_array($student2->id, $userlist3->get_userids()));
|
|
|
448 |
|
|
|
449 |
// The list of users should not return any users in a different context than course context.
|
|
|
450 |
$contextsystem = \context_system::instance();
|
|
|
451 |
$userlist4 = new \core_privacy\local\request\userlist($contextsystem, $component);
|
|
|
452 |
provider::get_users_in_context($userlist4);
|
|
|
453 |
$this->assertCount(0, $userlist4);
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
/**
|
|
|
457 |
* Test that data for users in approved userlist is deleted.
|
|
|
458 |
*/
|
11 |
efrain |
459 |
public function test_delete_data_for_users(): void {
|
1 |
efrain |
460 |
global $DB;
|
|
|
461 |
|
|
|
462 |
$this->resetAfterTest(true);
|
|
|
463 |
|
|
|
464 |
$component = 'core_notes';
|
|
|
465 |
// Test setup.
|
|
|
466 |
$this->setAdminUser();
|
|
|
467 |
set_config('enablenotes', true);
|
|
|
468 |
// Create a teacher.
|
|
|
469 |
$teacher1 = $this->getDataGenerator()->create_user();
|
|
|
470 |
$this->setUser($teacher1);
|
|
|
471 |
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
|
|
472 |
// Create a student.
|
|
|
473 |
$student = $this->getDataGenerator()->create_user();
|
|
|
474 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
475 |
|
|
|
476 |
// Create Courses, then enrol a teacher and a student.
|
|
|
477 |
$nocourses = 3;
|
|
|
478 |
for ($c = 1; $c <= $nocourses; $c++) {
|
|
|
479 |
${'course' . $c} = $this->getDataGenerator()->create_course();
|
|
|
480 |
${'coursecontext' . $c} = \context_course::instance(${'course' . $c}->id);
|
|
|
481 |
|
|
|
482 |
role_assign($teacherrole->id, $teacher1->id, ${'coursecontext' . $c}->id);
|
|
|
483 |
role_assign($studentrole->id, $student->id, ${'coursecontext' . $c}->id);
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
// Create private notes for student in the course1 and course2 written by the teacher.
|
|
|
487 |
$this->help_create_user_note($student->id, NOTES_STATE_DRAFT, $course1->id,
|
|
|
488 |
"Test private user note about the student in Course 1 by the teacher");
|
|
|
489 |
$this->help_create_user_note($student->id, NOTES_STATE_DRAFT, $course2->id,
|
|
|
490 |
"Test private user note about the student in Course 2 by the teacher");
|
|
|
491 |
// Create public notes for student in the course3 written by the teacher.
|
|
|
492 |
$this->help_create_user_note($student->id, NOTES_STATE_PUBLIC, $course3->id,
|
|
|
493 |
"Test public user note about the student in Course 3 by the teacher");
|
|
|
494 |
|
|
|
495 |
// The list of users in coursecontext1 should return one user (teacher1).
|
|
|
496 |
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
|
|
497 |
provider::get_users_in_context($userlist1);
|
|
|
498 |
$this->assertCount(1, $userlist1);
|
|
|
499 |
$this->assertTrue(in_array($teacher1->id, $userlist1->get_userids()));
|
|
|
500 |
// The list of users in coursecontext2 should return one user (teacher1).
|
|
|
501 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
|
|
502 |
provider::get_users_in_context($userlist2);
|
|
|
503 |
$this->assertCount(1, $userlist2);
|
|
|
504 |
$this->assertTrue(in_array($teacher1->id, $userlist2->get_userids()));
|
|
|
505 |
// The list of users in coursecontext3 should return two users (teacher1 and student).
|
|
|
506 |
$userlist3 = new \core_privacy\local\request\userlist($coursecontext3, $component);
|
|
|
507 |
provider::get_users_in_context($userlist3);
|
|
|
508 |
$this->assertCount(2, $userlist3);
|
|
|
509 |
$this->assertTrue(in_array($teacher1->id, $userlist3->get_userids()));
|
|
|
510 |
$this->assertTrue(in_array($student->id, $userlist3->get_userids()));
|
|
|
511 |
|
|
|
512 |
$approvedlist = new approved_userlist($coursecontext3, $component, [$student->id]);
|
|
|
513 |
// Delete using delete_data_for_user.
|
|
|
514 |
provider::delete_data_for_users($approvedlist);
|
|
|
515 |
// Re-fetch users in the coursecontext3.
|
|
|
516 |
$userlist3 = new \core_privacy\local\request\userlist($coursecontext3, $component);
|
|
|
517 |
// The user data in coursecontext3 should not be removed.
|
|
|
518 |
provider::get_users_in_context($userlist3);
|
|
|
519 |
$this->assertCount(2, $userlist3);
|
|
|
520 |
$this->assertTrue(in_array($teacher1->id, $userlist3->get_userids()));
|
|
|
521 |
$this->assertTrue(in_array($student->id, $userlist3->get_userids()));
|
|
|
522 |
|
|
|
523 |
$approvedlist = new approved_userlist($coursecontext3, $component, [$teacher1->id]);
|
|
|
524 |
// Delete using delete_data_for_user.
|
|
|
525 |
provider::delete_data_for_users($approvedlist);
|
|
|
526 |
// Re-fetch users in the coursecontext3.
|
|
|
527 |
$userlist3 = new \core_privacy\local\request\userlist($coursecontext3, $component);
|
|
|
528 |
// The user data in coursecontext3 should be removed.
|
|
|
529 |
provider::get_users_in_context($userlist3);
|
|
|
530 |
$this->assertCount(0, $userlist3);
|
|
|
531 |
|
|
|
532 |
// Re-fetch users in the coursecontext1.
|
|
|
533 |
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
|
|
534 |
provider::get_users_in_context($userlist1);
|
|
|
535 |
$this->assertCount(1, $userlist1);
|
|
|
536 |
|
|
|
537 |
$approvedlist = new approved_userlist($coursecontext1, $component, [$student->id]);
|
|
|
538 |
// Delete using delete_data_for_user.
|
|
|
539 |
provider::delete_data_for_users($approvedlist);
|
|
|
540 |
// Re-fetch users in the coursecontext1.
|
|
|
541 |
$userlist3 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
|
|
542 |
// The user data in coursecontext1 should not be removed.
|
|
|
543 |
provider::get_users_in_context($userlist3);
|
|
|
544 |
$this->assertCount(1, $userlist3);
|
|
|
545 |
$this->assertTrue(in_array($teacher1->id, $userlist3->get_userids()));
|
|
|
546 |
|
|
|
547 |
$approvedlist = new approved_userlist($coursecontext1, $component, [$teacher1->id]);
|
|
|
548 |
// Delete using delete_data_for_user.
|
|
|
549 |
provider::delete_data_for_users($approvedlist);
|
|
|
550 |
// Re-fetch users in the coursecontext1.
|
|
|
551 |
$userlist3 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
|
|
552 |
// The user data in coursecontext1 should be removed.
|
|
|
553 |
provider::get_users_in_context($userlist3);
|
|
|
554 |
$this->assertCount(0, $userlist3);
|
|
|
555 |
|
|
|
556 |
// Re-fetch users in the coursecontext2.
|
|
|
557 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
|
|
558 |
provider::get_users_in_context($userlist2);
|
|
|
559 |
$this->assertCount(1, $userlist2);
|
|
|
560 |
|
|
|
561 |
// The list of users should not return any users for contexts different than course context.
|
|
|
562 |
$systemcontext = \context_system::instance();
|
|
|
563 |
$userlist4 = new \core_privacy\local\request\userlist($systemcontext, $component);
|
|
|
564 |
provider::get_users_in_context($userlist4);
|
|
|
565 |
$this->assertCount(0, $userlist4);
|
|
|
566 |
}
|
|
|
567 |
|
|
|
568 |
/**
|
|
|
569 |
* Helper function to create user notes for testing.
|
|
|
570 |
*
|
|
|
571 |
* @param int $userid The ID of the User associated with the note.
|
|
|
572 |
* @param string $state The publish status
|
|
|
573 |
* @param int $courseid The ID of the Course associated with the note.
|
|
|
574 |
* @param string $content The note content.
|
|
|
575 |
*/
|
|
|
576 |
protected function help_create_user_note($userid, $state, $courseid, $content) {
|
|
|
577 |
$note = (object) [
|
|
|
578 |
'userid' => $userid,
|
|
|
579 |
'publishstate' => $state,
|
|
|
580 |
'courseid' => $courseid,
|
|
|
581 |
'content' => $content,
|
|
|
582 |
];
|
|
|
583 |
note_save($note);
|
|
|
584 |
}
|
|
|
585 |
}
|