protected function IntegrationTest::checkSearchAutocomplete in Search API Autocomplete 8
Tests autocompletion in the search form.
Parameters
bool $click_url_suggestion: (optional) TRUE to click the URL-based suggestion, FALSE to click one of the "normal" search keys suggestions.
1 call to IntegrationTest::checkSearchAutocomplete()
- IntegrationTest::testModule in tests/
src/ FunctionalJavascript/ IntegrationTest.php - Tests the complete functionality of the module via the UI.
File
- tests/
src/ FunctionalJavascript/ IntegrationTest.php, line 211
Class
- IntegrationTest
- Tests the functionality of the whole module from a user's perspective.
Namespace
Drupal\Tests\search_api_autocomplete\FunctionalJavascriptCode
protected function checkSearchAutocomplete($click_url_suggestion = FALSE) {
$assert_session = $this
->assertSession();
$this
->drupalGet('search-api-autocomplete-test');
$assert_session
->elementAttributeContains('css', 'input[data-drupal-selector="edit-keys"]', 'data-search-api-autocomplete-search', $this->searchId);
$elements = $this
->getAutocompleteSuggestions();
$suggestions = [];
$suggestion_elements = [];
foreach ($elements as $element) {
$label = $this
->getElementText($element, '.autocomplete-suggestion-label');
$user_input = $this
->getElementText($element, '.autocomplete-suggestion-user-input');
$suffix = $this
->getElementText($element, '.autocomplete-suggestion-suggestion-suffix');
$count = $this
->getElementText($element, '.autocomplete-suggestion-results-count');
$keys = $label . $user_input . $suffix;
$suggestions[] = [
'keys' => $keys,
'count' => $count,
];
$suggestion_elements[$keys] = $element;
}
$expected = [
[
'keys' => 'Tést-suggester-1',
'count' => 1,
],
[
'keys' => 'Tést-suggester-2',
'count' => 2,
],
[
'keys' => 'Tést-suggester-url',
'count' => NULL,
],
[
'keys' => 'Tést-backend-1',
'count' => 1,
],
[
'keys' => 'Tést-backend-2',
'count' => 2,
],
];
$this
->assertEquals($expected, $suggestions);
// Make sure the query looks as it should.
/** @var \Drupal\search_api\Query\QueryInterface $query */
list($query) = $this
->getMethodArguments('backend', 'getAutocompleteSuggestions');
$this
->assertFalse($query
->wasAborted());
$this
->assertEquals([
'body',
], $query
->getFulltextFields());
$this
->assertEquals([
'en',
], array_values($query
->getLanguages()));
if ($click_url_suggestion) {
// Click the URL suggestion and verify it correctly redirects the browser
// to that URL.
$suggestion_elements['Tést-suggester-url']
->click();
$this
->logPageChange();
$assert_session
->addressEquals("/user/{$this->adminUser->id()}");
return;
}
// Click one of the search key suggestions. The form should now auto-submit.
$keys = 'Tést-suggester-1';
$suggestion_elements[$keys]
->click();
$this
->logPageChange();
$keys = urlencode($keys);
// @todo Replace with $assert_session->addressMatches() (and prepend "^")
// once we depend on Drupal 9.1+.
$this
->assertRegExp("#/search-api-autocomplete-test\\?(?:.*&)?keys={$keys}#", $this
->getUrl());
// Check that autocomplete in the "Name" filter works, too, and that it sets
// the correct fields on the query.
$this
->getAutocompleteSuggestions('edit-name-value');
list($query) = $this
->getMethodArguments('suggester', 'getAutocompleteSuggestions');
$this
->assertEquals([
'name',
], $query
->getFulltextFields());
}