function TokenReplaceKernelTest::testClear

Same name in other branches
  1. 8.9.x 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 60

Class

TokenReplaceKernelTest
Tests 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->assertEquals($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->assertEquals($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.