function DateRangeFieldTest::testInvalidField

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

Tests that invalid values are caught and marked as invalid.

File

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

Class

DateRangeFieldTest
Tests Daterange field functionality.

Namespace

Drupal\Tests\datetime_range\Functional

Code

public function testInvalidField() : void {
    // 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->assertSession()
        ->fieldValueEquals("{$field_name}[0][value][date]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][value][time]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][end_value][date]", '');
    $this->assertSession()
        ->fieldValueEquals("{$field_name}[0][end_value][time]", '');
    // 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->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',
        "{$field_name}[0][end_value][date]" => '2012-12-01',
        "{$field_name}[0][end_value][time]" => '12: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',
        "{$field_name}[0][end_value][date]" => '2012-12-01',
        "{$field_name}[0][end_value][time]" => '12: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',
        "{$field_name}[0][end_value][date]" => '2012-12-01',
        "{$field_name}[0][end_value][time]" => '12:00:00',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid hour value.
    $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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid minutes 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid seconds 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid year value.
    $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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid month 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid day 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid hour value.
    $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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid minutes 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // Invalid seconds 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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('date is invalid');
    // End date before start date.
    $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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The ' . $field_label . ' end date cannot be before the start date');
    // End date before start date.
    $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->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The ' . $field_label . ' end date cannot be before the start date');
}

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