Skip to content

Commit

Permalink
Simplify code with support methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz committed Feb 15, 2024
1 parent 96ab3b4 commit b111fe7
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 101 deletions.
26 changes: 26 additions & 0 deletions module/VuFind/src/VuFindTest/Feature/AutocompleteTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,30 @@ public function getAndAssertFirstAutocompleteValue(Element $page, string $text):
);
return $this->findCss($page, '.autocomplete-results .ac-item');
}

/**
* For the provided search, assert the first autocomplete value and return the
* associated page element.
*
* @param string $search Search term(s)
* @param string $expected First expected Autocomplete suggestion
* @param ?string $type Search type (null for default)
* @param ?Element $page Existing page to use for searching (will load Search/Home if not provided)
*
* @return NodeElement
*/
protected function assertAutocompleteValueAndReturnItem(
string $search,
string $expected,
?string $type = null,
?Element $page = null,
): NodeElement {
$page ??= $this->getSearchHomePage();
if ($type) {
$this->findCssAndSetValue($page, '#searchForm_type', $type);
}
$this->findCssAndSetValue($page, '#searchForm_lookfor', $search);
$acItem = $this->getAndAssertFirstAutocompleteValue($page, $expected);
return $acItem;
}
}
14 changes: 14 additions & 0 deletions module/VuFind/src/VuFindTest/Integration/MinkTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ protected function getVuFindUrl($path = '')
return $base . $path;
}

/**
* Load the Search/Home page as a foundation for searching.
*
* @param ?Session $session Mink session (will be automatically established if not provided).
*
* @return Element
*/
protected function getSearchHomePage(?Session $session = null): Element
{
$session ??= $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
return $session->getPage();
}

/**
* Get query string for the current page
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@

namespace VuFindTest\Mink;

use Behat\Mink\Element\Element;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Session;

/**
* Mink test class for autocomplete functionality.
*
Expand All @@ -46,46 +42,6 @@ class AutocompleteTest extends \VuFindTest\Integration\MinkTestCase
{
use \VuFindTest\Feature\AutocompleteTrait;

/**
* Load the Search/Home page as a foundation for searching.
*
* @param ?Session $session Mink session (will be automatically established if not provided).
*
* @return Element
*/
protected function loadSearchHome(?Session $session = null): Element
{
$session ??= $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
return $session->getPage();
}

/**
* For the provided search, assert the first autocomplete value and return the
* associated page element.
*
* @param string $search Search term(s)
* @param string $expected First expected Autocomplete suggestion
* @param ?string $type Search type (null for default)
* @param ?Element $page Existing page to use for searching (will load Search/Home if not provided)
*
* @return NodeElement
*/
protected function assertAutocompleteValueAndReturnItem(
string $search,
string $expected,
?string $type = null,
?Element $page = null,
): NodeElement {
$page ??= $this->loadSearchHome();
if ($type) {
$this->findCssAndSetValue($page, '#searchForm_type', $type);
}
$this->findCssAndSetValue($page, '#searchForm_lookfor', $search);
$acItem = $this->getAndAssertFirstAutocompleteValue($page, $expected);
return $acItem;
}

/**
* Test that default autocomplete behavior is correct.
*
Expand Down Expand Up @@ -155,7 +111,7 @@ public function testMultipleAutocompletesInSingleSession(): void
{
// Load the page first so we'll use the same context across all testing:
$session = $this->getMinkSession();
$page = $this->loadSearchHome($session);
$page = $this->getSearchHomePage($session);

// First do a search in All Fields
$this->assertAutocompleteValueAndReturnItem('jsto', 'Al Gore', null, $page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class BasicTest extends \VuFindTest\Integration\MinkTestCase
*/
public function testHomePage(): void
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->assertTrue(false !== strstr($page->getContent(), 'VuFind'));
}

Expand All @@ -61,9 +59,7 @@ public function testHomePage(): void
public function testAjaxStatus(): void
{
// Search for a known record:
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->findCss($page, '#searchForm_lookfor')
->setValue('id:testsample1');
$this->clickCss($page, '.btn.btn-primary');
Expand All @@ -90,9 +86,7 @@ public function testAjaxStatus(): void
*/
public function testLanguage(): void
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
// Check footer help-link
$this->assertEquals(
'Search Tips',
Expand Down Expand Up @@ -129,9 +123,7 @@ public function testThemeSwitcher(): void
]
);

