diff --git a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php index bc59c9a14..3c597b5db 100644 --- a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php +++ b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php @@ -36,17 +36,26 @@ class EntryFilterType extends AbstractType $builder ->add('readingTime', NumberRangeFilterType::class, [ 'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { - $value = $values['value']; + $lower = $values['value']['left_number'][0]; + $upper = $values['value']['right_number'][0]; - if (null === $value['left_number'][0] || null === $value['right_number'][0]) { + $min = (int) ($lower * $this->user->getConfig()->getReadingSpeed()); + $max = (int) ($upper * $this->user->getConfig()->getReadingSpeed()); + + if (null === $lower && null === $upper) { + // no value? no filter return; + } elseif (null === $lower && null !== $upper) { + // only lower value is defined: query all entries with reading LOWER THAN this value + $expression = $filterQuery->getExpr()->lte($field, $max); + } elseif (null !== $lower && null === $upper) { + // only upper value is defined: query all entries with reading GREATER THAN this value + $expression = $filterQuery->getExpr()->gte($field, $min); + } else { + // both value are defined, perform a between + $expression = $filterQuery->getExpr()->between($field, $min, $max); } - $min = (int) ($value['left_number'][0] * $this->user->getConfig()->getReadingSpeed()); - $max = (int) ($value['right_number'][0] * $this->user->getConfig()->getReadingSpeed()); - - $expression = $filterQuery->getExpr()->between($field, $min, $max); - return $filterQuery->createCondition($expression); }, 'label' => 'entry.filters.reading_time.label', diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index bea771bca..5c739c78a 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -499,6 +499,42 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertCount(1, $crawler->filter('div[class=entry]')); } + public function testFilterOnReadingTimeOnlyUpper() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/unread/list'); + + $form = $crawler->filter('button[id=submit-filter]')->form(); + + $data = [ + 'entry_filter[readingTime][right_number]' => 22, + ]; + + $crawler = $client->submit($form, $data); + + $this->assertCount(2, $crawler->filter('div[class=entry]')); + } + + public function testFilterOnReadingTimeOnlyLower() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/unread/list'); + + $form = $crawler->filter('button[id=submit-filter]')->form(); + + $data = [ + 'entry_filter[readingTime][left_number]' => 22, + ]; + + $crawler = $client->submit($form, $data); + + $this->assertCount(4, $crawler->filter('div[class=entry]')); + } + public function testFilterOnUnreadStatus() { $this->logInAs('admin');