function DateTimeFieldTest::testInvalidField

Same name in other branches
  1. 8.9.x core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php \Drupal\Tests\datetime\Functional\DateTimeFieldTest::testInvalidField()
  2. 10 core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php \Drupal\Tests\datetime\Functional\DateTimeFieldTest::testInvalidField()
  3. 11.x core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php \Drupal\Tests\datetime\Functional\DateTimeFieldTest::testInvalidField()

Tests that invalid values are caught and marked as invalid.

File

core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php, line 794

Class

DateTimeFieldTest
Tests Datetime field functionality.

Namespace

Drupal\Tests\datetime\Functional

Code

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->assertSession()
        ->fieldValueEquals("{$field_name}[0][value][date]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value][time]", '');
    // 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid year value.
    $date_value = 'aaaa-12-01';
    $edit = [
        "{$field_name}[0][value][date]" => $date_value,
        "{$field_name}[0][value][time]" => '00:00:00',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid month value.
    $date_value = '2012-75-01';
    $edit = [
        "{$field_name}[0][value][date]" => $date_value,
        "{$field_name}[0][value][time]" => '00:00:00',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid day value.
    $date_value = '2012-12-99';
    $edit = [
        "{$field_name}[0][value][date]" => $date_value,
        "{$field_name}[0][value][time]" => '00:00:00',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid time value.
    $date_value = '2012-12-01';
    $time_value = '';
    $edit = [
        "{$field_name}[0][value][date]" => $date_value,
        "{$field_name}[0][value][time]" => $time_value,
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid hour value.
    $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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid minutes 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid seconds 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.