Changed search order parsing to be more strict

This commit is contained in:
Marcin Kurczewski 2014-10-01 22:20:07 +02:00
parent 61e95ac708
commit 52d44284fb
2 changed files with 30 additions and 3 deletions

View file

@ -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)

View file

@ -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';