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
Comment #2
mpp commentedComment #3
mpp commentedComment #4
mpp commentedComment #5
mpp commentedComment #6
mpp commentedComment #7
mpp commentedComment #8
mpp commentedAdded an example usage in the documentation header.
Comment #9
hideaway commented+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.
Comment #10
mpp commentedFixed the indentation of the example scenario.
Comment #11
drunken monkeyThanks 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/Contribfolder for such things. However, I guess onlysearch_api.behat.incwill 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.
Comment #12
mpp commentedThanks for the review drunken monkey!
I added the word "tag" here:
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.
Comment #13
mpp commentedTests are green. Setting back to RTBC based on the feedback from #9 & #11.
Comment #15
drunken monkeyGreat, thanks for the feedback and the small correction.
I also added
@codeand@endcodeinto the doc block, at the appropriate places, and then committed.Thanks again!
Comment #16
mpp commentedThank you!