X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fsystem%2Ftests%2Fsrc%2FFunctionalJavascript%2FForm%2FRebuildTest.php;fp=web%2Fcore%2Fmodules%2Fsystem%2Fsrc%2FTests%2FForm%2FRebuildTest.php;h=caf52094d574c9f12d402da7b40dc11dbbd58646;hb=1c1cb0980bfa6caf0c24cce671b6bb541dc87583;hp=2e138bdfb322ce200baf9a1a431321569fc04eeb;hpb=9424afc6c1f518c301bf87a23c047d1873435d05;p=yaffs-website diff --git a/web/core/modules/system/src/Tests/Form/RebuildTest.php b/web/core/modules/system/tests/src/FunctionalJavascript/Form/RebuildTest.php similarity index 53% rename from web/core/modules/system/src/Tests/Form/RebuildTest.php rename to web/core/modules/system/tests/src/FunctionalJavascript/Form/RebuildTest.php index 2e138bdfb..caf52094d 100644 --- a/web/core/modules/system/src/Tests/Form/RebuildTest.php +++ b/web/core/modules/system/tests/src/FunctionalJavascript/Form/RebuildTest.php @@ -1,12 +1,12 @@ drupalLogin($this->webUser); } - /** - * Tests preservation of values. - */ - public function testRebuildPreservesValues() { - $edit = [ - 'checkbox_1_default_off' => TRUE, - 'checkbox_1_default_on' => FALSE, - 'text_1' => 'foo', - ]; - $this->drupalPostForm('form-test/form-rebuild-preserve-values', $edit, 'Add more'); - - // Verify that initial elements retained their submitted values. - $this->assertFieldChecked('edit-checkbox-1-default-off', 'A submitted checked checkbox retained its checked state during a rebuild.'); - $this->assertNoFieldChecked('edit-checkbox-1-default-on', 'A submitted unchecked checkbox retained its unchecked state during a rebuild.'); - $this->assertFieldById('edit-text-1', 'foo', 'A textfield retained its submitted value during a rebuild.'); - - // Verify that newly added elements were initialized with their default values. - $this->assertFieldChecked('edit-checkbox-2-default-on', 'A newly added checkbox was initialized with a default checked state.'); - $this->assertNoFieldChecked('edit-checkbox-2-default-off', 'A newly added checkbox was initialized with a default unchecked state.'); - $this->assertFieldById('edit-text-2', 'DEFAULT 2', 'A newly added textfield was initialized with its default value.'); - } - /** * Tests that a form's action is retained after an Ajax submission. * @@ -68,6 +47,7 @@ class RebuildTest extends WebTestBase { * followed by a non-Ajax submission, which triggers a validation error. */ public function testPreserveFormActionAfterAJAX() { + $page = $this->getSession()->getPage(); // Create a multi-valued field for 'page' nodes to use for Ajax testing. $field_name = 'field_ajax_test'; FieldStorageConfig::create([ @@ -81,8 +61,26 @@ class RebuildTest extends WebTestBase { 'entity_type' => 'node', 'bundle' => 'page', ])->save(); + + // Also create a file field to test server side validation error. + $field_file_name = 'field_file_test'; + FieldStorageConfig::create([ + 'field_name' => $field_file_name, + 'entity_type' => 'node', + 'type' => 'file', + 'cardinality' => 1, + ])->save(); + FieldConfig::create([ + 'field_name' => $field_file_name, + 'entity_type' => 'node', + 'bundle' => 'page', + 'label' => 'Test file', + 'required' => TRUE, + ])->save(); + entity_get_form_display('node', 'page', 'default') ->setComponent($field_name, ['type' => 'text_textfield']) + ->setComponent($field_file_name, ['type' => 'file_generic']) ->save(); // Log in a user who can create 'page' nodes. @@ -93,27 +91,31 @@ class RebuildTest extends WebTestBase { // submission and verify it worked by ensuring the updated page has two text // field items in the field for which we just added an item. $this->drupalGet('node/add/page'); - $this->drupalPostAjaxForm(NULL, [], ['field_ajax_test_add_more' => t('Add another item')], NULL, [], [], 'node-page-form'); - $this->assert(count($this->xpath('//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.'); + $page->find('css', '[value="Add another item"]')->click(); + $this->assertSession()->assertWaitOnAjaxRequest(); + $this->assertTrue(count($this->xpath('//div[contains(@class, "field--name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.'); - // Submit the form with the non-Ajax "Save" button, leaving the title field + // Submit the form with the non-Ajax "Save" button, leaving the file field // blank to trigger a validation error, and ensure that a validation error // occurred, because this test is for testing what happens when a form is // re-rendered without being re-built, which is what happens when there's - // a validation error. - $this->drupalPostForm(NULL, [], t('Save')); - $this->assertText('Title field is required.', 'Non-AJAX submission correctly triggered a validation error.'); + // a server side validation error. + $edit = [ + 'title[0][value]' => $this->randomString(), + ]; + $this->drupalPostForm(NULL, $edit, 'Save'); + $this->assertSession()->pageTextContains('Test file field is required.', 'Non-AJAX submission correctly triggered a validation error.'); // Ensure that the form contains two items in the multi-valued field, so we // know we're testing a form that was correctly retrieved from cache. - $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.'); + $this->assertTrue(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.'); // Ensure that the form's action is correct. $forms = $this->xpath('//form[contains(@class, "node-page-form")]'); - $this->assertEqual(1, count($forms)); + $this->assertEquals(1, count($forms)); // Strip query params off the action before asserting. - $url = parse_url($forms[0]['action'])['path']; - $this->assertEqual(Url::fromRoute('node.add', ['node_type' => 'page'])->toString(), $url); + $url = parse_url($forms[0]->getAttribute('action'))['path']; + $this->assertEquals(Url::fromRoute('node.add', ['node_type' => 'page'])->toString(), $url); } }