function TokenReplaceKernelTest::testSystemTokenRecognition

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

Tests whether token-replacement works in various contexts.

File

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

Class

TokenReplaceKernelTest
Tests token replacement.

Namespace

Drupal\Tests\system\Kernel\Token

Code

public function testSystemTokenRecognition() : void {
    // Generate prefixes and suffixes for the token context.
    $tests = [
        [
            'prefix' => 'this is the ',
            'suffix' => ' site',
        ],
        [
            'prefix' => 'this is the',
            'suffix' => 'site',
        ],
        [
            'prefix' => '[',
            'suffix' => ']',
        ],
        [
            'prefix' => '',
            'suffix' => ']]]',
        ],
        [
            'prefix' => '[[[',
            'suffix' => '',
        ],
        [
            'prefix' => ':[:',
            'suffix' => '--]',
        ],
        [
            'prefix' => '-[-',
            'suffix' => ':]:',
        ],
        [
            'prefix' => '[:',
            'suffix' => ']',
        ],
        [
            'prefix' => '[site:',
            'suffix' => ':name]',
        ],
        [
            '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, [], [
            'langcode' => $this->interfaceLanguage
                ->getId(),
        ]);
        $this->assertSame($expected, $output, "Token recognized in string {$input}");
    }
    // Test token replacement when the string contains no tokens.
    $this->assertEquals('No tokens here.', $this->tokenService
        ->replace('No tokens here.'));
}

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