3 namespace Drupal\system\Tests\Ajax;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests that AJAX-enabled forms work when multiple instances of the same form
15 class MultiFormTest extends AjaxTestBase {
22 public static $modules = ['form_test'];
24 protected function setUp() {
27 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
29 // Create a multi-valued field for 'page' nodes to use for Ajax testing.
30 $field_name = 'field_ajax_test';
31 FieldStorageConfig::create([
32 'entity_type' => 'node',
33 'field_name' => $field_name,
35 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
38 'field_name' => $field_name,
39 'entity_type' => 'node',
42 entity_get_form_display('node', 'page', 'default')
43 ->setComponent($field_name, ['type' => 'text_textfield'])
46 // Log in a user who can create 'page' nodes.
47 $this->drupalLogin ($this->drupalCreateUser(['create page content']));
51 * Tests that pages with the 'node_page_form' included twice work correctly.
53 public function testMultiForm() {
54 // HTML IDs for elements within the field are potentially modified with
55 // each Ajax submission, but these variables are stable and help target the
57 $field_name = 'field_ajax_test';
59 $form_xpath = '//form[starts-with(@id, "node-page-form")]';
60 $field_xpath = '//div[contains(@class, "field--name-field-ajax-test")]';
61 $button_name = $field_name . '_add_more';
62 $button_value = t('Add another item');
63 $button_xpath_suffix = '//input[@name="' . $button_name . '"]';
64 $field_items_xpath_suffix = '//input[@type="text"]';
66 // Ensure the initial page contains both node forms and the correct number
67 // of field items and "add more" button for the multi-valued field within
69 $this->drupalGet('form-test/two-instances-of-same-form');
71 $fields = $this->xpath($form_xpath . $field_xpath);
72 $this->assertEqual(count($fields), 2);
73 foreach ($fields as $field) {
74 $this->assertEqual(count($field->xpath('.' . $field_items_xpath_suffix)), 1, 'Found the correct number of field items on the initial page.');
75 $this->assertFieldsByValue($field->xpath('.' . $button_xpath_suffix), NULL, 'Found the "add more" button on the initial page.');
78 $this->assertNoDuplicateIds(t('Initial page contains unique IDs'), 'Other');
80 // Submit the "add more" button of each form twice. After each corresponding
81 // page update, ensure the same as above.
83 for ($i = 0; $i < 2; $i++) {
84 $forms = $this->xpath($form_xpath);
85 foreach ($forms as $offset => $form) {
86 $form_html_id = (string) $form['id'];
87 $this->drupalPostAjaxForm(NULL, [], [$button_name => $button_value], NULL, [], [], $form_html_id);
88 $form = $this->xpath($form_xpath)[$offset];
89 $field = $form->xpath('.' . $field_xpath);
91 $this->assertEqual(count($field[0]->xpath('.' . $field_items_xpath_suffix)), $i + 2, 'Found the correct number of field items after an AJAX submission.');
92 $this->assertFieldsByValue($field[0]->xpath('.' . $button_xpath_suffix), NULL, 'Found the "add more" button after an AJAX submission.');
93 $this->assertNoDuplicateIds(t('Updated page contains unique IDs'), 'Other');