You are here

public function TokenReplaceKernelTest::testSystemDateTokenReplacement in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testSystemDateTokenReplacement()

Tests the generation of all system date tokens.

File

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

Class

TokenReplaceKernelTest
Generates text using placeholders for dummy content to check token replacement.

Namespace

Drupal\Tests\system\Kernel\Token

Code

public function testSystemDateTokenReplacement() {

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

  // Generate and test tokens.
  $tests = [];
  $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, [
    'langcode' => $this->interfaceLanguage
      ->getId(),
  ]);
  $tests['[date:raw]'] = Xss::filter($date);

  // 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) {
    $output = $this->tokenService
      ->replace($input, [
      'date' => $date,
    ], [
      'langcode' => $this->interfaceLanguage
        ->getId(),
    ]);
    $this
      ->assertEquals($expected, $output, new FormattableMarkup('Date token %token replaced.', [
      '%token' => $input,
    ]));
  }
}