function TokenReplaceTest::testSystemSiteTokenReplacement

Tests the generation of all system site information tokens.

File

drupal/core/modules/system/lib/Drupal/system/Tests/System/TokenReplaceTest.php, line 106
Definition of Drupal\system\Tests\System\TokenReplaceTest.

Class

TokenReplaceTest
Test token replacement in strings.

Namespace

Drupal\system\Tests\System

Code

function testSystemSiteTokenReplacement() {
  $language_interface = language(LANGUAGE_TYPE_INTERFACE);
  $url_options = array(
    'absolute' => TRUE,
    'language' => $language_interface,
  );

  // Set a few site variables.
  config('system.site')
    ->set('name', '<strong>Drupal<strong>')
    ->set('slogan', '<blink>Slogan</blink>')
    ->save();

  // Generate and test sanitized tokens.
  $tests = array();
  $tests['[site:name]'] = check_plain(config('system.site')
    ->get('name'));
  $tests['[site:slogan]'] = filter_xss_admin(config('system.site')
    ->get('slogan'));
  $tests['[site:mail]'] = 'simpletest@example.com';
  $tests['[site:url]'] = url('<front>', $url_options);
  $tests['[site:url-brief]'] = preg_replace(array(
    '!^https?://!',
    '!/$!',
  ), '', url('<front>', $url_options));
  $tests['[site:login-url]'] = url('user', $url_options);

  // 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) {
    $output = token_replace($input, array(), array(
      'langcode' => $language_interface->langcode,
    ));
    $this
      ->assertEqual($output, $expected, format_string('Sanitized system site information token %token replaced.', array(
      '%token' => $input,
    )));
  }

  // Generate and test unsanitized tokens.
  $tests['[site:name]'] = config('system.site')
    ->get('name');
  $tests['[site:slogan]'] = config('system.site')
    ->get('slogan');
  foreach ($tests as $input => $expected) {
    $output = token_replace($input, array(), array(
      'langcode' => $language_interface->langcode,
      'sanitize' => FALSE,
    ));
    $this
      ->assertEqual($output, $expected, format_string('Unsanitized system site information token %token replaced.', array(
      '%token' => $input,
    )));
  }
}