Skip to content

Commit

Permalink
Merge branch 'release-10.1' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Nov 21, 2024
2 parents 6eb50e0 + b6cf4d7 commit 8efc209
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 192 deletions.
95 changes: 86 additions & 9 deletions module/VuFind/src/VuFindTest/Feature/SearchFacetFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Behat\Mink\Element\Element;
use Behat\Mink\Element\NodeElement;

use function count;

/**
* Trait for working with faceting and filtering of search results.
*
Expand All @@ -46,18 +48,19 @@
trait SearchFacetFilterTrait
{
/**
* CSS selector for finding the active filter values
* CSS selector for finding the active filter nodes
*
* @var string
*/
protected $activeFilterSelector = '.active-filters.hidden-xs .filters .filter-value';
protected $activeFilterNodesSelector
= '.active-filters.hidden-xs .filters > a, .active-filters.hidden-xs .filters > div';

/**
* CSS selector for finding the active filter labels
* CSS selector for finding the active filter values
*
* @var string
*/
protected $activeFilterLabelSelector = '.active-filters.hidden-xs .filters .filters-title';
protected $activeFilterSelector = '.active-filters.hidden-xs .filters .filter-value';

/**
* CSS selector for finding the active filter list
Expand Down Expand Up @@ -105,16 +108,42 @@ trait SearchFacetFilterTrait
* Check that a filter is applied
*
* @param Element $page Page
* @param int $index Filter index (0-based)
* @param string $expectedType Filter type or empty string for checkbox filter
* @param string $expectedFilter Filter description
*
* @return void
*/
protected function assertAppliedFilter(Element $page, string $expectedFilter): void
protected function assertAppliedFilter(
Element $page,
int $index,
string $expectedType,
string $expectedFilter
): void {
$appliedFilter = $this->findCss($page, $this->activeFilterNodesSelector, null, $index);
if ($expectedType) {
$this->assertEquals($expectedType . ':', $this->findCssAndGetText($appliedFilter, '.filters-title'));
} else {
$this->unFindCss($appliedFilter, '.filters-title');
}
$this->assertEquals("Remove Filter $expectedFilter", $this->findCssAndGetText($appliedFilter, '.filter-value'));
}

/**
* Check that a set of filters (and nothing more) is applied
*
* @param Element $page Page
* @param array $filters Filters (array of 'type:filter')
*
* @return void
*/
protected function assertAppliedFilters(Element $page, array $filters): void
{
$filter = $this->findCss($page, $this->activeFilterSelector);
$label = $this->findCss($page, $this->activeFilterLabelSelector);
$this->assertEquals('hierarchy:', $label->getText());
$this->assertEquals("Remove Filter $expectedFilter", $filter->getText());
$this->assertFilterCount($page, count($filters));
foreach ($filters as $index => $current) {
[$type, $filter] = explode(':', $current, 2);
$this->assertAppliedFilter($page, $index, $type, $filter);
}
}

/**
Expand Down Expand Up @@ -158,6 +187,12 @@ protected function assertNoFilters(Element $page): void
*/
protected function assertFilterCount(Element $page, int $expected): void
{
if (0 === $expected) {
$this->unFindCss($page, $this->activeFilterSelector);
return;
}
// Ensure that enough page has loaded:
$this->findCss($page, $this->activeFilterSelector);
$items = $page->findAll('css', $this->activeFilterSelector);
$this->assertCount($expected, $items);
}
Expand All @@ -183,4 +218,46 @@ protected function assertFullListFacetCount(
$excludes = $page->findAll('css', "#modal #facet-list-$list .exclude");
$this->assertCount($exclusionActive ? $expected : 0, $excludes);
}

