You are here

public function DateTimeFieldTest::testDefaultValue in Drupal 8

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

Test default value functionality.

File

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

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
      ->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 value is selected in instance settings page');
    $this
      ->assertFieldByName('default_value_input[default_date]', '', 'The relative 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 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
      ->assertEqual($new_node
      ->get($field_name)
      ->offsetGet(0)->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',
    ];
    $this
      ->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
    $this
      ->assertText('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
      ->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 value is selected in instance settings page');
    $this
      ->assertFieldByName('default_value_input[default_date]', '+90 days', 'The relative default 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' => '+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_date = new DrupalDateTime('+90 days', date_default_timezone_get());
    $this
      ->assertEqual($new_node
      ->get($field_name)
      ->offsetGet(0)->value, $expected_date
      ->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));

    // Remove default value.
    $field_edit = [
      'default_value_input[default_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 value is selected in instance settings page');
    $this
      ->assertFieldByName('default_value_input[default_date]', '', 'The relative 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
      ->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');
  }
}