Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_file\privacy;
18
 
19
/**
20
 * Unit tests for mod/assign/submission/file/classes/privacy/
21
 *
1441 ariadna 22
 * @package    assignsubmission_file
1 efrain 23
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 25
 * @covers \assignsubmission_file\privacy\provider
1 efrain 26
 */
1441 ariadna 27
final class provider_test extends \mod_assign\tests\provider_testcase {
1 efrain 28
    /**
29
     * Convenience function for creating feedback data.
30
     *
31
     * @param  object   $assign         assign object
32
     * @param  stdClass $student        user object
33
     * @param  string   $filename       filename for the file submission
34
     * @return array   Submission plugin object and the submission object.
35
     */
36
    protected function create_file_submission($assign, $student, $filename) {
37
        global $CFG;
38
        // Create a file submission with the test pdf.
39
        $submission = $assign->get_user_submission($student->id, true);
40
 
41
        $this->setUser($student->id);
42
 
43
        $fs = get_file_storage();
44
        $pdfsubmission = (object) array(
45
            'contextid' => $assign->get_context()->id,
46
            'component' => 'assignsubmission_file',
47
            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
48
            'itemid' => $submission->id,
49
            'filepath' => '/',
50
            'filename' => $filename
51
        );
52
        $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
53
        $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
54
 
55
        $data = new \stdClass();
56
        $plugin = $assign->get_submission_plugin_by_type('file');
57
        $plugin->save($submission, $data);
58
 
59
        return [$plugin, $submission];
60
    }
61
 
62
    /**
63
     * Quick test to make sure that get_metadata returns something.
64
     */
11 efrain 65
    public function test_get_metadata(): void {
1 efrain 66
        $collection = new \core_privacy\local\metadata\collection('assignsubmission_file');
67
        $collection = \assignsubmission_file\privacy\provider::get_metadata($collection);
68
        $this->assertNotEmpty($collection);
69
    }
70
 
71
    /**
72
     * Test that submission files are exported for a user.
73
     */
11 efrain 74
    public function test_export_submission_user_data(): void {
1 efrain 75
        $this->resetAfterTest();
76
        // Create course, assignment, submission, and then a feedback comment.
77
        $course = $this->getDataGenerator()->create_course();
78
        // Student.
79
        $user1 = $this->getDataGenerator()->create_user();
80
        // Teacher.
81
        $user2 = $this->getDataGenerator()->create_user();
82
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
83
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
84
        $assign = $this->create_instance(['course' => $course]);
85
 
86
        $context = $assign->get_context();
87
 
88
        $studentfilename = 'user1file.pdf';
89
        list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
90
 
91
        $writer = \core_privacy\local\request\writer::with_context($context);
92
        $this->assertFalse($writer->has_any_data());
93
 
94
        // The student should have a file submission.
95
        $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, ['Attempt 1']);
96
        \assignsubmission_file\privacy\provider::export_submission_user_data($exportdata);
97
        // print_object($writer);
98
        $storedfile = $writer->get_files(['Attempt 1'])['user1file.pdf'];
99
        $this->assertInstanceOf('stored_file', $storedfile);
100
        $this->assertEquals($studentfilename, $storedfile->get_filename());
101
    }
102
 
103
    /**
104
     * Test that all submission files are deleted for this context.
105
     */
11 efrain 106
    public function test_delete_submission_for_context(): void {
1 efrain 107
        $this->resetAfterTest();
108
        // Create course, assignment, submission, and then a feedback comment.
109
        $course = $this->getDataGenerator()->create_course();
110
        // Student.
111
        $user1 = $this->getDataGenerator()->create_user();
112
        $user2 = $this->getDataGenerator()->create_user();
113
 
114
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
115
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
116
 
117
        $assign = $this->create_instance(['course' => $course]);
118
 
119
        $context = $assign->get_context();
120
 
121
        $studentfilename = 'user1file.pdf';
122
        list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
123
        $student2filename = 'user2file.pdf';
124
        list($plugin2, $submission2) = $this->create_file_submission($assign, $user2, $studentfilename);
125
 
126
        // Only need the context and assign object in this plugin for this operation.
127
        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
128
        \assignsubmission_file\privacy\provider::delete_submission_for_context($requestdata);
129
        // This checks that there are no files in this submission.
130
        $this->assertTrue($plugin->is_empty($submission));
131
        $this->assertTrue($plugin2->is_empty($submission2));
132
    }
133
 
134
    /**
135
     * Test that the comments for a user are deleted.
136
     */
