function DatetimeFieldTest::testDatetimeField

Tests date and time field.

File

drupal/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php, line 152
Contains \Drupal\datetime\Tests\DatetimeFieldTest.

Class

DatetimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testDatetimeField() {

  // Change the field to a datetime field.
  $this->field['settings']['datetime_type'] = 'datetime';
  field_update_field($this->field);

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

  // Submit a valid date and ensure it is accepted.
  $value = '2012-12-31 00:00:00';
  $date = new DrupalDateTime($value);
  $format_type = $date
    ->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP;
  $date_format = config('system.date')
    ->get('formats.html_date.pattern.' . $format_type);
  $time_format = config('system.date')
    ->get('formats.html_time.pattern.' . $format_type);
  $edit = array(
    'user_id' => 1,
    'name' => $this
      ->randomName(),
    "{$this->field['field_name']}[{$langcode}][0][value][date]" => $date
      ->format($date_format),
    "{$this->field['field_name']}[{$langcode}][0][value][time]" => $date
      ->format($time_format),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)/edit|', $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->display_options['settings'] = array(
        $setting => $new_value,
      );
      $display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
        ->setComponent($this->instance['field_name'], $this->display_options)
        ->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, format_string('Formatted date field using %value format displayed as %expected.', array(
            '%value' => $new_value,
            '%expected' => $expected,
          )));
          break;
      }
    }
  }

  // Verify that the plain formatter works.
  $this->display_options['type'] = 'datetime_plain';
  $display = entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'full')
    ->setComponent($this->instance['field_name'], $this->display_options)
    ->save();
  $expected = $date
    ->format(DATETIME_DATETIME_STORAGE_FORMAT);
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, format_string('Formatted date field using plain format displayed as %expected.', array(
    '%expected' => $expected,
  )));
}