function DateRangeFieldTest::testAlldayRangeField
Same name in other branches
- 9 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testAlldayRangeField()
- 8.9.x core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testAlldayRangeField()
- 10 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testAlldayRangeField()
Tests all-day field.
File
-
core/
modules/ datetime_range/ tests/ src/ Functional/ DateRangeFieldTest.php, line 439
Class
- DateRangeFieldTest
- Tests Daterange field functionality.
Namespace
Drupal\Tests\datetime_range\FunctionalCode
public function testAlldayRangeField() : void {
$field_name = $this->fieldStorage
->getName();
$field_label = $this->field
->label();
// Ensure field is set to an all-day field.
$this->fieldStorage
->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_ALLDAY);
$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 proper timezone.
$value = '2012-12-31 00:00:00';
$start_date = new DrupalDateTime($value, timezone_open(date_default_timezone_get()));
$end_value = '2013-06-06 23:59:59';
$end_date = new DrupalDateTime($end_value, timezone_open(date_default_timezone_get()));
// 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));
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Verify that the default formatter works.
$this->displayOptions['settings'] = [
'format_type' => 'long',
'separator' => 'THE_SEPARATOR',
] + $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');
$start_expected_iso = $this->dateFormatter
->format($start_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
$start_expected_markup = '<time datetime="' . $start_expected_iso . '">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter
->format($end_date->getTimestamp(), 'long');
$end_expected_iso = $this->dateFormatter
->format($end_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
$end_expected_markup = '<time datetime="' . $end_expected_iso . '">' . $end_expected . '</time>';
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($start_expected_markup, $output, "Formatted date field using long format displayed as {$start_expected} with {$start_expected_iso} attribute.");
$this->assertStringContainsString($end_expected_markup, $output, "Formatted date field using long format displayed as {$end_expected} with {$end_expected_iso} attribute.");
$this->assertStringContainsString(' THE_SEPARATOR ', $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::DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using plain format displayed as {$expected}.");
// 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, "Formatted date field using daterange_custom format displayed as {$expected}.");
// Verify that the 'timezone_override' setting works.
$this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings'] = [
'date_format' => 'm/d/Y g:i:s A',
'timezone_override' => 'America/New_York',
] + $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'], [
'timezone' => 'America/New_York',
]);
$expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], [
'timezone' => 'America/New_York',
]);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using daterange_custom format displayed as {$expected}.");
// 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, timezone_open(date_default_timezone_get()));
$end_value = '2012-12-31 23:59:59';
$end_date = new DrupalDateTime($end_value, timezone_open(date_default_timezone_get()));
$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->displayOptions = [
'type' => 'daterange_default',
'label' => 'hidden',
'settings' => [
'format_type' => 'long',
'separator' => 'THE_SEPARATOR',
] + $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');
$start_expected_iso = $this->dateFormatter
->format($start_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
$start_expected_markup = '<time datetime="' . $start_expected_iso . '">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter
->format($end_date->getTimestamp(), 'long');
$end_expected_iso = $this->dateFormatter
->format($end_date->getTimestamp(), 'custom', 'Y-m-d\\TH:i:s\\Z', 'UTC');
$end_expected_markup = '<time datetime="' . $end_expected_iso . '">' . $end_expected . '</time>';
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($start_expected_markup, $output, "Formatted date field using long format displayed as {$start_expected} with {$start_expected_iso} attribute.");
$this->assertStringContainsString($end_expected_markup, $output, "Formatted date field using long format displayed as {$end_expected} with {$end_expected_iso} attribute.");
$this->assertStringContainsString(' THE_SEPARATOR ', $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"]');
$this->displayOptions['type'] = 'daterange_plain';
$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::DATETIME_STORAGE_FORMAT) . ' THE_SEPARATOR ' . $end_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using plain format displayed as {$expected}.");
$this->assertStringContainsString(' THE_SEPARATOR ', $output, 'Found proper separator');
$this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings']['date_format'] = 'm/d/Y';
$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']) . ' THE_SEPARATOR ' . $end_date->format($this->displayOptions['settings']['date_format']);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using daterange_custom format displayed as {$expected}.");
$this->assertStringContainsString(' THE_SEPARATOR ', $output, 'Found proper separator');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.