function TimestampFormatterWithTimeDiffViewsTest::testTimestampFormatterWithTimeDiff

Same name in other branches
  1. 10 core/tests/Drupal/FunctionalJavascriptTests/Core/Field/TimestampFormatterWithTimeDiffViewsTest.php \Drupal\FunctionalJavascriptTests\Core\Field\TimestampFormatterWithTimeDiffViewsTest::testTimestampFormatterWithTimeDiff()

Tests the timestamp formatter used with time difference setting in views.

File

core/tests/Drupal/FunctionalJavascriptTests/Core/Field/TimestampFormatterWithTimeDiffViewsTest.php, line 38

Class

TimestampFormatterWithTimeDiffViewsTest
Tests the timestamp formatter used with time difference setting in views.

Namespace

Drupal\FunctionalJavascriptTests\Core\Field

Code

public function testTimestampFormatterWithTimeDiff() : void {
    $this->markTestSkipped("Skipped due to frequent random test failures. See https://www.drupal.org/project/drupal/issues/3400150");
    ViewTestData::createTestViews(self::class, [
        'views_test_formatter',
    ]);
    $data = $this->getRowData();
    // PHPStan requires non-empty data. Without this check complains, later,
    // that $delta and $time_diff might not be defined.
    \assert(!empty($data));
    // Create the entities.
    foreach ($data as $delta => $row) {
        EntityTest::create([
            'type' => 'test',
            // Using this also as field class.
'name' => "entity-{$delta}",
            'created' => $row['timestamp'],
        ])->save();
    }
    $this->drupalGet('formatter_timestamp_as_time_diff');
    $page = $this->getSession()
        ->getPage();
    foreach ($data as $delta => $row) {
        $time_diff = $page->find('css', ".entity-{$delta}")
            ->getText();
        $regex_pattern = "#{$row['pattern']}#";
        // Test that the correct time difference is displayed. Note that we are
        // able to check an exact match for rows that have a creation date more
        // distant, but we use regexp to check the entities that are only few
        // seconds away because of the latency introduced by the test run.
        $this->assertMatchesRegularExpression($regex_pattern, $time_diff);
    }
    // Wait at least 1 second + 1 millisecond to make sure the 'right now' time
    // difference was refreshed.
    $this->assertJsCondition("document.querySelector('.entity-{$delta} time').textContent >= '{$time_diff}'", 1001);
}

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