Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/features/joinup_search/search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions tests/src/Context/JoinupSearchContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions web/modules/custom/joinup_search/joinup_search.module
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types = 1);

namespace Drupal\joinup_search\Plugin\views\row;

use Drupal\search_api\Plugin\views\row\SearchApiRow;
use Drupal\search_api\SearchApiException;

/**
* Provides a row plugin for displaying a result as a search result item.
*
* @ViewsRow(
* id = "joinup_search_api",
* title = @Translation("Rendered entity"),
* help = @Translation("Displays entity of the matching search API item"),
* )
*
* @see joinup_search_views_plugins_row_alter()
*/
class JoinupSearchApiRow extends SearchApiRow {

/**
* {@inheritdoc}
*/
public function render($row) {
$parent = parent::render($row);

if ($this->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;
}

}