You are here

protected function JavascriptStatesTest::doTextfieldTriggerTests in Drupal 10

Same name and namespace in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php \Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest::doTextfieldTriggerTests()

Tests states of elements triggered by a textfield element.

1 call to JavascriptStatesTest::doTextfieldTriggerTests()
JavascriptStatesTest::testJavascriptStates in core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php
Tests the JavaScript #states functionality of form elements.

File

core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php, line 164

Class

JavascriptStatesTest
Tests the state of elements based on another elements.

Namespace

Drupal\FunctionalJavascriptTests\Core\Form

Code

protected function doTextfieldTriggerTests() {
  $this
    ->drupalGet('form-test/javascript-states-form');
  $page = $this
    ->getSession()
    ->getPage();

  // Find trigger and target elements.
  $trigger = $page
    ->findField('textfield_trigger');
  $this
    ->assertNotEmpty($trigger);
  $checkbox_checked_target = $page
    ->findField('checkbox_checked_when_textfield_trigger_filled');
  $this
    ->assertNotEmpty($checkbox_checked_target);
  $checkbox_unchecked_target = $page
    ->findField('checkbox_unchecked_when_textfield_trigger_filled');
  $this
    ->assertNotEmpty($checkbox_unchecked_target);
  $select_invisible_target = $page
    ->findField('select_invisible_when_textfield_trigger_filled');
  $this
    ->assertNotEmpty($select_invisible_target);
  $select_visible_target = $page
    ->findField('select_visible_when_textfield_trigger_filled');
  $this
    ->assertNotEmpty($select_visible_target);
  $textfield_required_target = $page
    ->findField('textfield_required_when_textfield_trigger_filled');
  $this
    ->assertNotEmpty($textfield_required_target);
  $details = $this
    ->assertSession()
    ->elementExists('css', '#edit-details-expanded-when-textfield-trigger-filled');
  $textfield_in_details = $details
    ->findField('textfield_in_details');
  $this
    ->assertNotEmpty($textfield_in_details);

  // Verify initial state.
  $this
    ->assertFalse($checkbox_checked_target
    ->isChecked());
  $this
    ->assertTrue($checkbox_unchecked_target
    ->isChecked());
  $this
    ->assertTrue($select_invisible_target
    ->isVisible());
  $this
    ->assertFalse($select_visible_target
    ->isVisible());
  $this
    ->assertFalse($textfield_required_target
    ->hasAttribute('required'));
  $this
    ->assertFalse($details
    ->hasAttribute('open'));
  $this
    ->assertFalse($textfield_in_details
    ->isVisible());

  // Change state: fill the textfield.
  $trigger
    ->setValue('filled');

  // Verify triggered state.
  $this
    ->assertTrue($checkbox_checked_target
    ->isChecked());
  $this
    ->assertFalse($checkbox_unchecked_target
    ->isChecked());
  $this
    ->assertFalse($select_invisible_target
    ->isVisible());
  $this
    ->assertTrue($select_visible_target
    ->isVisible());
  $this
    ->assertEquals('required', $textfield_required_target
    ->getAttribute('required'));
  $this
    ->assertTrue($details
    ->hasAttribute('open'));
  $this
    ->assertTrue($textfield_in_details
    ->isVisible());
}