diff options
author | ArthurHoaro <arthur@hoa.ro> | 2019-08-10 12:07:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-10 12:07:55 +0200 |
commit | 14a7d73c2d65de496ef270f784de82c5f584cb3a (patch) | |
tree | ed4c86aafc3cfdf8c5402af3103124a5f8a3f22b | |
parent | edcfe54c45aedd186cf5092295767d85bf37b780 (diff) | |
parent | 9f9627059a0b17de45a90e3c5fad9c1a49318151 (diff) | |
download | Shaarli-14a7d73c2d65de496ef270f784de82c5f584cb3a.tar.gz Shaarli-14a7d73c2d65de496ef270f784de82c5f584cb3a.tar.zst Shaarli-14a7d73c2d65de496ef270f784de82c5f584cb3a.zip |
Merge pull request #1350 from ArthurHoaro/hotfix/sort-consistency
Make sure that bookmark sort is consistent, even with equal timestamps
-rw-r--r-- | application/bookmark/LinkDB.php | 7 | ||||
-rw-r--r-- | tests/bookmark/LinkDBTest.php | 35 |
2 files changed, 40 insertions, 2 deletions
diff --git a/application/bookmark/LinkDB.php b/application/bookmark/LinkDB.php index efde8468..f01c7ee6 100644 --- a/application/bookmark/LinkDB.php +++ b/application/bookmark/LinkDB.php | |||
@@ -102,7 +102,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess | |||
102 | $isLoggedIn, | 102 | $isLoggedIn, |
103 | $hidePublicLinks | 103 | $hidePublicLinks |
104 | ) { | 104 | ) { |
105 | 105 | ||
106 | $this->datastore = $datastore; | 106 | $this->datastore = $datastore; |
107 | $this->loggedIn = $isLoggedIn; | 107 | $this->loggedIn = $isLoggedIn; |
108 | $this->hidePublicLinks = $hidePublicLinks; | 108 | $this->hidePublicLinks = $hidePublicLinks; |
@@ -415,7 +415,7 @@ You use the community supported version of the original Shaarli project, by Seba | |||
415 | $visibility = 'all', | 415 | $visibility = 'all', |
416 | $untaggedonly = false | 416 | $untaggedonly = false |
417 | ) { | 417 | ) { |
418 | 418 | ||
419 | // Filter link database according to parameters. | 419 | // Filter link database according to parameters. |
420 | $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; | 420 | $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : ''; |
421 | $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; | 421 | $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : ''; |
@@ -533,6 +533,9 @@ You use the community supported version of the original Shaarli project, by Seba | |||
533 | if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) { | 533 | if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) { |
534 | return $a['sticky'] ? -1 : 1; | 534 | return $a['sticky'] ? -1 : 1; |
535 | } | 535 | } |
536 | if ($a['created'] == $b['created']) { | ||
537 | return $a['id'] < $b['id'] ? 1 * $order : -1 * $order; | ||
538 | } | ||
536 | return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; | 539 | return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; |
537 | }); | 540 | }); |
538 | 541 | ||
diff --git a/tests/bookmark/LinkDBTest.php b/tests/bookmark/LinkDBTest.php index 2990a6b5..5bbdcea1 100644 --- a/tests/bookmark/LinkDBTest.php +++ b/tests/bookmark/LinkDBTest.php | |||
@@ -619,4 +619,39 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase | |||
619 | 619 | ||
620 | $this->assertEquals($expected, $tags, var_export($tags, true)); | 620 | $this->assertEquals($expected, $tags, var_export($tags, true)); |
621 | } | 621 | } |
622 | |||
623 | /** | ||
624 | * Make sure that bookmarks with the same timestamp have a consistent order: | ||
625 | * if their creation date is equal, bookmarks are sorted by ID DESC. | ||
626 | */ | ||
627 | public function testConsistentOrder() | ||
628 | { | ||
629 | $nextId = 42; | ||
630 | $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444'); | ||
631 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
632 | for ($i = 0; $i < 4; ++$i) { | ||
633 | $linkDB[$nextId + $i] = [ | ||
634 | 'id' => $nextId + $i, | ||
635 | 'url' => 'http://'. $i, | ||
636 | 'created' => $creation, | ||
637 | 'title' => true, | ||
638 | 'description' => true, | ||
639 | 'tags' => true, | ||
640 | ]; | ||
641 | } | ||
642 | |||
643 | // Check 4 new links 4 times | ||
644 | for ($i = 0; $i < 4; ++$i) { | ||
645 | $linkDB->save('tests'); | ||
646 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
647 | $count = 3; | ||
648 | foreach ($linkDB as $link) { | ||
649 | $this->assertEquals($nextId + $count, $link['id']); | ||
650 | $this->assertEquals('http://'. $count, $link['url']); | ||
651 | if (--$count < 0) { | ||
652 | break; | ||
653 | } | ||
654 | } | ||
655 | } | ||
656 | } | ||
622 | } | 657 | } |