From 27ddfec3c3847f10ab0de246f4a174b751c5f19e Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 6 Sep 2020 14:11:02 +0200 Subject: [PATCH] Fix visibility issue on daily page This filter (links by day) didn't apply any visibility parameter. Fixes #1543 --- application/bookmark/BookmarkFileService.php | 4 ++- application/bookmark/BookmarkFilter.php | 17 +++++++---- tests/bookmark/BookmarkFileServiceTest.php | 30 ++++++++++++++++++++ tests/bookmark/BookmarkFilterTest.php | 14 +++++++-- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index e3a611461..c9ec26093 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php @@ -362,7 +362,9 @@ public function days() */ public function filterDay($request) { - return $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_DAY, $request); + $visibility = $this->isLoggedIn ? BookmarkFilter::$ALL : BookmarkFilter::$PUBLIC; + + return $this->bookmarkFilter->filter(BookmarkFilter::$FILTER_DAY, $request, false, $visibility); } /** diff --git a/application/bookmark/BookmarkFilter.php b/application/bookmark/BookmarkFilter.php index 797a36b8e..6636bbfee 100644 --- a/application/bookmark/BookmarkFilter.php +++ b/application/bookmark/BookmarkFilter.php @@ -115,7 +115,7 @@ public function filter($type, $request, $casesensitive = false, $visibility = 'a return $this->filterTags($request, $casesensitive, $visibility); } case self::$FILTER_DAY: - return $this->filterDay($request); + return $this->filterDay($request, $visibility); default: return $this->noFilter($visibility); } @@ -425,21 +425,26 @@ public function filterUntagged($visibility) * print_r($mydb->filterDay('20120125')); * * @param string $day day to filter. - * + * @param string $visibility return only all/private/public bookmarks. + * @return array all link matching given day. * * @throws Exception if date format is invalid. */ - public function filterDay($day) + public function filterDay($day, $visibility) { if (!checkDateFormat('Ymd', $day)) { throw new Exception('Invalid date format'); } $filtered = []; - foreach ($this->bookmarks as $key => $l) { - if ($l->getCreated()->format('Ymd') == $day) { - $filtered[$key] = $l; + foreach ($this->bookmarks as $key => $bookmark) { + if ($visibility === static::$PUBLIC && $bookmark->isPrivate()) { + continue; + } + + if ($bookmark->getCreated()->format('Ymd') == $day) { + $filtered[$key] = $bookmark; } } diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index 7b1906d3f..c59fd443d 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -1061,6 +1061,36 @@ public function testCountTagsNoMarkdown() $this->assertEquals($expected, $tags, var_export($tags, true)); } + /** + * Test filterDay while logged in + */ + public function testFilterDayLoggedIn(): void + { + $bookmarks = $this->privateLinkDB->filterDay('20121206'); + $expectedIds = [4, 9, 1, 0]; + + static::assertCount(4, $bookmarks); + foreach ($bookmarks as $bookmark) { + $i = ($i ?? -1) + 1; + static::assertSame($expectedIds[$i], $bookmark->getId()); + } + } + + /** + * Test filterDay while logged out + */ + public function testFilterDayLoggedOut(): void + { + $bookmarks = $this->publicLinkDB->filterDay('20121206'); + $expectedIds = [4, 9, 1]; + + static::assertCount(3, $bookmarks); + foreach ($bookmarks as $bookmark) { + $i = ($i ?? -1) + 1; + static::assertSame($expectedIds[$i], $bookmark->getId()); + } + } + /** * Allows to test LinkDB's private methods * diff --git a/tests/bookmark/BookmarkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php index d4c71cb94..91e139c20 100644 --- a/tests/bookmark/BookmarkFilterTest.php +++ b/tests/bookmark/BookmarkFilterTest.php @@ -6,7 +6,6 @@ use PHPUnit\Framework\TestCase; use ReferenceLinkDB; use Shaarli\Config\ConfigManager; -use Shaarli\Formatter\FormatterFactory; use Shaarli\History; /** @@ -36,7 +35,7 @@ class BookmarkFilterTest extends TestCase /** * Instantiate linkFilter with ReferenceLinkDB data. */ - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { $conf = new ConfigManager('tests/utils/config/configJson'); $conf->set('resource.datastore', self::$testDatastore); @@ -189,6 +188,17 @@ public function testFilterDay() ); } + /** + * Return bookmarks for a given day + */ + public function testFilterDayRestrictedVisibility(): void + { + $this->assertEquals( + 3, + count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20121206', false, BookmarkFilter::$PUBLIC)) + ); + } + /** * 404 - day not found */