MDL-61308 assign_submission: Privacy code for user rights.
[moodle.git] / mod / assign / submission / comments / tests / privacy_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/assign/tests/privacy_test.php');
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  */
36 class assignsubmission_comments_privacy_testcase extends \mod_assign\tests\mod_assign_privacy_testcase {
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) {
48         $submission = $assign->get_user_submission($student->id, true);
50         $plugin = $assign->get_submission_plugin_by_type('comments');
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;
62         $comment = new comment($options);
63         $comment->set_post_permission(true);
65         $this->setUser($student);
67         $comment->add($submissiontext);
69         return [$plugin, $submission, $comment];
70     }
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     }
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]);
96         $context = $assign->get_context();
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);
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     }
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]);
124         $context = $assign->get_context();
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);
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     }
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]);
152         $context = $assign->get_context();
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);
160         $writer = \core_privacy\local\request\writer::with_context($context);
161         $this->assertFalse($writer->has_any_data());
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']);
167         $this->assertCount(2, $exportedcomments->comments);
168         $this->assertContains($studentcomment, $exportedcomments->comments[0]->content);
169         $this->assertContains($teachercomment, $exportedcomments->comments[1]->content);
170     }
172     /**
173      * Test that all comments are deleted for this context.
174      */
175     public function test_delete_submission_for_context() {
176         global $DB;
177         $this->resetAfterTest();
179         // Create course, assignment, submission, and then a feedback comment.
180         $course = $this->getDataGenerator()->create_course();
181         // Student.
182         $user1 = $this->getDataGenerator()->create_user();
183         $user2 = $this->getDataGenerator()->create_user();
184         // Teacher.
185         $user3 = $this->getDataGenerator()->create_user();
186         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
187         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
188         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
189         $assign = $this->create_instance(['course' => $course]);
191         $context = $assign->get_context();
193         $studentcomment = 'Comment from user 1';
194         list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
195         $studentcomment = 'Comment from user 2';
196         list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
197         $teachercomment1 = 'From the teacher';
198         $teachercomment2 = 'From the teacher for second student.';
199         $this->setUser($user3);
200         $comment->add($teachercomment1);
201         $comment2->add($teachercomment2);
203         // Only need the context in this plugin for this operation.
204         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
205         \assignsubmission_comments\privacy\provider::delete_submission_for_context($requestdata);
207         $results = $DB->get_records('comments', ['contextid' => $context->id]);
208         $this->assertEmpty($results);
209     }
211     /**
212      * Test that the comments for a user are deleted.
213      */
214     public function test_delete_submission_for_userid() {
215         global $DB;
216         $this->resetAfterTest();
217         // Create course, assignment, submission, and then a feedback comment.
218         $course = $this->getDataGenerator()->create_course();
219         // Student.
220         $user1 = $this->getDataGenerator()->create_user();
221         $user2 = $this->getDataGenerator()->create_user();
222         // Teacher.
223         $user3 = $this->getDataGenerator()->create_user();
224         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
225         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
226         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
227         $assign = $this->create_instance(['course' => $course]);
229         $context = $assign->get_context();
231         $studentcomment = 'Comment from user 1';
232         list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
233         $studentcomment = 'Comment from user 2';
234         list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
235         $teachercomment1 = 'From the teacher';
236         $teachercomment2 = 'From the teacher for second student.';
237         $this->setUser($user3);
238         $comment->add($teachercomment1);
239         $comment2->add($teachercomment2);
241         // Provide full details to delete the comments.
242         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, null, [], $user1);
243         \assignsubmission_comments\privacy\provider::delete_submission_for_userid($requestdata);
245         $results = $DB->get_records('comments', ['contextid' => $context->id]);
246         // We are only deleting the comments for user1 (one comment) so we should have three left.
247         $this->assertCount(3, $results);
248         foreach ($results as $result) {
249             // Check that none of the comments are from user1.
250             $this->assertNotEquals($user1->id, $result->userid);
251         }
252     }