Problem/Motivation

Since Search API v1.7 indexing content is postponed to the end of the page request. This breaks indexing content when testing in Behat.

Proposed resolution

Adding a Behat subcontext to fix this.

It allows for the `@search_api` tag to be added to Behat scenarios that require content to be indexed immediately.

Release notes snippet

Example Behat Search API test:

  @search_api
  Scenario: Article overview should only show published articles
    Given I am not logged in
    And "article" content:
      | title                  | moderation_state |
      | Test article published | published        |
      | Test article draft     | draft            |
    When I am on "/article-overview"
    Then I should see the text "Test article published"
    And I should not see the text "Test article draft"

Comments

mpp created an issue. See original summary.

mpp’s picture

Assigned: mpp » Unassigned
Status: Active » Needs review
StatusFileSize
new1.28 KB
mpp’s picture

StatusFileSize
new1.23 KB
mpp’s picture

StatusFileSize
new1.23 KB
mpp’s picture

Issue summary: View changes
mpp’s picture

Issue summary: View changes
mpp’s picture

Issue summary: View changes
mpp’s picture

StatusFileSize
new879 bytes
new1.65 KB

Added an example usage in the documentation header.

hideaway’s picture

+1 for this.

I run into the exact same issue where my content was not indexed during behat test. Because as OP explains, indexing happens at the end of a page request, but behat run is one single request, so the indexing won't happen. It happens when the test run is done, which is too late.

This is a good patch to resolve the issue.

mpp’s picture

StatusFileSize
new1.66 KB

Fixed the indentation of the example scenario.

drunken monkey’s picture

StatusFileSize
new1.5 KB
new1.6 KB

Thanks a lot for reporting this problem and providing a patch!

I have to admit I’ve never used Behat and have only limited idea of its behavior, and even less of its API. So I’ll have to take your word for it that this works as advertized. (Very welcome, therefore, that we already got a reviewer and tester here – thanks, hideaway!)

I had some stylistic changes to both the documentation’s wording and the code. (Using getContainer() works better with PhpStorm, unfortunately, for some reason.)
Please test/review to see whether you’re fine with them.

Apart from this, I would be very happy if this code could live somewhere else – e.g., there is already a src/Contrib folder for such things. However, I guess only search_api.behat.inc will be included automatically by default? Or is there some other place this can be put? (The whole setup, with a non-namespaced class in a file not named after that class is pretty unsatisfactory, from a coding standards point of view.)
However, I guess at this point we have so much stuff in the case directory that one more file won’t hurt either, so if this is the only place for it then I’ll also go with that.

mpp’s picture

StatusFileSize
new1.61 KB

Thanks for the review drunken monkey!

I added the word "tag" here:

+++ b/search_api.behat.inc
@@ -0,0 +1,51 @@
+   * Use the tag "@search_api" for tests that require this behavior. Example:

I'm running tests on Travis but I expect them to be green, keep you posted.

Unfortunately changing the location isn't possible (yet). Feel free to give your input on where to put subcontexts on https://github.com/jhedstrom/drupalextension/issues/536.

mpp’s picture

Status: Needs review » Reviewed & tested by the community

Tests are green. Setting back to RTBC based on the feedback from #9 & #11.

  • drunken monkey committed 6a55347 on 8.x-1.x authored by mpp
    Issue #3115214 by mpp, drunken monkey: Added Search API support for the...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Great, thanks for the feedback and the small correction.
I also added @code and @endcode into the doc block, at the appropriate places, and then committed.
Thanks again!

mpp’s picture

Thank you!

Status: Fixed » Closed (fixed)

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