function DateRangeFieldTest::testDatetimeRangeField

Same name in other branches
  1. 9 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testDatetimeRangeField()
  2. 8.9.x core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testDatetimeRangeField()
  3. 11.x core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testDatetimeRangeField()

Tests date and time field.

File

core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php, line 262

Class

DateRangeFieldTest
Tests Daterange field functionality.

Namespace

Drupal\Tests\datetime_range\Functional

Code

public function testDatetimeRangeField() : void {
    $field_name = $this->fieldStorage
        ->getName();
    $field_label = $this->field
        ->label();
    // Ensure the field to a datetime field.
    $this->fieldStorage
        ->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_DATETIME);
    $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][value][time]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][end_value][date]", '');
    $this->assertSession()
        ->fieldValueEquals("{$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');
    // Update the timezone to the system default.
    $start_date->setTimezone(timezone_open(date_default_timezone_get()));
    $end_date->setTimezone(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][value][time]" => $start_date->format($time_format),
        "{$field_name}[0][end_value][date]" => $end_date->format($date_format),
        "{$field_name}[0][end_value][time]" => $end_date->format($time_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()
        ->responseContains($start_date->format($time_format));
    $this->assertSession()
        ->responseContains($end_date->format($date_format));
    $this->assertSession()
        ->responseContains($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 'datetime_custom' formatter works.
    $this->displayOptions['type'] = 'daterange_custom';
    $this->displayOptions['settings'] = [
        'date_format' => 'm/d/Y g:i:s A',
    ] + $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, 'UTC');
    $start_date->setTimezone(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][value][time]" => $start_date->format($time_format),
        "{$field_name}[0][end_value][date]" => $start_date->format($date_format),
        "{$field_name}[0][end_value][time]" => $start_date->format($time_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>';
    $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->assertStringNotContainsString(' THE_SEPARATOR ', $output, 'Separator not found on page');
    // 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::DATETIME_STORAGE_FORMAT);
    $output = $this->renderTestEntity($id);
    $this->assertStringContainsString($expected, $output, "Formatted date field using plain format displayed as {$expected}.");
    $this->assertStringNotContainsString(' THE_SEPARATOR ', $output, 'Separator not found on page');
    $this->displayOptions['type'] = 'daterange_custom';
    $this->displayOptions['settings'] = [
        'date_format' => 'm/d/Y g:i:s A',
    ] + $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, "Formatted date field using daterange_custom format displayed as {$expected}.");
    $this->assertStringNotContainsString(' THE_SEPARATOR ', $output, 'Separator not found on page');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.