function DatetimeFieldTest::testDateField

Tests date field functionality.

File

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

Class

DatetimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testDateField() {

  // 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
    ->assertNoFieldByName("{$this->field['field_name']}[{$langcode}][0][value][time]", '', 'Time element not 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
    ->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
    ->assertNoRaw($date
    ->format($time_format));

  // The expected values will use the default time.
  datetime_date_default_time($date);

  // 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_DATE_STORAGE_FORMAT);
  $this
    ->renderTestEntity($id);
  $this
    ->assertText($expected, format_string('Formatted date field using plain format displayed as %expected.', array(
    '%expected' => $expected,
  )));
}