$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->waitForPageLoad($page);

// Default theme does not have an h1:
Expand All @@ -156,9 +148,7 @@ public function testThemeSwitcher(): void
*/
public function testLightboxJumps(): void
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
// Open Search tips lightbox
$this->clickCss($page, 'footer .help-link');
$this->waitForPageLoad($page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public static function setUpBeforeClass(): void
*/
protected function gotoSearch($query = 'Dewey')
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->findCssAndSetValue($page, '#searchForm_lookfor', $query);
$this->clickCss($page, '.btn.btn-primary');
$this->waitForPageLoad($page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class HomePageFacetsTest extends \VuFindTest\Integration\MinkTestCase
*/
public function testNormalFacets()
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->waitForPageLoad($page);
$this->assertEquals('A - General Works', $this->findCssAndGetText($page, '.home-facet.callnumber-first a'));
$this->clickCss($page, '.home-facet.callnumber-first a');
Expand Down Expand Up @@ -82,9 +80,7 @@ public function testHierarchicalFacets()
],
]
);
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->waitForPageLoad($page);
$this->assertEquals(
'level1a level1z',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ public function testLinkInSearchResults()
);

// Search for a known record:
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->findCss($page, '#searchForm_lookfor')
->setValue('id:testsample1');
$this->clickCss($page, '.btn.btn-primary');
Expand All @@ -177,12 +175,7 @@ public function testLinkInSearchResultsWithAutoloading()
);

// Search for a known record:
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$this->findCss($page, '#searchForm_lookfor')
->setValue('id:testsample1');
$this->clickCss($page, '.btn.btn-primary');
$page = $this->performSearch('id:testsample1');

// Verify the OpenURL
$this->assertOpenUrl($page, false /* do not click link */);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public static function setUpBeforeClass(): void
*/
protected function gotoSearch()
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->findCss($page, '#searchForm_lookfor')
->setValue('id:testdeweybrowse');
$this->clickCss($page, '.btn.btn-primary');
Expand Down Expand Up @@ -189,11 +187,7 @@ protected function localStorageDance()
$this->clickCss($page, '.result a.title');
$this->waitForPageLoad($page);
// Search for anything else
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$this->findCss($page, '#searchForm_lookfor')
->setValue('anything else');
$this->clickCss($page, '.btn.btn-primary');
$page = $this->performSearch('anything else');
$this->waitForPageLoad($page);
// Come back
$page = $this->gotoSearch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,8 @@ public function testTagSearchSort(
public function testTagAutocomplete(): void
{
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$this->findCss($page, '#searchForm_type')
->setValue('tag');
$this->findCss($page, '#searchForm_lookfor')
->setValue('fiv');
$acItem = $this->getAndAssertFirstAutocompleteValue($page, 'five');
$page = $this->getSearchHomePage($session);
$acItem = $this->assertAutocompleteValueAndReturnItem('fiv', 'five', 'tag', $page);
$acItem->click();
$this->waitForPageLoad($page);
$this->assertEquals(
Expand Down Expand Up @@ -570,11 +565,7 @@ public function testSMS(): void
public function testPrint(): void
{
// Go to a record view (manually search so we can access $session)
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$this->findCssAndSetValue($page, '#searchForm_lookfor', 'Dewey');
$this->findCss($page, '.btn.btn-primary')->click();
$page = $this->performSearch('Dewey');
$this->clickCss($page, '.result a.title');

// Click Print
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function testGridAjaxStatus()
);

// Search for a known record:
$session = $this->getMinkSession();
$session->visit($this->getVuFindUrl() . '/Search/Home');
$page = $session->getPage();
$page = $this->getSearchHomePage();
$this->findCss($page, '#searchForm_lookfor')
->setValue('id:testsample1');
$this->clickCss($page, '.btn.btn-primary');
Expand Down

0 comments on commit b111fe7

Please sign in to comment.