You are here

protected function JavascriptStatesTest::doRadiosTriggerTests in Drupal 9

Tests states of elements triggered by a radios element.

1 call to JavascriptStatesTest::doRadiosTriggerTests()
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 209

Class

JavascriptStatesTest
Tests the state of elements based on another elements.

Namespace

Drupal\FunctionalJavascriptTests\Core\Form

Code

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

  // Find trigger and target elements.
  $trigger = $page
    ->findField('radios_trigger');
  $this
    ->assertNotEmpty($trigger);
  $fieldset_visible_when_value2 = $this
    ->assertSession()
    ->elementExists('css', '#edit-fieldset-visible-when-radios-trigger-has-value2');
  $textfield_in_fieldset = $fieldset_visible_when_value2
    ->findField('textfield_in_fieldset');
  $this
    ->assertNotEmpty($textfield_in_fieldset);
  $checkbox_checked_target = $page
    ->findField('checkbox_checked_when_radios_trigger_has_value3');
  $this
    ->assertNotEmpty($checkbox_checked_target);
  $checkbox_unchecked_target = $page
    ->findField('checkbox_unchecked_when_radios_trigger_has_value3');
  $this
    ->assertNotEmpty($checkbox_unchecked_target);
  $textfield_invisible_target = $page
    ->findField('textfield_invisible_when_radios_trigger_has_value2');
  $this
    ->assertNotEmpty($textfield_invisible_target);
  $select_required_target = $page
    ->findField('select_required_when_radios_trigger_has_value2');
  $this
    ->assertNotEmpty($select_required_target);
  $details = $this
    ->assertSession()
    ->elementExists('css', '#edit-details-expanded-when-radios-trigger-has-value3');
  $textfield_in_details = $details
    ->findField('textfield_in_details');
  $this
    ->assertNotEmpty($textfield_in_details);

  // Verify initial state, both the fieldset and something inside it.
  $this
    ->assertFalse($fieldset_visible_when_value2
    ->isVisible());
  $this
    ->assertFalse($textfield_in_fieldset
    ->isVisible());
  $this
    ->assertFalse($checkbox_checked_target
    ->isChecked());
  $this
    ->assertTrue($checkbox_unchecked_target
    ->isChecked());
  $this
    ->assertTrue($textfield_invisible_target
    ->isVisible());
  $this
    ->assertFalse($select_required_target
    ->hasAttribute('required'));
  $this
    ->assertFalse($details
    ->hasAttribute('open'));
  $this
    ->assertFalse($textfield_in_details
    ->isVisible());

  // Change state: select the value2 radios option.
  $trigger
    ->selectOption('value2');

  // Verify triggered state.
  $this
    ->assertTrue($fieldset_visible_when_value2
    ->isVisible());
  $this
    ->assertTrue($textfield_in_fieldset
    ->isVisible());
  $this
    ->assertFalse($textfield_invisible_target
    ->isVisible());
  $this
    ->assertTrue($select_required_target
    ->hasAttribute('required'));

  // Checkboxes and details should not have changed state, yet.
  $this
    ->assertFalse($checkbox_checked_target
    ->isChecked());
  $this
    ->assertTrue($checkbox_unchecked_target
    ->isChecked());
  $this
    ->assertFalse($details
    ->hasAttribute('open'));
  $this
    ->assertFalse($textfield_in_details
    ->isVisible());

  // Change state: select the value3 radios option.
  $trigger
    ->selectOption('value3');

  // Fieldset and contents should re-disappear.
  $this
    ->assertFalse($fieldset_visible_when_value2
    ->isVisible());
  $this
    ->assertFalse($textfield_in_fieldset
    ->isVisible());

  // Textfield and select should revert to initial state.
  $this
    ->assertTrue($textfield_invisible_target
    ->isVisible());
  $this
    ->assertFalse($select_required_target
    ->hasAttribute('required'));

  // Checkbox states should now change.
  $this
    ->assertTrue($checkbox_checked_target
    ->isChecked());
  $this
    ->assertFalse($checkbox_unchecked_target
    ->isChecked());

  // Details should now be expanded.
  $this
    ->assertTrue($details
    ->hasAttribute('open'));
  $this
    ->assertTrue($textfield_in_details
    ->isVisible());
}