/**
* Apply a range facet and load results
*
* @param Element $page Mink page object
* @param string $facet Facet name (e.g. 'publishDate')
* @param ?string $from "From" value
* @param ?string $to "To" value
* @param bool $multiselection Use multi-facet selection?
*
* @return void
*/
protected function applyRangeFacet(
Element $page,
string $facet,
?string $from,
?string $to,
bool $multiselection
): void {
$sidebar = $this->findCss($page, '.sidebar');
$container = $this->findCss($sidebar, "#side-panel-$facet");
if ($multiselection) {
$checkbox = $this->findCss($sidebar, '.js-user-selection-multi-filters');
if (!$checkbox->getValue()) {
$checkbox->click();
}
}

if (null !== $from) {
$this->findCssAndSetValue($page, '.date-from input', $from);
}
if (null !== $to) {
$this->findCssAndSetValue($page, '.date-to input', $to);
}

if ($multiselection) {
$this->clickCss($sidebar, '.js-apply-multi-facets-selection');
} else {
$this->clickCss($container, 'input[type="submit"]');
}
$this->waitForPageLoad($page);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,6 @@ public function testHierarchicalFacetsFilters(): void
$this->clickCss($page, '#limit_hierarchical_facet_str_mv option', null, 1);
$this->clickCss($page, '.btn.btn-primary');
$this->waitForPageLoad($page);
$this->assertAppliedFilter($page, 'level1a/level2a');
$this->assertAppliedFilter($page, 0, 'hierarchy', 'level1a/level2a');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ protected function clickHierarchicalFacet(Element $page): void
// Click second level facet:
$this->clickCss($page, $this->facetSecondLevelLinkSelector);
// Check the active filter:
$this->assertAppliedFilter($page, 'level1a/level2a');
$this->assertAppliedFilters($page, ['hierarchy:level1a/level2a']);
// Check that the applied facet is displayed properly:
$this->findCss($page, $this->facetSecondLevelActiveLinkSelector);
}
Expand Down Expand Up @@ -783,11 +783,7 @@ public function testHierarchicalFacetExclude(): void
);
$this->clickCss($page, $this->facetExpandSelector);
$this->clickCss($page, $this->facetSecondLevelExcludeLinkSelector);
$this->assertEquals('hierarchy:', $this->findCssAndGetText($page, '.filters .filters-title'));
$this->assertEquals(
'Remove Filter level1a/level2a',
$this->findCssAndGetText($page, $this->activeFilterSelector)
);
$this->assertAppliedFilters($page, ['hierarchy:level1a/level2a']);
$this->assertEquals(
'Showing 1 - 7 results of 7',
$extractCount($this->findCssAndGetText($page, '.search-stats'))
Expand Down Expand Up @@ -950,48 +946,6 @@ public function testCollapseStatePersistence(): void
$this->clickCss($page, '#side-panel-building .collapsed'); // on
}

/**
* Assert that the filter used by these tests is still applied.
*
* @param Element $page Mink page object
*
* @return void
*/
protected function assertFilterIsStillThere(Element $page): void
{
$this->assertEquals(
'Remove Filter weird_ids.mrc',
$this->findCssAndGetText($page, $this->activeFilterSelector)
);
}

/**
* Assert that the "reset filters" button is present.
*
* @param \Behat\Mink\Element\Element $page Mink page object
*
* @return void
*/
protected function assertResetFiltersButton($page)
{
$reset = $page->findAll('css', '.reset-filters-btn');
// The toggle bar has its own reset button, so we should have 2:
$this->assertCount(2, $reset);
}

/**
* Assert that the "reset filters" button is not present.
*
* @param Element $page Mink page object
*
* @return void
*/
protected function assertNoResetFiltersButton(Element $page): void
{
$reset = $page->findAll('css', '.reset-filters-btn');
$this->assertCount(0, $reset);
}

/**
* Test retain current filters default behavior
*
Expand Down Expand Up @@ -1045,6 +999,7 @@ public function testFiltersOnRecord(): void
$this->assertFilterIsStillThere($page);
// Re-click the search button...
$this->clickCss($page, '#searchForm .btn.btn-primary');
$this->waitForPageLoad($page);
// Confirm that filter is STILL applied
$this->assertFilterIsStillThere($page);
}
Expand Down Expand Up @@ -1397,4 +1352,132 @@ public function testCheckboxFacets(bool $deferred, bool $counts): void
);
}
}

/**
* Data provider for testRangeFacets
*
* @return array
*/
public static function rangeFacetsProvider(): array
{
return [
[false],
[true],
];
}

