View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Core\Render\Element;
use Drupal\Tests\BrowserTestBase;
class ValidationTest extends BrowserTestBase {
protected static $modules = [
'form_test',
];
protected $defaultTheme = 'stark';
public function testValidate() {
$this
->drupalGet('form-test/validate');
$edit = [
'name' => 'element_validate',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->fieldValueEquals('name', '#value changed by #element_validate');
$this
->assertSession()
->pageTextContains('Name value: value changed by setValueForElement() in #element_validate');
$edit = [
'name' => 'validate',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->fieldValueEquals('name', '#value changed by #validate');
$this
->assertSession()
->pageTextContains('Name value: value changed by setValueForElement() in #validate');
$edit = [
'name' => 'element_validate_access',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->fieldNotExists('name');
$this
->assertSession()
->pageTextContains('Name value: element_validate_access');
$this
->submitForm([], 'Save');
$this
->assertSession()
->fieldValueNotEquals('name', 'Form element was hidden.');
$this
->assertSession()
->pageTextContains('Name value: element_validate_access');
$this
->drupalLogin($this
->drupalCreateUser());
$this
->drupalGet('form-test/validate');
$this
->assertSession()
->elementExists('css', 'input[name="form_token"]')
->setValue('invalid_token');
$this
->submitForm([
'name' => 'validate',
], 'Save');
$this
->assertSession()
->fieldValueNotEquals('name', '#value changed by #validate');
$this
->assertSession()
->pageTextNotContains('Name value: value changed by setValueForElement() in #validate');
$this
->assertSession()
->pageTextContains('The form has become outdated.');
}
public function testDisabledToken() {
$this
->drupalGet('form-test/validate-no-token');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('The form_test_validate_no_token form has been submitted successfully.');
}
public function testValidateLimitErrors() {
$edit = [
'test' => 'invalid',
'test_numeric_index[0]' => 'invalid',
'test_substring[foo]' => 'invalid',
];
$path = 'form-test/limit-validation-errors';
$this
->drupalGet($path);
$expected = 'formnovalidate';
foreach ([
'partial',
'partial-numeric-index',
'substring',
] as $type) {
$this
->assertSession()
->elementExists('xpath', "//input[@id='edit-{$type}' and @formnovalidate='{$expected}']");
}
$this
->assertSession()
->elementExists('xpath', "//input[@id='edit-full' and not(@formnovalidate)]");
$this
->drupalGet($path);
$this
->submitForm($edit, 'Partial validate');
$this
->assertSession()
->pageTextNotContains('Title field is required.');
$this
->assertSession()
->pageTextContains('Test element is invalid');
$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');
$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');
$this
->drupalGet($path);
$this
->submitForm([
'title' => '',
'test' => 'valid',
], 'Partial validate');
$this
->assertSession()
->pageTextContains('Only validated values appear in the form values.');
$this
->drupalGet($path);
$this
->submitForm($edit, 'Full validate');
$this
->assertSession()
->pageTextContains('Title field is required.');
$this
->assertSession()
->pageTextContains('Test element is invalid');
}
public function testPatternValidation() {
$textfield_error = 'One digit followed by lowercase letters field is not in the right format.';
$tel_error = 'Everything except numbers field is not in the right format.';
$password_error = 'Password field is not in the right format.';
$edit = [
'textfield' => 'invalid',
'tel' => 'valid',
];
$this
->drupalGet('form-test/pattern');
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->pageTextContains($textfield_error);
$this
->assertSession()
->pageTextNotContains($tel_error);
$this
->assertSession()
->pageTextNotContains($password_error);
$edit = [
'textfield' => '7seven',
'tel' => '818937',
'password' => '0100110',
];
$this
->drupalGet('form-test/pattern');
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->pageTextNotContains($textfield_error);
$this
->assertSession()
->pageTextContains($tel_error);
$this
->assertSession()
->pageTextNotContains($password_error);
$edit = [
'textfield' => '',
'tel' => '',
];
$this
->drupalGet('form-test/pattern');
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->pageTextNotContains($textfield_error);
$this
->assertSession()
->pageTextNotContains($tel_error);
$this
->assertSession()
->pageTextNotContains($password_error);
$edit = [
'password' => $this
->randomMachineName(),
];
$this
->drupalGet('form-test/pattern');
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->pageTextNotContains($textfield_error);
$this
->assertSession()
->pageTextNotContains($tel_error);
$this
->assertSession()
->pageTextContains($password_error);
$edit = [
'textfield' => '',
'tel' => '',
'url' => 'http://www.example.com/',
];
$this
->drupalGet('form-test/pattern');
$this
->submitForm($edit, 'Submit');
$this
->assertSession()
->pageTextNotContains('Client side validation field is not in the right format.');
}
public function testCustomRequiredError() {
$form = \Drupal::formBuilder()
->getForm('\\Drupal\\form_test\\Form\\FormTestValidateRequiredForm');
$edit = [];
$this
->drupalGet('form-test/validate-required');
$this
->submitForm($edit, 'Submit');
foreach (Element::children($form) as $key) {
if (isset($form[$key]['#required_error'])) {
$this
->assertSession()
->pageTextNotContains($form[$key]['#title'] . ' field is required.');
$this
->assertSession()
->pageTextContains((string) $form[$key]['#required_error']);
}
elseif (isset($form[$key]['#form_test_required_error'])) {
$this
->assertSession()
->pageTextNotContains($form[$key]['#title'] . ' field is required.');
$this
->assertSession()
->pageTextContains((string) $form[$key]['#form_test_required_error']);
}
}
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$edit = [
'textfield' => $this
->randomString(),
'checkboxes[foo]' => TRUE,
'select' => 'foo',
];
$this
->drupalGet('form-test/validate-required');
$this
->submitForm($edit, 'Submit');
foreach (Element::children($form) as $key) {
if (isset($form[$key]['#required_error'])) {
$this
->assertSession()
->pageTextNotContains($form[$key]['#title'] . ' field is required.');
$this
->assertSession()
->pageTextNotContains((string) $form[$key]['#required_error']);
}
elseif (isset($form[$key]['#form_test_required_error'])) {
$this
->assertSession()
->pageTextNotContains($form[$key]['#title'] . ' field is required.');
$this
->assertSession()
->pageTextNotContains((string) $form[$key]['#form_test_required_error']);
}
}
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}
}