You are here

public function FieldDateTest::testFieldDate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/FieldDateTest.php \Drupal\views\Tests\Handler\FieldDateTest::testFieldDate()

Sets up functional test of the views date field.

File

core/modules/views/src/Tests/Handler/FieldDateTest.php, line 73
Contains \Drupal\views\Tests\Handler\FieldDateTest.

Class

FieldDateTest
Tests the core Drupal\views\Plugin\views\field\Date handler.

Namespace

Drupal\views\Tests\Handler

Code

public function testFieldDate() {
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', array(
    'created' => array(
      'id' => 'created',
      'table' => 'views_test_data',
      'field' => 'created',
      'relationship' => 'none',
      // ISO 8601 format @see http://php.net/manual/en/function.date.php
      'custom_date_format' => 'c',
    ),
    'destroyed' => array(
      'id' => 'destroyed',
      'table' => 'views_test_data',
      'field' => 'destroyed',
      'relationship' => 'none',
      'custom_date_format' => 'c',
    ),
  ));
  $time = gmmktime(0, 0, 0, 1, 1, 2000);
  $this
    ->executeView($view);
  $timezones = array(
    NULL,
    'UTC',
    'America/New_York',
  );

  // Check each date/time in various timezones.
  foreach ($timezones as $timezone) {
    $dates = array(
      'short' => format_date($time, 'short', '', $timezone),
      'medium' => format_date($time, 'medium', '', $timezone),
      'long' => format_date($time, 'long', '', $timezone),
      'custom' => format_date($time, 'custom', 'c', $timezone),
      'fallback' => format_date($time, 'fallback', '', $timezone),
      'html_date' => format_date($time, 'html_date', '', $timezone),
      'html_datetime' => format_date($time, 'html_datetime', '', $timezone),
      'html_month' => format_date($time, 'html_month', '', $timezone),
      'html_time' => format_date($time, 'html_time', '', $timezone),
      'html_week' => format_date($time, 'html_week', '', $timezone),
      'html_year' => format_date($time, 'html_year', '', $timezone),
      'html_yearless_date' => format_date($time, 'html_yearless_date', '', $timezone),
    );
    $this
      ->assertRenderedDatesEqual($view, $dates, $timezone);
  }

  // Check times in the past.
  $time_since = $this->container
    ->get('date.formatter')
    ->formatTimeDiffSince($time);
  $intervals = array(
    'raw time ago' => $time_since,
    'time ago' => t('%time ago', array(
      '%time' => $time_since,
    )),
    'raw time span' => $time_since,
    'inverse time span' => -$time_since,
    'time span' => t('%time ago', array(
      '%time' => $time_since,
    )),
  );
  $this
    ->assertRenderedDatesEqual($view, $intervals);

  // Check times in the future.
  $time = gmmktime(0, 0, 0, 1, 1, 2050);
  $formatted = $this->container
    ->get('date.formatter')
    ->formatTimeDiffUntil($time);
  $intervals = array(
    'raw time span' => -$formatted,
    'time span' => t('%time hence', array(
      '%time' => $formatted,
    )),
  );
  $this
    ->assertRenderedFutureDatesEqual($view, $intervals);
}