function TokenReplaceKernelTest::testSystemDateTokenReplacement

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

Class

TokenReplaceKernelTest
Tests 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,
        ]));
    }
}

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