You are here

protected function JavascriptStatesTest::doCheckboxTriggerTests 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::doCheckboxTriggerTests()

Tests states of elements triggered by a checkbox element.

1 call to JavascriptStatesTest::doCheckboxTriggerTests()
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 71

Class

JavascriptStatesTest
Tests the state of elements based on another elements.

Namespace

Drupal\FunctionalJavascriptTests\Core\Form

Code

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

  // Find trigger and target elements.
  $trigger = $page
    ->findField('checkbox_trigger');
  $this
    ->assertNotEmpty($trigger);
  $textfield_invisible_element = $page
    ->findField('textfield_invisible_when_checkbox_trigger_checked');
  $this
    ->assertNotEmpty($textfield_invisible_element);
  $textfield_required_element = $page
    ->findField('textfield_required_when_checkbox_trigger_checked');
  $this
    ->assertNotEmpty($textfield_required_element);
  $details = $this
    ->assertSession()
    ->elementExists('css', '#edit-details-expanded-when-checkbox-trigger-checked');
  $textfield_in_details = $details
    ->findField('textfield_in_details');
  $this
    ->assertNotEmpty($textfield_in_details);
  $checkbox_checked_element = $page
    ->findField('checkbox_checked_when_checkbox_trigger_checked');
  $this
    ->assertNotEmpty($checkbox_checked_element);
  $checkbox_unchecked_element = $page
    ->findField('checkbox_unchecked_when_checkbox_trigger_checked');
  $this
    ->assertNotEmpty($checkbox_unchecked_element);
  $checkbox_visible_element = $page
    ->findField('checkbox_visible_when_checkbox_trigger_checked');
  $this
    ->assertNotEmpty($checkbox_visible_element);
  $text_format_invisible_value = $page
    ->findField('text_format_invisible_when_checkbox_trigger_checked[value]');
  $this
    ->assertNotEmpty($text_format_invisible_value);
  $text_format_invisible_format = $page
    ->findField('text_format_invisible_when_checkbox_trigger_checked[format]');
  $this
    ->assertNotEmpty($text_format_invisible_format);

  // Verify initial state.
  $this
    ->assertTrue($textfield_invisible_element
    ->isVisible());
  $this
    ->assertFalse($details
    ->hasAttribute('open'));
  $this
    ->assertFalse($textfield_in_details
    ->isVisible());
  $this
    ->assertFalse($textfield_required_element
    ->hasAttribute('required'));
  $this
    ->assertFalse($checkbox_checked_element
    ->isChecked());
  $this
    ->assertTrue($checkbox_unchecked_element
    ->isChecked());
  $this
    ->assertFalse($checkbox_visible_element
    ->isVisible());
  $this
    ->assertTrue($text_format_invisible_value
    ->isVisible());
  $this
    ->assertTrue($text_format_invisible_format
    ->isVisible());

  // Change state: check the checkbox.
  $trigger
    ->check();

  // Verify triggered state.
  $this
    ->assertFalse($textfield_invisible_element
    ->isVisible());
  $this
    ->assertEquals('required', $textfield_required_element
    ->getAttribute('required'));
  $this
    ->assertTrue($details
    ->hasAttribute('open'));
  $this
    ->assertTrue($textfield_in_details
    ->isVisible());
  $this
    ->assertTrue($checkbox_checked_element
    ->isChecked());
  $this
    ->assertFalse($checkbox_unchecked_element
    ->isChecked());
  $this
    ->assertTrue($checkbox_visible_element
    ->isVisible());
  $this
    ->assertFalse($text_format_invisible_value
    ->isVisible());
  $this
    ->assertFalse($text_format_invisible_format
    ->isVisible());
}