function ValidateTest::testCallerValidation

Same name and namespace in other branches
  1. 8.9.x core/modules/file/tests/src/Kernel/ValidateTest.php \Drupal\Tests\file\Kernel\ValidateTest::testCallerValidation()

Tests that the validators passed into are checked.

File

core/modules/file/tests/src/Kernel/ValidateTest.php, line 15

Class

ValidateTest
Tests the <a href="/api/drupal/core%21modules%21file%21file.module/function/file_validate/9" title="Checks that a file meets the criteria specified by the validators." class="local">file_validate</a>() function.

Namespace

Drupal\Tests\file\Kernel

Code

public function testCallerValidation() {
    $file = $this->createFile();
    // Empty validators.
    $this->assertEquals([], file_validate($file, []), 'Validating an empty array works successfully.');
    $this->assertFileHooksCalled([
        'validate',
    ]);
    // Use the file_test.module's test validator to ensure that passing tests
    // return correctly.
    file_test_reset();
    file_test_set_return('validate', []);
    $passing = [
        'file_test_validator' => [
            [],
        ],
    ];
    $this->assertEquals([], file_validate($file, $passing), 'Validating passes.');
    $this->assertFileHooksCalled([
        'validate',
    ]);
    // Now test for failures in validators passed in and by hook_validate.
    file_test_reset();
    file_test_set_return('validate', [
        'Epic fail',
    ]);
    $failing = [
        'file_test_validator' => [
            [
                'Failed',
                'Badly',
            ],
        ],
    ];
    $this->assertEquals([
        'Failed',
        'Badly',
        'Epic fail',
    ], file_validate($file, $failing), 'Validating returns errors.');
    $this->assertFileHooksCalled([
        'validate',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.