AjaxInGroupTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  4. /**
  5. * Tests that form elements in groups work correctly with AJAX.
  6. *
  7. * @group Ajax
  8. */
  9. class AjaxInGroupTest extends WebDriverTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected static $modules = ['ajax_forms_test'];
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected $defaultTheme = 'stark';
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. $this->drupalLogin($this->drupalCreateUser(['access content']));
  24. }
  25. /**
  26. * Submits forms with select and checkbox elements via Ajax.
  27. */
  28. public function testSimpleAjaxFormValue() {
  29. $this->drupalGet('/ajax_forms_test_get_form');
  30. $assert_session = $this->assertSession();
  31. $assert_session->responseContains('Test group');
  32. $assert_session->responseContains('AJAX checkbox in a group');
  33. $session = $this->getSession();
  34. $checkbox_original = $session->getPage()->findField('checkbox_in_group');
  35. $this->assertNotNull($checkbox_original, 'The checkbox_in_group is on the page.');
  36. $original_id = $checkbox_original->getAttribute('id');
  37. // Triggers a AJAX request/response.
  38. $checkbox_original->check();
  39. // The response contains a new nested "test group" form element, similar
  40. // to the one already in the DOM except for a change in the form build id.
  41. $checkbox_new = $assert_session->waitForElement('xpath', "//input[@name='checkbox_in_group' and not(@id='$original_id')]");
  42. $this->assertNotNull($checkbox_new, 'DOM update: clicking the checkbox refreshed the checkbox_in_group structure');
  43. $assert_session->responseContains('Test group');
  44. $assert_session->responseContains('AJAX checkbox in a group');
  45. $assert_session->responseContains('AJAX checkbox in a nested group');
  46. $assert_session->responseContains('Another AJAX checkbox in a nested group');
  47. }
  48. }