You are here

public function WebformClientSideValidationJavaScriptTest::testClientSideValidation in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_clientside_validation/tests/src/FunctionalJavascript/Validation/WebformClientSideValidationJavaScriptTest.php \Drupal\Tests\webform_clientside_validation\FunctionalJavascript\Validation\WebformClientSideValidationJavaScriptTest::testClientSideValidation()

Tests custom states.

File

modules/webform_clientside_validation/tests/src/FunctionalJavascript/Validation/WebformClientSideValidationJavaScriptTest.php, line 34

Class

WebformClientSideValidationJavaScriptTest
Tests for webform submission with client side validation.

Namespace

Drupal\Tests\webform_clientside_validation\FunctionalJavascript\Validation

Code

public function testClientSideValidation() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  /**************************************************************************/

  // Table select.

  /**************************************************************************/

  // Check that all radios and checkbox tables triggers client side validation.
  $this
    ->drupalGet('/webform/test_clientside_validation');
  $this
    ->assertCssSelect('#edit-tableselect-checkboxes.required');
  $this
    ->assertCssSelect('#edit-tableselect-checkboxes-one[required]');
  $this
    ->assertCssSelect('#edit-tableselect-radios.required');
  $this
    ->assertCssSelect('#edit-tableselect-radios-one[required]');
  $this
    ->submitForm([], 'Submit');
  $this
    ->assertRaw('This field is required.');
  $assert_session
    ->waitForText('tableselect_radios field is required.');

  // Check that all radios and checkbox tables triggers client side validation
  // with conditional logic.
  $this
    ->drupalGet('/webform/test_clientside_validation_state');
  $this
    ->assertNoCssSelect('#edit-tableselect-checkboxes.required');
  $this
    ->assertNoCssSelect('#edit-tableselect-checkboxes-one[required]');
  $this
    ->assertNoCssSelect('#edit-tableselect-radios.required');
  $this
    ->assertNoCssSelect('#edit-tableselect-radios-one[required]');
  $this
    ->click('#edit-trigger');
  $this
    ->assertCssSelect('#edit-tableselect-checkboxes.required');
  $this
    ->assertCssSelect('#edit-tableselect-checkboxes-one[required]');
  $this
    ->assertCssSelect('#edit-tableselect-radios.required');
  $this
    ->assertCssSelect('#edit-tableselect-radios-one[required]');

  /**************************************************************************/

  // Other elements.

  /**************************************************************************/

  // Check that custom 'other' error messages work.
  $this
    ->drupalGet('/webform/test_clientside_validation');
  $page
    ->findById('edit-select-other-select')
    ->selectOption('_other_');
  $page
    ->findById('edit-radios-other-radios-other-')
    ->selectOption('_other_');
  $page
    ->findById('edit-checkboxes-other-checkboxes-other-')
    ->check();
  $this
    ->submitForm([], 'Submit');
  $custom_errors = [
    'select-other' => 'Custom select_other required message.',
    'checkboxes-other' => 'Custom checkboxes_other required message.',
    'radios-other' => 'Custom radios_other required message.',
  ];
  foreach ($custom_errors as $element_type => $expected_error) {
    $element = $page
      ->find('css', "#edit-{$element_type}-other-error.error");
    static::assertNotNull($element);
    static::assertEquals($expected_error, $element
      ->getText());
  }
}