public function DateRangeFieldTest::testDateRangeField in Drupal 10
Same name and namespace in other branches
- 8 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testDateRangeField()
- 9 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testDateRangeField()
Tests date field functionality.
File
- core/
modules/ datetime_range/ tests/ src/ Functional/ DateRangeFieldTest.php, line 52
Class
- DateRangeFieldTest
- Tests Daterange field functionality.
Namespace
Drupal\Tests\datetime_range\FunctionalCode
public function testDateRangeField() {
$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', DateRangeItem::DATETIME_TYPE_DATE);
$this->fieldStorage
->save();
// Display creation form.
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[0][value][date]", '');
$this
->assertSession()
->fieldValueEquals("{$field_name}[0][end_value][date]", '');
$this
->assertSession()
->elementExists('xpath', '//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class, "js-form-required")]');
$this
->assertSession()
->fieldNotExists("{$field_name}[0][value][time]");
$this
->assertSession()
->fieldNotExists("{$field_name}[0][end_value][time]");
$this
->assertSession()
->elementTextContains('xpath', '//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label);
$this
->assertSession()
->elementExists('xpath', '//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]');
$this
->assertSession()
->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0--description"]');
// Build up dates in the UTC timezone.
$value = '2012-12-31 00:00:00';
$start_date = new DrupalDateTime($value, 'UTC');
$end_value = '2013-06-06 00:00:00';
$end_date = new DrupalDateTime($end_value, 'UTC');
// Submit a valid date and ensure it is accepted.
$date_format = DateFormat::load('html_date')
->getPattern();
$time_format = DateFormat::load('html_time')
->getPattern();
$edit = [
"{$field_name}[0][value][date]" => $start_date
->format($date_format),
"{$field_name}[0][end_value][date]" => $end_date
->format($date_format),
];
$this
->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
$this
->assertSession()
->responseContains($start_date
->format($date_format));
$this
->assertSession()
->responseNotContains($start_date
->format($time_format));
$this
->assertSession()
->responseContains($end_date
->format($date_format));
$this
->assertSession()
->responseNotContains($end_date
->format($time_format));
// Verify the date doesn't change when entity is edited through the form.
$entity = EntityTest::load($id);
$this
->assertEquals('2012-12-31', $entity->{$field_name}->value);
$this
->assertEquals('2013-06-06', $entity->{$field_name}->end_value);
$this
->drupalGet('entity_test/manage/' . $id . '/edit');
$this
->submitForm([], 'Save');
$this
->drupalGet('entity_test/manage/' . $id . '/edit');
$this
->submitForm([], 'Save');
$this
->drupalGet('entity_test/manage/' . $id . '/edit');
$this
->submitForm([], 'Save');
$entity = EntityTest::load($id);
$this
->assertEquals('2012-12-31', $entity->{$field_name}->value);
$this
->assertEquals('2013-06-06', $entity->{$field_name}->end_value);
// Formats that display a time component for date-only fields will display
// the default time, so that is applied before calculating the expected
// value.
$this
->massageTestDate($start_date);
$this
->massageTestDate($end_date);
// Reset display options since these get changed below.
$this->displayOptions = [
'type' => 'daterange_default',
'label' => 'hidden',
'settings' => [
'format_type' => 'long',
'separator' => 'THESEPARATOR',
] + $this->defaultSettings,
];
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Verify that the default formatter works.
$display_repository
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$start_expected = $this->dateFormatter
->format($start_date
->getTimestamp(), 'long', '', DateTimeItemInterface::STORAGE_TIMEZONE);
$start_expected_iso = $this->dateFormatter
->format($start_date
->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
$start_expected_markup = '<time datetime="' . $start_expected_iso . '">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter
->format($end_date
->getTimestamp(), 'long', '', DateTimeItemInterface::STORAGE_TIMEZONE);
$end_expected_iso = $this->dateFormatter
->format($end_date
->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
$end_expected_markup = '<time datetime="' . $end_expected_iso . '">' . $end_expected . '</time>';
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
'%value' => 'long',
'%expected' => $start_expected,
'%expected_iso' => $start_expected_iso,
'%timezone' => $timezone,
]));
$this
->assertStringContainsString($end_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
'%value' => 'long',
'%expected' => $end_expected,
'%expected_iso' => $end_expected_iso,
'%timezone' => $timezone,
]));
$this
->assertStringContainsString(' THESEPARATOR ', $output, 'Found proper separator');
// Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view()
$this
->drupalGet('entity_test/' . $id);
$this
->assertSession()
->elementExists('xpath', '//div[@data-field-item-attr="foobar"]');
// Verify that the plain formatter works.
$this->displayOptions['type'] = 'daterange_plain';
$this->displayOptions['settings'] = $this->defaultSettings;
$this->container
->get('entity_display.repository')
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $start_date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT) . ' - ' . $end_date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Verify that the custom formatter works.
$this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings'] = [
'date_format' => 'm/d/Y',
] + $this->defaultSettings;
$display_repository
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $start_date
->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date
->format($this->displayOptions['settings']['date_format']);
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Test that allowed markup in custom format is preserved and XSS is
// removed.
$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\\>';
$display_repository
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = '<strong>' . $start_date
->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83)) - <strong>' . $end_date
->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Test formatters when start date and end date are the same
$this
->drupalGet('entity_test/add');
$value = '2012-12-31 00:00:00';
$start_date = new DrupalDateTime($value, 'UTC');
$date_format = DateFormat::load('html_date')
->getPattern();
$time_format = DateFormat::load('html_time')
->getPattern();
$edit = [
"{$field_name}[0][value][date]" => $start_date
->format($date_format),
"{$field_name}[0][end_value][date]" => $start_date
->format($date_format),
];
$this
->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
$this
->massageTestDate($start_date);
$this->displayOptions = [
'type' => 'daterange_default',
'label' => 'hidden',
'settings' => [
'format_type' => 'long',
'separator' => 'THESEPARATOR',
] + $this->defaultSettings,
];
$display_repository
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$start_expected = $this->dateFormatter
->format($start_date
->getTimestamp(), 'long', '', DateTimeItemInterface::STORAGE_TIMEZONE);
$start_expected_iso = $this->dateFormatter
->format($start_date
->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
$start_expected_markup = '<time datetime="' . $start_expected_iso . '">' . $start_expected . '</time>';
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
'%value' => 'long',
'%expected' => $start_expected,
'%expected_iso' => $start_expected_iso,
'%timezone' => $timezone,
]));
$this
->assertStringNotContainsString(' THESEPARATOR ', $output, 'Separator not found on page in ' . $timezone);
// Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view()
$this
->drupalGet('entity_test/' . $id);
$this
->assertSession()
->elementExists('xpath', '//time[@data-field-item-attr="foobar"]');
$this->displayOptions['type'] = 'daterange_plain';
$this->displayOptions['settings'] = $this->defaultSettings;
$this->container
->get('entity_display.repository')
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $start_date
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
$this
->assertStringNotContainsString(' THESEPARATOR ', $output, 'Separator not found on page');
$this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings'] = [
'date_format' => 'm/d/Y',
] + $this->defaultSettings;
$display_repository
->getViewDisplay($this->field
->getTargetEntityTypeId(), $this->field
->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $start_date
->format($this->displayOptions['settings']['date_format']);
$output = $this
->renderTestEntity($id);
$this
->assertStringContainsString($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
$this
->assertStringNotContainsString(' THESEPARATOR ', $output, 'Separator not found on page');
}
}