function SanitizeNameTest::testFileNameTransliteration

Same name and namespace in other branches
  1. 11.x core/modules/file/tests/src/Unit/SanitizeNameTest.php \Drupal\Tests\file\Unit\SanitizeNameTest::testFileNameTransliteration()

Test file name sanitization.

@dataProvider provideFilenames

@covers \Drupal\file\EventSubscriber\FileEventSubscriber::sanitizeFilename
@covers \Drupal\Core\File\Event\FileUploadSanitizeNameEvent::__construct

Parameters

string $original: The original filename.

string $expected: The expected filename.

array $options: Array of filename sanitization options, in this order: 0: boolean Transliterate. 1: string Replace whitespace. 2: string Replace non-alphanumeric characters. 3: boolean De-duplicate separators. 4: boolean Convert to lowercase.

string $language_id: Optional language code for transliteration. Defaults to 'en'.

File

core/modules/file/tests/src/Unit/SanitizeNameTest.php, line 46

Class

SanitizeNameTest
Filename sanitization tests.

Namespace

Drupal\Tests\file\Unit

Code

public function testFileNameTransliteration($original, $expected, array $options, $language_id = 'en') : void {
  $sanitization_options = [
    'transliterate' => $options[0],
    'replacement_character' => $options[1],
    'replace_whitespace' => $options[2],
    'replace_non_alphanumeric' => $options[3],
    'deduplicate_separators' => $options[4],
    'lowercase' => $options[5],
  ];
  $config_factory = $this->getConfigFactoryStub([
    'file.settings' => [
      'filename_sanitization' => $sanitization_options,
    ],
  ]);
  $language = new Language([
    'id' => $language_id,
  ]);
  $language_manager = $this->prophesize(LanguageManagerInterface::class);
  $language_manager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->willReturn($language);
  $event = new FileUploadSanitizeNameEvent($original, $language_id);
  $subscriber = new FileEventSubscriber($config_factory, new PhpTransliteration(), $language_manager->reveal());
  $subscriber->sanitizeFilename($event);
  // Check the results of the configured sanitization.
  $this->assertEquals($expected, $event->getFilename());
}

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