You are here

public function DateRangeFieldTest::testInvalidField in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testInvalidField()
  2. 10 core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php \Drupal\Tests\datetime_range\Functional\DateRangeFieldTest::testInvalidField()

Test that invalid values are caught and marked as invalid.

File

core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php, line 1166

Class

DateRangeFieldTest
Tests Daterange field functionality.

Namespace

Drupal\Tests\datetime_range\Functional

Code

public function testInvalidField() {

  // Change the field to a datetime field.
  $this->fieldStorage
    ->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_DATETIME);
  $this->fieldStorage
    ->save();
  $field_name = $this->fieldStorage
    ->getName();
  $field_label = $this->field
    ->label();
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][value][date]", '', 'Start date element found.');
  $this
    ->assertFieldByName("{$field_name}[0][value][time]", '', 'Start time element found.');
  $this
    ->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.');
  $this
    ->assertFieldByName("{$field_name}[0][end_value][time]", '', 'End time element found.');

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

  // Submit invalid start times and ensure they is not accepted.
  $time_value = '';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => $time_value,
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => '12:00:00',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', 'Empty start time value has been caught.');
  $time_value = '49:00:00';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => $time_value,
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => '12:00:00',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', new FormattableMarkup('Invalid start hour value %time has been caught.', [
    '%time' => $time_value,
  ]));
  $time_value = '12:99:00';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => $time_value,
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => '12:00:00',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', new FormattableMarkup('Invalid start minute value %time has been caught.', [
    '%time' => $time_value,
  ]));
  $time_value = '12:15:99';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => $time_value,
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => '12:00:00',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', new FormattableMarkup('Invalid start second value %time has been caught.', [
    '%time' => $time_value,
  ]));

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

  // Submit invalid start times and ensure they is not accepted.
  $time_value = '';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => '12:00:00',
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => $time_value,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', 'Empty end time value has been caught.');
  $time_value = '49:00:00';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => '12:00:00',
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => $time_value,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', new FormattableMarkup('Invalid end hour value %time has been caught.', [
    '%time' => $time_value,
  ]));
  $time_value = '12:99:00';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => '12:00:00',
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => $time_value,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', new FormattableMarkup('Invalid end minute value %time has been caught.', [
    '%time' => $time_value,
  ]));
  $time_value = '12:15:99';
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => '12:00:00',
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => $time_value,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText('date is invalid', new FormattableMarkup('Invalid end second value %time has been caught.', [
    '%time' => $time_value,
  ]));
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => '12:00:00',
    "{$field_name}[0][end_value][date]" => '2010-12-01',
    "{$field_name}[0][end_value][time]" => '12:00:00',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText(new FormattableMarkup('The @title end date cannot be before the start date', [
    '@title' => $field_label,
  ]), 'End date before start date has been caught.');
  $edit = [
    "{$field_name}[0][value][date]" => '2012-12-01',
    "{$field_name}[0][value][time]" => '12:00:00',
    "{$field_name}[0][end_value][date]" => '2012-12-01',
    "{$field_name}[0][end_value][time]" => '11:00:00',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText(new FormattableMarkup('The @title end date cannot be before the start date', [
    '@title' => $field_label,
  ]), 'End time before start time has been caught.');
}