MDL-61308 assignsubmission_comments: Unit test ordering fix.
[moodle.git] / mod / assign / submission / comments / tests / privacy_test.php
CommitLineData
58435433
AG
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 assignsubmission_comments.
19 *
20 * @package assignsubmission_comments
21 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/mod/assign/tests/privacy_test.php');
29
30/**
31 * Unit tests for mod/assign/submission/comments/classes/privacy/
32 *
33 * @copyright 2018 Adrian Greeve <adrian@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class assignsubmission_comments_privacy_testcase extends \mod_assign\tests\mod_assign_privacy_testcase {
37
38 /**
39 * Convenience function for creating feedback data.
40 *
41 * @param object $assign assign object
42 * @param stdClass $student user object
43 * @param string $submissiontext Submission text
44 * @return array Submission plugin object and the submission object and the comment object.
45 */
46 protected function create_comment_submission($assign, $student, $submissiontext) {
47
48 $submission = $assign->get_user_submission($student->id, true);
49
50 $plugin = $assign->get_submission_plugin_by_type('comments');
51
52 $context = $assign->get_context();
53 $options = new stdClass();
54 $options->area = 'submission_comments';
55 $options->course = $assign->get_course();
56 $options->context = $context;
57 $options->itemid = $submission->id;
58 $options->component = 'assignsubmission_comments';
59 $options->showcount = true;
60 $options->displaycancel = true;
61
62 $comment = new comment($options);
63 $comment->set_post_permission(true);
64
65 $this->setUser($student);
66
67 $comment->add($submissiontext);
68
69 return [$plugin, $submission, $comment];
70 }
71
72 /**
73 * Quick test to make sure that get_metadata returns something.
74 */
75 public function test_get_metadata() {
76 $collection = new \core_privacy\local\metadata\collection('assignsubmission_comments');
77 $collection = \assignsubmission_comments\privacy\provider::get_metadata($collection);
78 $this->assertNotEmpty($collection);
79 }
80
81 /**
82 * Test returning the context for a user who has made a comment in an assignment.
83 */
84 public function test_get_context_for_userid_within_submission() {
85 $this->resetAfterTest();
86 // Create course, assignment, submission, and then a feedback comment.
87 $course = $this->getDataGenerator()->create_course();
88 // Student.
89 $user1 = $this->getDataGenerator()->create_user();
90 // Teacher.
91 $user2 = $this->getDataGenerator()->create_user();
92 $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
93 $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
94 $assign = $this->create_instance(['course' => $course]);
95
96 $context = $assign->get_context();
97
98 $studentcomment = 'Comment from user 1';
99 list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
100 $teachercomment = 'From the teacher';
101 $this->setUser($user2);
102 $comment->add($teachercomment);
103
104 $contextlist = new \core_privacy\local\request\contextlist();
105 \assignsubmission_comments\privacy\provider::get_context_for_userid_within_submission($user2->id, $contextlist);
106 $this->assertEquals($context->id, $contextlist->get_contextids()[0]);
107 }
108
109 /**
110 * Test returning student ids given a user ID.
111 */
112 public function test_get_student_user_ids() {
113 $this->resetAfterTest();
114 // Create course, assignment, submission, and then a feedback comment.
115 $course = $this->getDataGenerator()->create_course();
116 // Student.
117 $user1 = $this->getDataGenerator()->create_user();
118 // Teacher.
119 $user2 = $this->getDataGenerator()->create_user();
120 $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
121 $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
122 $assign = $this->create_instance(['course' => $course]);
123
124 $context = $assign->get_context();
125
126 $studentcomment = 'Comment from user 1';
127 list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
128 $teachercomment = 'From the teacher';
129 $this->setUser($user2);
130 $comment->add($teachercomment);
131
132 $useridlist = new mod_assign\privacy\useridlist($user2->id, $assign->get_instance()->id);
133 \assignsubmission_comments\privacy\provider::get_student_user_ids($useridlist);
134 $this->assertEquals($user1->id, $useridlist->get_userids()[0]->id);
135 }
136
137 /**
138 * Test that comments are exported for a user.
139 */
140 public function test_export_submission_user_data() {
141 $this->resetAfterTest();
142 // Create course, assignment, submission, and then a feedback comment.
143 $course = $this->getDataGenerator()->create_course();
144 // Student.
145 $user1 = $this->getDataGenerator()->create_user();
146 // Teacher.
147 $user2 = $this->getDataGenerator()->create_user();
148 $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
149 $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
150 $assign = $this->create_instance(['course' => $course]);
151
152 $context = $assign->get_context();
153
154 $studentcomment = 'Comment from user 1';
155 list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
156 $teachercomment = 'From the teacher';
157 $this->setUser($user2);
158 $comment->add($teachercomment);
159
160 $writer = \core_privacy\local\request\writer::with_context($context);
161 $this->assertFalse($writer->has_any_data());
162
163 // The student should be able to see the teachers feedback.
164 $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission);
165 \assignsubmission_comments\privacy\provider::export_submission_user_data($exportdata);
166 $exportedcomments = $writer->get_data(['Comments']);
bfb57d2c
AG
167
168 // Can't rely on these comments coming out in order.
169 if ($exportedcomments->comments[0]->userid == $user1->id) {
170 $exportedstudentcomment = $exportedcomments->comments[0]->content;
171 $exportedteachercomment = $exportedcomments->comments[1]->content;
172 } else {
173 $exportedstudentcomment = $exportedcomments->comments[1]->content;
174 $exportedteachercomment = $exportedcomments->comments[0]->content;
175 }
58435433 176 $this->assertCount(2, $exportedcomments->comments);
bfb57d2c
AG
177 $this->assertContains($studentcomment, $exportedstudentcomment);
178 $this->assertContains($teachercomment, $exportedteachercomment);
58435433
AG
179 }
180
181 /**
182 * Test that all comments are deleted for this context.
183 */
184 public function test_delete_submission_for_context() {
185 global $DB;
186 $this->resetAfterTest();
187
188 // Create course, assignment, submission, and then a feedback comment.
189 $course = $this->getDataGenerator()->create_course();
190 // Student.
191 $user1 = $this->getDataGenerator()->create_user();
192 $user2 = $this->getDataGenerator()->create_user();
193 // Teacher.
194 $user3 = $this->getDataGenerator()->create_user();
195 $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
196 $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
197 $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
198 $assign = $this->create_instance(['course' => $course]);
199
200 $context = $assign->get_context();
201
202 $studentcomment = 'Comment from user 1';
203 list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
204 $studentcomment = 'Comment from user 2';
205 list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
206 $teachercomment1 = 'From the teacher';
207 $teachercomment2 = 'From the teacher for second student.';
208 $this->setUser($user3);
209 $comment->add($teachercomment1);
210 $comment2->add($teachercomment2);
211
212 // Only need the context in this plugin for this operation.
213 $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
214 \assignsubmission_comments\privacy\provider::delete_submission_for_context($requestdata);
215
216 $results = $DB->get_records('comments', ['contextid' => $context->id]);
217 $this->assertEmpty($results);
218 }
219
220 /**
221 * Test that the comments for a user are deleted.
222 */
223 public function test_delete_submission_for_userid() {
224 global $DB;
225 $this->resetAfterTest();
226 // Create course, assignment, submission, and then a feedback comment.
227 $course = $this->getDataGenerator()->create_course();
228 // Student.
229 $user1 = $this->getDataGenerator()->create_user();
230 $user2 = $this->getDataGenerator()->create_user();
231 // Teacher.
232 $user3 = $this->getDataGenerator()->create_user();
233 $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
234 $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
235 $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
236 $assign = $this->create_instance(['course' => $course]);
237
238 $context = $assign->get_context();
239
240 $studentcomment = 'Comment from user 1';
241 list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
242 $studentcomment = 'Comment from user 2';
243 list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
244 $teachercomment1 = 'From the teacher';
245 $teachercomment2 = 'From the teacher for second student.';
246 $this->setUser($user3);
247 $comment->add($teachercomment1);
248 $comment2->add($teachercomment2);
249
250 // Provide full details to delete the comments.
251 $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, null, [], $user1);
252 \assignsubmission_comments\privacy\provider::delete_submission_for_userid($requestdata);
253
254 $results = $DB->get_records('comments', ['contextid' => $context->id]);
255 // We are only deleting the comments for user1 (one comment) so we should have three left.
256 $this->assertCount(3, $results);
257 foreach ($results as $result) {
258 // Check that none of the comments are from user1.
259 $this->assertNotEquals($user1->id, $result->userid);
260 }
261 }
262}