Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 - https://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;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
23
 
24
/**
25
 * Unit tests for document services.
26
 *
27
 * @package    assignfeedback_editpdf
28
 * @category   test
29
 * @copyright  2022 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @covers     \assignfeedback_editpdf\document_services
32
 */
33
final class document_services_test extends \advanced_testcase {
34
    use \mod_assign_test_generator;
35
 
36
    /**
37
     * Test that the save file method saves the file.
38
     */
39
    public function test_save_file_saves_the_file(): void {
40
        global $DB;
41
        $this->resetAfterTest();
42
 
43
        $course = $this->getDataGenerator()->create_course();
44
        $assign = $this->create_instance($course);
45
        $user = $this->getDataGenerator()->create_user();
46
        $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
47
 
48
        $method = new \ReflectionMethod('\assignfeedback_editpdf\document_services', 'save_file');
49
 
50
        $filearea = document_services::TMP_ROTATED_JPG_FILEAREA;
51
        $content = 'some random content';
52
        $tempfile = make_request_directory() . DIRECTORY_SEPARATOR . 'mock.file';
53
        file_put_contents($tempfile, $content);
54
 
55
        // Invoke the method and confirm, that the file is saved.
56
        $file1 = $method->invoke(null, $assign, $user->id, 1, $filearea, $tempfile);
57
        $this->assertInstanceOf('stored_file', $file1);
58
        $this->assertEquals(1, $DB->count_records('files', ['id' => $file1->get_id()]));
59
 
60
        // Invoke the method again and confirm, that exising file is returned.
61
        $file2 = $method->invoke(null, $assign, $user->id, 1, $filearea, $tempfile);
62
        $this->assertEquals($file1->get_id(), $file2->get_id());
63
    }
64
 
65
    /**
66
     * Test that save_rotated_image_file() method saves the file.
67
     */
68
    public function test_save_rotated_image_file_saves_the_file(): void {
69
        global $CFG, $DB;
70
        $this->resetAfterTest();
71
 
72
        $course = $this->getDataGenerator()->create_course();
73
        $assign = $this->create_instance($course);
74
        $user = $this->getDataGenerator()->create_user();
75
        $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
76
 
77
        $method = new \ReflectionMethod('\assignfeedback_editpdf\document_services', 'save_rotated_image_file');
78
 
79
        $imagecontent = file_get_contents($CFG->dirroot . '/lib/filestorage/tests/fixtures/testimage.png');
80
        $imageresource = imagecreatefromstring($imagecontent);
81
 
82
        // Invoke the method and confirm, that the file is saved.
83
        $file1 = $method->invoke(null, $assign, $user->id, 1, $imageresource, 'testimage.png');
84
        $this->assertInstanceOf('stored_file', $file1);
85
        $this->assertEquals(1, $DB->count_records('files', ['id' => $file1->get_id()]));
86
 
87
        // Invoke the method again and confirm, that exising file is returned.
88
        $file2 = $method->invoke(null, $assign, $user->id, 1, $imageresource, 'testimage.png');
89
        $this->assertEquals($file1->get_id(), $file2->get_id());
90
    }
91
 
92
    /**
1441 ariadna 93
     * Test that save_jpg_to_pdf() method safely rejects a non-JPEG file with a JPEG extension.
94
     */
95
    public function test_save_jpg_to_pdf_rejects_non_jpeg(): void {
96
        $this->resetAfterTest();
97
 
98
        $course = $this->getDataGenerator()->create_course();
99
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
100
        $user = $this->getDataGenerator()->create_and_enrol($course);
101
 
102
        $assign = $this->create_instance($course, [
103
            'assignsubmission_file_enabled' => 1,
104
            'assignsubmission_file_maxfiles' => 1,
105
            'assignsubmission_file_maxsizebytes' => 1024 * 1024,
106
        ]);
107
        $fileplugin = $assign->get_plugin_by_type('assignsubmission', 'file');
108
        $generator->create_submission([
109
            'userid' => $user->id,
110
            'cmid' => $assign->get_course_module()->id,
111
            'file' => 'mod/assign/feedback/editpdf/tests/fixtures/heic.jpg',
112
        ]);
113
        $submission = $assign->get_user_submission($user->id, false);
114
        $files = $fileplugin->get_files($submission, $user);
115
        $this->assertEquals('image/jpeg', $files['/heic.jpg']->get_mimetype());
116
 
117
        // Invoke the save_jpg_to_pdf method expecting there to be no exceptions.
118
        $method = new \ReflectionMethod('\assignfeedback_editpdf\document_services', 'save_jpg_to_pdf');
119
        $retfile = $method->invoke(null, $assign, $user->id, 1, $files['/heic.jpg']);
120
        $this->assertNull($retfile);
121
        $this->assertDebuggingCalled("Could not convert {$files['/heic.jpg']->get_contenthash()} jpg to pdf: " .
122
            "TCPDF ERROR: [Image] Unable to get the size of the image: ", DEBUG_ALL);
123
    }
124
 
125
    /**
1 efrain 126
     * Test that get_combined_document_for_attempt() method rotates the image only once.
127
     */
128
    public function test_get_combined_document_for_attempt_rotates_image(): void {
129
        global $CFG, $DB;
130
        $this->resetAfterTest();
131
 
132
        $course = $this->getDataGenerator()->create_course();
133
        $assignparams = [
134
            'assignsubmission_file_enabled' => 1,
135
            'assignsubmission_file_maxfiles' => 1,
136
            'assignsubmission_file_maxsizebytes' => 1024 * 1024,
137
        ];
138
        $assign = $this->create_instance($course, $assignparams);
139
        $student = $this->getDataGenerator()->create_user();
140
        $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
141
        $this->setUser($student);
142
 
143
        $notices = [];
144
        $submission = $assign->get_user_submission($student->id, true, 1);
145
        $data = (object) ['files_filemanager' => $submission->id];
146
        $assign->save_submission($data, $notices);
147
 
148
        // This image was manually rotated to be upside down. Also, Orientation, ExifImageWidth
149
        // and ExifImageLength EXIF tags were written into its metadata.
150
        // This is needed to make sure that this image will be rotated by stored_file::rotate_image()
151
        // and stored as a new rotated file.
152
        $filename = 'testimage_rotated.jpg';
153
        $filepath = $CFG->dirroot . '/lib/filestorage/tests/fixtures/' . $filename;
154
        $filerecord = [
155
            'contextid' => $assign->get_context()->id,
156
            'component' => 'assignsubmission_file',
157
            'filearea'  => ASSIGNSUBMISSION_FILE_FILEAREA,
158
            'itemid'    => $submission->id,
159
            'filepath'  => '/',
160
            'filename'  => $filename,
161
        ];
162
        $fs = get_file_storage();
163
        $fs->create_file_from_pathname($filerecord, $filepath);
164
 
165
        $params = [
166
            'filearea' => document_services::TMP_ROTATED_JPG_FILEAREA,
167
            'component' => document_services::COMPONENT,
168
            'filename' => $filename,
169
        ];
170
 
171
        // Combine the document and get the rotated file.
172
        document_services::get_combined_document_for_attempt($assign, $student->id, 1);
173
        $records = $DB->get_records('files', $params);
174
        $this->assertCount(1, $records);
175
        $record1 = reset($records);
176
 
177
        // Polling file converters do this twice: one call to start a conversion and another one
178
        // to poll the converted file. So we combine the document again here.
179
        document_services::get_combined_document_for_attempt($assign, $student->id, 1);
180
        $records = $DB->get_records('files', $params);
181
        $this->assertCount(1, $records);
182
        $record2 = reset($records);
183
 
184
        // Confirm, that the second get_combined_document_for_attempt() call doesn't create new
185
        // rotated file and re-uses the one that was created as part of the first
186
        // get_combined_document_for_attempt() call.
187
        $this->assertEquals($record1->id, $record2->id);
188
    }
189
}