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 assignfeedback_editpdf\privacy;
18
 
19
use assignfeedback_editpdf\page_editor;
20
use mod_assign\privacy\assign_plugin_request_data;
21
 
22
/**
1441 ariadna 23
 * Unit tests for mod/assign/feedback/editpdf/classes/privacy/provider.
1 efrain 24
 *
1441 ariadna 25
 * @package    assignfeedback_editpdf
1 efrain 26
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 28
 * @covers    \assignfeedback_editpdf\privacy\provider
1 efrain 29
 */
1441 ariadna 30
final class provider_test extends \mod_assign\tests\provider_testcase {
31
    #[\Override]
32
    public static function setUpBeforeClass(): void {
33
        global $CFG;
1 efrain 34
 
1441 ariadna 35
        parent::setUpBeforeClass();
36
        require_once($CFG->dirroot . '/mod/assign/locallib.php');
37
    }
38
 
39
    #[\Override]
1 efrain 40
    public function setUp(): void {
41
        // Skip this test if ghostscript is not supported.
42
        $result = \assignfeedback_editpdf\pdf::test_gs_path(false);
43
        if ($result->status !== \assignfeedback_editpdf\pdf::GSPATH_OK) {
44
            $this->markTestSkipped('Ghostscript not setup');
45
            return;
46
        }
47
        parent::setUp();
48
    }
49
 
50
    /**
51
     * Convenience function for creating feedback data.
52
     *
53
     * @param  object    $assign         assign object
54
     * @param  \stdClass $student        user object
55
     * @param  \stdClass $teacher        user object
56
     * @return array   Feedback plugin object and the grade object.
57
     */
58
    protected function create_feedback($assign, $student, $teacher) {
59
        global $CFG;
60
 
61
        // Create a file submission with the test pdf.
62
        $submission = $assign->get_user_submission($student->id, true);
63
 
64
        $this->setUser($student->id);
65
 
66
        $fs = get_file_storage();
67
        $pdfsubmission = (object) array(
68
            'contextid' => $assign->get_context()->id,
69
            'component' => 'assignsubmission_file',
70
            'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
71
            'itemid' => $submission->id,
72
            'filepath' => '/',
73
            'filename' => 'submission.pdf'
74
        );
75
        $sourcefile = $CFG->dirroot.'/mod/assign/feedback/editpdf/tests/fixtures/submission.pdf';
76
        $fi = $fs->create_file_from_pathname($pdfsubmission, $sourcefile);
77
 
78
        $data = new \stdClass();
79
        $plugin = $assign->get_submission_plugin_by_type('file');
80
        $plugin->save($submission, $data);
81
 
82
        $this->setUser($teacher->id);
83
 
84
        $plugin = $assign->get_feedback_plugin_by_type('editpdf');
85
 
86
        $grade = $assign->get_user_grade($student->id, true);
87
 
88
        $comment = new \assignfeedback_editpdf\comment();
89
 
90
        $comment->rawtext = 'Comment text';
91
        $comment->width = 100;
92
        $comment->x = 100;
93
        $comment->y = 100;
94
        $comment->colour = 'red';
95
        page_editor::set_comments($grade->id, 0, [$comment]);
96
 
97
        $annotation = new \assignfeedback_editpdf\annotation();
98
 
99
        $annotation->path = '';
100
        $annotation->x = 100;
101
        $annotation->y = 100;
102
        $annotation->endx = 200;
103
        $annotation->endy = 200;
104
        $annotation->type = 'line';
105
        $annotation->colour = 'red';
106
 
107
        page_editor::set_annotations($grade->id, 0, [$annotation]);
108
 
109
        $comments = page_editor::get_comments($grade->id, 0, true);
110
        $annotations = page_editor::get_annotations($grade->id, 0, false);
111
        page_editor::release_drafts($grade->id);
112
        $storedfile = \assignfeedback_editpdf\document_services::generate_feedback_document($assign->get_instance()->id, $student->id,
113
                $grade->attemptnumber);
114
 
115
        return [$plugin, $grade, $storedfile];
116
    }
117
 
118
    /**
119
     * Quick test to make sure that get_metadata returns something.
120
     */
11 efrain 121
    public function test_get_metadata(): void {
1 efrain 122
        $collection = new \core_privacy\local\metadata\collection('assignfeedback_editpdf');
123
        $collection = \assignfeedback_editpdf\privacy\provider::get_metadata($collection);
124
        $this->assertNotEmpty($collection);
125
    }
126
 
127
    /**
128
     * Test that feedback comments are exported for a user.
129
     */
11 efrain 130
    public function test_export_feedback_user_data(): void {
1 efrain 131
        $this->resetAfterTest();
132
        // Create course, assignment, submission, and then a feedback comment.
133
        $course = $this->getDataGenerator()->create_course();
134
        // Student.
135
        $user1 = $this->getDataGenerator()->create_user();
136
        // Teacher.
137
        $user2 = $this->getDataGenerator()->create_user();
138
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
139
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
140
        $assign = $this->create_instance(['course' => $course,
141
                'assignsubmission_file_enabled' => 1,
142
                'assignsubmission_file_maxfiles' => 1,
143
                'assignfeedback_editpdf_enabled' => 1,
144
                'assignsubmission_file_maxsizebytes' => 1000000]);
145
 
146
        $context = $assign->get_context();
147
 
148
        list($plugin, $grade, $storedfile) = $this->create_feedback($assign, $user1, $user2);
149
 
150
        // Check that we have data.
151
        $this->assertFalse($plugin->is_empty($grade));
152
 
153
        $writer = \core_privacy\local\request\writer::with_context($context);
154
        $this->assertFalse($writer->has_any_data());
155
 
156
        // The student should be able to see the teachers feedback.
157
        $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $grade, [], $user1);
158
        \assignfeedback_editpdf\privacy\provider::export_feedback_user_data($exportdata);
159
        // print_object($writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')]));
160
        // print_object($writer->get_files(['PDF feedback', $storedfile->get_filename()]));
161
        $pdffile = $writer->get_files([get_string('privacy:path', 'assignfeedback_editpdf')])[$storedfile->get_filename()];
162
        // The writer should have returned a stored file.
163
        $this->assertInstanceOf('stored_file', $pdffile);
164
    }
