Skip to content

Commit

Permalink
[#299] Updated SearchApiTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 30, 2024
1 parent 5cd3f5c commit 448fbea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ A migration map of the step definitions available in v2 to v3.
| `Then I :can visit :path with HTTP credentials :user :pass` | `Given the basic authentication with the username :username and the password :password` |
| `When I visit :path then the final URL should be :alias` | Removed. Use `When I visit :path` and `Then the path should be :path` |
|   | |
| **[`SearchApiTrait`](src/SearchApiTrait.php) ([example](tests/behat/features/search.feature))** | |
| `When I index :type :title for search` | `When I add the :content_type content with the title :title to the search index` |
| `When I index :limit Search API items` | `When I run search indexing for :count item(s)` |
|   | |
| **[`WaitTrait`](src/WaitTrait.php) ([example](tests/behat/features/wait.feature))** | |
| `Then /^(?:\|I )wait (\d+) second(s?)$/` | `When I wait for :number second(s)` |
| `Given I wait :timeout seconds for AJAX to finish` | `When I wait for :number second(s) for AJAX to finish` |
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ For migration from v2 to v3, see [MIGRATION.md](MIGRATION.md).
| `Then /^the option "([^"]*)" from select "([^"]*)" is selected$/` | Assert that the specified option is selected in the specified select element. |
|   | |
| **[`SearchApiTrait`](src/SearchApiTrait.php) ([example](tests/behat/features/search.feature))** | |
| `When I index :type :title for search` | Index a node with all Search API indices. |
| `When I index :limit Search API items` | Index a specified number of items across all active Search API indices. |
| `When I add the :content_type content with the title :title to the search index` | Index a node with all Search API indices. |
| `When I run search indexing for :count item(s)` | Index a specified number of items across all active Search API indices. |
|   | |
| **[`TaxonomyTrait`](src/TaxonomyTrait.php) ([example](tests/behat/features/taxonomy.feature))** | |
| `Given vocabulary :vid with name :name exists` | Assert that the specified vocabulary exists. |
Expand Down
18 changes: 10 additions & 8 deletions src/SearchApiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ trait SearchApiTrait {
use ContentTrait;

/**
* Index a node with all Search API indices.
* Index a node of a specific content type with a specific title.
*
* @When I index :type :title for search
* @When I add the :content_type content with the title :title to the search index
*/
public function searchApiIndexContent(string $type, string $title): void {
$nids = $this->contentLoadMultiple($type, [
Expand All @@ -43,18 +43,20 @@ public function searchApiIndexContent(string $type, string $title): void {
}

/**
* Index a number of items across all active Search API indices.
* Run indexing for a specific number of items.
*
* @When I index :limit Search API items
* @When I index 1 Search API item
* @When I run search indexing for :count item(s)
*/
public function searchApiDoIndex(string|int $limit = 1): void {
public function searchApiDoIndex(string|int $limit): void {
$limit = intval($limit);

$index_storage = \Drupal::entityTypeManager()->getStorage('search_api_index');

/** @var \Drupal\search_api\IndexInterface[] $indexes */
$indexes = $index_storage->loadByProperties(['status' => TRUE]);
if (!$indexes) {
return;

if (empty($indexes)) {
throw new \RuntimeException('No active search indexes found');
}

foreach ($indexes as $index) {
Expand Down
12 changes: 6 additions & 6 deletions tests/behat/features/search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Feature: Ensure Search works.

@api
Scenario: Assert "I index :type :title for search" works as expected
Scenario: Assert "When I add the :content_type content with the title :title to the search index" works as expected
Given article content:
| title | moderation_state |
| [MYTEST] TESTPUBLISHEDARTICLE TESTUNIQUETEXT | published |
Expand All @@ -17,8 +17,8 @@ Feature: Ensure Search works.
And I should not see the text "[MYTEST] TESTDRAFTARTICLE TESTUNIQUETEXT"

# Index nodes and preform another search.
When I index article "[MYTEST] TESTPUBLISHEDARTICLE TESTUNIQUETEXT" for search
And I index article "[MYTEST] TESTDRAFTARTICLE TESTUNIQUETEXT" for search
When I add the "article" content with the title "[MYTEST] TESTPUBLISHEDARTICLE TESTUNIQUETEXT" to the search index
And I add the "article" content with the title "[MYTEST] TESTDRAFTARTICLE TESTUNIQUETEXT" to the search index

# Perform another search.
When I go to "/search"
Expand All @@ -30,14 +30,14 @@ Feature: Ensure Search works.
Then I should not see the text "[MYTEST] TESTDRAFTARTICLE TESTUNIQUETEXT"

@api @testmode
Scenario: Assert that testmode correctly works with search
Scenario: Assert "When I add the :content_type content with the title :title to the search index" works as expected with test mode
Given article content:
| title | moderation_state |
| TESTPUBLISHEDARTICLE 1 | published |
| [MYTEST] TESTPUBLISHEDARTICLE 2 | published |
And I am logged in as a user with the "administrator" role
And I index article "TESTPUBLISHEDARTICLE 1" for search
And I index article "[MYTEST] TESTPUBLISHEDARTICLE 2" for search
When I add the "article" content with the title "TESTPUBLISHEDARTICLE 1" to the search index
And I add the "article" content with the title "[MYTEST] TESTPUBLISHEDARTICLE 2" to the search index

When I go to "/search"
And I fill in "edit-search-api-fulltext" with "TESTPUBLISHEDARTICLE"
Expand Down

0 comments on commit 448fbea

Please sign in to comment.