Chris@0: ''];
Chris@0:
Chris@0: /**
Chris@0: * {@inheritdoc}
Chris@0: */
Chris@0: protected function getTestFieldType() {
Chris@0: return 'datetime';
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests date field functionality.
Chris@0: */
Chris@0: public function testDateField() {
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0:
Chris@0: // Loop through defined timezones to test that date-only fields work at the
Chris@0: // extremes.
Chris@0: foreach (static::$timezones as $timezone) {
Chris@0:
Chris@0: $this->setSiteTimezone($timezone);
Chris@0: $this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
Chris@0: $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class,"js-form-required")]', TRUE, 'Required markup found');
Chris@0: $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Time element not found.');
Chris@0: $this->assertFieldByXPath('//input[@aria-describedby="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA described-by found');
Chris@0: $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA description found');
Chris@0:
Chris@0: // Build up a date in the UTC timezone. Note that using this will also
Chris@0: // mimic the user in a different timezone simply entering '2012-12-31' via
Chris@0: // the UI.
Chris@0: $value = '2012-12-31 00:00:00';
Chris@14: $date = new DrupalDateTime($value, DateTimeItemInterface::STORAGE_TIMEZONE);
Chris@0:
Chris@0: // Submit a valid date and ensure it is accepted.
Chris@0: $date_format = DateFormat::load('html_date')->getPattern();
Chris@0: $time_format = DateFormat::load('html_time')->getPattern();
Chris@0:
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date->format($date_format),
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0: $this->assertRaw($date->format($date_format));
Chris@0: $this->assertNoRaw($date->format($time_format));
Chris@0:
Chris@0: // Verify the date doesn't change if using a timezone that is UTC+12 when
Chris@0: // the entity is edited through the form.
Chris@0: $entity = EntityTest::load($id);
Chris@0: $this->assertEqual('2012-12-31', $entity->{$field_name}->value);
Chris@0: $this->drupalGet('entity_test/manage/' . $id . '/edit');
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $this->drupalGet('entity_test/manage/' . $id . '/edit');
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $this->drupalGet('entity_test/manage/' . $id . '/edit');
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $entity = EntityTest::load($id);
Chris@0: $this->assertEqual('2012-12-31', $entity->{$field_name}->value);
Chris@0:
Chris@0: // Reset display options since these get changed below.
Chris@0: $this->displayOptions = [
Chris@0: 'type' => 'datetime_default',
Chris@0: 'label' => 'hidden',
Chris@0: 'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
Chris@0: ];
Chris@0: // Verify that the date is output according to the formatter settings.
Chris@0: $options = [
Chris@0: 'format_type' => ['short', 'medium', 'long'],
Chris@0: ];
Chris@0: // Formats that display a time component for date-only fields will display
Chris@0: // the default time, so that is applied before calculating the expected
Chris@0: // value.
Chris@14: $this->massageTestDate($date);
Chris@0: foreach ($options as $setting => $values) {
Chris@0: foreach ($values as $new_value) {
Chris@0: // Update the entity display settings.
Chris@0: $this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@0:
Chris@0: $this->renderTestEntity($id);
Chris@0: switch ($setting) {
Chris@0: case 'format_type':
Chris@0: // Verify that a date is displayed. Since this is a date-only
Chris@0: // field, it is expected to display the time as 00:00:00.
Chris@18: /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
Chris@18: $date_formatter = $this->container->get('date.formatter');
Chris@18: $expected = $date_formatter->format($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
Chris@18: $expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $expected_markup = '';
Chris@0: $this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
Chris@0: '%value' => $new_value,
Chris@0: '%expected' => $expected,
Chris@0: '%expected_iso' => $expected_iso,
Chris@0: '%timezone' => $timezone,
Chris@0: ]));
Chris@0: break;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // Verify that the plain formatter works.
Chris@0: $this->displayOptions['type'] = 'datetime_plain';
Chris@0: $this->displayOptions['settings'] = $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@14: $expected = $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected in %timezone.', [
Chris@0: '%expected' => $expected,
Chris@0: '%timezone' => $timezone,
Chris@0: ]));
Chris@0:
Chris@0: // Verify that the 'datetime_custom' formatter works.
Chris@0: $this->displayOptions['type'] = 'datetime_custom';
Chris@0: $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@0: $expected = $date->format($this->displayOptions['settings']['date_format']);
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected in %timezone.', [
Chris@0: '%expected' => $expected,
Chris@0: '%timezone' => $timezone,
Chris@0: ]));
Chris@0:
Chris@0: // Test that allowed markup in custom format is preserved and XSS is
Chris@0: // removed.
Chris@0: $this->displayOptions['settings']['date_format'] = '\\<\\s\\t\\r\\o\\n\\g\\>m/d/Y\\<\\/\\s\\t\\r\\o\\n\\g\\>\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\\S\\t\\r\\i\\n\\g\\.\\f\\r\\o\\m\\C\\h\\a\\r\\C\\o\\d\\e\\(\\8\\8\\,\\8\\3\\,\\8\\3\\)\\)\\<\\/\\s\\c\\r\\i\\p\\t\\>';
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@0: $expected = '' . $date->format('m/d/Y') . 'alert(String.fromCharCode(88,83,83))';
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
Chris@0: '%expected' => $expected,
Chris@0: '%timezone' => $timezone,
Chris@0: ]));
Chris@0:
Chris@0: // Verify that the 'datetime_time_ago' formatter works for intervals in the
Chris@0: // past. First update the test entity so that the date difference always
Chris@0: // has the same interval. Since the database always stores UTC, and the
Chris@0: // interval will use this, force the test date to use UTC and not the local
Chris@17: // or user timezone.
Chris@0: $timestamp = REQUEST_TIME - 87654321;
Chris@0: $entity = EntityTest::load($id);
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0: $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
Chris@0: $entity->{$field_name}->value = $date->format($date_format);
Chris@0: $entity->save();
Chris@0:
Chris@0: $this->displayOptions['type'] = 'datetime_time_ago';
Chris@0: $this->displayOptions['settings'] = [
Chris@0: 'future_format' => '@interval in the future',
Chris@0: 'past_format' => '@interval in the past',
Chris@0: 'granularity' => 3,
Chris@0: ];
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@17: $expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
Chris@17: '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
Chris@0: ]);
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
Chris@0: '%expected' => $expected,
Chris@0: '%timezone' => $timezone,
Chris@0: ]));
Chris@0:
Chris@0: // Verify that the 'datetime_time_ago' formatter works for intervals in the
Chris@0: // future. First update the test entity so that the date difference always
Chris@0: // has the same interval. Since the database always stores UTC, and the
Chris@0: // interval will use this, force the test date to use UTC and not the local
Chris@17: // or user timezone.
Chris@0: $timestamp = REQUEST_TIME + 87654321;
Chris@0: $entity = EntityTest::load($id);
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0: $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
Chris@0: $entity->{$field_name}->value = $date->format($date_format);
Chris@0: $entity->save();
Chris@0:
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@17: $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
Chris@17: '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
Chris@0: ]);
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
Chris@0: '%expected' => $expected,
Chris@0: '%timezone' => $timezone,
Chris@0: ]));
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests date and time field.
Chris@0: */
Chris@0: public function testDatetimeField() {
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0: $field_label = $this->field->label();
Chris@0: // Change the field to a datetime field.
Chris@0: $this->fieldStorage->setSetting('datetime_type', 'datetime');
Chris@0: $this->fieldStorage->save();
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
Chris@0: $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
Chris@0: $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
Chris@0: $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
Chris@0: $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
Chris@0:
Chris@0: // Build up a date in the UTC timezone.
Chris@0: $value = '2012-12-31 00:00:00';
Chris@0: $date = new DrupalDateTime($value, 'UTC');
Chris@0:
Chris@0: // Update the timezone to the system default.
Chris@0: $date->setTimezone(timezone_open(drupal_get_user_timezone()));
Chris@0:
Chris@0: // Submit a valid date and ensure it is accepted.
Chris@0: $date_format = DateFormat::load('html_date')->getPattern();
Chris@0: $time_format = DateFormat::load('html_time')->getPattern();
Chris@0:
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date->format($date_format),
Chris@0: "{$field_name}[0][value][time]" => $date->format($time_format),
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0: $this->assertRaw($date->format($date_format));
Chris@0: $this->assertRaw($date->format($time_format));
Chris@0:
Chris@0: // Verify that the date is output according to the formatter settings.
Chris@0: $options = [
Chris@0: 'format_type' => ['short', 'medium', 'long'],
Chris@0: ];
Chris@0: foreach ($options as $setting => $values) {
Chris@0: foreach ($values as $new_value) {
Chris@0: // Update the entity display settings.
Chris@0: $this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@0:
Chris@0: $this->renderTestEntity($id);
Chris@0: switch ($setting) {
Chris@0: case 'format_type':
Chris@0: // Verify that a date is displayed.
Chris@18: $date_formatter = $this->container->get('date.formatter');
Chris@18: $expected = $date_formatter->format($date->getTimestamp(), $new_value);
Chris@18: $expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
Chris@0: $output = $this->renderTestEntity($id);
Chris@0: $expected_markup = '';
Chris@17: $this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
Chris@0: break;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // Verify that the plain formatter works.
Chris@0: $this->displayOptions['type'] = 'datetime_plain';
Chris@0: $this->displayOptions['settings'] = $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@14: $expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
Chris@0: $output = $this->renderTestEntity($id);
Chris@17: $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
Chris@0:
Chris@0: // Verify that the 'datetime_custom' formatter works.
Chris@0: $this->displayOptions['type'] = 'datetime_custom';
Chris@0: $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@0: $expected = $date->format($this->displayOptions['settings']['date_format']);
Chris@0: $output = $this->renderTestEntity($id);
Chris@17: $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
Chris@0:
Chris@0: // Verify that the 'timezone_override' setting works.
Chris@0: $this->displayOptions['type'] = 'datetime_custom';
Chris@0: $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings;
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@0: $expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
Chris@0: $output = $this->renderTestEntity($id);
Chris@17: $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
Chris@0:
Chris@0: // Verify that the 'datetime_time_ago' formatter works for intervals in the
Chris@0: // past. First update the test entity so that the date difference always
Chris@0: // has the same interval. Since the database always stores UTC, and the
Chris@0: // interval will use this, force the test date to use UTC and not the local
Chris@17: // or user timezone.
Chris@0: $timestamp = REQUEST_TIME - 87654321;
Chris@0: $entity = EntityTest::load($id);
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0: $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
Chris@14: $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
Chris@0: $entity->save();
Chris@0:
Chris@0: $this->displayOptions['type'] = 'datetime_time_ago';
Chris@0: $this->displayOptions['settings'] = [
Chris@0: 'future_format' => '@interval from now',
Chris@0: 'past_format' => '@interval earlier',
Chris@0: 'granularity' => 3,
Chris@0: ];
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@17: $expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
Chris@17: '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
Chris@0: ]);
Chris@0: $output = $this->renderTestEntity($id);
Chris@17: $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
Chris@0:
Chris@0: // Verify that the 'datetime_time_ago' formatter works for intervals in the
Chris@0: // future. First update the test entity so that the date difference always
Chris@0: // has the same interval. Since the database always stores UTC, and the
Chris@0: // interval will use this, force the test date to use UTC and not the local
Chris@17: // or user timezone.
Chris@0: $timestamp = REQUEST_TIME + 87654321;
Chris@0: $entity = EntityTest::load($id);
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0: $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
Chris@14: $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
Chris@0: $entity->save();
Chris@0:
Chris@0: entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
Chris@0: ->setComponent($field_name, $this->displayOptions)
Chris@0: ->save();
Chris@17: $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
Chris@17: '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
Chris@0: ]);
Chris@0: $output = $this->renderTestEntity($id);
Chris@17: $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests Date List Widget functionality.
Chris@0: */
Chris@0: public function testDatelistWidget() {
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0: $field_label = $this->field->label();
Chris@0:
Chris@0: // Ensure field is set to a date only field.
Chris@0: $this->fieldStorage->setSetting('datetime_type', 'date');
Chris@0: $this->fieldStorage->save();
Chris@0:
Chris@0: // Change the widget to a datelist widget.
Chris@0: entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'datetime_datelist',
Chris@0: 'settings' => [
Chris@0: 'date_order' => 'YMD',
Chris@0: ],
Chris@0: ])
Chris@0: ->save();
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
Chris@0: $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
Chris@0: $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
Chris@0:
Chris@0: // Assert that Hour and Minute Elements do not appear on Date Only
Chris@0: $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element not found on Date Only.');
Chris@0: $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-minute\"]", NULL, 'Minute element not found on Date Only.');
Chris@0:
Chris@0: // Go to the form display page to assert that increment option does not appear on Date Only
Chris@0: $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
Chris@0: $this->drupalGet($fieldEditUrl);
Chris@0:
Chris@0: // Click on the widget settings button to open the widget settings form.
Chris@0: $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
Chris@0: $xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
Chris@0: $this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.');
Chris@0:
Chris@0: // Change the field to a datetime field.
Chris@0: $this->fieldStorage->setSetting('datetime_type', 'datetime');
Chris@0: $this->fieldStorage->save();
Chris@0:
Chris@0: // Change the widget to a datelist widget.
Chris@0: entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'datetime_datelist',
Chris@0: 'settings' => [
Chris@0: 'increment' => 1,
Chris@0: 'date_order' => 'YMD',
Chris@0: 'time_type' => '12',
Chris@0: ],
Chris@0: ])
Chris@0: ->save();
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Go to the form display page to assert that increment option does appear on Date Time
Chris@0: $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
Chris@0: $this->drupalGet($fieldEditUrl);
Chris@0:
Chris@0: // Click on the widget settings button to open the widget settings form.
Chris@0: $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
Chris@0: $this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.');
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0:
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-year\"]", NULL, 'Year element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-year", '', 'No year selected.');
Chris@0: $this->assertOptionByText("edit-$field_name-0-value-year", t('Year'));
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-month\"]", NULL, 'Month element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-month", '', 'No month selected.');
Chris@0: $this->assertOptionByText("edit-$field_name-0-value-month", t('Month'));
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-day\"]", NULL, 'Day element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-day", '', 'No day selected.');
Chris@0: $this->assertOptionByText("edit-$field_name-0-value-day", t('Day'));
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-hour", '', 'No hour selected.');
Chris@0: $this->assertOptionByText("edit-$field_name-0-value-hour", t('Hour'));
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-minute\"]", NULL, 'Minute element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-minute", '', 'No minute selected.');
Chris@0: $this->assertOptionByText("edit-$field_name-0-value-minute", t('Minute'));
Chris@0: $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-second\"]", NULL, 'Second element not found.');
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-ampm\"]", NULL, 'AMPM element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-ampm", '', 'No ampm selected.');
Chris@0: $this->assertOptionByText("edit-$field_name-0-value-ampm", t('AM/PM'));
Chris@0:
Chris@0: // Submit a valid date and ensure it is accepted.
Chris@0: $date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15];
Chris@0:
Chris@0: $edit = [];
Chris@0: // Add the ampm indicator since we are testing 12 hour time.
Chris@0: $date_value['ampm'] = 'am';
Chris@0: foreach ($date_value as $part => $value) {
Chris@0: $edit["{$field_name}[0][value][$part]"] = $value;
Chris@0: }
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0:
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-day", '31', 'Correct day selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-hour", '5', 'Correct hour selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-minute", '15', 'Correct minute selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-ampm", 'am', 'Correct ampm selected.');
Chris@0:
Chris@0: // Test the widget using increment other than 1 and 24 hour mode.
Chris@0: entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'datetime_datelist',
Chris@0: 'settings' => [
Chris@0: 'increment' => 15,
Chris@0: 'date_order' => 'YMD',
Chris@0: 'time_type' => '24',
Chris@0: ],
Chris@0: ])
Chris@0: ->save();
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0:
Chris@0: // Other elements are unaffected by the changed settings.
Chris@0: $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element found.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-hour", '', 'No hour selected.');
Chris@0: $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-ampm\"]", NULL, 'AMPM element not found.');
Chris@0:
Chris@0: // Submit a valid date and ensure it is accepted.
Chris@0: $date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15];
Chris@0:
Chris@0: $edit = [];
Chris@0: foreach ($date_value as $part => $value) {
Chris@0: $edit["{$field_name}[0][value][$part]"] = $value;
Chris@0: }
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0:
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-day", '31', 'Correct day selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-hour", '17', 'Correct hour selected.');
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-minute", '15', 'Correct minute selected.');
Chris@0:
Chris@0: // Test the widget for partial completion of fields.
Chris@0: entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'datetime_datelist',
Chris@0: 'settings' => [
Chris@0: 'increment' => 1,
Chris@0: 'date_order' => 'YMD',
Chris@0: 'time_type' => '24',
Chris@0: ],
Chris@0: ])
Chris@0: ->save();
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Test the widget for validation notifications.
Chris@0: foreach ($this->datelistDataProvider($field_label) as $data) {
Chris@0: list($date_value, $expected) = $data;
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0:
Chris@0: // Submit a partial date and ensure and error message is provided.
Chris@0: $edit = [];
Chris@0: foreach ($date_value as $part => $value) {
Chris@0: $edit["{$field_name}[0][value][$part]"] = $value;
Chris@0: }
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertResponse(200);
Chris@0: foreach ($expected as $expected_text) {
Chris@0: $this->assertText(t($expected_text));
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // Test the widget for complete input with zeros as part of selections.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0:
Chris@0: $date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0'];
Chris@0: $edit = [];
Chris@0: foreach ($date_value as $part => $value) {
Chris@0: $edit["{$field_name}[0][value][$part]"] = $value;
Chris@0: }
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertResponse(200);
Chris@0: preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
Chris@0: $id = $match[1];
Chris@0: $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
Chris@0:
Chris@0: // Test the widget to ensure zeros are not deselected on validation.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0:
Chris@0: $date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0'];
Chris@0: $edit = [];
Chris@0: foreach ($date_value as $part => $value) {
Chris@0: $edit["{$field_name}[0][value][$part]"] = $value;
Chris@0: }
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertResponse(200);
Chris@0: $this->assertOptionSelected("edit-$field_name-0-value-minute", '0', 'Correct minute selected.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * The data provider for testing the validation of the datelist widget.
Chris@0: *
Chris@0: * @param string $field_label
Chris@0: * The label of the field being tested.
Chris@0: *
Chris@0: * @return array
Chris@0: * An array of datelist input permutations to test.
Chris@0: */
Chris@0: protected function datelistDataProvider($field_label) {
Chris@0: return [
Chris@0: // Nothing selected.
Chris@0: [
Chris@0: ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
Chris@0: ["The $field_label date is required."],
Chris@0: ],
Chris@0: // Year only selected, validation error on Month, Day, Hour, Minute.
Chris@0: [
Chris@0: ['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
Chris@0: [
Chris@0: "The $field_label date is incomplete.",
Chris@0: 'A value must be selected for month.',
Chris@0: 'A value must be selected for day.',
Chris@0: 'A value must be selected for hour.',
Chris@0: 'A value must be selected for minute.',
Chris@0: ],
Chris@0: ],
Chris@0: // Year and Month selected, validation error on Day, Hour, Minute.
Chris@0: [
Chris@0: ['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''],
Chris@0: [
Chris@0: "The $field_label date is incomplete.",
Chris@0: 'A value must be selected for day.',
Chris@0: 'A value must be selected for hour.',
Chris@0: 'A value must be selected for minute.',
Chris@0: ],
Chris@0: ],
Chris@0: // Year, Month and Day selected, validation error on Hour, Minute.
Chris@0: [
Chris@0: ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''],
Chris@0: [
Chris@0: "The $field_label date is incomplete.",
Chris@0: 'A value must be selected for hour.',
Chris@0: 'A value must be selected for minute.',
Chris@0: ],
Chris@0: ],
Chris@0: // Year, Month, Day and Hour selected, validation error on Minute only.
Chris@0: [
Chris@0: ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''],
Chris@0: [
Chris@0: "The $field_label date is incomplete.",
Chris@0: 'A value must be selected for minute.',
Chris@0: ],
Chris@0: ],
Chris@0: ];
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test default value functionality.
Chris@0: */
Chris@0: public function testDefaultValue() {
Chris@0: // Create a test content type.
Chris@0: $this->drupalCreateContentType(['type' => 'date_content']);
Chris@0:
Chris@0: // Create a field storage with settings to validate.
Chris@17: $field_name = mb_strtolower($this->randomMachineName());
Chris@0: $field_storage = FieldStorageConfig::create([
Chris@0: 'field_name' => $field_name,
Chris@0: 'entity_type' => 'node',
Chris@0: 'type' => 'datetime',
Chris@0: 'settings' => ['datetime_type' => 'date'],
Chris@0: ]);
Chris@0: $field_storage->save();
Chris@0:
Chris@0: $field = FieldConfig::create([
Chris@0: 'field_storage' => $field_storage,
Chris@0: 'bundle' => 'date_content',
Chris@0: ]);
Chris@0: $field->save();
Chris@0:
Chris@0: // Loop through defined timezones to test that date-only defaults work at
Chris@0: // the extremes.
Chris@0: foreach (static::$timezones as $timezone) {
Chris@0:
Chris@0: $this->setSiteTimezone($timezone);
Chris@0: $this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
Chris@0:
Chris@0: // Set now as default_value.
Chris@0: $field_edit = [
Chris@0: 'default_value_input[default_date_type]' => 'now',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
Chris@0:
Chris@0: // Check that default value is selected in default value form.
Chris@0: $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
Chris@0: $this->assertOptionSelected('edit-default-value-input-default-date-type', 'now', 'The default value is selected in instance settings page');
Chris@0: $this->assertFieldByName('default_value_input[default_date]', '', 'The relative default value is empty in instance settings page');
Chris@0:
Chris@0: // Check if default_date has been stored successfully.
Chris@0: $config_entity = $this->config('field.field.node.date_content.' . $field_name)
Chris@0: ->get();
Chris@0: $this->assertEqual($config_entity['default_value'][0], [
Chris@0: 'default_date_type' => 'now',
Chris@0: 'default_date' => 'now',
Chris@0: ], 'Default value has been stored successfully');
Chris@0:
Chris@0: // Clear field cache in order to avoid stale cache values.
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Create a new node to check that datetime field default value is today.
Chris@0: $new_node = Node::create(['type' => 'date_content']);
Chris@0: $expected_date = new DrupalDateTime('now', drupal_get_user_timezone());
Chris@0: $this->assertEqual($new_node->get($field_name)
Chris@14: ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
Chris@0:
Chris@0: // Set an invalid relative default_value to test validation.
Chris@0: $field_edit = [
Chris@0: 'default_value_input[default_date_type]' => 'relative',
Chris@0: 'default_value_input[default_date]' => 'invalid date',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
Chris@0:
Chris@0: $this->assertText('The relative date value entered is invalid.');
Chris@0:
Chris@0: // Set a relative default_value.
Chris@0: $field_edit = [
Chris@0: 'default_value_input[default_date_type]' => 'relative',
Chris@0: 'default_value_input[default_date]' => '+90 days',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
Chris@0:
Chris@0: // Check that default value is selected in default value form.
Chris@0: $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
Chris@0: $this->assertOptionSelected('edit-default-value-input-default-date-type', 'relative', 'The default value is selected in instance settings page');
Chris@0: $this->assertFieldByName('default_value_input[default_date]', '+90 days', 'The relative default value is displayed in instance settings page');
Chris@0:
Chris@0: // Check if default_date has been stored successfully.
Chris@0: $config_entity = $this->config('field.field.node.date_content.' . $field_name)
Chris@0: ->get();
Chris@0: $this->assertEqual($config_entity['default_value'][0], [
Chris@0: 'default_date_type' => 'relative',
Chris@0: 'default_date' => '+90 days',
Chris@0: ], 'Default value has been stored successfully');
Chris@0:
Chris@0: // Clear field cache in order to avoid stale cache values.
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Create a new node to check that datetime field default value is +90
Chris@0: // days.
Chris@0: $new_node = Node::create(['type' => 'date_content']);
Chris@0: $expected_date = new DrupalDateTime('+90 days', drupal_get_user_timezone());
Chris@0: $this->assertEqual($new_node->get($field_name)
Chris@14: ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
Chris@0:
Chris@0: // Remove default value.
Chris@0: $field_edit = [
Chris@0: 'default_value_input[default_date_type]' => '',
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
Chris@0:
Chris@0: // Check that default value is selected in default value form.
Chris@0: $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
Chris@0: $this->assertOptionSelected('edit-default-value-input-default-date-type', '', 'The default value is selected in instance settings page');
Chris@0: $this->assertFieldByName('default_value_input[default_date]', '', 'The relative default value is empty in instance settings page');
Chris@0:
Chris@0: // Check if default_date has been stored successfully.
Chris@0: $config_entity = $this->config('field.field.node.date_content.' . $field_name)
Chris@0: ->get();
Chris@0: $this->assertTrue(empty($config_entity['default_value']), 'Empty default value has been stored successfully');
Chris@0:
Chris@0: // Clear field cache in order to avoid stale cache values.
Chris@0: \Drupal::entityManager()->clearCachedFieldDefinitions();
Chris@0:
Chris@0: // Create a new node to check that datetime field default value is not
Chris@0: // set.
Chris@0: $new_node = Node::create(['type' => 'date_content']);
Chris@0: $this->assertNull($new_node->get($field_name)->value, 'Default value is not set');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test that invalid values are caught and marked as invalid.
Chris@0: */
Chris@0: public function testInvalidField() {
Chris@0: // Change the field to a datetime field.
Chris@0: $this->fieldStorage->setSetting('datetime_type', 'datetime');
Chris@0: $this->fieldStorage->save();
Chris@0: $field_name = $this->fieldStorage->getName();
Chris@0:
Chris@0: // Display creation form.
Chris@0: $this->drupalGet('entity_test/add');
Chris@0: $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
Chris@0: $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
Chris@0:
Chris@0: // Submit invalid dates and ensure they is not accepted.
Chris@0: $date_value = '';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => '12:00:00',
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', 'Empty date value has been caught.');
Chris@0:
Chris@0: $date_value = 'aaaa-12-01';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => '00:00:00',
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', ['%date' => $date_value]));
Chris@0:
Chris@0: $date_value = '2012-75-01';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => '00:00:00',
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', ['%date' => $date_value]));
Chris@0:
Chris@0: $date_value = '2012-12-99';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => '00:00:00',
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', ['%date' => $date_value]));
Chris@0:
Chris@0: $date_value = '2012-12-01';
Chris@0: $time_value = '';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => $time_value,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', 'Empty time value has been caught.');
Chris@0:
Chris@0: $date_value = '2012-12-01';
Chris@0: $time_value = '49:00:00';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => $time_value,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', ['%time' => $time_value]));
Chris@0:
Chris@0: $date_value = '2012-12-01';
Chris@0: $time_value = '12:99:00';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => $time_value,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', ['%time' => $time_value]));
Chris@0:
Chris@0: $date_value = '2012-12-01';
Chris@0: $time_value = '12:15:99';
Chris@0: $edit = [
Chris@0: "{$field_name}[0][value][date]" => $date_value,
Chris@0: "{$field_name}[0][value][time]" => $time_value,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0: $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', ['%time' => $time_value]));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests that 'Date' field storage setting form is disabled if field has data.
Chris@0: */
Chris@0: public function testDateStorageSettings() {
Chris@0: // Create a test content type.
Chris@0: $this->drupalCreateContentType(['type' => 'date_content']);
Chris@0:
Chris@0: // Create a field storage with settings to validate.
Chris@17: $field_name = mb_strtolower($this->randomMachineName());
Chris@0: $field_storage = FieldStorageConfig::create([
Chris@0: 'field_name' => $field_name,
Chris@0: 'entity_type' => 'node',
Chris@0: 'type' => 'datetime',
Chris@0: 'settings' => [
Chris@0: 'datetime_type' => 'date',
Chris@0: ],
Chris@0: ]);
Chris@0: $field_storage->save();
Chris@0: $field = FieldConfig::create([
Chris@0: 'field_storage' => $field_storage,
Chris@0: 'field_name' => $field_name,
Chris@0: 'bundle' => 'date_content',
Chris@0: ]);
Chris@0: $field->save();
Chris@0:
Chris@0: entity_get_form_display('node', 'date_content', 'default')
Chris@0: ->setComponent($field_name, [
Chris@0: 'type' => 'datetime_default',
Chris@0: ])
Chris@0: ->save();
Chris@0: $edit = [
Chris@0: 'title[0][value]' => $this->randomString(),
Chris@0: 'body[0][value]' => $this->randomString(),
Chris@0: $field_name . '[0][value][date]' => '2016-04-01',
Chris@0: ];
Chris@0: $this->drupalPostForm('node/add/date_content', $edit, t('Save'));
Chris@0: $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name . '/storage');
Chris@0: $result = $this->xpath("//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]");
Chris@0: $this->assertEqual(count($result), 1, "Changing datetime setting is disabled.");
Chris@0: $this->assertText('There is data for this field in the database. The field settings can no longer be changed.');
Chris@0: }
Chris@0:
Chris@0: }