You are here

public function TokenReplaceUnitTest::testSystemSiteTokenReplacement 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::testSystemSiteTokenReplacement()

Tests the generation of all system site information tokens.

File

core/modules/system/src/Tests/System/TokenReplaceUnitTest.php, line 87
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 testSystemSiteTokenReplacement() {

  // The use of the \Drupal::url() method requires the url_alias table to exist.
  $this
    ->installSchema('system', 'url_alias');
  $url_options = array(
    '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 = array();
  $tests['[site:name]'] = Html::escape($config
    ->get('name'));
  $tests['[site:slogan]'] = $safe_slogan;
  $tests['[site:mail]'] = $config
    ->get('mail');
  $tests['[site:url]'] = \Drupal::url('<front>', [], $url_options);
  $tests['[site:url-brief]'] = preg_replace(array(
    '!^https?://!',
    '!/$!',
  ), '', \Drupal::url('<front>', [], $url_options));
  $tests['[site:login-url]'] = \Drupal::url('user.page', [], $url_options);
  $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: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
    ->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
  foreach ($tests as $input => $expected) {
    $bubbleable_metadata = new BubbleableMetadata();
    $output = $this->tokenService
      ->replace($input, array(), array(
      'langcode' => $this->interfaceLanguage
        ->getId(),
    ), $bubbleable_metadata);
    $this
      ->assertEqual($output, $expected, new FormattableMarkup('System site information token %token replaced.', [
      '%token' => $input,
    ]));
    $this
      ->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
  }
}