function DateRangeFieldTest::doTestFromToSetting

Same name in other branches
  1. 10 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::doTestFromToSetting()

Performs the test of the 'from_to' setting for given test data.

1 call to DateRangeFieldTest::doTestFromToSetting()
DateRangeFieldTest::testFromSetting in core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php
Tests displaying dates with the 'from_to' setting.

File

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

Class

DateRangeFieldTest
Tests Daterange field functionality.

Namespace

Drupal\Tests\datetime_range\Functional

Code

public function doTestFromToSetting(array $expected, string $datetime_type, string $field_formatter_type, array $display_settings = []) : void {
    $field_name = $this->fieldStorage
        ->getName();
    // Ensure the field to a datetime field.
    $this->fieldStorage
        ->setSetting('datetime_type', $datetime_type);
    $this->fieldStorage
        ->save();
    // 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();
    $edit = [
        "{$field_name}[0][value][date]" => $start_date->format($date_format),
        "{$field_name}[0][end_value][date]" => $end_date->format($date_format),
    ];
    // Supply time as well when field is a datetime field.
    if ($datetime_type === DateRangeItem::DATETIME_TYPE_DATETIME) {
        $time_format = DateFormat::load('html_time')->getPattern();
        $edit["{$field_name}[0][value][time]"] = $start_date->format($time_format);
        $edit["{$field_name}[0][end_value][time]"] = $end_date->format($time_format);
    }
    $this->drupalGet('entity_test/add');
    $this->submitForm($edit, t('Save'));
    preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
    $id = $match[1];
    $this->assertSession()
        ->pageTextContains(t('entity_test @id has been created.', [
        '@id' => $id,
    ]));
    // Now set display options.
    $this->displayOptions = [
        'type' => $field_formatter_type,
        'label' => 'hidden',
        'settings' => $display_settings + [
            'format_type' => 'short',
            'separator' => 'THE_SEPARATOR',
        ] + $this->defaultSettings,
    ];
    \Drupal::service('entity_display.repository')->getViewDisplay($this->field
        ->getTargetEntityTypeId(), $this->field
        ->getTargetBundle(), 'full')
        ->setComponent($field_name, $this->displayOptions)
        ->save();
    $output = $this->renderTestEntity($id);
    foreach ($expected as $content => $is_expected) {
        if ($is_expected) {
            $this->assertStringContainsString($content, $output);
        }
        else {
            $this->assertStringNotContainsString($content, $output);
        }
    }
}

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