Proyectos de Subversion Moodle

Rev

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

Rev 11 Rev 1441
Línea 25... Línea 25...
25
 
25
 
Línea 26... Línea 26...
26
namespace core;
26
namespace core;
27
 
27
 
-
 
28
use file_exception;
-
 
29
use file_reference_exception;
28
use file_exception;
30
use file_storage;
29
use file_reference_exception;
31
use file_system;
30
use repository;
32
use repository;
Línea 31... Línea 33...
31
use stored_file;
33
use stored_file;
Línea 45... Línea 47...
45
 * @category  test
47
 * @category  test
46
 * @copyright 2012 David Mudrak <david@moodle.com>
48
 * @copyright 2012 David Mudrak <david@moodle.com>
47
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
49
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 * @coversDefaultClass \file_storage
50
 * @coversDefaultClass \file_storage
49
 */
51
 */
50
class file_storage_test extends \advanced_testcase {
52
final class file_storage_test extends \advanced_testcase {
Línea 51... Línea 53...
51
 
53
 
52
    /**
54
    /**
53
     * Files can be created from strings.
55
     * Files can be created from strings.
54
     *
56
     *
Línea 2130... Línea 2132...
2130
    /**
2132
    /**
2131
     * Data provider to return fixture files and their expected mimetype
2133
     * Data provider to return fixture files and their expected mimetype
2132
     *
2134
     *
2133
     * @return array[]
2135
     * @return array[]
2134
     */
2136
     */
2135
    public function filepath_mimetype_provider(): array {
2137
    public static function filepath_mimetype_provider(): array {
2136
        return [
2138
        return [
2137
            [__DIR__ . '/fixtures/testimage.jpg', 'image/jpeg'],
2139
            [__DIR__ . '/fixtures/testimage.jpg', 'image/jpeg'],
2138
            [__DIR__ . '/fixtures/testimage.svg', 'image/svg+xml'],
2140
            [__DIR__ . '/fixtures/testimage.svg', 'image/svg+xml'],
2139
            [__DIR__ . '/fixtures/testimage_basic.svg', 'image/svg+xml'],
2141
            [__DIR__ . '/fixtures/testimage_basic.svg', 'image/svg+xml'],
2140
        ];
2142
        ];
Línea 2211... Línea 2213...
2211
        $hash3 = \file_storage::get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath3, $filename);
2213
        $hash3 = \file_storage::get_pathname_hash($contextid, $component, $filearea, $itemid, $filepath3, $filename);
2212
        $this->assertEquals($hash1, $hash2);
2214
        $this->assertEquals($hash1, $hash2);
2213
        $this->assertEquals($hash2, $hash3);
2215
        $this->assertEquals($hash2, $hash3);
2214
    }
2216
    }
Línea -... Línea 2217...
-
 
2217
 
-
 
2218
    /**
-
 
2219
     * Test that the before_file_created hook has no impact if not called.
-
 
2220
     *
-
 
2221
     * @covers \core_files\hook\before_file_created
-
 
2222
     */
-
 
2223
    public function test_before_file_created_hook_executed_nochange(): void {
-
 
2224
        global $TESTCALLBACK; // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
-
 
2225
 
-
 
2226
        $this->resetAfterTest(true);
-
 
2227
        $testdata = self::get_fixture_path('core_files', 'hook/before_file_created_hooks.php');
-
 
2228
 
-
 
2229
        \core\di::set(
-
 
2230
            \core\hook\manager::class,
-
 
2231
            \core\hook\manager::phpunit_get_instance([]),
-
 
2232
        );
-
 
2233
 
-
 
2234
        // Create a file.
-
 
2235
        $fs = get_file_storage();
-
 
2236
        $file = $fs->create_file_from_pathname(
-
 
2237
            (object) [
-
 
2238
                'contextid' => 1,
-
 
2239
                'component' => 'core',
-
 
2240
                'filearea' => 'phpunit',
-
 
2241
                'itemid' => 0,
-
 
2242
                'filepath' => '/',
-
 
2243
                'filename' => 'testfile.csv',
-
 
2244
            ],
-
 
2245
            $testdata,
-
 
2246
        );
-
 
2247
 
-
 
2248
        // The content should have been updated.
-
 
2249
        $this->assertEquals(
-
 
2250
            file_get_contents($testdata),
-
 
2251
            $file->get_content(),
-
 
2252
        );
-
 
2253
 
-
 
2254
        // The content hash should match the new content.
-
 
2255
        $this->assertEquals(
-
 
2256
            file_storage::hash_from_path($testdata),
-
 
2257
            $file->get_contenthash(),
-
 
2258
        );
-
 
2259
    }
-
 
2260
 
-
 
2261
    /**
-
 
2262
     * Test that the before_file_created hook is called before a file is created.
-
 
2263
     *
-
 
2264
     * @covers \core_files\hook\before_file_created
-
 
2265
     */
-
 
2266
    public function test_before_file_created_hook_executed_filepath(): void {
-
 
2267
        global $TESTCALLBACK; // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
-
 
2268
 
-
 
2269
        $this->resetAfterTest(true);
-
 
2270
        $testdata = self::get_fixture_path('core', 'tabfile.csv');
-
 
2271
 
-
 
2272
        // The before_file_created test hook calls a callback function at TESTCALLBACK.
-
 
2273
        $TESTCALLBACK = function( // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
-
 
2274
            \core_files\hook\before_file_created $hook,
-
 
2275
        ) use ($testdata) {
-
 
2276
            if ($hook->get_filecontent() === '') {
-
 
2277
                return;
-
 
2278
            }
-
 
2279
            $hook->update_filepath($testdata);
-
 
2280
        };
-
 
2281
 
-
 
2282
        \core\di::set(
-
 
2283
            \core\hook\manager::class,
-
 
2284
            \core\hook\manager::phpunit_get_instance([
-
 
2285
                'example' => self::get_fixture_path('core_files', 'hook/before_file_created_hooks.php'),
-
 
2286
            ]),
-
 
2287
        );
-
 
2288
 
-
 
2289
        // Create a file.
-
 
2290
        $fs = get_file_storage();
-
 
2291
        $file = $fs->create_file_from_pathname(
-
 
2292
            (object) [
-
 
2293
                'contextid' => 1,
-
 
2294
                'component' => 'core',
-
 
2295
                'filearea' => 'phpunit',
-
 
2296
                'itemid' => 0,
-
 
2297
                'filepath' => '/',
-
 
2298
                'filename' => 'testfile.csv',
-
 
2299
            ],
-
 
2300
            self::get_fixture_path('core_files', 'hook/before_file_created_hooks.php'),
-
 
2301
        );
-
 
2302
 
-
 
2303
        // The content should have been updated.
-
 
2304
        $this->assertEquals(
-
 
2305
            file_get_contents($testdata),
-
 
2306
            $file->get_content(),
-
 
2307
        );
-
 
2308
 
-
 
2309
        // The content hash should match the new content.
-
 
2310
        $this->assertEquals(
-
 
2311
            file_storage::hash_from_path($testdata),
-
 
2312
            $file->get_contenthash(),
-
 
2313
        );
-
 
2314
    }
-
 
2315
 
-
 
2316
    /**
-
 
2317
     * Test that the before_file_created hook is called before a file is created with content.
-
 
2318
     *
-
 
2319
     * @covers \core_files\hook\before_file_created
-
 
2320
     */
-
 
2321
    public function test_before_file_created_hook_executed_filecontent(): void {
-
 
2322
        global $TESTCALLBACK; // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
-
 
2323
 
-
 
2324
        $this->resetAfterTest(true);
-
 
2325
        $testdata = 'New content';
-
 
2326
 
-
 
2327
        // The before_file_created test hook calls a callback function at TESTCALLBACK.
-
 
2328
        $TESTCALLBACK = function( // phpcs:ignore moodle.NamingConventions.ValidVariableName.VariableNameLowerCase
-
 
2329
            \core_files\hook\before_file_created $hook,
-
 
2330
        ) use ($testdata) {
-
 
2331
            if ($hook->get_filecontent() === '') {
-
 
2332
                return;
-
 
2333
            }
-
 
2334
            $hook->update_filecontent($testdata);
-
 
2335
        };
-
 
2336
 
-
 
2337
        \core\di::set(
-
 
2338
            \core\hook\manager::class,
-
 
2339
            \core\hook\manager::phpunit_get_instance([
-
 
2340
                'example' => self::get_fixture_path('core_files', 'hook/before_file_created_hooks.php'),
-
 
2341
            ]),
-
 
2342
        );
-
 
2343
 
-
 
2344
        // Create a file.
-
 
2345
        $fs = get_file_storage();
-
 
2346
        $file = $fs->create_file_from_string(
-
 
2347
            (object) [
-
 
2348
                'contextid' => 1,
-
 
2349
                'component' => 'core',
-
 
2350
                'filearea' => 'phpunit',
-
 
2351
                'itemid' => 0,
-
 
2352
                'filepath' => '/',
-
 
2353
                'filename' => 'testfile.csv',
-
 
2354
            ],
-
 
2355
            'Original content',
-
 
2356
        );
-
 
2357
 
-
 
2358
        // The content should have been updated.
-
 
2359
        $this->assertEquals(
-
 
2360
            $testdata,
-
 
2361
            $file->get_content(),
-
 
2362
        );
-
 
2363
 
-
 
2364
        // The content hash should match the new content.
-
 
2365
        $this->assertEquals(
-
 
2366
            file_storage::hash_from_string($testdata),
-
 
2367
            $file->get_contenthash(),
-
 
2368
        );
2215
 
2369
    }
Línea 2216... Línea 2370...
2216
}
2370
}
2217
 
2371
 
2218
class test_stored_file_inspection extends stored_file {
2372
class test_stored_file_inspection extends stored_file {