function JavascriptStatesTest::doMultipleSelectTriggerTests

Same name in other branches
  1. 11.x core/tests/Drupal/FunctionalJavascriptTests/Core/Form/JavascriptStatesTest.php \Drupal\FunctionalJavascriptTests\Core\Form\JavascriptStatesTest::doMultipleSelectTriggerTests()

Tests states of elements triggered by a multiple select element.

1 call to JavascriptStatesTest::doMultipleSelectTriggerTests()
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 466

Class

JavascriptStatesTest
Tests the state of elements based on another elements.

Namespace

Drupal\FunctionalJavascriptTests\Core\Form

Code

protected function doMultipleSelectTriggerTests() {
    $this->drupalGet('form-test/javascript-states-form');
    $page = $this->getSession()
        ->getPage();
    // Find trigger and target elements.
    $trigger = $page->findField('multiple_select_trigger[]');
    $this->assertNotEmpty($trigger);
    $item_visible_value2 = $this->assertSession()
        ->elementExists('css', '#edit-item-visible-when-multiple-select-trigger-has-value2');
    $item_visible_no_value = $this->assertSession()
        ->elementExists('css', '#edit-item-visible-when-multiple-select-trigger-has-no-value');
    $textfield_visible_value3 = $page->findField('textfield_visible_when_multiple_select_trigger_has_value3');
    $this->assertNotEmpty($textfield_visible_value3);
    $textfield_visible_value2_or_value3 = $page->findField('textfield_visible_when_multiple_select_trigger_has_value2_or_value3');
    $this->assertNotEmpty($textfield_visible_value2_or_value3);
    $textfield_visible_value2_and_value3 = $page->findField('textfield_visible_when_multiple_select_trigger_has_value2_and_value3');
    $this->assertNotEmpty($textfield_visible_value2_and_value3);
    // Verify initial state.
    $this->assertFalse($item_visible_value2->isVisible());
    $this->assertTrue($item_visible_no_value->isVisible());
    $this->assertFalse($textfield_visible_value3->isVisible());
    $this->assertFalse($textfield_visible_value2_or_value3->isVisible());
    $this->assertFalse($textfield_visible_value2_and_value3->isVisible());
    // Change state: select the 'Value 2' option.
    $trigger->setValue('value2');
    $this->assertTrue($item_visible_value2->isVisible());
    $this->assertFalse($item_visible_no_value->isVisible());
    $this->assertFalse($textfield_visible_value3->isVisible());
    $this->assertTrue($textfield_visible_value2_or_value3->isVisible());
    $this->assertFalse($textfield_visible_value2_and_value3->isVisible());
    // Change state: select the 'Value 3' option.
    $trigger->setValue('value3');
    $this->assertFalse($item_visible_value2->isVisible());
    $this->assertFalse($item_visible_no_value->isVisible());
    $this->assertTrue($textfield_visible_value3->isVisible());
    $this->assertTrue($textfield_visible_value2_or_value3->isVisible());
    $this->assertFalse($textfield_visible_value2_and_value3->isVisible());
    // Change state: select 'Value2' and 'Value 3' options.
    $trigger->setValue([
        'value2',
        'value3',
    ]);
    $this->assertFalse($item_visible_value2->isVisible());
    $this->assertFalse($item_visible_no_value->isVisible());
    $this->assertFalse($textfield_visible_value3->isVisible());
    $this->assertFalse($textfield_visible_value2_or_value3->isVisible());
    $this->assertTrue($textfield_visible_value2_and_value3->isVisible());
    // Restore initial trigger state (clear the values).
    $trigger->setValue([]);
    // Make sure the initial element states are restored.
    $this->assertFalse($item_visible_value2->isVisible());
    $this->assertFalse($textfield_visible_value3->isVisible());
    $this->assertFalse($textfield_visible_value2_or_value3->isVisible());
    // @todo These last two look to be correct, but the assertion is failing.
    // @see https://www.drupal.org/project/drupal/issues/3367310
    // $this->assertTrue($item_visible_no_value->isVisible());
    // $this->assertFalse($textfield_visible_value2_and_value3->isVisible());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.