function FileEncodingConstraintValidatorTest::testFileEncodings

Tests the FileEncodingConstraintValidator.

@legacy-covers ::validate

Attributes

#[DataProvider('providerTestFileValidateEncodings')]

Parameters

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

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

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

File

core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileEncodingConstraintValidatorTest.php, line 36

Class

FileEncodingConstraintValidatorTest
Tests the FileEncodingConstraintValidator.

Namespace

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

Code

public function testFileEncodings(array $file_properties, array $encodings, array $expected_errors) : void {
  $data = 'Räme';
  $data = mb_convert_encoding($data, $file_properties['encoding']);
  file_put_contents($file_properties['uri'], $data);
  $file = File::create($file_properties);
  // Test for failure.
  $validators = [
    'FileEncoding' => [
      'encodings' => $encodings,
    ],
  ];
  $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.