SanitizeNameTest.php 1.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

namespace Drupal\Tests\transliterate_filenames\Unit;

use Drupal\Component\Transliteration\PhpTransliteration;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\transliterate_filenames\SanitizeName;

/**
 * @coversDefaultClass \Drupal\transliterate_filenames\SanitizeName
 * @group transliterate_filenames
 */
class SanitizeNameTest extends UnitTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $container = new ContainerBuilder();
    \Drupal::setContainer($container);

    $container->set('transliterate_filenames.sanitize_name', new SanitizeName(new PhpTransliteration()));
  }

  /**
   * Tests sanitize filename.
   *
   * @param string $filename
   *   The tested file name.
   * @param string $expected
   *   The expected name of sanitized file.
   *
   * @dataProvider providerSanitizeName
   */
  public function testSanitizeName($filename, $expected) {
    $sanitize_filename = \Drupal::service('transliterate_filenames.sanitize_name');
    $this->assertEquals($expected, $sanitize_filename->sanitizeFilename($filename));
  }

  /**
   * Provides data for self::testSanitizeName().
   */
  public function providerSanitizeName() {
    return [
48
      // Transliterate Non-US-ASCII.
49
      ['ąęółżźćśń.pdf', 'aeolzzcsn.pdf'],
50
51
52
53
54
55
56
57
58
59
      // Remove unknown unicodes.
      [chr(0xF8) . chr(0x80) . chr(0x80) . '.txt', '.txt'],
      // Force lowercase.
      ['LOWERCASE.txt', 'lowercase.txt'],
      // Replace whitespace.
      ['test whitespace.txt', 'test-whitespace.txt'],
      ['test   whitespace.txt', 'test-whitespace.txt'],
      // Remove multiple consecutive non-alphabetical characters.
      ['---___.txt', '-_.txt'],
      ['--  --.txt', '-.txt'],
60
61
62
63
    ];
  }

}