function AjaxFormCacheTest::testBlockForms

Same name in other branches
  1. 8.9.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxFormCacheTest::testBlockForms()
  2. 10 core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxFormCacheTest::testBlockForms()
  3. 11.x core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php \Drupal\FunctionalJavascriptTests\Ajax\AjaxFormCacheTest::testBlockForms()

Tests AJAX forms in blocks.

File

core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php, line 49

Class

AjaxFormCacheTest
Tests the usage of form caching for AJAX forms.

Namespace

Drupal\FunctionalJavascriptTests\Ajax

Code

public function testBlockForms() {
    $this->container
        ->get('module_installer')
        ->install([
        'block',
        'search',
    ]);
    $this->rebuildContainer();
    $this->drupalLogin($this->rootUser);
    $this->drupalPlaceBlock('search_form_block', [
        'weight' => -5,
    ]);
    $this->drupalPlaceBlock('ajax_forms_test_block');
    $this->drupalGet('');
    $session = $this->getSession();
    // Select first option and trigger ajax update.
    $session->getPage()
        ->selectFieldOption('edit-test1', 'option1');
    // DOM update: The InsertCommand in the AJAX response changes the text
    // in the option element to 'Option1!!!'.
    $opt1_selector = $this->assertSession()
        ->waitForElement('css', "select[data-drupal-selector='edit-test1'] option:contains('Option 1!!!')");
    $this->assertNotEmpty($opt1_selector);
    $this->assertTrue($opt1_selector->isSelected());
    // Confirm option 3 exists.
    $page = $session->getPage();
    $opt3_selector = $page->find('xpath', '//select[@data-drupal-selector="edit-test1"]//option[@value="option3"]');
    $this->assertNotEmpty($opt3_selector);
    // Confirm success message appears after a submit.
    $page->findButton('edit-submit')
        ->click();
    $this->assertSession()
        ->waitForButton('edit-submit');
    $updated_page = $session->getPage();
    $updated_page->hasContent('Submission successful.');
}

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