Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / src / Tests / Ajax / MultiFormTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Ajax;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Tests that AJAX-enabled forms work when multiple instances of the same form
11  * are on a page.
12  *
13  * @group Ajax
14  */
15 class MultiFormTest extends AjaxTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['form_test'];
23
24   protected function setUp() {
25     parent::setUp();
26
27     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
28
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,
34       'type' => 'text',
35       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
36     ])->save();
37     FieldConfig::create([
38       'field_name' => $field_name,
39       'entity_type' => 'node',
40       'bundle' => 'page',
41     ])->save();
42     entity_get_form_display('node', 'page', 'default')
43       ->setComponent($field_name, ['type' => 'text_textfield'])
44       ->save();
45
46     // Log in a user who can create 'page' nodes.
47     $this->drupalLogin($this->drupalCreateUser(['create page content']));
48   }
49
50   /**
51    * Tests that pages with the 'node_page_form' included twice work correctly.
52    */
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
56     // desired elements.
57     $field_name = 'field_ajax_test';
58
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"]';
65
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
68     // each form.
69     $this->drupalGet('form-test/two-instances-of-same-form');
70
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.');
76     }
77
78     $this->assertNoDuplicateIds(t('Initial page contains unique IDs'), 'Other');
79
80     // Submit the "add more" button of each form twice. After each corresponding
81     // page update, ensure the same as above.
82
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);
90
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');
94       }
95     }
96   }
97
98 }