MDL-61308 assign_feedback: Privacy code for user rights.
[moodle.git] / mod / assign / feedback / editpdf / 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 assignfeedback_editpdf.
19  *
20  * @package    assignfeedback_editpdf
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/locallib.php');
29 require_once($CFG->dirroot . '/mod/assign/tests/privacy_test.php');
31 use \assignfeedback_editpdf\page_editor;
32 use \mod_assign\privacy\assign_plugin_request_data;
34 /**
35  * Unit tests for mod/assign/feedback/editpdf/classes/privacy/
36  *
37  * @copyright  2018 Adrian Greeve <adrian@moodle.com>
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class assignfeedback_editpdf_privacy_testcase extends \mod_assign\tests\mod_assign_privacy_testcase {
42     public function setUp() {
43         // Skip this test if ghostscript is not supported.
44         $result = \assignfeedback_editpdf\pdf::test_gs_path(false);
45         if ($result->status !== \assignfeedback_editpdf\pdf::GSPATH_OK) {
46             $this->markTestSkipped('Ghostscript not setup');
47             return;
48         }
49         parent::setUp();
50     }
52     /**
53      * Convenience function for creating feedback data.
54      *
55      * @param  object   $assign         assign object
56      * @param  stdClass $student        user object
57      * @param  stdClass $teacher        user object
58      * @return array   Feedback plugin object and the grade object.
59      */
60     protected function create_feedback($assign, $student, $teacher) {
61         global $CFG;
63         // Create a file submission with the test pdf.
64         $submission = $assign->get_user_submission($student->id, true);
66         $this->setUser($student->id);
68         $fs = get_file_storage();
69         $pdfsubmission = (object) array(
70             'contextid' => $assign->get_context()->id,
71             'component' => 'assignsubmission_file',
72             'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
73             'itemid' => $submission->id,
74             'filepath' => '/',
75             'filename' => 'submission.pdf'
76         );
77         $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
78         $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
80         $data = new \stdClass();
81         $plugin = $assign->get_submission_plugin_by_type('file');
82         $plugin->save($submission, $data);
84         $this->setUser($teacher->id);
86         $plugin = $assign->get_feedback_plugin_by_type('editpdf');
88         $grade = $assign->get_user_grade($student->id, true);
90         $comment = new \assignfeedback_editpdf\comment();
92         $comment->rawtext = 'Comment text';
93         $comment->width = 100;
94         $comment->x = 100;
95         $comment->y = 100;
96         $comment->colour = 'red';
97         page_editor::set_comments($grade->id, 0, [$comment]);
99         $annotation = new \assignfeedback_editpdf\annotation();
101         $annotation->path = '';
102         $annotation->x = 100;
103         $annotation->y = 100;
104         $annotation->endx = 200;
105         $annotation->endy = 200;
106         $annotation->type = 'line';
107         $annotation->colour = 'red';
109         page_editor::set_annotations($grade->id, 0, [$annotation]);
111         $comments = page_editor::get_comments($grade->id, 0, true);
112         $annotations = page_editor::get_annotations($grade->id, 0, false);
113         page_editor::release_drafts($grade->id);
114         $storedfile = \assignfeedback_editpdf\document_services::generate_feedback_document($assign->get_instance()->id, $student->id,
115                 $grade->attemptnumber);
117         return [$plugin, $grade, $storedfile];
118     }
120     /**
121      * Quick test to make sure that get_metadata returns something.
122      */
123     public function test_get_metadata() {
124         $collection = new \core_privacy\local\metadata\collection('assignfeedback_editpdf');
125         $collection = \assignfeedback_editpdf\privacy\provider::get_metadata($collection);
126         $this->assertNotEmpty($collection);
127     }
129     /**
130      * Test that feedback comments are exported for a user.
131      */
132     public function test_export_feedback_user_data() {
133         $this->resetAfterTest();
134         // Create course, assignment, submission, and then a feedback comment.
135         $course = $this->getDataGenerator()->create_course();
136         // Student.
137         $user1 = $this->getDataGenerator()->create_user();
138         // Teacher.
139         $user2 = $this->getDataGenerator()->create_user();
140         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
141         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
142         $assign = $this->create_instance(['course' => $course,
143                 'assignsubmission_file_enabled' => 1,
144                 'assignsubmission_file_maxfiles' => 1,
145                 'assignfeedback_editpdf_enabled' => 1,
146                 'assignsubmission_file_maxsizebytes' => 1000000]);
148         $context = $assign->get_context();
150         list($plugin, $grade, $storedfile) = $this->create_feedback($assign, $user1, $user2);
152         // Check that we have data.
153         $this->assertFalse($plugin->is_empty($grade));
155         $writer = \core_privacy\local\request\writer::with_context($context);
156         $this->assertFalse($writer->has_any_data());
158         // The student should be able to see the teachers feedback.
159         $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
160         \assignfeedback_editpdf\privacy\provider::export_feedback_user_data($exportdata);
161         // print_object($writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')]));
162         // print_object($writer->get_files(['PDF feedback', $storedfile->get_filename()]));
163         $pdffile = $writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')])[$storedfile->get_filename()];
164         // The writer should have returned a stored file.
165         $this->assertInstanceOf('stored_file', $pdffile);
166     }
168     /**
169      * Test that all feedback is deleted for a context.
170      */
171     public function test_delete_feedback_for_context() {
172         $this->resetAfterTest();
173         // Create course, assignment, submission, and then a feedback comment.
174         $course = $this->getDataGenerator()->create_course();
175         // Students.
176         $user1 = $this->getDataGenerator()->create_user();
177         $user2 = $this->getDataGenerator()->create_user();
178         // Teacher.
179         $user3 = $this->getDataGenerator()->create_user();
180         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
181         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
182         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
183         $assign = $this->create_instance(['course' => $course,
184                 'assignsubmission_file_enabled' => 1,
185                 'assignsubmission_file_maxfiles' => 1,
186                 'assignfeedback_editpdf_enabled' => 1,
187                 'assignsubmission_file_maxsizebytes' => 1000000]);
189         $context = $assign->get_context();
191         list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
192         list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
194         // Check that we have data.
195         $this->assertFalse($plugin1->is_empty($grade1));
196         $this->assertFalse($plugin2->is_empty($grade2));
198         $requestdata = new assign_plugin_request_data($context, $assign);
199         \assignfeedback_editpdf\privacy\provider::delete_feedback_for_context($requestdata);
201         // Check that we now have no data.
202         $this->assertTrue($plugin1->is_empty($grade1));
203         $this->assertTrue($plugin2->is_empty($grade2));
204     }
206     /**
207      * Test that a grade item is deleted for a user.
208      */
209     public function test_delete_feedback_for_grade() {
210         $this->resetAfterTest();
211         // Create course, assignment, submission, and then a feedback comment.
212         $course = $this->getDataGenerator()->create_course();
213         // Students.
214         $user1 = $this->getDataGenerator()->create_user();
215         $user2 = $this->getDataGenerator()->create_user();
216         // Teacher.
217         $user3 = $this->getDataGenerator()->create_user();
218         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
219         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
220         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
221         $assign = $this->create_instance(['course' => $course,
222                 'assignsubmission_file_enabled' => 1,
223                 'assignsubmission_file_maxfiles' => 1,
224                 'assignfeedback_editpdf_enabled' => 1,
225                 'assignsubmission_file_maxsizebytes' => 1000000]);
227         $context = $assign->get_context();
229         list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
230         list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
232         // Check that we have data.
233         $this->assertFalse($plugin1->is_empty($grade1));
234         $this->assertFalse($plugin2->is_empty($grade2));
236         $requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
237         \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grade($requestdata);
239         // Check that we now have no data for user 1.
240         $this->assertTrue($plugin1->is_empty($grade1));
241         // Check that user 2 data is still there.
242         $this->assertFalse($plugin2->is_empty($grade2));
243     }