function TokenReplaceKernelTest::testSystemSiteTokenReplacement

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

Tests the generation of all system site information tokens.

File

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

Class

TokenReplaceKernelTest
Tests token replacement.

Namespace

Drupal\Tests\system\Kernel\Token

Code

public function testSystemSiteTokenReplacement() : void {
    $url_options = [
        'absolute' => TRUE,
        'language' => $this->interfaceLanguage,
    ];
    $slogan = '<blink>Slogan</blink>';
    $safe_slogan = Xss::filterAdmin($slogan);
    // Set a few site variables.
    $config = $this->config('system.site');
    $config->set('name', '<strong>Drupal<strong>')
        ->set('slogan', $slogan)
        ->set('mail', 'simpletest@example.com')
        ->save();
    // Generate and test tokens.
    $tests = [];
    $tests['[site:name]'] = Html::escape($config->get('name'));
    $tests['[site:slogan]'] = $safe_slogan;
    $tests['[site:mail]'] = $config->get('mail');
    $tests['[site:url]'] = Url::fromRoute('<front>', [], $url_options)->toString();
    $tests['[site:url-brief]'] = preg_replace([
        '!^https?://!',
        '!/$!',
    ], '', Url::fromRoute('<front>', [], $url_options)->toString());
    $tests['[site:base-url]'] = 'http://localhost';
    $tests['[site:login-url]'] = Url::fromRoute('user.page', [], $url_options)->toString();
    $base_bubbleable_metadata = new BubbleableMetadata();
    $metadata_tests = [];
    $metadata_tests['[site:name]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
    $metadata_tests['[site:slogan]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
    $metadata_tests['[site:mail]'] = BubbleableMetadata::createFromObject(\Drupal::config('system.site'));
    $bubbleable_metadata = clone $base_bubbleable_metadata;
    $metadata_tests['[site:base-url]'] = $bubbleable_metadata->addCacheContexts([
        'url.site',
    ]);
    $metadata_tests['[site:url]'] = $bubbleable_metadata->addCacheContexts([
        'url.site',
    ]);
    $metadata_tests['[site:url-brief]'] = $bubbleable_metadata;
    $metadata_tests['[site:login-url]'] = $bubbleable_metadata;
    // Test to make sure that we generated something for each token.
    $this->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
    foreach ($tests as $input => $expected) {
        $bubbleable_metadata = new BubbleableMetadata();
        $output = $this->tokenService
            ->replace($input, [], [
            'langcode' => $this->interfaceLanguage
                ->getId(),
        ], $bubbleable_metadata);
        $this->assertEquals($expected, $output, "System site information token {$input} replaced.");
        $this->assertEquals($metadata_tests[$input], $bubbleable_metadata);
    }
    // Test [site:base-url] and [site:base-path] token with a subdirectory.
    $request_stack = \Drupal::requestStack();
    // Test request with subdirectory on homepage.
    $server = [
        'SCRIPT_NAME' => '/subdir/index.php',
        'SCRIPT_FILENAME' => $this->root . '/subdir/index.php',
        'SERVER_NAME' => 'http://localhost',
    ];
    $request = Request::create('/subdir/', 'GET', [], [], [], $server);
    $request->setSession(new Session(new MockArraySessionStorage()));
    $request->server
        ->add($server);
    $request_stack->push($request);
    $bubbleable_metadata = new BubbleableMetadata();
    $this->container
        ->get('router.request_context')
        ->setCompleteBaseUrl('http://localhost/subdir');
    $this->assertEquals('http://localhost/subdir', $this->tokenService
        ->replace('[site:base-url]', [], [
        'langcode' => $this->interfaceLanguage
            ->getId(),
    ], $bubbleable_metadata));
    $this->assertEquals((new BubbleableMetadata())->addCacheContexts([
        'url.site',
    ]), $bubbleable_metadata);
    $bubbleable_metadata = new BubbleableMetadata();
    $this->assertEquals('/subdir', $this->tokenService
        ->replace('[site:base-path]', [], [
        'langcode' => $this->interfaceLanguage
            ->getId(),
    ], $bubbleable_metadata));
    $this->assertEquals((new BubbleableMetadata())->addCacheContexts([
        'url.site',
    ]), $bubbleable_metadata);
}

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