You are here

public function DateTimeDayFieldTest::testDateTimeDayTypeDefaultWithWidgetField in Date time day 8

Test the default field type and widget.

File

tests/src/Functional/DateTimeDayFieldTest.php, line 66

Class

DateTimeDayFieldTest
Tests date_time_day field functionality.

Namespace

Drupal\Tests\date_time_day\Functional

Code

public function testDateTimeDayTypeDefaultWithWidgetField() {
  $field_name = $this->fieldStorage
    ->getName();
  $field_label = $this->field
    ->label();

  // Loop through defined timezones to test that date-only fields work at the
  // extremes.
  foreach (static::$timezones as $timezone) {
    $this
      ->setSiteTimezone($timezone);
    $this
      ->assertEquals($timezone, $this
      ->config('system.date')
      ->get('timezone.default'), 'Time zone set to ' . $timezone);

    // Ensure field is set to a date-only field.
    $this->fieldStorage
      ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
    $this->fieldStorage
      ->setSetting('time_type', DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT);
    $this->fieldStorage
      ->save();

    // Set correct form widget type.
    EntityFormDisplay::load('entity_test.entity_test.default')
      ->setComponent($field_name, [
      'type' => 'datetimeday_default',
    ])
      ->save();

    // Display creation form.
    $this
      ->drupalGet('entity_test/add');
    $this
      ->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
    $this
      ->assertFieldByName("{$field_name}[0][start_time_value]", '', 'Start time element found.');
    $this
      ->assertFieldByName("{$field_name}[0][end_time_value]", '', 'End time element found.');
    $this
      ->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class, "js-form-required")]', TRUE, 'Required markup found');
    $this
      ->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
    $this
      ->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
    $this
      ->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');

    // Build up dates in the UTC timezone.
    $date_value = '2012-12-30 00:00:00';
    $date = new DrupalDateTime($date_value, 'UTC');
    $start_time_value = '10:00';
    $end_time_value = '19:00';

    // Submit a valid date and ensure it is accepted.
    $date_format = DateFormat::load('html_date')
      ->getPattern();
    $edit = [
      "{$field_name}[0][value][date]" => $date
        ->format($date_format),
      "{$field_name}[0][start_time_value]" => $start_time_value,
      "{$field_name}[0][end_time_value]" => $end_time_value,
    ];
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\\d+)|', $this
      ->getUrl(), $match);
    $id = $match[1];
    $this
      ->assertSession()
      ->pageTextContains("entity_test {$id} has been created.");
    $this
      ->assertRaw('2012-12-30');
    $this
      ->assertRaw($start_time_value);
    $this
      ->assertRaw($end_time_value);

    // Verify the date doesn't change when entity is edited through the form.
    $entity = EntityTest::load($id);
    $this
      ->assertEqual('2012-12-30', $entity->{$field_name}->value);
    $this
      ->assertEqual($start_time_value, $entity->{$field_name}->start_time_value);
    $this
      ->assertEqual($end_time_value, $entity->{$field_name}->end_time_value);
    $this
      ->drupalGet('entity_test/manage/' . $id . '/edit');
    $this
      ->drupalPostForm(NULL, [], t('Save'));
    $this
      ->drupalGet('entity_test/manage/' . $id . '/edit');
    $this
      ->drupalPostForm(NULL, [], t('Save'));
    $this
      ->drupalGet('entity_test/manage/' . $id . '/edit');
    $this
      ->drupalPostForm(NULL, [], t('Save'));
    $entity = EntityTest::load($id);
    $this
      ->assertEqual('2012-12-30', $entity->{$field_name}->value);
    $this
      ->assertEqual($start_time_value, $entity->{$field_name}->start_time_value);
    $this
      ->assertEqual($end_time_value, $entity->{$field_name}->end_time_value);
  }
}