function DatetimeFieldTest::testDefaultValue

Test default value functionality.

File

drupal/core/modules/datetime/lib/Drupal/datetime/Tests/DatetimeFieldTest.php, line 289
Contains \Drupal\datetime\Tests\DatetimeFieldTest.

Class

DatetimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testDefaultValue() {

  // Change the field to a datetime field.
  $this->field['settings']['datetime_type'] = 'datetime';
  field_update_field($this->field);

  // Set the default value to 'now'.
  $this->instance['settings']['default_value'] = 'now';
  $this->instance['default_value_function'] = 'datetime_default_value';
  field_update_instance($this->instance);

  // Display creation form.
  $date = new DrupalDateTime();
  $date_format = 'Y-m-d';
  $this
    ->drupalGet('entity_test/add');
  $langcode = Language::LANGCODE_NOT_SPECIFIED;

  // See if current date is set. We cannot test for the precise time because
  // it may be a few seconds between the time the comparison date is created
  // and the form date, so we just test the date and that the time is not
  // empty.
  $this
    ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][value][date]", $date
    ->format($date_format), 'Date element found.');
  $this
    ->assertNoFieldByName("{$this->field['field_name']}[{$langcode}][0][value][time]", '', 'Time element found.');

  // Set the default value to 'blank'.
  $this->instance['settings']['default_value'] = 'blank';
  $this->instance['default_value_function'] = 'datetime_default_value';
  field_update_instance($this->instance);

  // Display creation form.
  $date = new DrupalDateTime();
  $this
    ->drupalGet('entity_test/add');

  // See that no date is set.
  $this
    ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][value][date]", '', 'Date element found.');
  $this
    ->assertFieldByName("{$this->field['field_name']}[{$langcode}][0][value][time]", '', 'Time element found.');
}