You are here

function PluginsTest::testEscaping in Translation Management Tool 8

Tests escaping and unescaping text.

File

tests/src/Kernel/PluginsTest.php, line 139

Class

PluginsTest
Tests interaction between core and the plugins.

Namespace

Drupal\Tests\tmgmt\Kernel

Code

function testEscaping() {
  $plugin = $this->default_translator
    ->getPlugin();
  $tests = array(
    array(
      'item' => array(
        '#text' => 'no escaping',
      ),
      'expected' => 'no escaping',
    ),
    array(
      'item' => array(
        '#text' => 'single placeholder',
        '#escape' => array(
          7 => array(
            'string' => 'placeholder',
          ),
        ),
      ),
      'expected' => 'single [[[placeholder]]]',
    ),
    array(
      'item' => array(
        '#text' => 'two placeholder, the second placeholder',
        '#escape' => array(
          4 => array(
            'string' => 'placeholder',
          ),
          28 => array(
            'string' => 'placeholder',
          ),
        ),
      ),
      'expected' => 'two [[[placeholder]]], the second [[[placeholder]]]',
    ),
    array(
      'item' => array(
        '#text' => 'something, something else',
        '#escape' => array(
          0 => array(
            'string' => 'something',
          ),
          21 => array(
            'string' => 'else',
          ),
        ),
      ),
      'expected' => '[[[something]]], something [[[else]]]',
    ),
    array(
      'item' => array(
        '#text' => 'something, something else',
        '#escape' => array(
          21 => array(
            'string' => 'else',
          ),
          11 => array(
            'string' => 'something',
          ),
        ),
      ),
      'expected' => 'something, [[[something]]] [[[else]]]',
    ),
  );
  foreach ($tests as $test) {
    $escaped = $plugin
      ->escapeText($test['item']);

    // Assert that the string was escaped as expected.
    $this
      ->assertEqual($escaped, $test['expected']);

    // Assert that the string is the same as the original when unescaped.
    $this
      ->assertEqual($plugin
      ->unescapeText($escaped), $test['item']['#text']);
  }
}