Test that invalid values are caught and marked as invalid.
function testInvalidField() {
// Change the field to a datetime field.
$this->field['settings']['datetime_type'] = 'datetime';
field_update_field($this->field);
// Display creation form.
$this
->drupalGet('entity_test/add');
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$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.');
// Submit invalid dates and ensure they is not accepted.
$date_value = '';
$edit = array(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => '12:00:00',
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertText('date is invalid', 'Empty date value has been caught.');
$date_value = 'aaaa-12-01';
$edit = array(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => '00:00:00',
);
$this
->drupalPost(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(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => '00:00:00',
);
$this
->drupalPost(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(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => '00:00:00',
);
$this
->drupalPost(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(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => $time_value,
);
$this
->drupalPost(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(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => $time_value,
);
$this
->drupalPost(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(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => $time_value,
);
$this
->drupalPost(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(
"{$this->field['field_name']}[{$langcode}][0][value][date]" => $date_value,
"{$this->field['field_name']}[{$langcode}][0][value][time]" => $time_value,
);
$this
->drupalPost(NULL, $edit, t('Save'));
$this
->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array(
'%time' => $time_value,
)));
}