| 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 assignsubmission_comments\privacy;
|
|
|
18 |
|
|
|
19 |
use mod_assign\privacy\useridlist;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Unit tests for mod/assign/submission/comments/classes/privacy/
|
|
|
23 |
*
|
| 1441 |
ariadna |
24 |
* @package assignsubmission_comments
|
| 1 |
efrain |
25 |
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 1441 |
ariadna |
27 |
* @covers \assignsubmission_comments\privacy\provider
|
| 1 |
efrain |
28 |
*/
|
| 1441 |
ariadna |
29 |
final class provider_test extends \mod_assign\tests\provider_testcase {
|
| 1 |
efrain |
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Convenience function for creating feedback data.
|
|
|
33 |
*
|
|
|
34 |
* @param object $assign assign object
|
|
|
35 |
* @param stdClass $student user object
|
|
|
36 |
* @param string $submissiontext Submission text
|
|
|
37 |
* @return array Submission plugin object and the submission object and the comment object.
|
|
|
38 |
*/
|
|
|
39 |
protected function create_comment_submission($assign, $student, $submissiontext) {
|
|
|
40 |
|
|
|
41 |
$submission = $assign->get_user_submission($student->id, true);
|
|
|
42 |
|
|
|
43 |
$plugin = $assign->get_submission_plugin_by_type('comments');
|
|
|
44 |
|
|
|
45 |
$context = $assign->get_context();
|
|
|
46 |
$options = new \stdClass();
|
|
|
47 |
$options->area = 'submission_comments';
|
|
|
48 |
$options->course = $assign->get_course();
|
|
|
49 |
$options->context = $context;
|
|
|
50 |
$options->itemid = $submission->id;
|
|
|
51 |
$options->component = 'assignsubmission_comments';
|
|
|
52 |
$options->showcount = true;
|
|
|
53 |
$options->displaycancel = true;
|
|
|
54 |
|
|
|
55 |
$comment = new \comment($options);
|
|
|
56 |
$comment->set_post_permission(true);
|
|
|
57 |
|
|
|
58 |
$this->setUser($student);
|
|
|
59 |
|
|
|
60 |
$comment->add($submissiontext);
|
|
|
61 |
|
|
|
62 |
return [$plugin, $submission, $comment];
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Quick test to make sure that get_metadata returns something.
|
|
|
67 |
*/
|
| 11 |
efrain |
68 |
public function test_get_metadata(): void {
|
| 1 |
efrain |
69 |
$collection = new \core_privacy\local\metadata\collection('assignsubmission_comments');
|
|
|
70 |
$collection = \assignsubmission_comments\privacy\provider::get_metadata($collection);
|
|
|
71 |
$this->assertNotEmpty($collection);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Test returning the context for a user who has made a comment in an assignment.
|
|
|
76 |
*/
|
| 11 |
efrain |
77 |
public function test_get_context_for_userid_within_submission(): void {
|
| 1 |
efrain |
78 |
$this->resetAfterTest();
|
|
|
79 |
// Create course, assignment, submission, and then a feedback comment.
|
|
|
80 |
$course = $this->getDataGenerator()->create_course();
|
|
|
81 |
// Student.
|
|
|
82 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
83 |
// Teacher.
|
|
|
84 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
85 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
86 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
|
|
|
87 |
$assign = $this->create_instance(['course' => $course]);
|
|
|
88 |
|
|
|
89 |
$context = $assign->get_context();
|
|
|
90 |
|
|
|
91 |
$studentcomment = 'Comment from user 1';
|
|
|
92 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
|
|
|
93 |
$teachercomment = 'From the teacher';
|
|
|
94 |
$this->setUser($user2);
|
|
|
95 |
$comment->add($teachercomment);
|
|
|
96 |
|
|
|
97 |
$contextlist = new \core_privacy\local\request\contextlist();
|
|
|
98 |
\assignsubmission_comments\privacy\provider::get_context_for_userid_within_submission($user2->id, $contextlist);
|
|
|
99 |
$this->assertEquals($context->id, $contextlist->get_contextids()[0]);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Test returning student ids given a user ID.
|
|
|
104 |
*/
|
| 11 |
efrain |
105 |
public function test_get_student_user_ids(): void {
|
| 1 |
efrain |
106 |
$this->resetAfterTest();
|
|
|
107 |
// Create course, assignment, submission, and then a feedback comment.
|
|
|
108 |
$course = $this->getDataGenerator()->create_course();
|
|
|
109 |
// Student.
|
|
|
110 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
111 |
// Teacher.
|
|
|
112 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
113 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
114 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
|
|
|
115 |
$assign = $this->create_instance(['course' => $course]);
|
|
|
116 |
|
|
|
117 |
$context = $assign->get_context();
|
|
|
118 |
|
|
|
119 |
$studentcomment = 'Comment from user 1';
|
|
|
120 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
|
|
|
121 |
$teachercomment = 'From the teacher';
|
|
|
122 |
$this->setUser($user2);
|
|
|
123 |
$comment->add($teachercomment);
|
|
|
124 |
|
|
|
125 |
$useridlist = new useridlist($user2->id, $assign->get_instance()->id);
|
|
|
126 |
\assignsubmission_comments\privacy\provider::get_student_user_ids($useridlist);
|
|
|
127 |
$this->assertEquals($user1->id, $useridlist->get_userids()[0]->id);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Test returning users related to a given context.
|
|
|
132 |
*/
|
| 11 |
efrain |
133 |
public function test_get_userids_from_context(): void {
|
| 1 |
efrain |
134 |
// Get a bunch of users making comments.
|
|
|
135 |
// Some in one context some in another.
|
|
|
136 |
$this->resetAfterTest();
|
|
|
137 |
$course = $this->getDataGenerator()->create_course();
|
|
|
138 |
// Only in first context.
|
|
|
139 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
140 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
141 |
// First and second context.
|
|
|
142 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
143 |
// Second context only.
|
|
|
144 |
$user4 = $this->getDataGenerator()->create_user();
|
|
|
145 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
146 |
$assign1 = $this->create_instance(['course' => $course]);
|
|
|
147 |
$assign2 = $this->create_instance(['course' => $course]);
|
|
|
148 |
|
|
|
149 |
$assigncontext1 = $assign1->get_context();
|
|
|
150 |
$assigncontext2 = $assign2->get_context();
|
|
|
151 |
|
|
|
152 |
$user1comment = 'Comment from user 1';
|
|
|
153 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign1, $user1, $user1comment);
|
|
|
154 |
$user2comment = 'From user 2';
|
|
|
155 |
$this->setUser($user2);
|
|
|
156 |
$comment->add($user2comment);
|
|
|
157 |
$user3comment = 'User 3 comment';
|
|
|
158 |
$this->setUser($user3);
|
|
|
159 |
$comment->add($user3comment);
|
|
|
160 |
$user4comment = 'Comment from user 4';
|
|
|
161 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign2, $user4, $user4comment);
|
|
|
162 |
$user3secondcomment = 'Comment on user 4 post.';
|
|
|
163 |
$this->setUser($user3);
|
|
|
164 |
$comment->add($user3comment);
|
|
|
165 |
|
|
|
166 |
$userlist = new \core_privacy\local\request\userlist($assigncontext1, 'assignsubmission_comments');
|
|
|
167 |
\assignsubmission_comments\privacy\provider::get_userids_from_context($userlist);
|
|
|
168 |
$userids = $userlist->get_userids();
|
|
|
169 |
$this->assertCount(3, $userids);
|
|
|
170 |
// User 1,2 and 3 are the expected ones in the array. User 4 isn't.
|
|
|
171 |
$this->assertContainsEquals($user1->id, $userids);
|
|
|
172 |
$this->assertContainsEquals($user2->id, $userids);
|
|
|
173 |
$this->assertContainsEquals($user3->id, $userids);
|
|
|
174 |
$this->assertNotContainsEquals($user4->id, $userids);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Test that comments are exported for a user.
|
|
|
179 |
*/
|
| 11 |
efrain |
180 |
public function test_export_submission_user_data(): void {
|
| 1 |
efrain |
181 |
$this->resetAfterTest();
|
|
|
182 |
// Create course, assignment, submission, and then a feedback comment.
|
|
|
183 |
$course = $this->getDataGenerator()->create_course();
|
|
|
184 |
// Student.
|
|
|
185 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
186 |
// Teacher.
|
|
|
187 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
188 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
189 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
|
|
|
190 |
$assign = $this->create_instance(['course' => $course]);
|
|
|
191 |
|
|
|
192 |
$context = $assign->get_context();
|
|
|
193 |
|
|
|
194 |
$studentcomment = 'Comment from user 1';
|
|
|
195 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
|
|
|
196 |
$teachercomment = 'From the teacher';
|
|
|
197 |
$this->setUser($user2);
|
|
|
198 |
$comment->add($teachercomment);
|
|
|
199 |
|
|
|
200 |
$writer = \core_privacy\local\request\writer::with_context($context);
|
|
|
201 |
$this->assertFalse($writer->has_any_data());
|
|
|
202 |
|
|
|
203 |
// The student should be able to see the teachers feedback.
|
|
|
204 |
$exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission);
|
|
|
205 |
\assignsubmission_comments\privacy\provider::export_submission_user_data($exportdata);
|
|
|
206 |
$exportedcomments = $writer->get_data(['Comments']);
|
|
|
207 |
|
|
|
208 |
// Can't rely on these comments coming out in order.
|
|
|
209 |
if ($exportedcomments->comments[0]->userid == $user1->id) {
|
|
|
210 |
$exportedstudentcomment = $exportedcomments->comments[0]->content;
|
|
|
211 |
$exportedteachercomment = $exportedcomments->comments[1]->content;
|
|
|
212 |
} else {
|
|
|
213 |
$exportedstudentcomment = $exportedcomments->comments[1]->content;
|
|
|
214 |
$exportedteachercomment = $exportedcomments->comments[0]->content;
|
|
|
215 |
}
|
|
|
216 |
$this->assertCount(2, $exportedcomments->comments);
|
|
|
217 |
$this->assertStringContainsString($studentcomment, $exportedstudentcomment);
|
|
|
218 |
$this->assertStringContainsString($teachercomment, $exportedteachercomment);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Test that all comments are deleted for this context.
|
|
|
223 |
*/
|
| 11 |
efrain |
224 |
public function test_delete_submission_for_context(): void {
|
| 1 |
efrain |
225 |
global $DB;
|
|
|
226 |
$this->resetAfterTest();
|
|
|
227 |
|
|
|
228 |
// Create course, assignment, submission, and then a feedback comment.
|
|
|
229 |
$course = $this->getDataGenerator()->create_course();
|
|
|
230 |
// Student.
|
|
|
231 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
232 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
233 |
// Teacher.
|
|
|
234 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
235 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
236 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
|
|
|
237 |
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
|
|
|
238 |
$assign = $this->create_instance(['course' => $course]);
|
|
|
239 |
|
|
|
240 |
$context = $assign->get_context();
|
|
|
241 |
|
|
|
242 |
$studentcomment = 'Comment from user 1';
|
|
|
243 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
|
|
|
244 |
$studentcomment = 'Comment from user 2';
|
|
|
245 |
list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
|
|
|
246 |
$teachercomment1 = 'From the teacher';
|
|
|
247 |
$teachercomment2 = 'From the teacher for second student.';
|
|
|
248 |
$this->setUser($user3);
|
|
|
249 |
$comment->add($teachercomment1);
|
|
|
250 |
$comment2->add($teachercomment2);
|
|
|
251 |
|
|
|
252 |
// Only need the context in this plugin for this operation.
|
|
|
253 |
$requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
|
|
|
254 |
\assignsubmission_comments\privacy\provider::delete_submission_for_context($requestdata);
|
|
|
255 |
|
|
|
256 |
$results = $DB->get_records('comments', ['contextid' => $context->id]);
|
|
|
257 |
$this->assertEmpty($results);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
/**
|
|
|
261 |
* Test that the comments for a user are deleted.
|
|
|
262 |
*/
|
| 11 |
efrain |
263 |
public function test_delete_submission_for_userid(): void {
|
| 1 |
efrain |
264 |
global $DB;
|
|
|
265 |
$this->resetAfterTest();
|
|
|
266 |
// Create course, assignment, submission, and then a feedback comment.
|
|
|
267 |
$course = $this->getDataGenerator()->create_course();
|
|
|
268 |
// Student.
|
|
|
269 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
270 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
271 |
// Teacher.
|
|
|
272 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
273 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
274 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
|
|
|
275 |
$this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
|
|
|
276 |
$assign = $this->create_instance(['course' => $course]);
|
|
|
277 |
|
|
|
278 |
$context = $assign->get_context();
|
|
|
279 |
|
|
|
280 |
$studentcomment = 'Comment from user 1';
|
|
|
281 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
|
|
|
282 |
$studentcomment = 'Comment from user 2';
|
|
|
283 |
list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
|
|
|
284 |
$teachercomment1 = 'From the teacher';
|
|
|
285 |
$teachercomment2 = 'From the teacher for second student.';
|
|
|
286 |
$this->setUser($user3);
|
|
|
287 |
$comment->add($teachercomment1);
|
|
|
288 |
$comment2->add($teachercomment2);
|
|
|
289 |
|
|
|
290 |
// Provide full details to delete the comments.
|
|
|
291 |
$requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, null, [], $user1);
|
|
|
292 |
\assignsubmission_comments\privacy\provider::delete_submission_for_userid($requestdata);
|
|
|
293 |
|
|
|
294 |
$results = $DB->get_records('comments', ['contextid' => $context->id]);
|
|
|
295 |
// We are only deleting the comments for user1 (one comment) so we should have three left.
|
|
|
296 |
$this->assertCount(3, $results);
|
|
|
297 |
foreach ($results as $result) {
|
|
|
298 |
// Check that none of the comments are from user1.
|
|
|
299 |
$this->assertNotEquals($user1->id, $result->userid);
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
/**
|
|
|
304 |
* Test deletion of all submissions for a context works.
|
|
|
305 |
*/
|
| 11 |
efrain |
306 |
public function test_delete_submissions(): void {
|
| 1 |
efrain |
307 |
global $DB;
|
|
|
308 |
// Get a bunch of users making comments.
|
|
|
309 |
// Some in one context some in another.
|
|
|
310 |
$this->resetAfterTest();
|
|
|
311 |
$course = $this->getDataGenerator()->create_course();
|
|
|
312 |
// Only in first context.
|
|
|
313 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
314 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
315 |
// First and second context.
|
|
|
316 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
317 |
// Second context only.
|
|
|
318 |
$user4 = $this->getDataGenerator()->create_user();
|
|
|
319 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
|
|
|
320 |
$assign1 = $this->create_instance(['course' => $course]);
|
|
|
321 |
$assign2 = $this->create_instance(['course' => $course]);
|
|
|
322 |
|
|
|
323 |
$assigncontext1 = $assign1->get_context();
|
|
|
324 |
$assigncontext2 = $assign2->get_context();
|
|
|
325 |
|
|
|
326 |
$user1comment = 'Comment from user 1';
|
|
|
327 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign1, $user1, $user1comment);
|
|
|
328 |
$user2comment = 'From user 2';
|
|
|
329 |
$this->setUser($user2);
|
|
|
330 |
$comment->add($user2comment);
|
|
|
331 |
$user3comment = 'User 3 comment';
|
|
|
332 |
$this->setUser($user3);
|
|
|
333 |
$comment->add($user3comment);
|
|
|
334 |
$user4comment = 'Comment from user 4';
|
|
|
335 |
list($plugin, $submission, $comment) = $this->create_comment_submission($assign2, $user4, $user4comment);
|
|
|
336 |
$user3secondcomment = 'Comment on user 4 post.';
|
|
|
337 |
$this->setUser($user3);
|
|
|
338 |
$comment->add($user3comment);
|
|
|
339 |
|
|
|
340 |
// There should be three entries. One for the first three users.
|
|
|
341 |
$results = $DB->get_records('comments', ['contextid' => $assigncontext1->id]);
|
|
|
342 |
$this->assertCount(3, $results);
|
|
|
343 |
|
|
|
344 |
$deletedata = new \mod_assign\privacy\assign_plugin_request_data($assigncontext1, $assign1);
|
|
|
345 |
$deletedata->set_userids([$user1->id, $user3->id]);
|
|
|
346 |
\assignsubmission_comments\privacy\provider::delete_submissions($deletedata);
|
|
|
347 |
|
|
|
348 |
// We should be left with just a comment from user 2.
|
|
|
349 |
$results = $DB->get_records('comments', ['contextid' => $assigncontext1->id]);
|
|
|
350 |
$this->assertCount(1, $results);
|
|
|
351 |
$this->assertEquals($user2comment, current($results)->content);
|
|
|
352 |
}
|
|
|
353 |
}
|