See #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase)

Scope:

  • SearchAdvancedSearchFormTest
  • SearchBlockTest
  • SearchCommentTest
  • SearchConfigSettingsFormTest
  • SearchEmbedFormTest
  • SearchLanguageTest
  • SearchNodeUpdateAndDeletionTest
  • SearchNumberMatchingTest
  • SearchNumbersTest
  • SearchPageCacheTagsTest
  • SearchPageTextTest
  • SearchPreprocessLangcodeTest
  • SearchQueryAlterTest
  • SearchRankingTest

Out of Scope:

A way to port Simpletest AssertContentTrait::parse() was looked for, but this is not needed, since we can do better just using Mink for this.

SearchTestBase also needed to be updated because currently the only method on that would cause a fatal when used.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

GoZ created an issue. See original summary.

GoZ’s picture

Status: Active » Needs review
FileSize
26.44 KB

Many tests have been converted and are OK.

Some needs work, some other require postponed.

  • SearchAdvancedSearchFormTest
  • SearchBlockTest: postponed, waiting after #2830773: Add legacy for clickLinkPartialName() method for browser tests and a way to port Simpletest AssertContentTrait::parse().
  • SearchCommentTest: needs work
  • SearchConfigSettingsFormTest: needs to work and postponed, waiting a way to port Simpletest AssertContentTrait::parse()
  • SearchEmbedFormTest
  • SearchLanguageTest
  • SearchNodeUpdateAndDeletionTest
  • SearchNumberMatchingTest
  • SearchNumbersTest
  • SearchPageCacheTagsTest
  • SearchPageTextTest
  • SearchPreprocessLangcodeTest
  • SearchQueryAlterTest
  • SearchRankingTest
GoZ’s picture

comment instead of edit summary. Oops

GoZ’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: convert_web_tests_to-2863842-2.patch, failed testing.

GoZ’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
27.24 KB
3.33 KB

Here is some fix, but the rest should be waiting for postponed (see summary)

Status: Needs review » Needs work

The last submitted patch, 6: convert_web_tests_to-2863842-6.patch, failed testing.

GoZ’s picture

Status: Needs work » Postponed

Fail due to clickLinkPartialName() and parse() as expected. Postponed, waiting after #2830773: Add legacy for clickLinkPartialName() method for browser tests and port of Simpletest AssertContentTrait::parse()

GoZ’s picture

Status: Postponed » Needs review
FileSize
21.17 KB
3.06 KB

Remove two tests from this patch so it can be commited. 2 missing tests needs more work due to other issues. Let's deal with them in another issue.

  • SearchBlockTest: postponed, waiting after #2830773: Add legacy for clickLinkPartialName() method for browser tests and a way to port Simpletest AssertContentTrait::parse().
  • SearchConfigSettingsFormTest: postponed, waiting a way to port Simpletest AssertContentTrait::parse(). Should fail using php-fpm see #2157061: Path Alias test fails on php-fpm

Status: Needs review » Needs work

The last submitted patch, 9: convert_web_tests_to-2863842-9.patch, failed testing.

GoZ’s picture

Status: Needs work » Needs review
FileSize
20.77 KB
265 bytes

What are you doing here TextFieldTest ? Please leave.

Sorry for this irrelevant test.

dawehner’s picture

Does someone mind to create a followup issue so we don't forget to the convert the remaining bits?

dawehner’s picture

Status: Needs review » Needs work
Issue tags: +Needs followup
GoZ’s picture

I create #2870595: Convert postponed WTB to BTB for search module SearchBlockTest and SearchConfigSettingsFormTest to deal with postponed SearchBlockTest and SearchConfigSettingsFormTest so we can close this issue and working on remaining conversions in another thread.

dawehner’s picture

