function TokenReplaceTest::testSystemTokenRecognition

Test whether token-replacement works in various contexts.

File

drupal/core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php, line 80
Definition of Drupal\system\Tests\System\TokenReplaceTest.

Class

TokenReplaceTest
Test token replacement in strings.

Namespace

Drupal\system\Tests\System

Code

function testSystemTokenRecognition() {
  $token_service = \Drupal::token();
  $language_interface = language(Language::TYPE_INTERFACE);

  // Generate prefixes and suffixes for the token context.
  $tests = array(
    array(
      'prefix' => 'this is the ',
      'suffix' => ' site',
    ),
    array(
      'prefix' => 'this is the',
      'suffix' => 'site',
    ),
    array(
      'prefix' => '[',
      'suffix' => ']',
    ),
    array(
      'prefix' => '',
      'suffix' => ']]]',
    ),
    array(
      'prefix' => '[[[',
      'suffix' => '',
    ),
    array(
      'prefix' => ':[:',
      'suffix' => '--]',
    ),
    array(
      'prefix' => '-[-',
      'suffix' => ':]:',
    ),
    array(
      'prefix' => '[:',
      'suffix' => ']',
    ),
    array(
      'prefix' => '[site:',
      'suffix' => ':name]',
    ),
    array(
      'prefix' => '[site:',
      'suffix' => ']',
    ),
  );

  // Check if the token is recognized in each of the contexts.
  foreach ($tests as $test) {
    $input = $test['prefix'] . '[site:name]' . $test['suffix'];
    $expected = $test['prefix'] . 'Drupal' . $test['suffix'];
    $output = $token_service
      ->replace($input, array(), array(
      'langcode' => $language_interface->langcode,
    ));
    $this
      ->assertTrue($output == $expected, format_string('Token recognized in string %string', array(
      '%string' => $input,
    )));
  }
}