MultiFormTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Drupal\FunctionalJavascriptTests\Ajax;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. use Drupal\field\Entity\FieldConfig;
  6. use Drupal\field\Entity\FieldStorageConfig;
  7. use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
  8. /**
  9. * Tests that AJAX-enabled forms work when multiple instances of the same form
  10. * are on a page.
  11. *
  12. * @group Ajax
  13. */
  14. class MultiFormTest extends WebDriverTestBase {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = ['node', 'form_test'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
  25. // Create a multi-valued field for 'page' nodes to use for Ajax testing.
  26. $field_name = 'field_ajax_test';
  27. FieldStorageConfig::create([
  28. 'entity_type' => 'node',
  29. 'field_name' => $field_name,
  30. 'type' => 'text',
  31. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  32. ])->save();
  33. FieldConfig::create([
  34. 'field_name' => $field_name,
  35. 'entity_type' => 'node',
  36. 'bundle' => 'page',
  37. ])->save();
  38. entity_get_form_display('node', 'page', 'default')
  39. ->setComponent($field_name, ['type' => 'text_textfield'])
  40. ->save();
  41. // Log in a user who can create 'page' nodes.
  42. $this->drupalLogin($this->drupalCreateUser(['create page content']));
  43. }
  44. /**
  45. * Tests that pages with the 'node_page_form' included twice work correctly.
  46. */
  47. public function testMultiForm() {
  48. // HTML IDs for elements within the field are potentially modified with
  49. // each Ajax submission, but these variables are stable and help target the
  50. // desired elements.
  51. $field_name = 'field_ajax_test';
  52. $form_xpath = '//form[starts-with(@id, "node-page-form")]';
  53. $field_xpath = '//div[contains(@class, "field--name-field-ajax-test")]';
  54. $button_name = $field_name . '_add_more';
  55. $button_value = t('Add another item');
  56. $button_xpath_suffix = '//input[@name="' . $button_name . '"]';
  57. $field_items_xpath_suffix = '//input[@type="text"]';
  58. // Ensure the initial page contains both node forms and the correct number
  59. // of field items and "add more" button for the multi-valued field within
  60. // each form.
  61. $this->drupalGet('form-test/two-instances-of-same-form');
  62. // Wait for javascript on the page to prepare the form attributes.
  63. $this->assertSession()->assertWaitOnAjaxRequest();
  64. $session = $this->getSession();
  65. $page = $session->getPage();
  66. $fields = $page->findAll('xpath', $form_xpath . $field_xpath);
  67. $this->assertEqual(count($fields), 2);
  68. foreach ($fields as $field) {
  69. $this->assertCount(1, $field->findAll('xpath', '.' . $field_items_xpath_suffix), 'Found the correct number of field items on the initial page.');
  70. $this->assertFieldsByValue($field->find('xpath', '.' . $button_xpath_suffix), NULL, 'Found the "add more" button on the initial page.');
  71. }
  72. $this->assertNoDuplicateIds();
  73. // Submit the "add more" button of each form twice. After each corresponding
  74. // page update, ensure the same as above.
  75. for ($i = 0; $i < 2; $i++) {
  76. $forms = $page->find('xpath', $form_xpath);
  77. foreach ($forms as $offset => $form) {
  78. $button = $form->findButton($button_value);
  79. $this->assertNotNull($button, 'Add Another Item button exists');
  80. $button->press();
  81. // Wait for page update.
  82. $this->assertSession()->assertWaitOnAjaxRequest();
  83. // After AJAX request and response page will update.
  84. $page_updated = $session->getPage();
  85. $field = $page_updated->findAll('xpath', '.' . $field_xpath);
  86. $this->assertEqual(count($field[0]->find('xpath', '.' . $field_items_xpath_suffix)), $i + 2, 'Found the correct number of field items after an AJAX submission.');
  87. $this->assertFieldsByValue($field[0]->find('xpath', '.' . $button_xpath_suffix), NULL, 'Found the "add more" button after an AJAX submission.');
  88. $this->assertNoDuplicateIds();
  89. }
  90. }
  91. }
  92. /**
  93. * Asserts that each HTML ID is used for just a single element on the page.
  94. *
  95. * @param string $message
  96. * (optional) A message to display with the assertion.
  97. */
  98. protected function assertNoDuplicateIds($message = '') {
  99. $args = ['@url' => $this->getUrl()];
  100. if (!$elements = $this->xpath('//*[@id]')) {
  101. $this->fail(new FormattableMarkup('The page @url contains no HTML IDs.', $args));
  102. return;
  103. }
  104. $message = $message ?: new FormattableMarkup('The page @url does not contain duplicate HTML IDs', $args);
  105. $seen_ids = [];
  106. foreach ($elements as $element) {
  107. $id = $element->getAttribute('id');
  108. if (isset($seen_ids[$id])) {
  109. $this->fail($message);
  110. return;
  111. }
  112. $seen_ids[$id] = TRUE;
  113. }
  114. $this->assertTrue(TRUE, $message);
  115. }
  116. }