Skip to content
Snippets Groups Projects
Commit 6b7513d1 authored by Fran Garcia-Linares's avatar Fran Garcia-Linares
Browse files

Merge branch '3446458-autocomplete-does-not' into '2.x'

Exact search first, otherwise partial.

See merge request !34
parents e292c549 cb349251
No related branches found
No related tags found
No related merge requests found
Pipeline #173263 failed
...@@ -29,15 +29,21 @@ class AutocompleteController extends ControllerBase { ...@@ -29,15 +29,21 @@ class AutocompleteController extends ControllerBase {
* Autocomplete results. * Autocomplete results.
*/ */
public function searchAutocomplete(BranchInterface $branch, Request $request) { public function searchAutocomplete(BranchInterface $branch, Request $request) {
$results = [];
$input = $request->query->get('q'); $input = $request->query->get('q');
if (!$input) { if (!$input) {
return new JsonResponse([]); return new JsonResponse([]);
} }
$input = Xss::filter($input); $input = Xss::filter($input);
$ids = DocBlock::searchByTitle($input, $branch, 50); // Try exact search results first.
$ids = DocBlock::searchByTitle($input, $branch, 50, TRUE);
// And if none are found then try partial search.
if (empty($ids)) {
$ids = DocBlock::searchByTitle($input, $branch, 50);
}
$docBlocks = $ids ? DocBlock::loadMultiple($ids) : []; $docBlocks = $ids ? DocBlock::loadMultiple($ids) : [];
$results = [];
foreach ($docBlocks as $docBlock) { foreach ($docBlocks as $docBlock) {
$title = Html::escape($docBlock->getTitle()); $title = Html::escape($docBlock->getTitle());
if (!isset($results[$title])) { if (!isset($results[$title])) {
......
...@@ -256,13 +256,13 @@ class SearchController extends ControllerBase { ...@@ -256,13 +256,13 @@ class SearchController extends ControllerBase {
$branch = $this->getBranch($branch, $project, TRUE); $branch = $this->getBranch($branch, $project, TRUE);
$term = Xss::filter($term); $term = Xss::filter($term);
// Try exact match and redirect if found. // Try exact match and redirect if only one result.
$matches = DocBlock::searchByTitle($term, $branch, 10, TRUE); $matches = DocBlock::searchByTitle($term, $branch, 10, TRUE);
if ($matches) { if ($matches) {
// Check for case sensitivity. // Check for case sensitivity.
$matches = DocBlock::loadMultiple($matches); $matches = DocBlock::loadMultiple($matches);
foreach ($matches as $match) { if (count($matches) === 1) {
if ($match->getObjectName() == $term) { foreach ($matches as $match) {
$url = Formatter::objectUrl($match); $url = Formatter::objectUrl($match);
if ($url) { if ($url) {
return new RedirectResponse($url->toString()); return new RedirectResponse($url->toString());
......
...@@ -168,15 +168,20 @@ class SpecialIssuesTest extends WebPagesBase { ...@@ -168,15 +168,20 @@ class SpecialIssuesTest extends WebPagesBase {
* Tests searching case sensitivity. * Tests searching case sensitivity.
*/ */
protected function verifySearchCaseSensitivity() { protected function verifySearchCaseSensitivity() {
// Search for lower-case function name should get to the function page. // Search for lower-case function name should get to the search page.
$this->drupalGet('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search'); $this->drupalGet('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search');
$this->submitForm(['search' => 'sample_function'], 'Search'); $this->submitForm(['search' => 'sample_function'], 'Search');
$this->assertUrlContains('dup_names.php/function/sample_function', 'Got to function page with lower-case search'); $this->assertUrlContains('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search/sample_function', 'Got to search page as multiple results match');
// Search for upper-case function name should get to the constant page. // Search for upper-case function name should get to the search page.
$this->drupalGet('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search'); $this->drupalGet('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search');
$this->submitForm(['search' => 'SAMPLE_FUNCTION'], 'Search'); $this->submitForm(['search' => 'SAMPLE_FUNCTION'], 'Search');
$this->assertUrlContains('dup_names.php/constant/SAMPLE_FUNCTION', 'Got to constant page with upper-case search'); $this->assertUrlContains('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search/SAMPLE_FUNCTION', 'Got to search page as multiple results match');
// Search for lower-case function name should get to the function page.
$this->drupalGet('api/' . $this->branchInfo['project'] . '/' . $this->branchInfo['branch_name'] . '/search');
$this->submitForm(['search' => 'sample_class_function'], 'Search');
$this->assertUrlContains('sample.php/function/sample_class_function', 'Got to function page as it is unique');
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment