Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / system / src / Tests / Form / RebuildTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Form;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Url;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
10
11 /**
12  * Tests functionality of \Drupal\Core\Form\FormBuilderInterface::rebuildForm().
13  *
14  * @group Form
15  * @todo Add tests for other aspects of form rebuilding.
16  */
17 class RebuildTest extends WebTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['node', 'form_test'];
25
26   /**
27    * A user for testing.
28    *
29    * @var \Drupal\user\UserInterface
30    */
31   protected $webUser;
32
33   protected function setUp() {
34     parent::setUp();
35
36     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
37
38     $this->webUser = $this->drupalCreateUser(['access content']);
39     $this->drupalLogin($this->webUser);
40   }
41
42   /**
43    * Tests preservation of values.
44    */
45   public function testRebuildPreservesValues() {
46     $edit = [
47       'checkbox_1_default_off' => TRUE,
48       'checkbox_1_default_on' => FALSE,
49       'text_1' => 'foo',
50     ];
51     $this->drupalPostForm('form-test/form-rebuild-preserve-values', $edit, 'Add more');
52
53     // Verify that initial elements retained their submitted values.
54     $this->assertFieldChecked('edit-checkbox-1-default-off', 'A submitted checked checkbox retained its checked state during a rebuild.');
55     $this->assertNoFieldChecked('edit-checkbox-1-default-on', 'A submitted unchecked checkbox retained its unchecked state during a rebuild.');
56     $this->assertFieldById('edit-text-1', 'foo', 'A textfield retained its submitted value during a rebuild.');
57
58     // Verify that newly added elements were initialized with their default values.
59     $this->assertFieldChecked('edit-checkbox-2-default-on', 'A newly added checkbox was initialized with a default checked state.');
60     $this->assertNoFieldChecked('edit-checkbox-2-default-off', 'A newly added checkbox was initialized with a default unchecked state.');
61     $this->assertFieldById('edit-text-2', 'DEFAULT 2', 'A newly added textfield was initialized with its default value.');
62   }
63
64   /**
65    * Tests that a form's action is retained after an Ajax submission.
66    *
67    * The 'action' attribute of a form should not change after an Ajax submission
68    * followed by a non-Ajax submission, which triggers a validation error.
69    */
70   public function testPreserveFormActionAfterAJAX() {
71     // Create a multi-valued field for 'page' nodes to use for Ajax testing.
72     $field_name = 'field_ajax_test';
73     FieldStorageConfig::create([
74       'field_name' => $field_name,
75       'entity_type' => 'node',
76       'type' => 'text',
77       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
78     ])->save();
79     FieldConfig::create([
80       'field_name' => $field_name,
81       'entity_type' => 'node',
82       'bundle' => 'page',
83     ])->save();
84     entity_get_form_display('node', 'page', 'default')
85       ->setComponent($field_name, ['type' => 'text_textfield'])
86       ->save();
87
88     // Log in a user who can create 'page' nodes.
89     $this->webUser = $this->drupalCreateUser(['create page content']);
90     $this->drupalLogin($this->webUser);
91
92     // Get the form for adding a 'page' node. Submit an "add another item" Ajax
93     // submission and verify it worked by ensuring the updated page has two text
94     // field items in the field for which we just added an item.
95     $this->drupalGet('node/add/page');
96     $this->drupalPostAjaxForm(NULL, [], ['field_ajax_test_add_more' => t('Add another item')], NULL, [], [], 'node-page-form');
97     $this->assert(count($this->xpath('//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.');
98
99     // Submit the form with the non-Ajax "Save" button, leaving the title field
100     // blank to trigger a validation error, and ensure that a validation error
101     // occurred, because this test is for testing what happens when a form is
102     // re-rendered without being re-built, which is what happens when there's
103     // a validation error.
104     $this->drupalPostForm(NULL, [], t('Save'));
105     $this->assertText('Title field is required.', 'Non-AJAX submission correctly triggered a validation error.');
106
107     // Ensure that the form contains two items in the multi-valued field, so we
108     // know we're testing a form that was correctly retrieved from cache.
109     $this->assert(count($this->xpath('//form[contains(@id, "node-page-form")]//div[contains(@class, "js-form-item-field-ajax-test")]//input[@type="text"]')) == 2, 'Form retained its state from cache.');
110
111     // Ensure that the form's action is correct.
112     $forms = $this->xpath('//form[contains(@class, "node-page-form")]');
113     $this->assertEqual(1, count($forms));
114     // Strip query params off the action before asserting.
115     $url = parse_url($forms[0]['action'])['path'];
116     $this->assertEqual(Url::fromRoute('node.add', ['node_type' => 'page'])->toString(), $url);
117   }
118
119 }