Fixed managing default search order

This commit is contained in:
Marcin Kurczewski 2014-10-03 19:47:55 +02:00
parent 9c1a028c81
commit 0049d59354
2 changed files with 27 additions and 9 deletions

View file

@ -9,7 +9,7 @@ abstract class AbstractSearchParser
public function createFilterFromInputReader(\Szurubooru\Helpers\InputReader $inputReader) public function createFilterFromInputReader(\Szurubooru\Helpers\InputReader $inputReader)
{ {
$filter = $this->createFilter(); $filter = $this->createFilter();
$filter->setOrder(array_merge($filter->getOrder(), $this->getOrder($inputReader->order))); $filter->setOrder($this->getOrder($inputReader->order) + $filter->getOrder());
if ($inputReader->page) if ($inputReader->page)
{ {
@ -24,7 +24,7 @@ abstract class AbstractSearchParser
if ($token instanceof \Szurubooru\SearchServices\Tokens\NamedSearchToken) if ($token instanceof \Szurubooru\SearchServices\Tokens\NamedSearchToken)
{ {
if ($token->getKey() === 'order') if ($token->getKey() === 'order')
$filter->setOrder(array_merge($filter->getOrder(), $this->getOrder($token->getValue()))); $filter->setOrder($this->getOrder($token->getValue()) + $filter->getOrder());
else else
$this->decorateFilterFromNamedToken($filter, $token); $this->decorateFilterFromNamedToken($filter, $token);
} }

View file

@ -21,7 +21,7 @@ class UserSearchParserTest extends AbstractTestCase
public function testDefaultOrder() public function testDefaultOrder()
{ {
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
$this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder()); $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_ASC], $filter->getOrder());
} }
public function testInvalidOrder() public function testInvalidOrder()
@ -42,24 +42,42 @@ class UserSearchParserTest extends AbstractTestCase
{ {
$this->inputReader->order = 'name,desc'; $this->inputReader->order = 'name,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
$this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); $this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder());
}
public function testTokenOverwriteDefaultOrder()
{
$this->inputReader->query = 'order:name,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
$this->assertOrderEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder());
} }
public function testTokenOrder() public function testTokenOrder()
{ {
$this->inputReader->query = 'order:name,desc'; $this->inputReader->query = 'order:registration_time,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
$this->assertEquals([UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], $filter->getOrder()); $this->assertOrderEquals([
UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC,
UserFilter::ORDER_NAME => UserFilter::ORDER_ASC],
$filter->getOrder());
} }
public function testParamAndTokenOrder() public function testParamAndTokenOrder()
{ {
$this->inputReader->order = 'registration_time,desc'; $this->inputReader->order = 'name,desc';
$this->inputReader->query = 'order:name,desc'; $this->inputReader->query = 'order:registration_time,desc';
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
$this->assertEquals([ $this->assertOrderEquals([
UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC, UserFilter::ORDER_REGISTRATION_TIME => UserFilter::ORDER_DESC,
UserFilter::ORDER_NAME => UserFilter::ORDER_DESC], UserFilter::ORDER_NAME => UserFilter::ORDER_DESC],
$filter->getOrder()); $filter->getOrder());
} }
private function assertOrderEquals($expected, $actual)
{
$this->assertEquals($expected, $actual);
//also test associative array's key order - something that PHPUnit doesn't seem to do
$this->assertEquals(array_values($expected), array_values($actual));
$this->assertEquals(array_keys($expected), array_keys($actual));
}
} }