diff --git a/tests/features/joinup_search/search.feature b/tests/features/joinup_search/search.feature index a12fee99f0..4ba2a48fac 100644 --- a/tests/features/joinup_search/search.feature +++ b/tests/features/joinup_search/search.feature @@ -648,3 +648,39 @@ Feature: Global search And I should see the following facet summary "Collection, News" Then I should remove the following facet summary "News" And the page should show only the tiles "Radio cooking collection" + + @terms @javascript + Scenario: Show first three search results as featured + Given collection: + | title | Mice in space | + | state | validated | + And document content: + | title | collection | topic | state | logo | body | + | Microgravity | Mice in space | HR, Statistics and Analysis, E-justice | validated | alan.jpg | Conventional studies investigating the effects of reduced gravity on muscle mass and function have used a ground control group that is not directly comparable to the space experimental group. | + And event content: + | title | collection | topic | state | logo | body | + | Stay at the ISS | Mice in space | EU and European Policies | validated | charles.jpg | Two groups of mice (six per group) were housed aboard the International Space Station for 35 days. One group was subjected to artificial gravity (1 g) and the other to microgravity. | + And news content: + | title | body | collection | topic | spatial coverage | state | logo | + | Muscle atrophy | Researchers from the University of Tsukuba | Mice in space | Finance in EU | Luxembourg | validated | blaise.jpg | + | El Cabo da Roca | The best in town | Mice in space | Statistics and Analysis | Luxembourg | validated | charles.jpg | + | Funny news 1 | Dummy body | Mice in space | HR | Luxembourg | validated | ada.png | + | Funny news 2 | Dummy body | Mice in space | E-inclusion | Luxembourg | validated | alan.jpg | + | Funny news 3 | Dummy body | Mice in space | E-inclusion | Luxembourg | validated | charles.jpg | + | Funny news 4 | Dummy body | Mice in space | HR | Luxembourg | validated | ada.png | + | Funny news 5 | Dummy body | Mice in space | HR | Luxembourg | validated | ada.png | + | Dummy news 1 | Dummy body | Mice in space | Statistics and Analysis | Luxembourg | validated | charles.jpg | + | Dummy news 2 | Dummy body | Mice in space | E-inclusion | Luxembourg | validated | alan.jpg | + | Dummy news 3 | Dummy body | Mice in space | Statistics and Analysis | Luxembourg | validated | ada.png | + | Dummy news 4 | Dummy body | Mice in space | E-inclusion | Luxembourg | validated | charles.jpg | + + When I visit the search page + Then I should see the 3 tiles with image + And I should see the following links: + | Current page | + | Go to page 2 | + | Go to next page | + | Go to last page | + + Then I go to "/search?sort_by=relevance&page=1" + And I should see the 0 tiles with image diff --git a/tests/src/Context/JoinupSearchContext.php b/tests/src/Context/JoinupSearchContext.php index ba20b9113c..678e5e706b 100644 --- a/tests/src/Context/JoinupSearchContext.php +++ b/tests/src/Context/JoinupSearchContext.php @@ -504,6 +504,21 @@ public function iClickActionsInFacetsForm(string $name) { $element->click(); } + /** + * Check number of search results as featured. + * + * @param int $number + * Number of tiles with image. + * + * @Given I should see the :number tiles with image + */ + public function numberOfTilesWithImage(int $number) { + $session = $this->getSession()->getPage(); + $elements = $session->findAll('css', '.card > img'); + + Assert::assertSame(count($elements), $number); + } + /** * Opens the dropdown for the given facet element. * diff --git a/web/modules/custom/joinup_search/joinup_search.module b/web/modules/custom/joinup_search/joinup_search.module index 60991a190e..8c8309a42a 100644 --- a/web/modules/custom/joinup_search/joinup_search.module +++ b/web/modules/custom/joinup_search/joinup_search.module @@ -14,6 +14,7 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Template\Attribute; +use Drupal\joinup_search\Plugin\views\row\JoinupSearchApiRow; use Drupal\search_api\IndexInterface; use Drupal\search_api\Query\ConditionGroupInterface; use Drupal\search_api\Query\QueryInterface; @@ -401,3 +402,10 @@ function joinup_search_theme_suggestions_joinup_search_global_search(array $vari return []; } + +/** + * Implements hook_views_plugins_row_alter(). + */ +function joinup_search_views_plugins_row_alter(array &$plugins): void { + $plugins['search_api']['class'] = JoinupSearchApiRow::class; +} diff --git a/web/modules/custom/joinup_search/src/Plugin/views/row/JoinupSearchApiRow.php b/web/modules/custom/joinup_search/src/Plugin/views/row/JoinupSearchApiRow.php new file mode 100644 index 0000000000..e52a96d79e --- /dev/null +++ b/web/modules/custom/joinup_search/src/Plugin/views/row/JoinupSearchApiRow.php @@ -0,0 +1,45 @@ +view->id() == 'search') { + $datasource_id = $row->search_api_datasource; + if ($row->index <= 2 && $this->view->getCurrentPage() <= 0) { + $view_mode = 'search_result_featured'; + try { + return $this->index->getDatasource($datasource_id)->viewItem($row->_object, $view_mode); + } + catch (SearchApiException $e) { + $this->logException($e); + return ''; + } + } + } + return $parent; + } + +}