function DateTest::testFormattedDateDiff

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormattedDateDiff()
  2. 8.9.x core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormattedDateDiff()
  3. 11.x core/tests/Drupal/Tests/Core/Datetime/DateTest.php \Drupal\Tests\Core\Datetime\DateTest::testFormattedDateDiff()

Tests FormattedDateDiff.

@covers \Drupal\Core\Datetime\FormattedDateDiff::toRenderable
@covers \Drupal\Core\Datetime\FormattedDateDiff::getString
@covers \Drupal\Core\Datetime\FormattedDateDiff::getCacheMaxAge

File

core/tests/Drupal/Tests/Core/Datetime/DateTest.php, line 407

Class

DateTest
@coversDefaultClass \Drupal\Core\Datetime\DateFormatter[[api-linebreak]] @group Datetime

Namespace

Drupal\Tests\Core\Datetime

Code

public function testFormattedDateDiff() : void {
  $string = '10 minutes';
  $max_age = 60;
  $object = new FormattedDateDiff($string, $max_age);
  // Test conversion to a render array.
  $expected = [
    '#markup' => $string,
    '#cache' => [
      'max-age' => $max_age,
    ],
  ];
  $this->assertEquals($expected, $object->toRenderable());
  // Test retrieving the formatted time difference string.
  $this->assertEquals($string, $object->getString());
  // Test applying cacheability data to an existing build.
  $build = [];
  CacheableMetadata::createFromObject($object)->applyTo($build);
  $this->assertEquals($max_age, $build['#cache']['max-age']);
}

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