Skip to content

Commit

Permalink
Fix visibility issue on daily page
Browse files Browse the repository at this point in the history
This filter (links by day) didn't apply any visibility parameter.

Fixes shaarli#1543
  • Loading branch information
ArthurHoaro committed Sep 6, 2020
1 parent 21163a3 commit 27ddfec
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
4 changes: 3 additions & 1 deletion application/bookmark/BookmarkFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
17 changes: 11 additions & 6 deletions application/bookmark/BookmarkFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/bookmark/BookmarkFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
14 changes: 12 additions & 2 deletions tests/bookmark/BookmarkFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\TestCase;
use ReferenceLinkDB;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\History;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 27ddfec

Please sign in to comment.