function DatetimeFieldTest::testDatelistWidget

Tests Date List Widget functionality.

File

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

Class

DatetimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testDatelistWidget() {

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

  // Change the widget to a datelist widget.
  entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
    ->setComponent($this->instance['field_name'], array(
    'type' => 'datetime_datelist',
    'settings' => array(
      'increment' => 1,
      'date_order' => 'YMD',
      'time_type' => '12',
    ),
  ))
    ->save();
  field_cache_clear();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $field_name = $this->field['field_name'];
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $this
    ->assertFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-year\"]", NULL, 'Year element found.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-year", '', 'No year selected.');
  $this
    ->assertFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-month\"]", NULL, 'Month element found.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-month", '', 'No month selected.');
  $this
    ->assertFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-day\"]", NULL, 'Day element found.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-day", '', 'No day selected.');
  $this
    ->assertFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-hour\"]", NULL, 'Hour element found.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-hour", '', 'No hour selected.');
  $this
    ->assertFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-minute\"]", NULL, 'Minute element found.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-minute", '', 'No minute selected.');
  $this
    ->assertNoFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-second\"]", NULL, 'Second element not found.');
  $this
    ->assertFieldByXPath("//*[@id=\"edit-{$field_name}-{$langcode}-0-value-ampm\"]", NULL, 'AMPM element found.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-ampm", '', 'No ampm selected.');

  // Submit a valid date and ensure it is accepted.
  $date_value = array(
    'year' => 2012,
    'month' => 12,
    'day' => 31,
    'hour' => 5,
    'minute' => 15,
  );
  $date = new DrupalDateTime($date_value);
  $edit = array(
    'user_id' => 1,
    'name' => $this
      ->randomName(),
  );

  // Add the ampm indicator since we are testing 12 hour time.
  $date_value['ampm'] = 'am';
  foreach ($date_value as $part => $value) {
    $edit["{$this->field['field_name']}[{$langcode}][0][value][{$part}]"] = $value;
  }
  $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
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-year", '2012', 'Correct year selected.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-month", '12', 'Correct month selected.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-day", '31', 'Correct day selected.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-hour", '5', 'Correct hour selected.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-minute", '15', 'Correct minute selected.');
  $this
    ->assertOptionSelected("edit-{$field_name}-{$langcode}-0-value-ampm", 'am', 'Correct ampm selected.');
}