You are here

function ValidationTest::testPatternValidation in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Form/ValidationTest.php \Drupal\system\Tests\Form\ValidationTest::testPatternValidation()

Tests #pattern validation.

File

core/modules/system/src/Tests/Form/ValidationTest.php, line 149
Contains \Drupal\system\Tests\Form\ValidationTest.

Class

ValidationTest
Tests form processing and alteration via form validation handlers.

Namespace

Drupal\system\Tests\Form

Code

function testPatternValidation() {
  $textfield_error = t('%name field is not in the right format.', array(
    '%name' => 'One digit followed by lowercase letters',
  ));
  $tel_error = t('%name field is not in the right format.', array(
    '%name' => 'Everything except numbers',
  ));
  $password_error = t('%name field is not in the right format.', array(
    '%name' => 'Password',
  ));

  // Invalid textfield, valid tel.
  $edit = array(
    'textfield' => 'invalid',
    'tel' => 'valid',
  );
  $this
    ->drupalPostForm('form-test/pattern', $edit, 'Submit');
  $this
    ->assertRaw($textfield_error);
  $this
    ->assertNoRaw($tel_error);
  $this
    ->assertNoRaw($password_error);

  // Valid textfield, invalid tel, valid password.
  $edit = array(
    '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);

  // Non required fields are not validated if empty.
  $edit = array(
    'textfield' => '',
    'tel' => '',
  );
  $this
    ->drupalPostForm('form-test/pattern', $edit, 'Submit');
  $this
    ->assertNoRaw($textfield_error);
  $this
    ->assertNoRaw($tel_error);
  $this
    ->assertNoRaw($password_error);

  // Invalid password.
  $edit = array(
    'password' => $this
      ->randomMachineName(),
  );
  $this
    ->drupalPostForm('form-test/pattern', $edit, 'Submit');
  $this
    ->assertNoRaw($textfield_error);
  $this
    ->assertNoRaw($tel_error);
  $this
    ->assertRaw($password_error);

  // The pattern attribute overrides #pattern and is not validated on the
  // server side.
  $edit = array(
    '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.', array(
    '%name' => 'Client side validation',
  )));
}