165
 
166
    /**
167
     * Test that all feedback is deleted for a context.
168
     */
11 efrain 169
    public function test_delete_feedback_for_context(): void {
1 efrain 170
        $this->resetAfterTest();
171
        // Create course, assignment, submission, and then a feedback comment.
172
        $course = $this->getDataGenerator()->create_course();
173
        // Students.
174
        $user1 = $this->getDataGenerator()->create_user();
175
        $user2 = $this->getDataGenerator()->create_user();
176
        // Teacher.
177
        $user3 = $this->getDataGenerator()->create_user();
178
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
179
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
180
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
181
        $assign = $this->create_instance(['course' => $course,
182
                'assignsubmission_file_enabled' => 1,
183
                'assignsubmission_file_maxfiles' => 1,
184
                'assignfeedback_editpdf_enabled' => 1,
185
                'assignsubmission_file_maxsizebytes' => 1000000]);
186
 
187
        $context = $assign->get_context();
188
 
189
        list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
190
        list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
191
 
192
        // Check that we have data.
193
        $this->assertFalse($plugin1->is_empty($grade1));
194
        $this->assertFalse($plugin2->is_empty($grade2));
195
 
196
        $requestdata = new assign_plugin_request_data($context, $assign);
197
        \assignfeedback_editpdf\privacy\provider::delete_feedback_for_context($requestdata);
198
 
199
        // Check that we now have no data.
200
        $this->assertTrue($plugin1->is_empty($grade1));
201
        $this->assertTrue($plugin2->is_empty($grade2));
202
    }
203
 
204
    /**
205
     * Test that a grade item is deleted for a user.
206
     */
11 efrain 207
    public function test_delete_feedback_for_grade(): void {
1 efrain 208
        $this->resetAfterTest();
209
        // Create course, assignment, submission, and then a feedback comment.
210
        $course = $this->getDataGenerator()->create_course();
211
        // Students.
212
        $user1 = $this->getDataGenerator()->create_user();
213
        $user2 = $this->getDataGenerator()->create_user();
214
        // Teacher.
215
        $user3 = $this->getDataGenerator()->create_user();
216
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
217
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
218
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
219
        $assign = $this->create_instance(['course' => $course,
220
                'assignsubmission_file_enabled' => 1,
221
                'assignsubmission_file_maxfiles' => 1,
222
                'assignfeedback_editpdf_enabled' => 1,
223
                'assignsubmission_file_maxsizebytes' => 1000000]);
224
 
225
        $context = $assign->get_context();
226
 
227
        list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign, $user1, $user3);
