From 1bdbc39f021c2505865cebe828310c549f586826 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 17 Jun 2016 23:48:51 +0200 Subject: [PATCH 1/3] Handle only upper or only lower reading filter When we select only one value in the reading time filter, we need to perform a query with only one value (greater than OR lower than). --- .../CoreBundle/Form/Type/EntryFilterType.php | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php index bc59c9a14..9d63a8a92 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; + } else if (null === $lower && null !== $upper) { + // only lower value is defined: query all entries with reading LOWER THAN this value + $expression = $filterQuery->getExpr()->lte($field, $max); + } else if (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', From 2c00dddf01e4c94e856d7c9e9e2e85b2de8dceec Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 18 Jun 2016 00:22:04 +0200 Subject: [PATCH 2/3] fixup! Handle only upper or only lower reading filter --- src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php index 9d63a8a92..3c597b5db 100644 --- a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php +++ b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php @@ -45,10 +45,10 @@ class EntryFilterType extends AbstractType if (null === $lower && null === $upper) { // no value? no filter return; - } else if (null === $lower && null !== $upper) { + } 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); - } else if (null !== $lower && null === $upper) { + } 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 { From 95859e54c512ec4a3ee1c28404981f5c4b9481df Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 23 Jun 2016 10:46:47 +0200 Subject: [PATCH 3/3] Add some tests about readingTime --- .../Controller/EntryControllerTest.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) 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');