+++ b/core/modules/search/tests/src/Functional/SearchAdvancedSearchFormTest.php
@@ -53,14 +53,14 @@ public function testNodeType() {
-    $this->drupalPostForm('search/node', $edit, t('Advanced search'));
+    $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
...
-    $this->drupalPostForm('search/node', array_merge($edit, ['type[page]' => 'page']), t('Advanced search'));
+    $this->drupalPostForm('search/node', array_merge($edit, ['type[page]' => 'page']), 'edit-submit--2');
...
-    $this->drupalPostForm('search/node', array_merge($edit, ['type[article]' => 'article']), t('Advanced search'));
+    $this->drupalPostForm('search/node', array_merge($edit, ['type[article]' => 'article']), 'edit-submit--2');

I'm wondering is this change really needed? Reading the function documentation explains that you should be able to provide a label.

GoZ’s picture

I can't use label because there is a conflict with another 'Advanced search' label in form.

michielnugter’s picture

Status: Needs review » Reviewed & tested by the community

Follow-ups created and the feedback from @dawehner was explained. I think this is good to got!

Thanks @GoZ!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: convert_web_tests_to-2863842-11.patch, failed testing.

michielnugter’s picture

Status: Needs work » Reviewed & tested by the community

Unrelated fail

dawehner’s picture

+1 for the RTBC

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Re #15 I think this makes the tests way less readable. However reading the docs for \Drupal\Tests\BrowserTestBase::submitForm() in BrowserTestBase I think we should just use that method instead and supply the $form_html_id param. Or maybe we should open an issue to add a param to drupalPostForm(). I think I prefer converting these calls to use \Drupal\Tests\BrowserTestBase::submitForm()

naveenvalecha’s picture

Title: Convert web tests to browser tests for search module » [PP-1] Convert web tests to browser tests for search module
Issue summary: View changes
Status: Needs work » Postponed

Created a follow-up for #21 #2887411: Add $form_html_id to drupalPostForm in BrowserTestBase and convert SearchTestBase::submitGetForm() to BrowserTestBase::drupalPostForm()

Shall we convert this particular test in follow-up and move with the tests which have minimal disruptions that way we'll have very fewer tests which are pending on postponed issues

//Naveen

naveenvalecha’s picture

larowlan’s picture

We can use ::findButton and then ::press instead of ::submitForm if there are conflicts

$page = $this->getSession()->getPage();
$button = $page->findButton('edit-submit-2');
$page->fillField('some field', 'some value');
$button->press();

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Lendude’s picture

Title: [PP-1] Convert web tests to browser tests for search module » Convert web tests to browser tests for search module
Issue summary: View changes
Status: Postponed » Needs review
FileSize
6.6 KB
23.94 KB

Unpostponed everything and converted everything.

We will not need parse() because we can do much better with the steps provided by @larowlan in #24. The existing \Drupal\Tests\search\Functional\SearchTestBase::submitGetForm method in HEAD is broken and will fatal when used. This means it is not used anywhere right now so I have little problem rewriting it to suit our needs, so I've done so.

Also the follow up in #2887411: Add $form_html_id to drupalPostForm in BrowserTestBase and convert SearchTestBase::submitGetForm() to BrowserTestBase::drupalPostForm() won't help here to address #15 because the other label is also inside that form, so the collision will still occur even when restricting to the form. So using the #id makes sense even if it reduces readability (and having two buttons with the same label might be an accessibility issue and might be better solved by making sure we have two buttons that can be distinguished between? So might be something that should be fixed outside this test.)

Status: Needs review » Needs work

The last submitted patch, 27: 2863842-27.patch, failed testing. View results

Lendude’s picture

Status: Needs work » Needs review
FileSize
1.62 KB
25.03 KB

fixed deprecation notices

borisson_’s picture

Assigned: GoZ » Unassigned
+++ b/core/modules/search/tests/src/Functional/SearchCommentTest.php
@@ -299,14 +301,12 @@ public function assertCommentAccess($assume_access, $message) {
-      $expected_node_result = $this->assertText(t('Your search yielded no results.'));
-      $expected_comment_result = $this->assertText(t('Your search yielded no results.'));
+      $this->assertSession()->pageTextContains(t('Your search yielded no results.'));
     }
-    $this->assertTrue($expected_node_result && $expected_comment_result, $message);

Looks like we're losing some coverage here.

I'm not 100% sure though, but I think that we should fix that. That's the only thing I could find though. Also unassigned this issue.

Anonymous’s picture

Status: Needs review » Needs work

#15 / #21 / #27: Yep, as @Lendude said, #id does not help here:

<form>
  <details>
    <summary role="button">Advanced search</summary>
    ...
    <input type="submit" value="Advanced search">
  </details>
</form>

But findButton() uses very wide xpath, include any html element with @role="button". As result a collision between summary and input.

We can introduce custom method to get submit button by preferred filter, or manually set edit fields + strict xpath, or even change label value in the setUp(). But while it is really a rare case, when form have few elements with button role + same text. So, +1 to just less readable 'edit-submit--2'.


/**
   * Simulates submission of a form using GET instead of POST.
   *
   * Forms that use the GET method cannot be submitted with
   * WebTestBase::drupalPostForm(), which explicitly uses POST to submit the
   * form. So this method finds the form, verifies that it has input fields and
   * a submit button matching the inputs to this method, and then calls
   * WebTestBase::drupalGet() to simulate the form submission to the 'action'
   * URL of the form (if set, or the current URL if not).
   *
   * See WebTestBase::drupalPostForm() for more detailed documentation of the
   * function parameters.
   */
  protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) {

It is true to WTB::drupalPostForm(), but BTB::drupalPostForm() uses action attribute of form. So after #2887411: Add $form_html_id to drupalPostForm in BrowserTestBase and convert SearchTestBase::submitGetForm() to BrowserTestBase::drupalPostForm() we can full replace submitGetForm on drupalPostForm with deprecated trigger.

In any case, after the convert, the doc becomes obsolete. Maybe something like:

  /**
   * Submission of a form.
   *
   * @todo: Use drupalPostForm() after https://www.drupal.org/project/drupal/issues/2887411

Here there can be a regression:

+++ b/core/modules/search/tests/src/Functional/SearchTestBase.php
@@ -57,36 +56,16 @@ protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) {
+    foreach ($edit as $selector => $value) {
+      $wrapper->fillField($selector, $value);
     }

If we use $form_html_id all good ($wrapper = form). In any case $wrapper = $page, and if page contains two field with same name, than fillField() set value only to the first. So, better will be:

+    $form = $this->assertSession()->elementExists('xpath', './ancestor::form', $button);
     foreach ($edit as $selector => $value) {
-      $wrapper->fillField($selector, $value);
+      $form->fillField($selector, $value);

#30: good catch, looks like unintentional deletion.
Anonymous’s picture

Status: Needs work » Needs review
FileSize
26.31 KB
2.63 KB

#30: Hah, we are mistaken, @Lendude++ as always!

   if ($assume_access) {
-      $expected_node_result = $this->assertText($this->node->label());
-      $expected_comment_result = $this->assertText($this->commentSubject);
+      $this->assertSession()->pageTextContains($this->node->label());
+      $this->assertSession()->pageTextContains($this->commentSubject);
     }
     else {
-      $expected_node_result = $this->assertText(t('Your search yielded no results.'));
-      $expected_comment_result = $this->assertText(t('Your search yielded no results.'));
+      $this->assertSession()->pageTextContains(t('Your search yielded no results.'));
     }
-    $this->assertTrue($expected_node_result && $expected_comment_result, $message);
   }

Node and comment used the same assert condition. So they can be combined. I just added a comment above this check to not forget about what we want to achieve here (in case someone in the future wants to optimize the check for search only nodes or comments).

#31: Done.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Awesome, looks great!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. It's a shame about the button thing. I've dug into the code and this happens because we have a summary element with the role of button and the same text. For me there's a question about whether or not our submit form should find such buttons. Anyhow the fix here works.
  2. \Drupal\search\Tests\SearchTestBase is already deprecated but we could do with a followup to trigger a silenced deprecation notice.
  3. diff --git a/core/modules/search/tests/src/Functional/SearchCommentTest.php b/core/modules/search/tests/src/Functional/SearchCommentTest.php
    index 714366185d..c964f88f2f 100644
    --- a/core/modules/search/tests/src/Functional/SearchCommentTest.php
    +++ b/core/modules/search/tests/src/Functional/SearchCommentTest.php
    @@ -305,7 +305,7 @@ public function assertCommentAccess($assume_access, $message) {
           $this->assertSession()->pageTextContains($this->commentSubject);
         }
         else {
    -      // Not expected node or comment results.
           $this->assertSession()->pageTextContains(t('Your search yielded no results.'));
         }
       }
    

    This comment with stating with the "Not" doesn't read correctly and it looks superfluous because the test searched for says enough. let's remove it.

  4. +++ b/core/modules/search/tests/src/Functional/SearchCommentTest.php
    @@ -299,14 +301,13 @@ public function assertCommentAccess($assume_access, $message) {
    -    $this->assertTrue($expected_node_result && $expected_comment_result, $message);
    

    This means the $message is not longer used. Which is problematic because it adds context for the test.

Lendude’s picture

#34.2 should be covered by #2886547: Add trigger_error to deprecated TestBase base classes/traits that have none, reference CR
#34.3 fixed
#34.4 true, how about using message as a fail text for the assertion.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @Lendude - looks great.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed ad90164 and pushed to 8.6.x. Thanks!

  • alexpott committed ad90164 on 8.6.x
    Issue #2863842 by GoZ, Lendude, vaplas, dawehner, alexpott, borisson_,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.