11 efrain 137
    public function test_delete_submission_for_userid(): void {
1 efrain 138
        $this->resetAfterTest();
139
        // Create course, assignment, submission, and then a feedback comment.
140
        $course = $this->getDataGenerator()->create_course();
141
        // Student.
142
        $user1 = $this->getDataGenerator()->create_user();
143
        $user2 = $this->getDataGenerator()->create_user();
144
 
145
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
146
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
147
 
148
        $assign = $this->create_instance(['course' => $course]);
149
 
150
        $context = $assign->get_context();
151
 
152
        $studentfilename = 'user1file.pdf';
153
        list($plugin, $submission) = $this->create_file_submission($assign, $user1, $studentfilename);
154
        $student2filename = 'user2file.pdf';
155
        list($plugin2, $submission2) = $this->create_file_submission($assign, $user2, $studentfilename);
156
 
157
        // Only need the context and assign object in this plugin for this operation.
158
        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission, [], $user1);
159
        \assignsubmission_file\privacy\provider::delete_submission_for_userid($requestdata);
160
        // This checks that there are no files in this submission.
161
        $this->assertTrue($plugin->is_empty($submission));
162
        // There should be files here.
163
        $this->assertFalse($plugin2->is_empty($submission2));
164
    }
165
 
166
    /**
167
     * Test deletion of bulk submissions for a context.
168
     */
11 efrain 169
    public function test_delete_submissions(): void {
1 efrain 170
        global $DB;
171
 
172
        $this->resetAfterTest();
173
        // Create course, assignment, submission, and then a feedback comment.
174
        $course = $this->getDataGenerator()->create_course();
175
        // Student.
176
        $user1 = $this->getDataGenerator()->create_user();
177
        $user2 = $this->getDataGenerator()->create_user();
178
        $user3 = $this->getDataGenerator()->create_user();
179
        $user4 = $this->getDataGenerator()->create_user();
180
 
181
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
182
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
183
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
184
        $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
185
 
186
        $assign1 = $this->create_instance(['course' => $course]);
187
        $assign2 = $this->create_instance(['course' => $course]);
188
 
189
        $context1 = $assign1->get_context();
190
        $context2 = $assign2->get_context();
191
 
192
        $student1filename = 'user1file.pdf';
193
        list($plugin1, $submission1) = $this->create_file_submission($assign1, $user1, $student1filename);
194
        $student2filename = 'user2file.pdf';
195
        list($plugin2, $submission2) = $this->create_file_submission($assign1, $user2, $student2filename);
196
        $student3filename = 'user3file.pdf';
197
        list($plugin3, $submission3) = $this->create_file_submission($assign1, $user3, $student3filename);
198
        $student4filename = 'user4file.pdf';
199
        list($plugin4, $submission4) = $this->create_file_submission($assign2, $user4, $student4filename);
200
        $student5filename = 'user5file.pdf';
201
        list($plugin5, $submission5) = $this->create_file_submission($assign2, $user3, $student5filename);
202
 
203
        $submissionids = [
204
            $submission1->id,
205
            $submission3->id
206
        ];
207
 
208
        $userids = [
209
            $user1->id,
210
            $user3->id
211
        ];
212
 
213
        $data = $DB->get_records('files', ['contextid' => $context1->id, 'component' => 'assignsubmission_file']);
214
        $this->assertCount(6, $data);
215
 
216
        $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign1->get_instance()->id]);
217
        $this->assertCount(3, $data);
218
 
219
        // Records in the second assignment (not being touched).
220
        $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign2->get_instance()->id]);
221
        $this->assertCount(2, $data);
222
 
223
        $deletedata = new \mod_assign\privacy\assign_plugin_request_data($context1, $assign1);
224
        $deletedata->set_userids($userids);
225
        $deletedata->populate_submissions_and_grades();
226
        \assignsubmission_file\privacy\provider::delete_submissions($deletedata);
227
        $data = $DB->get_records('files', ['contextid' => $context1->id, 'component' => 'assignsubmission_file']);
228
        $this->assertCount(2, $data);
229
 
230
        // Submission 1 and 3 have been removed. We should be left with submission2.
231
        $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign1->get_instance()->id]);
232
        $this->assertCount(1, $data);
233
 
234
        // This should be untouched.
235
        $data = $DB->get_records('assignsubmission_file', ['assignment' => $assign2->get_instance()->id]);
236
        $this->assertCount(2, $data);
237
    }
238
}