diff --git a/core/modules/search/tests/src/Functional/SearchBlockTest.php b/core/modules/search/tests/src/Functional/SearchBlockTest.php index 3c6b746017d6ceb401e3d634880497c57f13fc7b..49427b2ea5522beab784fb10ecd4aca5b0d523f5 100644 --- a/core/modules/search/tests/src/Functional/SearchBlockTest.php +++ b/core/modules/search/tests/src/Functional/SearchBlockTest.php @@ -47,14 +47,14 @@ public function testSearchFormBlock() { // Test a normal search via the block form, from the front page. $terms = ['keys' => 'test']; - $this->submitGetForm('', $terms, t('Search')); + $this->drupalPostForm('', $terms, t('Search')); $this->assertResponse(200); $this->assertText('Your search yielded no results'); // Test a search from the block on a 404 page. $this->drupalGet('foo'); $this->assertResponse(404); - $this->submitGetForm(NULL, $terms, t('Search')); + $this->drupalPostForm(NULL, $terms, t('Search')); $this->assertResponse(200); $this->assertText('Your search yielded no results'); @@ -62,7 +62,7 @@ public function testSearchFormBlock() { $visibility['request_path']['pages'] = 'search'; $block->setVisibilityConfig('request_path', $visibility['request_path']); - $this->submitGetForm('', $terms, t('Search')); + $this->drupalPostForm('', $terms, t('Search')); $this->assertResponse(200); $this->assertText('Your search yielded no results'); @@ -78,7 +78,7 @@ public function testSearchFormBlock() { // Test an empty search via the block form, from the front page. $terms = ['keys' => '']; - $this->submitGetForm('', $terms, t('Search')); + $this->drupalPostForm('', $terms, t('Search')); $this->assertResponse(200); $this->assertText('Please enter some keywords'); @@ -92,16 +92,16 @@ public function testSearchFormBlock() { // Test that after entering a too-short keyword in the form, you can then // search again with a longer keyword. First test using the block form. - $this->submitGetForm('node', ['keys' => $this->randomMachineName(1)], t('Search')); + $this->drupalPostForm('node', ['keys' => $this->randomMachineName(1)], t('Search')); $this->assertText('You must include at least one keyword to match in the content', 'Keyword message is displayed when searching for short word'); $this->assertNoText(t('Please enter some keywords'), 'With short word entered, no keywords message is not displayed'); - $this->submitGetForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), 'search-block-form'); + $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), [], 'search-block-form'); $this->assertNoText('You must include at least one keyword to match in the content', 'Keyword message is not displayed when searching for long word after short word search'); // Same test again, using the search page form for the second search this // time. - $this->submitGetForm('node', ['keys' => $this->randomMachineName(1)], t('Search')); - $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), [], [], 'search-form'); + $this->drupalPostForm('node', ['keys' => $this->randomMachineName(1)], t('Search')); + $this->drupalPostForm(NULL, ['keys' => $this->randomMachineName()], t('Search'), [], 'search-form'); $this->assertNoText('You must include at least one keyword to match in the content', 'Keyword message is not displayed when searching for long word after short word search'); } diff --git a/core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php b/core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php index f404f8f1335c5a52c432335008829e26f2ce1177..2692a37a6a1449b729982b17ced362f99e8f5152 100644 --- a/core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php +++ b/core/modules/search/tests/src/Functional/SearchConfigSettingsFormTest.php @@ -174,7 +174,7 @@ public function testSearchModuleDisabling() { // Run a search from the search block on the node page. Verify you get // to this plugin's search results page. $terms = ['keys' => $info['keys']]; - $this->submitGetForm('node', $terms, t('Search')); + $this->drupalPostForm('node', $terms, t('Search')); $current = $this->getURL(); $expected = \Drupal::url('search.view_' . $entity->id(), [], ['query' => ['keys' => $info['keys']], 'absolute' => TRUE]); $this->assertEqual($current, $expected, 'Block redirected to right search page'); diff --git a/core/modules/search/tests/src/Functional/SearchTestBase.php b/core/modules/search/tests/src/Functional/SearchTestBase.php index 570658de06ac379ec6ac29c4305dd9a21da088fc..e426a92a44f159327de75fb39898450c5c90ae4e 100644 --- a/core/modules/search/tests/src/Functional/SearchTestBase.php +++ b/core/modules/search/tests/src/Functional/SearchTestBase.php @@ -41,26 +41,14 @@ protected function setUp() { * @param string $form_html_id * (optional) HTML ID of the form, to disambiguate. * - * @throws \Behat\Mink\Exception\ElementNotFoundException + * @deprecated in Drupal 8.6.x, to be removed before Drupal 9.0.x. Use + * \Drupal\Tests\BrowserTestBase::drupalPostForm() instead. * - * @todo: Replace after https://www.drupal.org/project/drupal/issues/2887411 + * @see https://www.drupal.org/node/2979950 */ protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) { - if (isset($path)) { - $this->drupalGet($path); - } - - $page = $this->getSession()->getPage(); - $wrapper = $page; - if ($form_html_id) { - $wrapper = $page->find('css', '#' . $form_html_id); - } - $button = $wrapper->findButton($submit); - $form = $this->assertSession()->elementExists('xpath', './ancestor::form', $button); - foreach ($edit as $selector => $value) { - $form->fillField($selector, $value); - } - $button->press(); + @trigger_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated in Drupal 8.6.x, for removal before the Drupal 9.0.0 release. Use \Drupal\Tests\BrowserTestBase::drupalPostForm() instead. See https://www.drupal.org/node/2979950.', E_USER_DEPRECATED); + $this->drupalPostForm($path, $edit, $submit, [], $form_html_id); } } diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index 2e8562b3f158e61f82a7ce16f5d10c90dedf0b16..92a000201871db98ae96a441f4f4200c62969359 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -23,7 +23,7 @@ class BrowserTestBaseTest extends BrowserTestBase { * * @var array */ - public static $modules = ['test_page_test', 'form_test', 'system_test']; + public static $modules = ['test_page_test', 'form_test', 'system_test', 'node']; /** * Tests basic page test. @@ -111,6 +111,15 @@ public function testForm() { // Test drupalPostForm() with no-html response. $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit'))); $this->assertTrue(1000, $values['beer']); + + // Test drupalPostForm() with form by HTML id. + $this->drupalCreateContentType(['type' => 'page']); + $this->drupalLogin($this->drupalCreateUser(['create page content'])); + $this->drupalGet('form-test/two-instances-of-same-form'); + $this->getSession()->getPage()->fillField('edit-title-0-value', 'form1'); + $this->getSession()->getPage()->fillField('edit-title-0-value--2', 'form2'); + $this->drupalPostForm(NULL, [], 'Save', [], 'node-page-form--2'); + $this->assertSession()->pageTextContains('Page form2 has been created.'); } /** diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 290be1a5cf015dd0a3e7cf580542952705ec0d98..4aaf6c5417b17eb5138c5f92227924a0598049b4 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -924,7 +924,7 @@ protected function submitForm(array $edit, $submit, $form_html_id = NULL) { /** * Executes a form submission. * - * It will be done as usual POST request with Mink. + * It will be done as usual submit form with Mink. * * @param \Drupal\Core\Url|string $path * Location of the post form. Either a Drupal path or an absolute path or @@ -990,13 +990,20 @@ protected function submitForm(array $edit, $submit, $form_html_id = NULL) { * POST data. * @param array $options * Options to be forwarded to the url generator. + * @param string|null $form_html_id + * (optional) HTML ID of the form to be submitted. On some pages + * there are many identical forms, so just using the value of the submit + * button is not enough. For example: 'trigger-node-presave-assign-form'. + * Note that this is not the Drupal $form_id, but rather the HTML ID of the + * form, which is typically the same thing but with hyphens replacing the + * underscores. * * @return string * (deprecated) The response content after submit form. It is necessary for * backwards compatibility and will be removed before Drupal 9.0. You should * just use the webAssert object for your assertions. */ - protected function drupalPostForm($path, $edit, $submit, array $options = []) { + protected function drupalPostForm($path, $edit, $submit, array $options = [], $form_html_id = NULL) { if (is_object($submit)) { // Cast MarkupInterface objects to string. $submit = (string) $submit; @@ -1012,7 +1019,7 @@ protected function drupalPostForm($path, $edit, $submit, array $options = []) { $this->drupalGet($path, $options); } - $this->submitForm($edit, $submit); + $this->submitForm($edit, $submit, $form_html_id); return $this->getSession()->getPage()->getContent(); }