You are here

public function DateTimeFieldTest::testDefaultValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php \Drupal\Tests\datetime\Functional\DateTimeFieldTest::testDefaultValue()

Tests default value functionality.

File

core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php, line 672

Class

DateTimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\Tests\datetime\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' => 'datetime',
    'settings' => [
      'datetime_type' => 'date',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'date_content',
  ]);
  $field
    ->save();

  // Loop through defined timezones to test that date-only defaults 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);

    // Set now as default_value.
    $field_edit = [
      'default_value_input[default_date_type]' => 'now',
    ];
    $this
      ->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this
      ->submitForm($field_edit, '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
      ->assertTrue($this
      ->assertSession()
      ->optionExists('edit-default-value-input-default-date-type', 'now')
      ->isSelected());

    // Check that the relative default value is empty.
    $this
      ->assertSession()
      ->fieldValueEquals('default_value_input[default_date]', '');

    // Check if default_date has been stored successfully.
    $config_entity = $this
      ->config('field.field.node.date_content.' . $field_name)
      ->get();
    $this
      ->assertEquals([
      'default_date_type' => 'now',
      'default_date' => 'now',
    ], $config_entity['default_value'][0], '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', date_default_timezone_get());
    $this
      ->assertEquals($expected_date
      ->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), $new_node
      ->get($field_name)
      ->offsetGet(0)->value);

    // 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',
    ];
    $this
      ->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this
      ->submitForm($field_edit, 'Save settings');
    $this
      ->assertSession()
      ->pageTextContains('The relative date value entered is invalid.');

    // Set a relative default_value.
    $field_edit = [
      'default_value_input[default_date_type]' => 'relative',
      'default_value_input[default_date]' => '+90 days',
    ];
    $this
      ->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this
      ->submitForm($field_edit, '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
      ->assertTrue($this
      ->assertSession()
      ->optionExists('edit-default-value-input-default-date-type', 'relative')
      ->isSelected());

    // Check that the relative default value is displayed.
    $this
      ->assertSession()
      ->fieldValueEquals('default_value_input[default_date]', '+90 days');

    // Check if default_date has been stored successfully.
    $config_entity = $this
      ->config('field.field.node.date_content.' . $field_name)
      ->get();
    $this
      ->assertEquals([
      'default_date_type' => 'relative',
      'default_date' => '+90 days',
    ], $config_entity['default_value'][0], '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_date = new DrupalDateTime('+90 days', date_default_timezone_get());
    $this
      ->assertEquals($expected_date
      ->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), $new_node
      ->get($field_name)
      ->offsetGet(0)->value);

    // Remove default value.
    $field_edit = [
      'default_value_input[default_date_type]' => '',
    ];
    $this
      ->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
    $this
      ->submitForm($field_edit, '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
      ->assertTrue($this
      ->assertSession()
      ->optionExists('edit-default-value-input-default-date-type', '')
      ->isSelected());

    // Check that the relative default value is empty.
    $this
      ->assertSession()
      ->fieldValueEquals('default_value_input[default_date]', '');

    // 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');
  }
}