Skip to content

Commit

Permalink
Merge pull request #1267 from NatLibFi/issue1260-reopened-vocabulary-…
Browse files Browse the repository at this point in the history
…search-using-0-as-the-query-argument-throws-an-error

Fix vocabulary search using "0" as the search string
  • Loading branch information
osma authored Mar 3, 2022
2 parents 3a1902b + e5b066d commit 2a61720
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion model/ConceptSearchParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getArrayClass()

public function getSearchTerm() : string
{
$term = $this->request->getQueryParamRaw('q') ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query');
$term = $this->request->getQueryParamRaw('q') !== null ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query');
if ((!isset($term) || strlen(trim($term)) === 0) && $this->rest)
$term = $this->request->getQueryParamRaw('label');
$term = trim($term); // surrounding whitespace is not considered significant
Expand Down
1 change: 0 additions & 1 deletion model/sparql/GenericSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,6 @@ protected function generateConceptSearchQueryInner($term, $lang, $searchLang, $p
}
$hitgroup
EOQ;

return $query;
}
/**
Expand Down
11 changes: 11 additions & 0 deletions tests/ConceptSearchParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public function testGetSearchTerm() {
$this->assertEquals('test', $params->getSearchTerm());
}

/**
* @covers ConceptSearchParameters::getSearchTerm
* Is created particularly for searches not using REST
*/
public function testSearchTermWithoutRest() {
$params = new ConceptSearchParameters($this->request, new GlobalConfig('/../tests/testconfig.ttl'), false);
$this->request->method('getQueryParamRaw')->will($this->returnValue('0'));
$this->assertEquals('0*', $params->getSearchTerm());
}

/**
* For https://github.com/NatLibFi/Skosmos/issues/1275, to verify
* that querying for `0` (zero) does not evaluate it as a boolean
Expand Down Expand Up @@ -250,4 +260,5 @@ public function testGetAdditionalFields() {
$params = new ConceptSearchParameters($this->request, new GlobalConfig('/../tests/testconfig.ttl'));
$this->assertEquals(array('broader', 'prefLabel'), $params->getAdditionalFields());
}

}
4 changes: 2 additions & 2 deletions tests/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function testSearchJsonLdWithAdditionalFields() {
/**
* Data provider for testSearchWithZero test.
*/
public function testSearchWithZeroData(): array {
public function provideSearchWithZeroData(): array {
return [
['0', true],
['1', true],
Expand All @@ -328,7 +328,7 @@ public function testSearchWithZeroData(): array {

/**
* @covers RestController::search
* @dataProvider testSearchWithZeroData
* @dataProvider provideSearchWithZeroData
* @param $query string the search query
* @param $isSuccess bool whether the request must succeed or fail
*/
Expand Down

0 comments on commit 2a61720

Please sign in to comment.