You are here

public function PathautoKernelTest::testCleanString in Pathauto 8

Test pathauto_cleanstring().

File

tests/src/Kernel/PathautoKernelTest.php, line 191

Class

PathautoKernelTest
Unit tests for Pathauto functions.

Namespace

Drupal\Tests\pathauto\Kernel

Code

public function testCleanString() {

  // Test with default settings defined in pathauto.settings.yml.
  $this
    ->installConfig([
    'pathauto',
  ]);

  // Add a custom setting for the copyright symbol defined in
  // pathauto_custom_punctuation_test_pathauto_punctuation_chars_alter().
  $this
    ->config('pathauto.settings')
    ->set('punctuation.copyright', PathautoGeneratorInterface::PUNCTUATION_REMOVE);
  \Drupal::service('pathauto.generator')
    ->resetCaches();
  $tests = [];

  // Test the 'ignored words' removal.
  $tests['this'] = 'this';
  $tests['this with that'] = 'this-with-that';
  $tests['this thing with that thing'] = 'thing-thing';

  // Test 'ignored words' removal and duplicate separator removal.
  $tests[' - Pathauto is the greatest - module ever - '] = 'pathauto-greatest-module-ever';

  // Test length truncation and lowering of strings.
  $long_string = $this
    ->randomMachineName(120);
  $tests[$long_string] = strtolower(substr($long_string, 0, 100));

  // Test that HTML tags are removed.
  $tests['This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.'] = 'text-has-html-tags';
  $tests[Html::escape('This <span class="text">text</span> has <br /><a href="http://example.com"><strong>HTML tags</strong></a>.')] = 'text-has-html-tags';

  // Transliteration.
  $tests['ľščťžýáíéňô'] = 'lsctzyaieno';

  // Transliteration of special chars that are converted to punctuation.
  $tests['© “Drupal”'] = 'drupal';
  foreach ($tests as $input => $expected) {
    $output = \Drupal::service('pathauto.alias_cleaner')
      ->cleanString($input);
    $this
      ->assertEquals($expected, $output, t("Drupal::service('pathauto.alias_cleaner')->cleanString('@input') expected '@expected', actual '@output'", [
      '@input' => $input,
      '@expected' => $expected,
      '@output' => $output,
    ]));
  }
}