function DateRangeFieldTest::testDefaultValue

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

Test default value functionality.

File

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

Class

DateRangeFieldTest
Tests Daterange field functionality.

Namespace

Drupal\Tests\datetime_range\Functional

Code

public function testDefaultValue() {
    // Create a test content type.
    $this->drupalCreateContentType([
        'type' => 'date_content',
    ]);
    // Create a field storage with settings to validate.
    $field_name = mb_strtolower($this->randomMachineName());
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'node',
        'type' => 'daterange',
        'settings' => [
            'datetime_type' => DateRangeItem::DATETIME_TYPE_DATE,
        ],
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'date_content',
    ]);
    $field->save();
    // Set now as default_value.
    $field_edit = [
        'default_value_input[default_date_type]' => 'now',
        'default_value_input[default_end_date_type]' => 'now',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    // Check that default value is selected in default value form.
    $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this->assertOptionSelected('edit-default-value-input-default-date-type', 'now', 'The default start value is selected in instance settings page');
    $this->assertFieldByName('default_value_input[default_date]', '', 'The relative start default value is empty in instance settings page');
    $this->assertOptionSelected('edit-default-value-input-default-end-date-type', 'now', 'The default end value is selected in instance settings page');
    $this->assertFieldByName('default_value_input[default_end_date]', '', 'The relative end default value is empty in instance settings page');
    // Check if default_date has been stored successfully.
    $config_entity = $this->config('field.field.node.date_content.' . $field_name)
        ->get();
    $this->assertEqual($config_entity['default_value'][0], [
        'default_date_type' => 'now',
        'default_date' => 'now',
        'default_end_date_type' => 'now',
        'default_end_date' => 'now',
    ], 'Default value has been stored successfully');
    // Clear field cache in order to avoid stale cache values.
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    // Create a new node to check that datetime field default value is today.
    $new_node = Node::create([
        'type' => 'date_content',
    ]);
    $expected_date = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
    $this->assertEqual($new_node->get($field_name)
        ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
    $this->assertEqual($new_node->get($field_name)
        ->offsetGet(0)->end_value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
    // Set an invalid relative default_value to test validation.
    $field_edit = [
        'default_value_input[default_date_type]' => 'relative',
        'default_value_input[default_date]' => 'invalid date',
        'default_value_input[default_end_date_type]' => 'relative',
        'default_value_input[default_end_date]' => '+1 day',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    $this->assertText('The relative start date value entered is invalid.');
    $field_edit = [
        'default_value_input[default_date_type]' => 'relative',
        'default_value_input[default_date]' => '+1 day',
        'default_value_input[default_end_date_type]' => 'relative',
        'default_value_input[default_end_date]' => 'invalid date',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    $this->assertText('The relative end date value entered is invalid.');
    // Set a relative default_value.
    $field_edit = [
        'default_value_input[default_date_type]' => 'relative',
        'default_value_input[default_date]' => '+45 days',
        'default_value_input[default_end_date_type]' => 'relative',
        'default_value_input[default_end_date]' => '+90 days',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    // Check that default value is selected in default value form.
    $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this->assertOptionSelected('edit-default-value-input-default-date-type', 'relative', 'The default start value is selected in instance settings page');
    $this->assertFieldByName('default_value_input[default_date]', '+45 days', 'The relative default start value is displayed in instance settings page');
    $this->assertOptionSelected('edit-default-value-input-default-end-date-type', 'relative', 'The default end value is selected in instance settings page');
    $this->assertFieldByName('default_value_input[default_end_date]', '+90 days', 'The relative default end value is displayed in instance settings page');
    // Check if default_date has been stored successfully.
    $config_entity = $this->config('field.field.node.date_content.' . $field_name)
        ->get();
    $this->assertEqual($config_entity['default_value'][0], [
        'default_date_type' => 'relative',
        'default_date' => '+45 days',
        'default_end_date_type' => 'relative',
        'default_end_date' => '+90 days',
    ], 'Default value has been stored successfully');
    // Clear field cache in order to avoid stale cache values.
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    // Create a new node to check that datetime field default value is +90 days.
    $new_node = Node::create([
        'type' => 'date_content',
    ]);
    $expected_start_date = new DrupalDateTime('+45 days', DateTimeItemInterface::STORAGE_TIMEZONE);
    $expected_end_date = new DrupalDateTime('+90 days', DateTimeItemInterface::STORAGE_TIMEZONE);
    $this->assertEqual($new_node->get($field_name)
        ->offsetGet(0)->value, $expected_start_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
    $this->assertEqual($new_node->get($field_name)
        ->offsetGet(0)->end_value, $expected_end_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
    // Remove default value.
    $field_edit = [
        'default_value_input[default_date_type]' => '',
        'default_value_input[default_end_date_type]' => '',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    // Check that default value is selected in default value form.
    $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this->assertOptionSelected('edit-default-value-input-default-date-type', '', 'The default start value is selected in instance settings page');
    $this->assertFieldByName('default_value_input[default_date]', '', 'The relative default start value is empty in instance settings page');
    $this->assertOptionSelected('edit-default-value-input-default-end-date-type', '', 'The default end value is selected in instance settings page');
    $this->assertFieldByName('default_value_input[default_end_date]', '', 'The relative default end value is empty in instance settings page');
    // Check if default_date has been stored successfully.
    $config_entity = $this->config('field.field.node.date_content.' . $field_name)
        ->get();
    $this->assertTrue(empty($config_entity['default_value']), 'Empty default value has been stored successfully');
    // Clear field cache in order to avoid stale cache values.
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    // Create a new node to check that datetime field default value is not set.
    $new_node = Node::create([
        'type' => 'date_content',
    ]);
    $this->assertNull($new_node->get($field_name)->value, 'Default value is not set');
    // Set now as default_value for start date only.
    \Drupal::service('entity_display.repository')->getFormDisplay('node', 'date_content')
        ->setComponent($field_name, [
        'type' => 'datetime_default',
    ])
        ->save();
    $expected_date = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
    $field_edit = [
        'default_value_input[default_date_type]' => 'now',
        'default_value_input[default_end_date_type]' => '',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    // Make sure only the start value is populated on node add page.
    $this->drupalGet('node/add/date_content');
    $this->assertFieldByName("{$field_name}[0][value][date]", $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), 'Start date element populated.');
    $this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element empty.');
    // Set now as default_value for end date only.
    $field_edit = [
        'default_value_input[default_date_type]' => '',
        'default_value_input[default_end_date_type]' => 'now',
    ];
    $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    // Make sure only the start value is populated on node add page.
    $this->drupalGet('node/add/date_content');
    $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Start date element empty.');
    $this->assertFieldByName("{$field_name}[0][end_value][date]", $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), 'End date element populated.');
}

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