summaryrefslogtreecommitdiffstatshomepage
path: root/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxInGroupTest.php
blob: a8282b2b6cfe6f7decdcfc6d1e8851d57811fb37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

declare(strict_types=1);

namespace Drupal\FunctionalJavascriptTests\Ajax;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;

/**
 * Tests that form elements in groups work correctly with AJAX.
 *
 * @group Ajax
 */
class AjaxInGroupTest extends WebDriverTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['ajax_forms_test'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->drupalLogin($this->drupalCreateUser(['access content']));
  }

  /**
   * Submits forms with select and checkbox elements via Ajax.
   */
  public function testSimpleAjaxFormValue(): void {
    $this->drupalGet('/ajax_forms_test_get_form');

    $assert_session = $this->assertSession();
    $assert_session->responseContains('Test group');
    $assert_session->responseContains('AJAX checkbox in a group');

    $session = $this->getSession();
    $checkbox_original = $session->getPage()->findField('checkbox_in_group');
    $this->assertNotNull($checkbox_original, 'The checkbox_in_group is on the page.');
    $original_id = $checkbox_original->getAttribute('id');

    // Triggers an AJAX request/response.
    $checkbox_original->check();

    // The response contains a new nested "test group" form element, similar
    // to the one already in the DOM except for a change in the form build id.
    $checkbox_new = $assert_session->waitForElement('xpath', "//input[@name='checkbox_in_group' and not(@id='$original_id')]");
    $this->assertNotNull($checkbox_new, 'DOM update: clicking the checkbox refreshed the checkbox_in_group structure');

    $assert_session->responseContains('Test group');
    $assert_session->responseContains('AJAX checkbox in a group');
    $assert_session->responseContains('AJAX checkbox in a nested group');
    $assert_session->responseContains('Another AJAX checkbox in a nested group');
  }

}