function TokenReplaceKernelTest::testClear

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testClear()
  2. 10 core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testClear()
  3. 11.x core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testClear()

Tests the clear parameter.

File

core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php, line 61

Class

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

Namespace

Drupal\Tests\system\Kernel\Token

Code

public function testClear() {
    // Valid token.
    $source = '[site:name]';
    // No user passed in, should be untouched.
    $source .= '[user:name]';
    // Non-existing token.
    $source .= '[bogus:token]';
    // Replace with the clear parameter, only the valid token should remain.
    $target = Html::escape($this->config('system.site')
        ->get('name'));
    $result = $this->tokenService
        ->replace($source, [], [
        'langcode' => $this->interfaceLanguage
            ->getId(),
        'clear' => TRUE,
    ]);
    $this->assertEqual($target, $result, 'Valid tokens replaced while invalid tokens ignored.');
    $target .= '[user:name]';
    $target .= '[bogus:token]';
    $result = $this->tokenService
        ->replace($source, [], [
        'langcode' => $this->interfaceLanguage
            ->getId(),
    ]);
    $this->assertEqual($target, $result, 'Valid tokens replaced while invalid tokens ignored.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.