You are here

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

Tests the generation of all system date tokens.

File

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

  // Set time to one hour before request.
  $date = REQUEST_TIME - 3600;

  // Generate and test tokens.
  $tests = array();
  $date_formatter = \Drupal::service('date.formatter');
  $tests['[date:short]'] = $date_formatter
    ->format($date, 'short', '', NULL, $this->interfaceLanguage
    ->getId());
  $tests['[date:medium]'] = $date_formatter
    ->format($date, 'medium', '', NULL, $this->interfaceLanguage
    ->getId());
  $tests['[date:long]'] = $date_formatter
    ->format($date, 'long', '', NULL, $this->interfaceLanguage
    ->getId());
  $tests['[date:custom:m/j/Y]'] = $date_formatter
    ->format($date, 'custom', 'm/j/Y', NULL, $this->interfaceLanguage
    ->getId());
  $tests['[date:since]'] = $date_formatter
    ->formatTimeDiffSince($date, array(
    'langcode' => $this->interfaceLanguage
      ->getId(),
  ));
  $tests['[date:raw]'] = Xss::filter($date);

  // 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 = $this->tokenService
      ->replace($input, array(
      'date' => $date,
    ), array(
      'langcode' => $this->interfaceLanguage
        ->getId(),
    ));
    $this
      ->assertEqual($output, $expected, format_string('Date token %token replaced.', array(
      '%token' => $input,
    )));
  }
}