class FileEncodingConstraintValidatorTest
Tests the FileEncodingConstraintValidator.
@group file
@coversDefaultClass \Drupal\file\Plugin\Validation\Constraint\FileEncodingConstraintValidator
Hierarchy
- class \Drupal\Tests\file\Kernel\Validation\FileValidatorTestBase
- class \Drupal\Tests\file\Kernel\Plugin\Validation\Constraint\FileEncodingConstraintValidatorTest implements \Drupal\Tests\file\Kernel\Validation\FileValidatorTestBase
Expanded class hierarchy of FileEncodingConstraintValidatorTest
File
-
core/
modules/ file/ tests/ src/ Kernel/ Plugin/ Validation/ Constraint/ FileEncodingConstraintValidatorTest.php, line 18
Namespace
Drupal\Tests\file\Kernel\Plugin\Validation\ConstraintView source
class FileEncodingConstraintValidatorTest extends FileValidatorTestBase {
/**
* Tests the FileEncodingConstraintValidator.
*
* @param array $file_properties
* The properties of the file being validated.
* @param string[] $encodings
* An array of the allowed file encodings.
* @param string[] $expected_errors
* The expected error messages as string.
*
* @dataProvider providerTestFileValidateEncodings
* @covers ::validate
*/
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);
}
/**
* Data provider for ::testFileEncoding.
*
* @return array[][]
* The test cases.
*/
public static function providerTestFileValidateEncodings() : array {
$utf8_encoded_txt_file_properties = [
'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt',
'status' => 0,
'encoding' => 'UTF-8',
];
$windows1252_encoded_txt_file = [
'filename' => 'druplicon-win.txt',
'uri' => 'public://druplicon-win.txt',
'status' => 1,
'encoding' => 'windows-1252',
];
return [
'UTF-8 encoded file validated with "UTF-8" encoding' => [
'file_properties' => $utf8_encoded_txt_file_properties,
'encodings' => [
'UTF-8',
],
'expected_errors' => [],
],
'Windows-1252 encoded file validated with "UTF-8" encoding' => [
'file_properties' => $windows1252_encoded_txt_file,
'encodings' => [
'UTF-8',
],
'expected_errors' => [
'The file is encoded with ASCII. It must be encoded with UTF-8',
],
],
];
}
/**
* Helper function that returns a .po file with invalid encoding.
*/
public function getInvalidEncodedPoFile() {
return <<<EOF
msgid ""
msgstr ""
"Project-Id-Version: Drupal 8\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=Windows-1252\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\\n"
msgid "Swamp"
msgstr "Räme"
EOF;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
ExtensionListTestTrait::getModulePath | protected | function | Gets the path for the specified module. | |
ExtensionListTestTrait::getThemePath | protected | function | Gets the path for the specified theme. | |
FileEncodingConstraintValidatorTest::getInvalidEncodedPoFile | public | function | Helper function that returns a .po file with invalid encoding. | |
FileEncodingConstraintValidatorTest::providerTestFileValidateEncodings | public static | function | Data provider for ::testFileEncoding. | |
FileEncodingConstraintValidatorTest::testFileEncodings | public | function | Tests the FileEncodingConstraintValidator. | |
FileValidatorTestBase::$file | protected | property | The file. | |
FileValidatorTestBase::$modules | protected static | property | Modules to install. | 1 |
FileValidatorTestBase::$validator | protected | property | The file validator. | |
FileValidatorTestBase::setUp | protected | function | 2 | |
StorageCopyTrait::replaceStorageContents | protected static | function | Copy the configuration from one storage to another and remove stale items. | |
TestRequirementsTrait::getDrupalRoot | protected static | function | Returns the Drupal root directory. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.