function FormValuesTest::testSimpleAjaxFormValue

Same name in other branches
  1. 9 core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php \Drupal\FunctionalJavascriptTests\Ajax\FormValuesTest::testSimpleAjaxFormValue()
  2. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php \Drupal\FunctionalJavascriptTests\Ajax\FormValuesTest::testSimpleAjaxFormValue()
  3. 11.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php \Drupal\FunctionalJavascriptTests\Ajax\FormValuesTest::testSimpleAjaxFormValue()

Submits forms with select and checkbox elements via Ajax.

@dataProvider formModeProvider

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/FormValuesTest.php, line 39

Class

FormValuesTest
Tests that form values are properly delivered to AJAX callbacks.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testSimpleAjaxFormValue($form_mode) : void {
    $this->drupalGet('ajax_forms_test_get_form');
    $session = $this->getSession();
    $assertSession = $this->assertSession();
    // Run the test both in a dialog and not in a dialog.
    if ($form_mode === 'direct') {
        $this->drupalGet('ajax_forms_test_get_form');
    }
    else {
        $this->drupalGet('ajax_forms_test_dialog_form_link');
        $assertSession->waitForElementVisible('css', '[data-once="ajax"]');
        $this->clickLink("Open form in {$form_mode}");
        $this->assertNotEmpty($assertSession->waitForElementVisible('css', '.ui-dialog [data-drupal-selector="edit-select"]'));
    }
    // Verify form values of a select element.
    foreach ([
        'green',
        'blue',
        'red',
    ] as $item) {
        // Updating the field will trigger an AJAX request/response.
        $session->getPage()
            ->selectFieldOption('select', $item);
        // The AJAX command in the response will update the DOM.
        $select = $assertSession->waitForElement('css', "div#ajax_selected_color:contains('{$item}')");
        $this->assertNotNull($select, "DataCommand has updated the page with a value of {$item}.");
        $condition = "(typeof jQuery !== 'undefined' && jQuery('[data-drupal-selector=\"edit-select\"]').is(':focus'))";
        $this->assertJsCondition($condition, 5000);
    }
    // Verify form values of a checkbox element.
    $session->getPage()
        ->checkField('checkbox');
    $div0 = $this->assertSession()
        ->waitForElement('css', "div#ajax_checkbox_value:contains('checked')");
    $this->assertNotNull($div0, 'DataCommand updates the DOM as expected when a checkbox is selected');
    $session->getPage()
        ->uncheckField('checkbox');
    $div1 = $this->assertSession()
        ->waitForElement('css', "div#ajax_checkbox_value:contains('unchecked')");
    $this->assertNotNull($div1, 'DataCommand updates the DOM as expected when a checkbox is de-selected');
}

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