You are here

function DateTimeFieldTest::testInvalidField 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::testInvalidField()

Test that invalid values are caught and marked as invalid.

File

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

Class

DateTimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\datetime\Tests

Code

function testInvalidField() {

  // Change the field to a datetime field.
  $this->fieldStorage
    ->setSetting('datetime_type', 'datetime');
  $this->fieldStorage
    ->save();
  $field_name = $this->fieldStorage
    ->getName();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
  $this
    ->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');

  // Submit invalid dates and ensure they is not accepted.
  $date_value = '';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => '12:00:00',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', 'Empty date value has been caught.');
  $date_value = 'aaaa-12-01';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => '00:00:00',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', format_string('Invalid year value %date has been caught.', array(
    '%date' => $date_value,
  )));
  $date_value = '2012-75-01';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => '00:00:00',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', format_string('Invalid month value %date has been caught.', array(
    '%date' => $date_value,
  )));
  $date_value = '2012-12-99';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => '00:00:00',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', format_string('Invalid day value %date has been caught.', array(
    '%date' => $date_value,
  )));
  $date_value = '2012-12-01';
  $time_value = '';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => $time_value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', 'Empty time value has been caught.');
  $date_value = '2012-12-01';
  $time_value = '49:00:00';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => $time_value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', array(
    '%time' => $time_value,
  )));
  $date_value = '2012-12-01';
  $time_value = '12:99:00';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => $time_value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', array(
    '%time' => $time_value,
  )));
  $date_value = '2012-12-01';
  $time_value = '12:15:99';
  $edit = array(
    "{$field_name}[0][value][date]" => $date_value,
    "{$field_name}[0][value][time]" => $time_value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array(
    '%time' => $time_value,
  )));
}