| 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 assignfeedback_file\privacy;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use mod_assign\privacy\assign_plugin_request_data;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | /**
 | 
        
           | 1441 | ariadna | 22 |  * Unit tests for mod/assign/feedback/file/classes/privacy/provider.
 | 
        
           | 1 | efrain | 23 |  *
 | 
        
           |  |  | 24 |  * @copyright  2018 Adrian Greeve <adrian@moodle.com>
 | 
        
           |  |  | 25 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           | 1441 | ariadna | 26 |  * @package    assignfeedback_file
 | 
        
           |  |  | 27 |  * @covers     \assignfeedback_file\privacy\provider
 | 
        
           | 1 | efrain | 28 |  */
 | 
        
           | 1441 | ariadna | 29 | final class provider_test extends \mod_assign\tests\provider_testcase {
 | 
        
           |  |  | 30 |     #[\Override]
 | 
        
           |  |  | 31 |     public static function setUpBeforeClass(): void {
 | 
        
           |  |  | 32 |         global $CFG;
 | 
        
           | 1 | efrain | 33 |   | 
        
           | 1441 | ariadna | 34 |         parent::setUpBeforeClass();
 | 
        
           |  |  | 35 |         require_once($CFG->dirroot . '/mod/assign/locallib.php');
 | 
        
           |  |  | 36 |     }
 | 
        
           |  |  | 37 |   | 
        
           | 1 | efrain | 38 |     /**
 | 
        
           |  |  | 39 |      * Convenience function for creating feedback data.
 | 
        
           |  |  | 40 |      *
 | 
        
           |  |  | 41 |      * @param  object   $assign         assign object
 | 
        
           |  |  | 42 |      * @param  \stdClass $student        user object
 | 
        
           |  |  | 43 |      * @param  \stdClass $teacher        user object
 | 
        
           |  |  | 44 |      * @param  string   $submissiontext Submission text
 | 
        
           |  |  | 45 |      * @param  string   $feedbacktext   Feedback text
 | 
        
           |  |  | 46 |      * @return array   Feedback plugin object and the grade object.
 | 
        
           |  |  | 47 |      */
 | 
        
           |  |  | 48 |     protected function create_feedback($assign, $student, $teacher, $submissiontext, $feedbacktext) {
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         $submission = new \stdClass();
 | 
        
           |  |  | 51 |         $submission->assignment = $assign->get_instance()->id;
 | 
        
           |  |  | 52 |         $submission->userid = $student->id;
 | 
        
           |  |  | 53 |         $submission->timecreated = time();
 | 
        
           |  |  | 54 |         $submission->onlinetext_editor = ['text' => $submissiontext,
 | 
        
           |  |  | 55 |                                          'format' => FORMAT_MOODLE];
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |         $this->setUser($student);
 | 
        
           |  |  | 58 |         $notices = [];
 | 
        
           |  |  | 59 |         $assign->save_submission($submission, $notices);
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |         $grade = $assign->get_user_grade($student->id, true);
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         $this->setUser($teacher);
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |         $context = \context_user::instance($teacher->id);
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |         $draftitemid = file_get_unused_draft_itemid();
 | 
        
           |  |  | 68 |         file_prepare_draft_area($draftitemid, $context->id, 'assignfeedback_file', 'feedback_files', 1);
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |         $dummy = array(
 | 
        
           |  |  | 71 |             'contextid' => $context->id,
 | 
        
           |  |  | 72 |             'component' => 'user',
 | 
        
           |  |  | 73 |             'filearea' => 'draft',
 | 
        
           |  |  | 74 |             'itemid' => $draftitemid,
 | 
        
           |  |  | 75 |             'filepath' => '/',
 | 
        
           |  |  | 76 |             'filename' => 'feedback1.txt'
 | 
        
           |  |  | 77 |         );
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |         $fs = get_file_storage();
 | 
        
           |  |  | 80 |         $file = $fs->create_file_from_string($dummy, $feedbacktext);
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |         // Create formdata.
 | 
        
           |  |  | 83 |         $data = new \stdClass();
 | 
        
           |  |  | 84 |         $data->{'files_' . $teacher->id . '_filemanager'} = $draftitemid;
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         $plugin = $assign->get_feedback_plugin_by_type('file');
 | 
        
           |  |  | 87 |         // Save the feedback.
 | 
        
           |  |  | 88 |         $plugin->save($grade, $data);
 | 
        
           |  |  | 89 |   | 
        
           |  |  | 90 |         return [$plugin, $grade];
 | 
        
           |  |  | 91 |     }
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |     /**
 | 
        
           |  |  | 94 |      * Quick test to make sure that get_metadata returns something.
 | 
        
           |  |  | 95 |      */
 | 
        
           | 11 | efrain | 96 |     public function test_get_metadata(): void {
 | 
        
           | 1 | efrain | 97 |         $collection = new \core_privacy\local\metadata\collection('assignfeedback_file');
 | 
        
           |  |  | 98 |         $collection = \assignfeedback_file\privacy\provider::get_metadata($collection);
 | 
        
           |  |  | 99 |         $this->assertNotEmpty($collection);
 | 
        
           |  |  | 100 |     }
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |     /**
 | 
        
           |  |  | 103 |      * Test that feedback comments are exported for a user.
 | 
        
           |  |  | 104 |      */
 | 
        
           | 11 | efrain | 105 |     public function test_export_feedback_user_data(): 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 |         $feedbacktext = '<p>first comment for this test</p>';
 | 
        
           |  |  | 120 |         list($plugin, $grade) = $this->create_feedback($assign, $user1, $user2, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |         $writer = \core_privacy\local\request\writer::with_context($context);
 | 
        
           |  |  | 123 |         $this->assertFalse($writer->has_any_data());
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 |         // The student should be able to see the teachers feedback.
 | 
        
           |  |  | 126 |         $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
 | 
        
           |  |  | 127 |         \assignfeedback_file\privacy\provider::export_feedback_user_data($exportdata);
 | 
        
           |  |  | 128 |         $feedbackfile = $writer->get_files([get_string('privacy:path', 'assignfeedback_file')])['feedback1.txt'];
 | 
        
           |  |  | 129 |         // Check that we got a stored file.
 | 
        
           |  |  | 130 |         $this->assertInstanceOf('stored_file', $feedbackfile);
 | 
        
           |  |  | 131 |         $this->assertEquals('feedback1.txt', $feedbackfile->get_filename());
 | 
        
           |  |  | 132 |     }
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |     /**
 | 
        
           |  |  | 135 |      * Test that all feedback is deleted for a context.
 | 
        
           |  |  | 136 |      */
 | 
        
           | 11 | efrain | 137 |     public function test_delete_feedback_for_context(): void {
 | 
        
           | 1 | efrain | 138 |         $this->resetAfterTest();
 | 
        
           |  |  | 139 |         // Create course, assignment, submission, and then a feedback comment.
 | 
        
           |  |  | 140 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 141 |         // Students.
 | 
        
           |  |  | 142 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 143 |         $user2 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 144 |         // Teacher.
 | 
        
           |  |  | 145 |         $user3 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 146 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 147 |         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 | 
        
           |  |  | 148 |         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 | 
        
           |  |  | 149 |         $assign = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 150 |   | 
        
           |  |  | 151 |         $context = $assign->get_context();
 | 
        
           |  |  | 152 |   | 
        
           |  |  | 153 |         $feedbacktext = '<p>first comment for this test</p>';
 | 
        
           |  |  | 154 |         list($plugin1, $grade1) = $this->create_feedback($assign, $user1, $user3, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 155 |         $feedbacktext = '<p>Comment for second submission.</p>';
 | 
        
           |  |  | 156 |         list($plugin2, $grade2) = $this->create_feedback($assign, $user2, $user3, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 157 |   | 
        
           |  |  | 158 |         // Check that we have data.
 | 
        
           |  |  | 159 |         $this->assertFalse($plugin1->is_empty($grade1));
 | 
        
           |  |  | 160 |         $this->assertFalse($plugin2->is_empty($grade2));
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 |         $requestdata = new assign_plugin_request_data($context, $assign);
 | 
        
           |  |  | 163 |         \assignfeedback_file\privacy\provider::delete_feedback_for_context($requestdata);
 | 
        
           |  |  | 164 |   | 
        
           |  |  | 165 |         // Check that we now have no data.
 | 
        
           |  |  | 166 |         $this->assertTrue($plugin1->is_empty($grade1));
 | 
        
           |  |  | 167 |         $this->assertTrue($plugin2->is_empty($grade2));
 | 
        
           |  |  | 168 |     }
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 |     /**
 | 
        
           |  |  | 171 |      * Test that a grade item is deleted for a user.
 | 
        
           |  |  | 172 |      */
 | 
        
           | 11 | efrain | 173 |     public function test_delete_feedback_for_grade(): void {
 | 
        
           | 1 | efrain | 174 |         $this->resetAfterTest();
 | 
        
           |  |  | 175 |         // Create course, assignment, submission, and then a feedback comment.
 | 
        
           |  |  | 176 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 177 |         // Students.
 | 
        
           |  |  | 178 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 179 |         $user2 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 180 |         // Teacher.
 | 
        
           |  |  | 181 |         $user3 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 182 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 183 |         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 | 
        
           |  |  | 184 |         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
 | 
        
           |  |  | 185 |         $assign = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 186 |   | 
        
           |  |  | 187 |         $context = $assign->get_context();
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 |         $feedbacktext = '<p>first comment for this test</p>';
 | 
        
           |  |  | 190 |         list($plugin1, $grade1) = $this->create_feedback($assign, $user1, $user3, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 191 |         $feedbacktext = '<p>Comment for second submission.</p>';
 | 
        
           |  |  | 192 |         list($plugin2, $grade2) = $this->create_feedback($assign, $user2, $user3, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 193 |   | 
        
           |  |  | 194 |         // Check that we have data.
 | 
        
           |  |  | 195 |         $this->assertFalse($plugin1->is_empty($grade1));
 | 
        
           |  |  | 196 |         $this->assertFalse($plugin2->is_empty($grade2));
 | 
        
           |  |  | 197 |   | 
        
           |  |  | 198 |         $requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
 | 
        
           |  |  | 199 |         \assignfeedback_file\privacy\provider::delete_feedback_for_grade($requestdata);
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |         // Check that we now have no data.
 | 
        
           |  |  | 202 |         $this->assertTrue($plugin1->is_empty($grade1));
 | 
        
           |  |  | 203 |         // User 2's data should still be intact.
 | 
        
           |  |  | 204 |         $this->assertFalse($plugin2->is_empty($grade2));
 | 
        
           |  |  | 205 |     }
 | 
        
           |  |  | 206 |   | 
        
           |  |  | 207 |     /**
 | 
        
           |  |  | 208 |      * Test that a grade item is deleted for a user.
 | 
        
           |  |  | 209 |      */
 | 
        
           | 11 | efrain | 210 |     public function test_delete_feedback_for_grades(): void {
 | 
        
           | 1 | efrain | 211 |         $this->resetAfterTest();
 | 
        
           |  |  | 212 |         // Create course, assignment, submission, and then a feedback comment.
 | 
        
           |  |  | 213 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 214 |         // Students.
 | 
        
           |  |  | 215 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 216 |         $user2 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 217 |         $user3 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 218 |         $user4 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 219 |         // Teacher.
 | 
        
           |  |  | 220 |         $user5 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 221 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 222 |         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 | 
        
           |  |  | 223 |         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 | 
        
           |  |  | 224 |         $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 | 
        
           |  |  | 225 |         $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
 | 
        
           |  |  | 226 |         $assign1 = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 227 |         $assign2 = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 |         $context = $assign1->get_context();
 | 
        
           |  |  | 230 |   | 
        
           |  |  | 231 |         $feedbacktext = '<p>first comment for this test</p>';
 | 
        
           |  |  | 232 |         list($plugin1, $grade1) = $this->create_feedback($assign1, $user1, $user5, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 233 |         $feedbacktext = '<p>Comment for second submission.</p>';
 | 
        
           |  |  | 234 |         list($plugin2, $grade2) = $this->create_feedback($assign1, $user2, $user5, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 235 |         $feedbacktext = '<p>Comment for second submission.</p>';
 | 
        
           |  |  | 236 |         list($plugin3, $grade3) = $this->create_feedback($assign1, $user3, $user5, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 237 |         $feedbacktext = '<p>Comment for second submission.</p>';
 | 
        
           |  |  | 238 |         list($plugin4, $grade4) = $this->create_feedback($assign2, $user3, $user5, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 239 |         $feedbacktext = '<p>Comment for second submission.</p>';
 | 
        
           |  |  | 240 |         list($plugin5, $grade5) = $this->create_feedback($assign2, $user4, $user5, 'Submission text', $feedbacktext);
 | 
        
           |  |  | 241 |   | 
        
           |  |  | 242 |         // Check that we have data.
 | 
        
           |  |  | 243 |         $this->assertFalse($plugin1->is_empty($grade1));
 | 
        
           |  |  | 244 |         $this->assertFalse($plugin2->is_empty($grade2));
 | 
        
           |  |  | 245 |         $this->assertFalse($plugin3->is_empty($grade3));
 | 
        
           |  |  | 246 |         $this->assertFalse($plugin4->is_empty($grade4));
 | 
        
           |  |  | 247 |         $this->assertFalse($plugin5->is_empty($grade5));
 | 
        
           |  |  | 248 |   | 
        
           |  |  | 249 |         $deletedata = new assign_plugin_request_data($context, $assign1);
 | 
        
           |  |  | 250 |         $deletedata->set_userids([$user1->id, $user3->id]);
 | 
        
           |  |  | 251 |         $deletedata->populate_submissions_and_grades();
 | 
        
           |  |  | 252 |         \assignfeedback_file\privacy\provider::delete_feedback_for_grades($deletedata);
 | 
        
           |  |  | 253 |   | 
        
           |  |  | 254 |         // Check that we now have no data.
 | 
        
           |  |  | 255 |         $this->assertTrue($plugin1->is_empty($grade1));
 | 
        
           |  |  | 256 |         // User 2's data should still be intact.
 | 
        
           |  |  | 257 |         $this->assertFalse($plugin2->is_empty($grade2));
 | 
        
           |  |  | 258 |         // User 3's data in assignment 1 should be gone.
 | 
        
           |  |  | 259 |         $this->assertTrue($plugin3->is_empty($grade3));
 | 
        
           |  |  | 260 |         // User 3's data in assignment 2 should still be intact.
 | 
        
           |  |  | 261 |         $this->assertFalse($plugin4->is_empty($grade4));
 | 
        
           |  |  | 262 |         // User 4's data in assignment 2 should still be intact.
 | 
        
           |  |  | 263 |         $this->assertFalse($plugin5->is_empty($grade5));
 | 
        
           |  |  | 264 |     }
 | 
        
           |  |  | 265 | }
 |