function FileExtensionConstraintValidatorTest::testFileExtensionOnUri

Same name in other branches
  1. 10 core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileExtensionConstraintValidatorTest.php \Drupal\Tests\file\Kernel\Plugin\Validation\Constraint\FileExtensionConstraintValidatorTest::testFileExtensionOnUri()

Tests the FileExtensionConstraintValidator.

@dataProvider providerTestFileValidateExtensionsOnUri @covers ::validate

Parameters

array $file_properties: The properties of the file being validated.

string[] $extensions: An array of the allowed file extensions.

string[] $expected_errors: The expected error messages as string.

File

core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileExtensionConstraintValidatorTest.php, line 31

Class

FileExtensionConstraintValidatorTest
Tests the FileExtensionConstraintValidator.

Namespace

Drupal\Tests\file\Kernel\Plugin\Validation\Constraint

Code

public function testFileExtensionOnUri(array $file_properties, array $extensions, array $expected_errors) : void {
    $file = File::create($file_properties);
    // Test for failure.
    $validators = [
        'FileExtension' => [
            'extensions' => implode(' ', $extensions),
        ],
    ];
    $violations = $this->validator
        ->validate($file, $validators);
    $actual_errors = [];
    foreach ($violations as $violation) {
        $actual_errors[] = $violation->getMessage();
    }
    $this->assertEquals($expected_errors, $actual_errors);
}

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