From 5037842e587e20d0562edba41a8cff7ca38660e0 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Thu, 17 Feb 2022 20:25:16 +1300 Subject: [PATCH] Do not treat 0 as boolean value in the REST controller --- model/ConceptSearchParameters.php | 4 ++-- tests/ConceptSearchParametersTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/model/ConceptSearchParameters.php b/model/ConceptSearchParameters.php index 6975f39ad..2ffc16734 100644 --- a/model/ConceptSearchParameters.php +++ b/model/ConceptSearchParameters.php @@ -65,10 +65,10 @@ public function getArrayClass() return null; } - public function getSearchTerm() + public function getSearchTerm() : string { $term = $this->request->getQueryParamRaw('q') ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query'); - if (!$term && $this->rest) + if ((!isset($term) || strlen(trim($term)) === 0) && $this->rest) $term = $this->request->getQueryParamRaw('label'); $term = trim($term); // surrounding whitespace is not considered significant $term = Normalizer::normalize( $term, Normalizer::FORM_C ); //Normalize decomposed unicode characters #1184 diff --git a/tests/ConceptSearchParametersTest.php b/tests/ConceptSearchParametersTest.php index dab94397c..1b47cabc7 100644 --- a/tests/ConceptSearchParametersTest.php +++ b/tests/ConceptSearchParametersTest.php @@ -86,6 +86,26 @@ public function testGetSearchTerm() { $this->assertEquals('test', $params->getSearchTerm()); } + /** + * For https://github.com/NatLibFi/Skosmos/issues/1275, to verify + * that querying for `0` (zero) does not evaluate it as a boolean + * value, causing issues in the SKOSMOS search functionality. + * + * @covers ConceptSearchParameters::getSearchTerm + */ + public function testGetSearchTermZeroes() { + $params = new ConceptSearchParameters($this->request, new GlobalConfig('/../tests/testconfig.ttl'), true); + $this->assertEquals('', $params->getSearchTerm()); + $this->request->method('getQueryParamRaw')->will( + $this->returnValueMap([ + ['q', '0'], + ['query', '0'], + ['label', '10'] + ]) + ); + $this->assertEquals('0', $params->getSearchTerm()); + } + /** * @covers ConceptSearchParameters::getTypeLimit * @covers ConceptSearchParameters::getDefaultTypeLimit