Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 26... Línea 26...
26
 * @category   test
26
 * @category   test
27
 * @covers     \stored_file
27
 * @covers     \stored_file
28
 * @copyright  2022 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
28
 * @copyright  2022 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
30
 */
31
class stored_file_test extends advanced_testcase {
31
final class stored_file_test extends advanced_testcase {
Línea 32... Línea 32...
32
 
32
 
33
    /**
33
    /**
34
     * Test that the rotate_image() method does not rotate
34
     * Test that the rotate_image() method does not rotate
35
     * an image that is not supposed to be rotated.
-
 
36
     * @covers ::rotate_image()
35
     * an image that is not supposed to be rotated.
37
     */
36
     */
38
    public function test_rotate_image_does_not_rotate_image(): void {
37
    public function test_rotate_image_does_not_rotate_image(): void {
39
        global $CFG;
38
        global $CFG;
Línea 60... Línea 59...
60
    }
59
    }
Línea 61... Línea 60...
61
 
60
 
62
    /**
61
    /**
63
     * Test that the rotate_image() method rotates an image
62
     * Test that the rotate_image() method rotates an image
64
     * that is supposed to be rotated.
-
 
65
     * @covers ::rotate_image()
63
     * that is supposed to be rotated.
66
     */
64
     */
67
    public function test_rotate_image_rotates_image(): void {
65
    public function test_rotate_image_rotates_image(): void {
68
        global $CFG;
66
        global $CFG;
Línea 92... Línea 90...
92
        $this->assertEquals(297, $size['height']);
90
        $this->assertEquals(297, $size['height']);
93
    }
91
    }
Línea 94... Línea 92...
94
 
92
 
95
    /**
93
    /**
96
     * Ensure that get_content_file_handle returns a valid file handle.
-
 
97
     *
-
 
98
     * @covers ::get_psr_stream
94
     * Ensure that get_content_file_handle returns a valid file handle.
99
     */
95
     */
100
    public function test_get_psr_stream(): void {
96
    public function test_get_psr_stream(): void {
101
        global $CFG;
97
        global $CFG;
Línea 119... Línea 115...
119
        $this->assertEquals(file_get_contents($filepath), $stream->getContents());
115
        $this->assertEquals(file_get_contents($filepath), $stream->getContents());
120
        $this->assertFalse($stream->isWritable());
116
        $this->assertFalse($stream->isWritable());
121
        $stream->close();
117
        $stream->close();
122
    }
118
    }
Línea -... Línea 119...
-
 
119
 
-
 
120
    /**
-
 
121
     * If the data gets into an incorrect state where a file references itself, this should not
-
 
122
     * get into endless recursion (stack overflow) but should throw an exception.
-
 
123
     */
-
 
124
    public function test_sync_external_file_with_recursive_reference(): void {
-
 
125
        global $DB;
-
 
126
 
-
 
127
        $this->resetAfterTest();
-
 
128
 
-
 
129
        $fs = get_file_storage();
-
 
130
        $filerecord = [
-
 
131
            'contextid' => context_system::instance()->id,
-
 
132
            'component' => 'core',
-
 
133
            'filearea' => 'unittest',
-
 
134
            'itemid' => 0,
-
 
135
            'filepath' => '/',
-
 
136
            'filename' => 'hello.txt',
-
 
137
        ];
-
 
138
        $file = $fs->create_file_from_string($filerecord, 'hello world');
-
 
139
 
-
 
140
        $referenceid = $DB->get_field('repository_instances', 'id', ['typeid' => FILE_INTERNAL]);
-
 
141
        $referencestr = \file_storage::pack_reference($filerecord);
-
 
142
        $copyrecord = [
-
 
143
            'contextid' => context_system::instance()->id,
-
 
144
            'component' => 'core',
-
 
145
            'filearea' => 'unittest',
-
 
146
            'itemid' => 1,
-
 
147
            'filepath' => '/',
-
 
148
            'filename' => 'hello.txt',
-
 
149
        ];
-
 
150
        $copy = $fs->create_file_from_reference($copyrecord, $referenceid, $referencestr);
-
 
151
 
-
 
152
        // Hack the original file so that it has the reference id to itself from the copy.
-
 
153
        $DB->set_field('files', 'referencefileid', $copy->get_referencefileid(), ['id' => $file->get_id()]);
-
 
154
 
-
 
155
        // Now sync the original file.
-
 
156
        $hackedfile = $fs->get_file_by_id($file->get_id());
-
 
157
 
-
 
158
        try {
-
 
159
            $hackedfile->sync_external_file();
-
 
160
            $this->fail('Should not work because this is a recursive reference');
-
 
161
        } catch (\moodle_exception $e) {
-
 
162
            $this->assertStringContainsString('File references itself: ' . $file->get_id(), $e->getMessage());
-
 
163
        }
-
 
164
 
-
 
165
        // Create another file that references the copy.
-
 
166
        $reference2str = \file_storage::pack_reference($copyrecord);
-
 
167
        $copy2record = [
-
 
168
            'contextid' => context_system::instance()->id,
-
 
169
            'component' => 'core',
-
 
170
            'filearea' => 'unittest',
-
 
171
            'itemid' => 2,
-
 
172
            'filepath' => '/',
-
 
173
            'filename' => 'hello.txt',
-
 
174
        ];
-
 
175
        $copy2 = $fs->create_file_from_reference($copy2record, $referenceid, $reference2str);
-
 
176
 
-
 
177
        // Now we change the original file to reference this second one - 2 levels of redirection.
-
 
178
        $DB->set_field('files', 'referencefileid', $copy2->get_referencefileid(), ['id' => $file->get_id()]);
-
 
179
 
-
 
180
        // Again try to sync the original file.
-
 
181
        $hackedfile = $fs->get_file_by_id($file->get_id());
-
 
182
 
-
 
183
        try {
-
 
184
            $hackedfile->sync_external_file();
-
 
185
            $this->fail('Should not work because this is a recursive reference');
-
 
186
        } catch (\moodle_exception $e) {
-
 
187
            $this->assertStringContainsString('File references itself: ' . $file->get_id(), $e->getMessage());
-
 
188
        }
-
 
189
 
-
 
190
        // Put the hacked file back how it started so the situation is valid.
-
 
191
        $DB->set_field('files', 'referencefileid', 0, ['id' => $file->get_id()]);
-
 
192
        $copy2->sync_external_file();
-
 
193
        $copy->sync_external_file();
-
 
194
        $file->sync_external_file();
-
 
195
    }
123
 
196