You are here

public function TokenReplaceUnitTest::testSystemTokenRecognition in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/System/TokenReplaceUnitTest.php \Drupal\system\Tests\System\TokenReplaceUnitTest::testSystemTokenRecognition()

Test whether token-replacement works in various contexts.

File

core/modules/system/src/Tests/System/TokenReplaceUnitTest.php, line 35
Contains \Drupal\system\Tests\System\TokenReplaceUnitTest.

Class

TokenReplaceUnitTest
Generates text using placeholders for dummy content to check token replacement.

Namespace

Drupal\system\Tests\System

Code

public function testSystemTokenRecognition() {

  // 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 = $this->tokenService
      ->replace($input, array(), array(
      'langcode' => $this->interfaceLanguage
        ->getId(),
    ));
    $this
      ->assertTrue($output == $expected, format_string('Token recognized in string %string', array(
      '%string' => $input,
    )));
  }

  // Test token replacement when the string contains no tokens.
  $this
    ->assertEqual($this->tokenService
    ->replace('No tokens here.'), 'No tokens here.');
}