/**
* Test range facets
*
* @param bool $multiselection Use multi-facet selection?
*
* @dataProvider rangeFacetsProvider
*
* @return void
*/
public function testRangeFacets(bool $multiselection): void
{
$this->changeConfigs(
[
'facets' => [
'Results_Settings' => [
'multiFacetsSelection' => $multiselection,
],
'CheckboxFacets' => [
'format:Book' => 'Books',
],
],
]
);

$page = $this->performSearch('building:weird_ids.mrc');
$sidebar = $this->findCss($page, '.sidebar');

// Filter by date range and checkbox filter:
$checkboxFilters = $this->findCss($sidebar, '.checkbox-filters');
if ($multiselection) {
$this->clickCss($sidebar, '.js-user-selection-multi-filters');
$this->clickCss($checkboxFilters, 'a.checkbox-filter');
} else {
$this->clickCss($checkboxFilters, 'a.checkbox-filter');
$this->waitForPageLoad($page);
}
$this->applyRangeFacet($page, 'publishDate', '2000', '', $multiselection);

// Verify that we have two filters:
$this->assertAppliedFilters($page, [':Books', 'Year of Publication:2000 - *']);

// Change date range filter and check results:
$this->applyRangeFacet($page, 'publishDate', null, '2001', $multiselection);
$this->assertAppliedFilters($page, [':Books', 'Year of Publication:2000 - 2001']);

// Change date range filter again and check results:
$this->applyRangeFacet($page, 'publishDate', '2001', '2007', $multiselection);
$this->assertAppliedFilters($page, [':Books', 'Year of Publication:2001 - 2007']);

// Remove dates in range filter and check results:
$this->applyRangeFacet($page, 'publishDate', '', '', $multiselection);
$this->assertAppliedFilters($page, [':Books']);

// Add date range filter again and check results:
$this->applyRangeFacet($page, 'publishDate', '2001', '2007', $multiselection);
$this->assertAppliedFilters($page, [':Books', 'Year of Publication:2001 - 2007']);

if ($multiselection) {
// Apply another facet and change date range at the same time:
$this->clickCss($sidebar, '.js-user-selection-multi-filters');
$this->clickCss($page, '#side-collapse-institution a[data-title="MyInstitution"]');
$this->applyRangeFacet($page, 'publishDate', '2001', '2010', $multiselection);
$this->assertAppliedFilters(
$page,
[':Books', 'Institution:MyInstitution', 'Year of Publication:2001 - 2010']
);

// Remove all filters and check results:
$this->clickCss($sidebar, '.js-user-selection-multi-filters');
$this->clickCss($checkboxFilters, 'a.checkbox-filter');
$this->clickCss($page, '#side-collapse-institution a[data-title="MyInstitution"]');
$this->applyRangeFacet($page, 'publishDate', '', '', true);
$this->assertNoFilters($page);
}
}

/**
* Assert that the filter used by these tests is still applied.
*
* @param Element $page Mink page object
*
* @return void
*/
protected function assertFilterIsStillThere(Element $page): void
{
$this->assertAppliedFilters($page, ['Library:weird_ids.mrc']);
}

/**
* Assert that the "reset filters" button is present.
*
* @param \Behat\Mink\Element\Element $page Mink page object
*
* @return void
*/
protected function assertResetFiltersButton($page)
{
$reset = $page->findAll('css', '.reset-filters-btn');
// The toggle bar has its own reset button, so we should have 2:
$this->assertCount(2, $reset);
}

/**
* Assert that the "reset filters" button is not present.
*
* @param Element $page Mink page object
*
* @return void
*/
protected function assertNoResetFiltersButton(Element $page): void
{
$reset = $page->findAll('css', '.reset-filters-btn');
$this->assertCount(0, $reset);
}
}
Loading

0 comments on commit 8efc209

Please sign in to comment.