function JavascriptStatesTest::doCheckboxTriggerTests
Same name in other branches
- 9 core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php \Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest::doCheckboxTriggerTests()
- 10 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 81
Class
- JavascriptStatesTest
- Tests the state of elements based on another elements.
Namespace
Drupal\FunctionalJavascriptTests\Core\FormCode
protected function doCheckboxTriggerTests() : void {
$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);
$textfield_readonly_element = $page->findField('textfield_readonly_when_checkbox_trigger_checked');
$this->assertNotEmpty($textfield_readonly_element);
$textarea_readonly_element = $page->findField('textarea_readonly_when_checkbox_trigger_checked');
$this->assertNotEmpty($textarea_readonly_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);
$link = $page->findLink('Link states test');
$checkboxes_all_checked_element_value1 = $page->findField('checkboxes_all_checked_when_checkbox_trigger_checked[value1]');
$this->assertNotEmpty($checkboxes_all_checked_element_value1);
$checkboxes_all_checked_element_value2 = $page->findField('checkboxes_all_checked_when_checkbox_trigger_checked[value2]');
$this->assertNotEmpty($checkboxes_all_checked_element_value2);
$checkboxes_all_checked_element_value3 = $page->findField('checkboxes_all_checked_when_checkbox_trigger_checked[value3]');
$this->assertNotEmpty($checkboxes_all_checked_element_value3);
$checkboxes_some_checked_element_value1 = $page->findField('checkboxes_some_checked_when_checkbox_trigger_checked[value1]');
$this->assertNotEmpty($checkboxes_some_checked_element_value1);
$checkboxes_some_checked_element_value2 = $page->findField('checkboxes_some_checked_when_checkbox_trigger_checked[value2]');
$this->assertNotEmpty($checkboxes_some_checked_element_value2);
$checkboxes_some_checked_element_value3 = $page->findField('checkboxes_some_checked_when_checkbox_trigger_checked[value3]');
$this->assertNotEmpty($checkboxes_some_checked_element_value3);
$checkboxes_all_disabled_element_value1 = $page->findField('checkboxes_all_disabled_when_checkbox_trigger_checked[value1]');
$this->assertNotEmpty($checkboxes_all_disabled_element_value1);
$checkboxes_all_disabled_element_value2 = $page->findField('checkboxes_all_disabled_when_checkbox_trigger_checked[value2]');
$this->assertNotEmpty($checkboxes_all_disabled_element_value2);
$checkboxes_all_disabled_element_value3 = $page->findField('checkboxes_all_disabled_when_checkbox_trigger_checked[value3]');
$this->assertNotEmpty($checkboxes_all_disabled_element_value3);
$checkboxes_some_disabled_element_value1 = $page->findField('checkboxes_some_disabled_when_checkbox_trigger_checked[value1]');
$this->assertNotEmpty($checkboxes_some_disabled_element_value1);
$checkboxes_some_disabled_element_value2 = $page->findField('checkboxes_some_disabled_when_checkbox_trigger_checked[value2]');
$this->assertNotEmpty($checkboxes_some_disabled_element_value2);
$checkboxes_some_disabled_element_value3 = $page->findField('checkboxes_some_disabled_when_checkbox_trigger_checked[value3]');
$radios_checked_element = $page->findField('radios_checked_when_checkbox_trigger_checked');
$this->assertNotEmpty($radios_checked_element);
// We want to select the specific radio buttons, not the whole radios field itself.
$radios_all_disabled_value1 = $this->xpath('//input[@name=:name][@value=:value]', [
':name' => 'radios_all_disabled_when_checkbox_trigger_checked',
':value' => 'value1',
]);
$this->assertCount(1, $radios_all_disabled_value1);
// We want to access the radio button directly for the rest of the test, so
// take it out of the array we got back from xpath().
$radios_all_disabled_value1 = reset($radios_all_disabled_value1);
$radios_all_disabled_value2 = $this->xpath('//input[@name=:name][@value=:value]', [
':name' => 'radios_all_disabled_when_checkbox_trigger_checked',
':value' => 'value2',
]);
$this->assertCount(1, $radios_all_disabled_value2);
$radios_all_disabled_value2 = reset($radios_all_disabled_value2);
$radios_some_disabled_value1 = $this->xpath('//input[@name=:name][@value=:value]', [
':name' => 'radios_some_disabled_when_checkbox_trigger_checked',
':value' => 'value1',
]);
$this->assertCount(1, $radios_some_disabled_value1);
$radios_some_disabled_value1 = reset($radios_some_disabled_value1);
$radios_some_disabled_value2 = $this->xpath('//input[@name=:name][@value=:value]', [
':name' => 'radios_some_disabled_when_checkbox_trigger_checked',
':value' => 'value2',
]);
$this->assertCount(1, $radios_some_disabled_value2);
$radios_some_disabled_value2 = reset($radios_some_disabled_value2);
// 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($textfield_readonly_element->hasAttribute('readonly'));
$this->assertFalse($textarea_readonly_element->hasAttribute('readonly'));
$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());
$this->assertFalse($checkboxes_all_checked_element_value1->isChecked());
$this->assertFalse($checkboxes_all_checked_element_value2->isChecked());
$this->assertFalse($checkboxes_all_checked_element_value3->isChecked());
$this->assertFalse($checkboxes_some_checked_element_value1->isChecked());
$this->assertFalse($checkboxes_some_checked_element_value2->isChecked());
$this->assertFalse($checkboxes_some_checked_element_value3->isChecked());
$this->assertFalse($checkboxes_all_disabled_element_value1->hasAttribute('disabled'));
$this->assertFalse($checkboxes_all_disabled_element_value2->hasAttribute('disabled'));
$this->assertFalse($checkboxes_all_disabled_element_value3->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value1->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value2->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value3->hasAttribute('disabled'));
$this->assertFalse($radios_checked_element->isChecked());
$this->assertEquals(NULL, $radios_checked_element->getValue());
$this->assertFalse($radios_all_disabled_value1->hasAttribute('disabled'));
$this->assertFalse($radios_all_disabled_value2->hasAttribute('disabled'));
$this->assertFalse($radios_some_disabled_value1->hasAttribute('disabled'));
$this->assertFalse($radios_some_disabled_value2->hasAttribute('disabled'));
// Check if the link is visible.
$this->assertTrue($link->isVisible());
// Check enter password is visible.
$this->assertSession()
->pageTextContains('Enter password');
// 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($textfield_readonly_element->hasAttribute('readonly'));
$this->assertTrue($textarea_readonly_element->hasAttribute('readonly'));
$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());
// All 3 of the other set should be checked.
$this->assertTrue($checkboxes_all_checked_element_value1->isChecked());
$this->assertTrue($checkboxes_all_checked_element_value2->isChecked());
$this->assertTrue($checkboxes_all_checked_element_value3->isChecked());
// Value 1 and 3 should now be checked.
$this->assertTrue($checkboxes_some_checked_element_value1->isChecked());
$this->assertTrue($checkboxes_some_checked_element_value3->isChecked());
// Only value 2 should remain unchecked.
$this->assertFalse($checkboxes_some_checked_element_value2->isChecked());
// All 3 of these should be disabled.
$this->assertTrue($checkboxes_all_disabled_element_value1->hasAttribute('disabled'));
$this->assertTrue($checkboxes_all_disabled_element_value2->hasAttribute('disabled'));
$this->assertTrue($checkboxes_all_disabled_element_value3->hasAttribute('disabled'));
// Only values 1 and 3 should be disabled, 2 should still be enabled.
$this->assertTrue($checkboxes_some_disabled_element_value1->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value2->hasAttribute('disabled'));
$this->assertTrue($checkboxes_some_disabled_element_value3->hasAttribute('disabled'));
$this->assertEquals('value1', $radios_checked_element->getValue());
// Both of these should now be disabled.
$this->assertTrue($radios_all_disabled_value1->hasAttribute('disabled'));
$this->assertTrue($radios_all_disabled_value2->hasAttribute('disabled'));
// Only value1 should be disabled, value 2 should remain enabled.
$this->assertTrue($radios_some_disabled_value1->hasAttribute('disabled'));
$this->assertFalse($radios_some_disabled_value2->hasAttribute('disabled'));
// The link shouldn't be visible.
$this->assertFalse($link->isVisible());
// Check enter password is not visible.
$this->assertSession()
->pageTextNotContains('Enter password');
// Change state: uncheck the checkbox.
$trigger->uncheck();
// Verify triggered state, which should match the 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($textfield_readonly_element->hasAttribute('readonly'));
$this->assertFalse($textarea_readonly_element->hasAttribute('readonly'));
$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());
$this->assertFalse($checkboxes_all_checked_element_value1->isChecked());
$this->assertFalse($checkboxes_all_checked_element_value2->isChecked());
$this->assertFalse($checkboxes_all_checked_element_value3->isChecked());
$this->assertFalse($checkboxes_some_checked_element_value1->isChecked());
$this->assertFalse($checkboxes_some_checked_element_value2->isChecked());
$this->assertFalse($checkboxes_some_checked_element_value3->isChecked());
$this->assertFalse($checkboxes_all_disabled_element_value1->hasAttribute('disabled'));
$this->assertFalse($checkboxes_all_disabled_element_value2->hasAttribute('disabled'));
$this->assertFalse($checkboxes_all_disabled_element_value3->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value1->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value2->hasAttribute('disabled'));
$this->assertFalse($checkboxes_some_disabled_element_value3->hasAttribute('disabled'));
$this->assertFalse($radios_checked_element->isChecked());
$this->assertEquals(NULL, $radios_checked_element->getValue());
$this->assertFalse($radios_all_disabled_value1->hasAttribute('disabled'));
$this->assertFalse($radios_all_disabled_value2->hasAttribute('disabled'));
$this->assertFalse($radios_some_disabled_value1->hasAttribute('disabled'));
$this->assertFalse($radios_some_disabled_value2->hasAttribute('disabled'));
// Check if the link is turned back to visible state.
$this->assertTrue($link->isVisible());
// Check enter password is visible.
$this->assertSession()
->pageTextContains('Enter password');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.