| 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 assignsubmission_onlinetext\privacy;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | /**
 | 
        
           | 1441 | ariadna | 20 |  * Unit tests for mod/assign/submission/onlinetext/classes/privacy/provider.
 | 
        
           | 1 | efrain | 21 |  *
 | 
        
           |  |  | 22 |  * @copyright  2018 Adrian Greeve <adrian@moodle.com>
 | 
        
           |  |  | 23 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           | 1441 | ariadna | 24 |  * @package    assignsubmission_onlinetext
 | 
        
           |  |  | 25 |  * @covers \assignsubmission_onlinetext\privacy\provider
 | 
        
           | 1 | efrain | 26 |  */
 | 
        
           | 1441 | ariadna | 27 | final class provider_test extends \mod_assign\tests\provider_testcase {
 | 
        
           | 1 | efrain | 28 |   | 
        
           |  |  | 29 |     /**
 | 
        
           |  |  | 30 |      * Convenience function for creating feedback data.
 | 
        
           |  |  | 31 |      *
 | 
        
           |  |  | 32 |      * @param  object   $assign         assign object
 | 
        
           |  |  | 33 |      * @param  stdClass $student        user object
 | 
        
           |  |  | 34 |      * @param  string   $text           Submission text.
 | 
        
           |  |  | 35 |      * @return array   Submission plugin object and the submission object.
 | 
        
           |  |  | 36 |      */
 | 
        
           |  |  | 37 |     protected function create_online_submission($assign, $student, $text) {
 | 
        
           |  |  | 38 |         global $CFG;
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |         $this->setUser($student->id);
 | 
        
           |  |  | 41 |         $submission = $assign->get_user_submission($student->id, true);
 | 
        
           |  |  | 42 |         $data = new \stdClass();
 | 
        
           |  |  | 43 |         $data->onlinetext_editor = array(
 | 
        
           |  |  | 44 |             'itemid' => file_get_unused_draft_itemid(),
 | 
        
           |  |  | 45 |             'text' => $text,
 | 
        
           |  |  | 46 |             'format' => FORMAT_PLAIN
 | 
        
           |  |  | 47 |         );
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |         $submission = $assign->get_user_submission($student->id, true);
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |         $plugin = $assign->get_submission_plugin_by_type('onlinetext');
 | 
        
           |  |  | 52 |         $plugin->save($submission, $data);
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         return [$plugin, $submission];
 | 
        
           |  |  | 55 |     }
 | 
        
           |  |  | 56 |   | 
        
           |  |  | 57 |     /**
 | 
        
           |  |  | 58 |      * Quick test to make sure that get_metadata returns something.
 | 
        
           |  |  | 59 |      */
 | 
        
           | 11 | efrain | 60 |     public function test_get_metadata(): void {
 | 
        
           | 1 | efrain | 61 |         $collection = new \core_privacy\local\metadata\collection('assignsubmission_onlinetext');
 | 
        
           |  |  | 62 |         $collection = \assignsubmission_onlinetext\privacy\provider::get_metadata($collection);
 | 
        
           |  |  | 63 |         $this->assertNotEmpty($collection);
 | 
        
           |  |  | 64 |     }
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |     /**
 | 
        
           |  |  | 67 |      * Test that submission files and text are exported for a user.
 | 
        
           |  |  | 68 |      */
 | 
        
           | 11 | efrain | 69 |     public function test_export_submission_user_data(): void {
 | 
        
           | 1 | efrain | 70 |         $this->resetAfterTest();
 | 
        
           |  |  | 71 |         // Create course, assignment, submission, and then a feedback comment.
 | 
        
           |  |  | 72 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 73 |         // Student.
 | 
        
           |  |  | 74 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 75 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 76 |         $assign = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         $context = $assign->get_context();
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         $submissiontext = 'Just some text';
 | 
        
           |  |  | 81 |         list($plugin, $submission) = $this->create_online_submission($assign, $user1, $submissiontext);
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 |         $writer = \core_privacy\local\request\writer::with_context($context);
 | 
        
           |  |  | 84 |         $this->assertFalse($writer->has_any_data());
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         // The student should have some text submitted.
 | 
        
           |  |  | 87 |         $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, ['Attempt 1']);
 | 
        
           |  |  | 88 |         \assignsubmission_onlinetext\privacy\provider::export_submission_user_data($exportdata);
 | 
        
           |  |  | 89 |         $this->assertEquals($submissiontext, $writer->get_data(['Attempt 1',
 | 
        
           |  |  | 90 |                 get_string('privacy:path', 'assignsubmission_onlinetext')])->text);
 | 
        
           |  |  | 91 |     }
 | 
        
           |  |  | 92 |   | 
        
           |  |  | 93 |     /**
 | 
        
           |  |  | 94 |      * Test that all submission files are deleted for this context.
 | 
        
           |  |  | 95 |      */
 | 
        
           | 11 | efrain | 96 |     public function test_delete_submission_for_context(): void {
 | 
        
           | 1 | efrain | 97 |         $this->resetAfterTest();
 | 
        
           |  |  | 98 |         // Create course, assignment, submission, and then a feedback comment.
 | 
        
           |  |  | 99 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 100 |         // Student.
 | 
        
           |  |  | 101 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 102 |         $user2 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 105 |         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 | 
        
           |  |  | 106 |   | 
        
           |  |  | 107 |         $assign = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |         $context = $assign->get_context();
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |         $studenttext = 'Student one\'s text.';
 | 
        
           |  |  | 112 |         list($plugin, $submission) = $this->create_online_submission($assign, $user1, $studenttext);
 | 
        
           |  |  | 113 |         $studenttext2 = 'Student two\'s text.';
 | 
        
           |  |  | 114 |         list($plugin2, $submission2) = $this->create_online_submission($assign, $user2, $studenttext2);
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |         // Only need the context and assign object in this plugin for this operation.
 | 
        
           |  |  | 117 |         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
 | 
        
           |  |  | 118 |         \assignsubmission_onlinetext\privacy\provider::delete_submission_for_context($requestdata);
 | 
        
           |  |  | 119 |         // This checks that there is no content for these submissions.
 | 
        
           |  |  | 120 |         $this->assertTrue($plugin->is_empty($submission));
 | 
        
           |  |  | 121 |         $this->assertTrue($plugin2->is_empty($submission2));
 | 
        
           |  |  | 122 |     }
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |     /**
 | 
        
           |  |  | 125 |      * Test that the comments for a user are deleted.
 | 
        
           |  |  | 126 |      */
 | 
        
           | 11 | efrain | 127 |     public function test_delete_submission_for_userid(): void {
 | 
        
           | 1 | efrain | 128 |         $this->resetAfterTest();
 | 
        
           |  |  | 129 |         // Create course, assignment, submission, and then a feedback comment.
 | 
        
           |  |  | 130 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 131 |         // Student.
 | 
        
           |  |  | 132 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 133 |         $user2 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 136 |         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |         $assign = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |         $context = $assign->get_context();
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |         $studenttext = 'Student one\'s text.';
 | 
        
           |  |  | 143 |         list($plugin, $submission) = $this->create_online_submission($assign, $user1, $studenttext);
 | 
        
           |  |  | 144 |         $studenttext2 = 'Student two\'s text.';
 | 
        
           |  |  | 145 |         list($plugin2, $submission2) = $this->create_online_submission($assign, $user2, $studenttext2);
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         // Need more data for this operation.
 | 
        
           |  |  | 148 |         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, [], $user1);
 | 
        
           |  |  | 149 |         \assignsubmission_onlinetext\privacy\provider::delete_submission_for_userid($requestdata);
 | 
        
           |  |  | 150 |         // This checks that there is no content for the first submission.
 | 
        
           |  |  | 151 |         $this->assertTrue($plugin->is_empty($submission));
 | 
        
           |  |  | 152 |         // But there is for the second submission.
 | 
        
           |  |  | 153 |         $this->assertFalse($plugin2->is_empty($submission2));
 | 
        
           |  |  | 154 |     }
 | 
        
           |  |  | 155 |   | 
        
           | 11 | efrain | 156 |     public function test_delete_submissions(): void {
 | 
        
           | 1 | efrain | 157 |         global $DB;
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 |         $this->resetAfterTest();
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 162 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 163 |         $user2 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 164 |         $user3 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 165 |         // Only makes submissions in the second assignment.
 | 
        
           |  |  | 166 |         $user4 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 167 |   | 
        
           |  |  | 168 |         $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 | 
        
           |  |  | 169 |         $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
 | 
        
           |  |  | 170 |         $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
 | 
        
           |  |  | 171 |         $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
 | 
        
           |  |  | 172 |   | 
        
           |  |  | 173 |         $assign1 = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 174 |         $assign2 = $this->create_instance(['course' => $course]);
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 |         $context1 = $assign1->get_context();
 | 
        
           |  |  | 177 |         $context2 = $assign2->get_context();
 | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 |         $student1text = 'Student one\'s text.';
 | 
        
           |  |  | 180 |         list($plugin1, $submission1) = $this->create_online_submission($assign1, $user1, $student1text);
 | 
        
           |  |  | 181 |         $student2text = 'Student two\'s text.';
 | 
        
           |  |  | 182 |         list($plugin2, $submission2) = $this->create_online_submission($assign1, $user2, $student2text);
 | 
        
           |  |  | 183 |         $student3text = 'Student two\'s text.';
 | 
        
           |  |  | 184 |         list($plugin3, $submission3) = $this->create_online_submission($assign1, $user3, $student3text);
 | 
        
           |  |  | 185 |         // Now for submissions in assignment two.
 | 
        
           |  |  | 186 |         $student3text2 = 'Student two\'s text for the second assignment.';
 | 
        
           |  |  | 187 |         list($plugin4, $submission4) = $this->create_online_submission($assign2, $user3, $student3text2);
 | 
        
           |  |  | 188 |         $student4text = 'Student four\'s text.';
 | 
        
           |  |  | 189 |         list($plugin5, $submission5) = $this->create_online_submission($assign2, $user4, $student4text);
 | 
        
           |  |  | 190 |   | 
        
           |  |  | 191 |         $data = $DB->get_records('assignsubmission_onlinetext', ['assignment' => $assign1->get_instance()->id]);
 | 
        
           |  |  | 192 |         $this->assertCount(3, $data);
 | 
        
           |  |  | 193 |         // Delete the submissions for user 1 and 3.
 | 
        
           |  |  | 194 |         $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context1, $assign1);
 | 
        
           |  |  | 195 |         $requestdata->set_userids([$user1->id, $user2->id]);
 | 
        
           |  |  | 196 |         $requestdata->populate_submissions_and_grades();
 | 
        
           |  |  | 197 |         \assignsubmission_onlinetext\privacy\provider::delete_submissions($requestdata);
 | 
        
           |  |  | 198 |   | 
        
           |  |  | 199 |         // There should only be one record left for assignment one.
 | 
        
           |  |  | 200 |         $data = $DB->get_records('assignsubmission_onlinetext', ['assignment' => $assign1->get_instance()->id]);
 | 
        
           |  |  | 201 |         $this->assertCount(1, $data);
 | 
        
           |  |  | 202 |   | 
        
           |  |  | 203 |         // Check that the second assignment has not been touched.
 | 
        
           |  |  | 204 |         $data = $DB->get_records('assignsubmission_onlinetext', ['assignment' => $assign2->get_instance()->id]);
 | 
        
           |  |  | 205 |         $this->assertCount(2, $data);
 | 
        
           |  |  | 206 |     }
 | 
        
           |  |  | 207 | }
 |