3 namespace Drupal\system\Tests\Form;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Tests functionality of \Drupal\Core\Form\FormBuilderInterface::rebuildForm().
15 * @todo Add tests for other aspects of form rebuilding.
17 class RebuildTest extends WebTestBase {
24 public static $modules = ['node', 'form_test'];
29 * @var \Drupal\user\UserInterface
33 protected function setUp() {
36 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
38 $this->webUser = $this->drupalCreateUser(['access content']);
39 $this->drupalLogin($this->webUser);
43 * Tests preservation of values.
45 public function testRebuildPreservesValues() {
47 'checkbox_1_default_off' => TRUE,
48 'checkbox_1_default_on' => FALSE,
51 $this->drupalPostForm('form-test/form-rebuild-preserve-values', $edit, 'Add more');
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.');
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.');
65 * Tests that a form's action is retained after an Ajax submission.
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.
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',
77 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
80 'field_name' => $field_name,
81 'entity_type' => 'node',
84 entity_get_form_display('node', 'page', 'default')
85 ->setComponent($field_name, ['type' => 'text_textfield'])
88 // Log in a user who can create 'page' nodes.
89 $this->webUser = $this->drupalCreateUser(['create page content']);
90 $this->drupalLogin($this->webUser);
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.');
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.');
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.');
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);