228
        list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign, $user2, $user3);
229
 
230
        // Check that we have data.
231
        $this->assertFalse($plugin1->is_empty($grade1));
232
        $this->assertFalse($plugin2->is_empty($grade2));
233
 
234
        $requestdata = new assign_plugin_request_data($context, $assign, $grade1, [], $user1);
235
        \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grade($requestdata);
236
 
237
        // Check that we now have no data for user 1.
238
        $this->assertTrue($plugin1->is_empty($grade1));
239
        // Check that user 2 data is still there.
240
        $this->assertFalse($plugin2->is_empty($grade2));
241
    }
242
 
243
    /**
244
     * Test that a grade item is deleted for a user.
245
     */
11 efrain 246
    public function test_delete_feedback_for_grades(): void {
1 efrain 247
        global $DB;
248
 
249
        $this->resetAfterTest();
250
        // Create course, assignment, submission, and then a feedback comment.
251
        $course = $this->getDataGenerator()->create_course();
252
        // Students.
253
        $user1 = $this->getDataGenerator()->create_user();
254
        $user2 = $this->getDataGenerator()->create_user();
255
        $user3 = $this->getDataGenerator()->create_user();
256
        $user4 = $this->getDataGenerator()->create_user();
257
        // Teacher.
258
        $user5 = $this->getDataGenerator()->create_user();
259
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
260
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
261
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
262
        $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
263
        $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
264
        $assign1 = $this->create_instance(['course' => $course,
265
                'assignsubmission_file_enabled' => 1,
266
                'assignsubmission_file_maxfiles' => 1,
267
                'assignfeedback_editpdf_enabled' => 1,
268
                'assignsubmission_file_maxsizebytes' => 1000000]);
269
 
270
        $assign2 = $this->create_instance(['course' => $course,
271
                'assignsubmission_file_enabled' => 1,
272
                'assignsubmission_file_maxfiles' => 1,
273
                'assignfeedback_editpdf_enabled' => 1,
274
                'assignsubmission_file_maxsizebytes' => 1000000]);
275
 
276
        $context = $assign1->get_context();
277
 
278
        list($plugin1, $grade1, $storedfile1) = $this->create_feedback($assign1, $user1, $user5);
279
        list($plugin2, $grade2, $storedfile2) = $this->create_feedback($assign1, $user2, $user5);
280
        list($plugin3, $grade3, $storedfile3) = $this->create_feedback($assign1, $user3, $user5);
281
        list($plugin4, $grade4, $storedfile4) = $this->create_feedback($assign2, $user3, $user5);
282
        list($plugin5, $grade5, $storedfile5) = $this->create_feedback($assign2, $user4, $user5);
283
 
284
        // Check that we have data.
285
        $this->assertFalse($plugin1->is_empty($grade1));
286
        $this->assertFalse($plugin2->is_empty($grade2));
287
        $this->assertFalse($plugin3->is_empty($grade3));
288
        $this->assertFalse($plugin4->is_empty($grade4));
289
        $this->assertFalse($plugin5->is_empty($grade5));
290
 
291
        // Check that there are also files generated.
292
        $files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
293
        $this->assertCount(10, $files);
294
 
295
        $deletedata = new assign_plugin_request_data($context, $assign1);
296
        $deletedata->set_userids([$user1->id, $user3->id]);
297
        $deletedata->populate_submissions_and_grades();
298
        \assignfeedback_editpdf\privacy\provider::delete_feedback_for_grades($deletedata);
299
 
300
        // Check that we now have no data for user 1.
301
        $this->assertTrue($plugin1->is_empty($grade1));
302
        // Check that user 2 data is still there.
303
        $this->assertFalse($plugin2->is_empty($grade2));
304
        // User 3 in assignment 1 should be gone.
305
        $this->assertTrue($plugin3->is_empty($grade3));
306
        // User 3 in assignment 2 should still be here.
307
        $this->assertFalse($plugin4->is_empty($grade4));
308
        // User 4 in assignment 2 should also still be here.
309
        $this->assertFalse($plugin5->is_empty($grade5));
310
 
311
        // Check the files as well.
312
        $files = $DB->get_records('files', ['component' => 'assignfeedback_editpdf', 'filearea' => 'download']);
313
        // We should now only have six records here.
314
        $this->assertCount(6, $files);
315
    }
316
}