function ValidationTest::testValidateLimitErrors

Same name and namespace in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Form/ValidationTest.php \Drupal\Tests\system\Functional\Form\ValidationTest::testValidateLimitErrors()
  2. 10 core/modules/system/tests/src/Functional/Form/ValidationTest.php \Drupal\Tests\system\Functional\Form\ValidationTest::testValidateLimitErrors()
  3. 11.x core/modules/system/tests/src/Functional/Form/ValidationTest.php \Drupal\Tests\system\Functional\Form\ValidationTest::testValidateLimitErrors()

Tests partial form validation through #limit_validation_errors.

File

core/modules/system/tests/src/Functional/Form/ValidationTest.php, line 91

Class

ValidationTest
Tests form processing and alteration via form validation handlers.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testValidateLimitErrors() {
    $edit = [
        'test' => 'invalid',
        'test_numeric_index[0]' => 'invalid',
        'test_substring[foo]' => 'invalid',
    ];
    $path = 'form-test/limit-validation-errors';
    // Render the form, and verify that the buttons with limited server-side
    // validation have the proper 'formnovalidate' attribute (to prevent
    // client-side validation by the browser).
    $this->drupalGet($path);
    $expected = 'formnovalidate';
    foreach ([
        'partial',
        'partial-numeric-index',
        'substring',
    ] as $type) {
        // Verify the $type button has the proper formnovalidate attribute.
        $this->assertSession()
            ->elementExists('xpath', "//input[@id='edit-{$type}' and @formnovalidate='{$expected}']");
    }
    // The button with full server-side validation should not have the
    // 'formnovalidate' attribute.
    $this->assertSession()
        ->elementExists('xpath', "//input[@id='edit-full' and not(@formnovalidate)]");
    // Submit the form by pressing the 'Partial validate' button (uses
    // #limit_validation_errors) and ensure that the title field is not
    // validated, but the #element_validate handler for the 'test' field
    // is triggered.
    $this->drupalGet($path);
    $this->submitForm($edit, 'Partial validate');
    $this->assertSession()
        ->pageTextNotContains('Title field is required.');
    $this->assertSession()
        ->pageTextContains('Test element is invalid');
    // Edge case of #limit_validation_errors containing numeric indexes: same
    // thing with the 'Partial validate (numeric index)' button and the
    // 'test_numeric_index' field.
    $this->drupalGet($path);
    $this->submitForm($edit, 'Partial validate (numeric index)');
    $this->assertSession()
        ->pageTextNotContains('Title field is required.');
    $this->assertSession()
        ->pageTextContains('Test (numeric index) element is invalid');
    // Ensure something like 'foobar' isn't considered "inside" 'foo'.
    $this->drupalGet($path);
    $this->submitForm($edit, 'Partial validate (substring)');
    $this->assertSession()
        ->pageTextNotContains('Title field is required.');
    $this->assertSession()
        ->pageTextContains('Test (substring) foo element is invalid');
    // Ensure not validated values are not available to submit handlers.
    $this->drupalGet($path);
    $this->submitForm([
        'title' => '',
        'test' => 'valid',
    ], 'Partial validate');
    $this->assertSession()
        ->pageTextContains('Only validated values appear in the form values.');
    // Now test full form validation and ensure that the #element_validate
    // handler is still triggered.
    $this->drupalGet($path);
    $this->submitForm($edit, 'Full validate');
    $this->assertSession()
        ->pageTextContains('Title field is required.');
    $this->assertSession()
        ->pageTextContains('Test element is invalid');
}

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