public function DateTimeFieldTest::testInvalidField in Drupal 8
Same name and namespace in other branches
- 9 core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php \Drupal\Tests\datetime\Functional\DateTimeFieldTest::testInvalidField()
Test that invalid values are caught and marked as invalid.
File
- core/
modules/ datetime/ tests/ src/ Functional/ DateTimeFieldTest.php, line 788
Class
- DateTimeFieldTest
- Tests Datetime field functionality.
Namespace
Drupal\Tests\datetime\FunctionalCode
public 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 = [
"{$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 = [
"{$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', new FormattableMarkup('Invalid 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',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText('date is invalid', new FormattableMarkup('Invalid 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',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText('date is invalid', new FormattableMarkup('Invalid day value %date has been caught.', [
'%date' => $date_value,
]));
$date_value = '2012-12-01';
$time_value = '';
$edit = [
"{$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 = [
"{$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', new FormattableMarkup('Invalid hour value %time has been caught.', [
'%time' => $time_value,
]));
$date_value = '2012-12-01';
$time_value = '12:99:00';
$edit = [
"{$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', new FormattableMarkup('Invalid minute value %time has been caught.', [
'%time' => $time_value,
]));
$date_value = '2012-12-01';
$time_value = '12:15:99';
$edit = [
"{$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', new FormattableMarkup('Invalid second value %time has been caught.', [
'%time' => $time_value,
]));
}