Merge pull request #2157 from wallabag/lower-upper-readingtime

Handle only upper or only lower reading filter
This commit is contained in:
Jeremy Benoist 2016-06-23 11:38:10 +02:00 committed by GitHub
commit fb5c17a9ab
2 changed files with 52 additions and 7 deletions

View file

@ -36,16 +36,25 @@ class EntryFilterType extends AbstractType
$builder $builder
->add('readingTime', NumberRangeFilterType::class, [ ->add('readingTime', NumberRangeFilterType::class, [
'apply_filter' => function (QueryInterface $filterQuery, $field, $values) { '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; return;
} } elseif (null === $lower && null !== $upper) {
// only lower value is defined: query all entries with reading LOWER THAN this value
$min = (int) ($value['left_number'][0] * $this->user->getConfig()->getReadingSpeed()); $expression = $filterQuery->getExpr()->lte($field, $max);
$max = (int) ($value['right_number'][0] * $this->user->getConfig()->getReadingSpeed()); } 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); $expression = $filterQuery->getExpr()->between($field, $min, $max);
}
return $filterQuery->createCondition($expression); return $filterQuery->createCondition($expression);
}, },

View file

@ -499,6 +499,42 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertCount(1, $crawler->filter('div[class=entry]')); $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() public function testFilterOnUnreadStatus()
{ {
$this->logInAs('admin'); $this->logInAs('admin');