MDL-61308 assign_submission: Privacy code for user rights.
[moodle.git] / mod / assign / submission / file / 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_file.
19  *
20  * @package    assignsubmission_file
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/file/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_file_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   $filename       filename for the file submission
44      * @return array   Submission plugin object and the submission object.
45      */
46     protected function create_file_submission($assign, $student, $filename) {
47         global $CFG;
48         // Create a file submission with the test pdf.
49         $submission = $assign->get_user_submission($student->id, true);
51         $this->setUser($student->id);
53         $fs = get_file_storage();
54         $pdfsubmission = (object) array(
55             'contextid' => $assign->get_context()->id,
56             'component' => 'assignsubmission_file',
57             'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
58             'itemid' => $submission->id,
59             'filepath' => '/',
60             'filename' => $filename
61         );
62         $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
63         $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
65         $data = new \stdClass();
66         $plugin = $assign->get_submission_plugin_by_type('file');
67         $plugin->save($submission, $data);
69         return [$plugin, $submission];
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_file');
77         $collection = \assignsubmission_file\privacy\provider::get_metadata($collection);
78         $this->assertNotEmpty($collection);
79     }
81     /**
82      * Test that submission files are exported for a user.
83      */
84     public function test_export_submission_user_data() {
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         $studentfilename = 'user1file.pdf';
99         list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
101         $writer = \core_privacy\local\request\writer::with_context($context);
102         $this->assertFalse($writer->has_any_data());
104         // The student should have a file submission.
105         $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, ['Attempt 1']);
106         \assignsubmission_file\privacy\provider::export_submission_user_data($exportdata);
107         // print_object($writer);
108         $storedfile = $writer->get_files(['Attempt 1'])['user1file.pdf'];
109         $this->assertInstanceOf('stored_file', $storedfile);
110         $this->assertEquals($studentfilename, $storedfile->get_filename());
111     }
113     /**
114      * Test that all submission files are deleted for this context.
115      */
116     public function test_delete_submission_for_context() {
117         $this->resetAfterTest();
118         // Create course, assignment, submission, and then a feedback comment.
119         $course = $this->getDataGenerator()->create_course();
120         // Student.
121         $user1 = $this->getDataGenerator()->create_user();
122         $user2 = $this->getDataGenerator()->create_user();
124         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
125         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
127         $assign = $this->create_instance(['course' => $course]);
129         $context = $assign->get_context();
131         $studentfilename = 'user1file.pdf';
132         list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
133         $student2filename = 'user2file.pdf';
134         list($plugin2, $submission2) = $this->create_file_submission($assign, $user2, $studentfilename);
136         // Only need the context and assign object in this plugin for this operation.
137         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
138         \assignsubmission_file\privacy\provider::delete_submission_for_context($requestdata);
139         // This checks that there are no files in this submission.
140         $this->assertTrue($plugin->is_empty($submission));
141         $this->assertTrue($plugin2->is_empty($submission2));
142     }
144     /**
145      * Test that the comments for a user are deleted.
146      */
147     public function test_delete_submission_for_userid() {
148         $this->resetAfterTest();
149         // Create course, assignment, submission, and then a feedback comment.
150         $course = $this->getDataGenerator()->create_course();
151         // Student.
152         $user1 = $this->getDataGenerator()->create_user();
153         $user2 = $this->getDataGenerator()->create_user();
155         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
156         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
158         $assign = $this->create_instance(['course' => $course]);
160         $context = $assign->get_context();
162         $studentfilename = 'user1file.pdf';
163         list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
164         $student2filename = 'user2file.pdf';
165         list($plugin2, $submission2) = $this->create_file_submission($assign, $user2, $studentfilename);
167         // Only need the context and assign object in this plugin for this operation.
168         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, [], $user1);
169         \assignsubmission_file\privacy\provider::delete_submission_for_userid($requestdata);
170         // This checks that there are no files in this submission.
171         $this->assertTrue($plugin->is_empty($submission));
172         // There should be files here.
173         $this->assertFalse($plugin2->is_empty($submission2));
174     }