function TimestampFormatterWithTimeDiffTest::testTimestampFormatterWithTimeDiff

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

Tests the 'timestamp' formatter when is used with time difference setting.

File

core/tests/Drupal/FunctionalJavascriptTests/Core/Field/TimestampFormatterWithTimeDiffTest.php, line 90

Class

TimestampFormatterWithTimeDiffTest
Tests the 'timestamp' formatter when is used with time difference setting.

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");
    $this->drupalGet($this->entity
        ->toUrl());
    // Unit testing Drupal.timeDiff.format(). Not using @dataProvider mechanism
    // here in order to avoid installing the site for each case.
    foreach ($this->getFormatDiffTestCases() as $case) {
        $from = \DateTime::createFromFormat(\DateTimeInterface::RFC3339, $case['from'])->getTimestamp();
        $to = \DateTime::createFromFormat(\DateTimeInterface::RFC3339, $case['to'])->getTimestamp();
        $diff = $to - $from;
        $options = json_encode($case['options']);
        $expected_value = json_encode($case['expected_value']);
        $expected_formatted_value = $case['expected_formatted_value'];
        // Test the returned value.
        $this->assertJsCondition("JSON.stringify(Drupal.timeDiff.format({$diff}, {$options}).value) === '{$expected_value}'");
        // Test the returned formatted value.
        $this->assertJsCondition("Drupal.timeDiff.format({$diff}, {$options}).formatted === '{$expected_formatted_value}'");
    }
    // Unit testing Drupal.timeDiff.refreshInterval(). Not using @dataProvider
    // mechanism here in order to avoid reinstalling the site for each case.
    foreach ($this->getRefreshIntervalTestCases() as $case) {
        $interval = json_encode($case['time_diff']);
        $this->assertJsCondition("Drupal.timeDiff.refreshInterval({$interval}, {$case['configured_refresh_interval']}, {$case['granularity']}) === {$case['computed_refresh_interval']}");
    }
    // Test the UI.
    $time_element = $this->getSession()
        ->getPage()
        ->find('css', 'time');
    $time_diff = $time_element->getText();
    [
        $seconds_value,
    ] = explode(' ', $time_diff, 2);
    // Wait at least 1 second + 1 millisecond to make sure that the last time
    // difference value has been refreshed.
    $this->assertJsCondition("document.getElementsByTagName('time')[0].textContent != '{$time_diff}'", 1001);
    $time_diff = $time_element->getText();
    [
        $new_seconds_value,
    ] = explode(' ', $time_diff, 2);
    $this->assertGreaterThan($seconds_value, $new_seconds_value);
    // Once again.
    $this->assertJsCondition("document.getElementsByTagName('time')[0].textContent != '{$time_diff}'", 1001);
    $time_diff = $time_element->getText();
    $seconds_value = $new_seconds_value;
    [
        $new_seconds_value,
    ] = explode(' ', $time_diff, 2);
    $this->assertGreaterThan($seconds_value, $new_seconds_value);
}

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