View source
<?php
namespace Drupal\Tests\system\Functional\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Render\Element;
use Drupal\Tests\BrowserTestBase;
class ValidationTest extends BrowserTestBase {
public static $modules = [
'form_test',
];
protected $defaultTheme = 'stark';
public function testValidate() {
$this
->drupalGet('form-test/validate');
$edit = [
'name' => 'element_validate',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertFieldByName('name', '#value changed by #element_validate', 'Form element #value was altered.');
$this
->assertText('Name value: value changed by setValueForElement() in #element_validate', 'Form element value in $form_state was altered.');
$edit = [
'name' => 'validate',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertFieldByName('name', '#value changed by #validate', 'Form element #value was altered.');
$this
->assertText('Name value: value changed by setValueForElement() in #validate', 'Form element value in $form_state was altered.');
$edit = [
'name' => 'element_validate_access',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->assertNoFieldByName('name', 'Form element was hidden.');
$this
->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
$this
->drupalPostForm(NULL, [], 'Save');
$this
->assertNoFieldByName('name', 'Form element was hidden.');
$this
->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
$this
->drupalLogin($this
->drupalCreateUser());
$this
->drupalGet('form-test/validate');
$this
->assertSession()
->elementExists('css', 'input[name="form_token"]')
->setValue('invalid_token');
$this
->drupalPostForm(NULL, [
'name' => 'validate',
], 'Save');
$this
->assertNoFieldByName('name', '#value changed by #validate', 'Form element #value was not altered.');
$this
->assertNoText('Name value: value changed by setValueForElement() in #validate', 'Form element value in $form_state was not altered.');
$this
->assertText('The form has become outdated.');
}
public function testDisabledToken() {
$this
->drupalPostForm('form-test/validate-no-token', [], 'Save');
$this
->assertText('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) {
$element = $this
->xpath('//input[@id=:id and @formnovalidate=:expected]', [
':id' => 'edit-' . $type,
':expected' => $expected,
]);
$this
->assertTrue(!empty($element), new FormattableMarkup('The @type button has the proper formnovalidate attribute.', [
'@type' => $type,
]));
}
$element = $this
->xpath('//input[@id=:id and not(@formnovalidate)]', [
':id' => 'edit-full',
]);
$this
->assertTrue(!empty($element), 'The button with full server-side validation does not have the formnovalidate attribute.');
$this
->drupalPostForm($path, $edit, t('Partial validate'));
$this
->assertNoText(t('@name field is required.', [
'@name' => 'Title',
]));
$this
->assertText('Test element is invalid');
$this
->drupalPostForm($path, $edit, t('Partial validate (numeric index)'));
$this
->assertNoText(t('@name field is required.', [
'@name' => 'Title',
]));
$this
->assertText('Test (numeric index) element is invalid');
$this
->drupalPostForm($path, $edit, t('Partial validate (substring)'));
$this
->assertNoText(t('@name field is required.', [
'@name' => 'Title',
]));
$this
->assertText('Test (substring) foo element is invalid');
$this
->drupalPostForm($path, [
'title' => '',
'test' => 'valid',
], t('Partial validate'));
$this
->assertText('Only validated values appear in the form values.');
$this
->drupalPostForm($path, $edit, t('Full validate'));
$this
->assertText(t('@name field is required.', [
'@name' => 'Title',
]));
$this
->assertText('Test element is invalid');
}
public function testPatternValidation() {
$textfield_error = t('%name field is not in the right format.', [
'%name' => 'One digit followed by lowercase letters',
]);
$tel_error = t('%name field is not in the right format.', [
'%name' => 'Everything except numbers',
]);
$password_error = t('%name field is not in the right format.', [
'%name' => 'Password',
]);
$edit = [
'textfield' => 'invalid',
'tel' => 'valid',
];
$this
->drupalPostForm('form-test/pattern', $edit, 'Submit');
$this
->assertRaw($textfield_error);
$this
->assertNoRaw($tel_error);
$this
->assertNoRaw($password_error);
$edit = [
'textfield' => '7seven',
'tel' => '818937',
'password' => '0100110',
];
$this
->drupalPostForm('form-test/pattern', $edit, 'Submit');
$this
->assertNoRaw($textfield_error);
$this
->assertRaw($tel_error);
$this
->assertNoRaw($password_error);
$edit = [
'textfield' => '',
'tel' => '',
];
$this
->drupalPostForm('form-test/pattern', $edit, 'Submit');
$this
->assertNoRaw($textfield_error);
$this
->assertNoRaw($tel_error);
$this
->assertNoRaw($password_error);
$edit = [
'password' => $this
->randomMachineName(),
];
$this
->drupalPostForm('form-test/pattern', $edit, 'Submit');
$this
->assertNoRaw($textfield_error);
$this
->assertNoRaw($tel_error);
$this
->assertRaw($password_error);
$edit = [
'textfield' => '',
'tel' => '',
'url' => 'http://www.example.com/',
];
$this
->drupalPostForm('form-test/pattern', $edit, 'Submit');
$this
->assertNoRaw(t('%name field is not in the right format.', [
'%name' => 'Client side validation',
]));
}
public function testCustomRequiredError() {
$form = \Drupal::formBuilder()
->getForm('\\Drupal\\form_test\\Form\\FormTestValidateRequiredForm');
$edit = [];
$this
->drupalPostForm('form-test/validate-required', $edit, 'Submit');
foreach (Element::children($form) as $key) {
if (isset($form[$key]['#required_error'])) {
$this
->assertNoText(t('@name field is required.', [
'@name' => $form[$key]['#title'],
]));
$this
->assertText($form[$key]['#required_error']);
}
elseif (isset($form[$key]['#form_test_required_error'])) {
$this
->assertNoText(t('@name field is required.', [
'@name' => $form[$key]['#title'],
]));
$this
->assertText($form[$key]['#form_test_required_error']);
}
}
$this
->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.'));
$edit = [
'textfield' => $this
->randomString(),
'checkboxes[foo]' => TRUE,
'select' => 'foo',
];
$this
->drupalPostForm('form-test/validate-required', $edit, 'Submit');
foreach (Element::children($form) as $key) {
if (isset($form[$key]['#required_error'])) {
$this
->assertNoText(t('@name field is required.', [
'@name' => $form[$key]['#title'],
]));
$this
->assertNoText($form[$key]['#required_error']);
}
elseif (isset($form[$key]['#form_test_required_error'])) {
$this
->assertNoText(t('@name field is required.', [
'@name' => $form[$key]['#title'],
]));
$this
->assertNoText($form[$key]['#form_test_required_error']);
}
}
$this
->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.'));
}
}