You are here

function DateTimeFieldTest::testDefaultValue in Zircon Profile 8

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

Test default value functionality.

File

core/modules/datetime/src/Tests/DateTimeFieldTest.php, line 621
Contains \Drupal\datetime\Tests\DateTimeFieldTest.

Class

DateTimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testDefaultValue() {

  // Create a test content type.
  $this
    ->drupalCreateContentType(array(
    'type' => 'date_content',
  ));

  // Create a field storage with settings to validate.
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'datetime',
    'settings' => array(
      'datetime_type' => 'date',
    ),
  ));
  $field_storage
    ->save();
  $field = entity_create('field_config', array(
    'field_storage' => $field_storage,
    'bundle' => 'date_content',
  ));
  $field
    ->save();

  // Set now as default_value.
  $field_edit = array(
    '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], array(
    'default_date_type' => 'now',
    'default_date' => 'now',
  ), 'Default value has been stored successfully');

  // Clear field cache in order to avoid stale cache values.
  \Drupal::entityManager()
    ->clearCachedFieldDefinitions();

  // Create a new node to check that datetime field default value is today.
  $new_node = entity_create('node', array(
    'type' => 'date_content',
  ));
  $expected_date = new DrupalDateTime('now', DATETIME_STORAGE_TIMEZONE);
  $this
    ->assertEqual($new_node
    ->get($field_name)
    ->offsetGet(0)->value, $expected_date
    ->format(DATETIME_DATE_STORAGE_FORMAT));

  // Set an invalid relative default_value to test validation.
  $field_edit = array(
    '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 = array(
    '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], array(
    '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::entityManager()
    ->clearCachedFieldDefinitions();

  // Create a new node to check that datetime field default value is +90 days.
  $new_node = entity_create('node', array(
    'type' => 'date_content',
  ));
  $expected_date = new DrupalDateTime('+90 days', DATETIME_STORAGE_TIMEZONE);
  $this
    ->assertEqual($new_node
    ->get($field_name)
    ->offsetGet(0)->value, $expected_date
    ->format(DATETIME_DATE_STORAGE_FORMAT));

  // Remove default value.
  $field_edit = array(
    '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::entityManager()
    ->clearCachedFieldDefinitions();

  // Create a new node to check that datetime field default value is not set.
  $new_node = entity_create('node', array(
    'type' => 'date_content',
  ));
  $this
    ->assertNull($new_node
    ->get($field_name)->value, 'Default value is not set');
}