You are here

function DateTimeFieldTest::testDatetimeField in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/datetime/src/Tests/DateTimeFieldTest.php \Drupal\datetime\Tests\DateTimeFieldTest::testDatetimeField()

Tests date and time field.

File

core/modules/datetime/src/Tests/DateTimeFieldTest.php, line 246
Contains \Drupal\datetime\Tests\DateTimeFieldTest.

Class

DateTimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testDatetimeField() {
  $field_name = $this->fieldStorage
    ->getName();

  // Change the field to a datetime field.
  $this->fieldStorage
    ->setSetting('datetime_type', 'datetime');
  $this->fieldStorage
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
  $this
    ->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');

  // Build up a date in the UTC timezone.
  $value = '2012-12-31 00:00:00';
  $date = new DrupalDateTime($value, 'UTC');

  // Update the timezone to the system default.
  $date
    ->setTimezone(timezone_open(drupal_get_user_timezone()));

  // Submit a valid date and ensure it is accepted.
  $date_format = entity_load('date_format', 'html_date')
    ->getPattern();
  $time_format = entity_load('date_format', 'html_time')
    ->getPattern();
  $edit = array(
    "{$field_name}[0][value][date]" => $date
      ->format($date_format),
    "{$field_name}[0][value][time]" => $date
      ->format($time_format),
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )));
  $this
    ->assertRaw($date
    ->format($date_format));
  $this
    ->assertRaw($date
    ->format($time_format));

  // Verify that the date is output according to the formatter settings.
  $options = array(
    'format_type' => array(
      'short',
      'medium',
      'long',
    ),
  );
  foreach ($options as $setting => $values) {
    foreach ($values as $new_value) {

      // Update the entity display settings.
      $this->displayOptions['settings'] = array(
        $setting => $new_value,
      ) + $this->defaultSettings;
      entity_get_display($this->field
        ->getTargetEntityTypeId(), $this->field
        ->getTargetBundle(), 'full')
        ->setComponent($field_name, $this->displayOptions)
        ->save();
      $this
        ->renderTestEntity($id);
      switch ($setting) {
        case 'format_type':

          // Verify that a date is displayed.
          $expected = format_date($date
            ->getTimestamp(), $new_value);
          $this
            ->renderTestEntity($id);
          $this
            ->assertText($expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected.', array(
            '%value' => $new_value,
            '%expected' => $expected,
          )));
          break;
      }
    }
  }

  // Verify that the plain formatter works.
  $this->displayOptions['type'] = 'datetime_plain';
  $this->displayOptions['settings'] = $this->defaultSettings;
  entity_get_display($this->field
    ->getTargetEntityTypeId(), $this->field
    ->getTargetBundle(), 'full')
    ->setComponent($field_name, $this->displayOptions)
    ->save();
  $expected = $date
    ->format(DATETIME_DATETIME_STORAGE_FORMAT);
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', array(
    '%expected' => $expected,
  )));

  // Verify that the 'datetime_custom' formatter works.
  $this->displayOptions['type'] = 'datetime_custom';
  $this->displayOptions['settings'] = array(
    'date_format' => 'm/d/Y g:i:s A',
  ) + $this->defaultSettings;
  entity_get_display($this->field
    ->getTargetEntityTypeId(), $this->field
    ->getTargetBundle(), 'full')
    ->setComponent($field_name, $this->displayOptions)
    ->save();
  $expected = $date
    ->format($this->displayOptions['settings']['date_format']);
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array(
    '%expected' => $expected,
  )));

  // Verify that the 'timezone_override' setting works.
  $this->displayOptions['type'] = 'datetime_custom';
  $this->displayOptions['settings'] = array(
    'date_format' => 'm/d/Y g:i:s A',
    'timezone_override' => 'America/New_York',
  ) + $this->defaultSettings;
  entity_get_display($this->field
    ->getTargetEntityTypeId(), $this->field
    ->getTargetBundle(), 'full')
    ->setComponent($field_name, $this->displayOptions)
    ->save();
  $expected = $date
    ->format($this->displayOptions['settings']['date_format'], array(
    'timezone' => 'America/New_York',
  ));
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array(
    '%expected' => $expected,
  )));

  // Verify that the 'datetime_time_ago' formatter works for intervals in the
  // past.  First update the test entity so that the date difference always
  // has the same interval.  Since the database always stores UTC, and the
  // interval will use this, force the test date to use UTC and not the local
  // or user timezome.
  $timestamp = REQUEST_TIME - 87654321;
  $entity = entity_load('entity_test', $id);
  $field_name = $this->fieldStorage
    ->getName();
  $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
  $entity->{$field_name}->value = $date
    ->format(DATETIME_DATETIME_STORAGE_FORMAT);
  $entity
    ->save();
  $this->displayOptions['type'] = 'datetime_time_ago';
  $this->displayOptions['settings'] = array(
    'future_format' => '@interval from now',
    'past_format' => '@interval earlier',
    'granularity' => 3,
  );
  entity_get_display($this->field
    ->getTargetEntityTypeId(), $this->field
    ->getTargetBundle(), 'full')
    ->setComponent($field_name, $this->displayOptions)
    ->save();
  $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
    '@interval' => \Drupal::service('date.formatter')
      ->formatTimeDiffSince($timestamp, [
      'granularity' => $this->displayOptions['settings']['granularity'],
    ]),
  ]);
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array(
    '%expected' => $expected,
  )));

  // Verify that the 'datetime_time_ago' formatter works for intervals in the
  // future.  First update the test entity so that the date difference always
  // has the same interval.  Since the database always stores UTC, and the
  // interval will use this, force the test date to use UTC and not the local
  // or user timezome.
  $timestamp = REQUEST_TIME + 87654321;
  $entity = entity_load('entity_test', $id);
  $field_name = $this->fieldStorage
    ->getName();
  $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
  $entity->{$field_name}->value = $date
    ->format(DATETIME_DATETIME_STORAGE_FORMAT);
  $entity
    ->save();
  entity_get_display($this->field
    ->getTargetEntityTypeId(), $this->field
    ->getTargetBundle(), 'full')
    ->setComponent($field_name, $this->displayOptions)
    ->save();
  $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
    '@interval' => \Drupal::service('date.formatter')
      ->formatTimeDiffUntil($timestamp, [
      'granularity' => $this->displayOptions['settings']['granularity'],
    ]),
  ]);
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array(
    '%expected' => $expected,
  )));
}