function TokenReplaceKernelTest::testSystemDateTokenReplacement

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testSystemDateTokenReplacement()
  2. 8.9.x core/modules/system/tests/src/Kernel/Token/TokenReplaceKernelTest.php \Drupal\Tests\system\Kernel\Token\TokenReplaceKernelTest::testSystemDateTokenReplacement()
  3. 10 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 159

Class

TokenReplaceKernelTest
Tests token replacement.

Namespace

Drupal\Tests\system\Kernel\Token

Code

public function testSystemDateTokenReplacement() : void {
    // Set time to one hour before request.
    $date = \Drupal::time()->getRequestTime() - 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, "Date token {$input} replaced.");
    }
}

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