diff --git a/src/SearchServices/Parsers/AbstractSearchParser.php b/src/SearchServices/Parsers/AbstractSearchParser.php index 6d2708e7..f7b30b3b 100644 --- a/src/SearchServices/Parsers/AbstractSearchParser.php +++ b/src/SearchServices/Parsers/AbstractSearchParser.php @@ -96,9 +96,22 @@ abstract class AbstractSearchParser { $token = preg_split('/,|\s+/', $token); $orderToken = $token[0]; - $orderDir = (count($token) === 2 and $token[1] === 'asc') - ? \Szurubooru\SearchServices\Filters\IFilter::ORDER_ASC - : \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC; + + if (count($token) === 1) + { + $orderDir = \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC; + } + elseif (count($token) === 2) + { + if ($token[1] === 'desc') + $orderDir = \Szurubooru\SearchServices\Filters\IFilter::ORDER_DESC; + elseif ($token[1] === 'asc') + $orderDir = \Szurubooru\SearchServices\Filters\IFilter::ORDER_ASC; + else + throw new \Exception('Wrong search order direction'); + } + else + throw new \Exception('Wrong search order token'); $orderColumn = $this->getOrderColumn($orderToken); if ($orderColumn === null) diff --git a/tests/SearchServices/UserSearchParserTest.php b/tests/SearchServices/UserSearchParserTest.php index 950117ee..fa5c744b 100644 --- a/tests/SearchServices/UserSearchParserTest.php +++ b/tests/SearchServices/UserSearchParserTest.php @@ -24,6 +24,20 @@ class UserSearchParserTest extends AbstractTestCase $this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder()); } + public function testInvalidOrder() + { + $this->inputReader->order = 'invalid,desc'; + $this->setExpectedException(\Exception::class); + $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); + } + + public function testInvalidOrderDirection() + { + $this->inputReader->order = 'name,invalid'; + $this->setExpectedException(\Exception::class); + $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); + } + public function testParamOrder() { $this->inputReader->order = 'name,desc';