Proyectos de Subversion Moodle

Rev

| 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
    /**
93
     * Test that get_combined_document_for_attempt() method rotates the image only once.
94
     */
95
    public function test_get_combined_document_for_attempt_rotates_image(): void {
96
        global $CFG, $DB;
97
        $this->resetAfterTest();
98
 
99
        $course = $this->getDataGenerator()->create_course();
100
        $assignparams = [
101
            'assignsubmission_file_enabled' => 1,
102
            'assignsubmission_file_maxfiles' => 1,
103
            'assignsubmission_file_maxsizebytes' => 1024 * 1024,
104
        ];
105
        $assign = $this->create_instance($course, $assignparams);
106
        $student = $this->getDataGenerator()->create_user();
107
        $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
108
        $this->setUser($student);
109
 
110
        $notices = [];
111
        $submission = $assign->get_user_submission($student->id, true, 1);
112
        $data = (object) ['files_filemanager' => $submission->id];
113
        $assign->save_submission($data, $notices);
114
 
115
        // This image was manually rotated to be upside down. Also, Orientation, ExifImageWidth
116
        // and ExifImageLength EXIF tags were written into its metadata.
117
        // This is needed to make sure that this image will be rotated by stored_file::rotate_image()
118
        // and stored as a new rotated file.
119
        $filename = 'testimage_rotated.jpg';
120
        $filepath = $CFG->dirroot . '/lib/filestorage/tests/fixtures/' . $filename;
121
        $filerecord = [
122
            'contextid' => $assign->get_context()->id,
123
            'component' => 'assignsubmission_file',
124
            'filearea'  => ASSIGNSUBMISSION_FILE_FILEAREA,
125
            'itemid'    => $submission->id,
126
            'filepath'  => '/',
127
            'filename'  => $filename,
128
        ];
129
        $fs = get_file_storage();
130
        $fs->create_file_from_pathname($filerecord, $filepath);
131
 
132
        $params = [
133
            'filearea' => document_services::TMP_ROTATED_JPG_FILEAREA,
134
            'component' => document_services::COMPONENT,
135
            'filename' => $filename,
136
        ];
137
 
138
        // Combine the document and get the rotated file.
139
        document_services::get_combined_document_for_attempt($assign, $student->id, 1);
140
        $records = $DB->get_records('files', $params);
141
        $this->assertCount(1, $records);
142
        $record1 = reset($records);
143
 
144
        // Polling file converters do this twice: one call to start a conversion and another one
145
        // to poll the converted file. So we combine the document again here.
146
        document_services::get_combined_document_for_attempt($assign, $student->id, 1);
147
        $records = $DB->get_records('files', $params);
148
        $this->assertCount(1, $records);
149
        $record2 = reset($records);
150
 
151
        // Confirm, that the second get_combined_document_for_attempt() call doesn't create new
152
        // rotated file and re-uses the one that was created as part of the first
153
        // get_combined_document_for_attempt() call.
154
        $this->assertEquals($record1->id, $record2->id);
155
    }
156
}