]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #1350 from ArthurHoaro/hotfix/sort-consistency
authorArthurHoaro <arthur@hoa.ro>
Sat, 10 Aug 2019 10:07:55 +0000 (12:07 +0200)
committerGitHub <noreply@github.com>
Sat, 10 Aug 2019 10:07:55 +0000 (12:07 +0200)
Make sure that bookmark sort is consistent, even with equal timestamps

application/bookmark/LinkDB.php
tests/bookmark/LinkDBTest.php

index efde8468fca97e896f1da161e444993bdd711b4d..f01c7ee6cadaea8422baf7e650ee85e639e84172 100644 (file)
@@ -102,7 +102,7 @@ class LinkDB implements Iterator, Countable, ArrayAccess
         $isLoggedIn,
         $hidePublicLinks
     ) {
-    
+
         $this->datastore = $datastore;
         $this->loggedIn = $isLoggedIn;
         $this->hidePublicLinks = $hidePublicLinks;
@@ -415,7 +415,7 @@ You use the community supported version of the original Shaarli project, by Seba
         $visibility = 'all',
         $untaggedonly = false
     ) {
-    
+
         // Filter link database according to parameters.
         $searchtags = isset($filterRequest['searchtags']) ? escape($filterRequest['searchtags']) : '';
         $searchterm = isset($filterRequest['searchterm']) ? escape($filterRequest['searchterm']) : '';
@@ -533,6 +533,9 @@ You use the community supported version of the original Shaarli project, by Seba
             if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
                 return $a['sticky'] ? -1 : 1;
             }
+            if ($a['created'] == $b['created']) {
+                return $a['id'] < $b['id'] ? 1 * $order : -1 * $order;
+            }
             return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
         });
 
index 2990a6b56a0360756b3b8ee157fc7125e4372a21..5bbdcea1ebef39f560a0d6c6a003127c279bfbaf 100644 (file)
@@ -619,4 +619,39 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
 
         $this->assertEquals($expected, $tags, var_export($tags, true));
     }
+
+    /**
+     * Make sure that bookmarks with the same timestamp have a consistent order:
+     * if their creation date is equal, bookmarks are sorted by ID DESC.
+     */
+    public function testConsistentOrder()
+    {
+        $nextId = 42;
+        $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444');
+        $linkDB = new LinkDB(self::$testDatastore, true, false);
+        for ($i = 0; $i < 4; ++$i) {
+            $linkDB[$nextId + $i] = [
+                'id' => $nextId + $i,
+                'url' => 'http://'. $i,
+                'created' => $creation,
+                'title' => true,
+                'description' => true,
+                'tags' => true,
+            ];
+        }
+
+        // Check 4 new links 4 times
+        for ($i = 0; $i < 4; ++$i) {
+            $linkDB->save('tests');
+            $linkDB = new LinkDB(self::$testDatastore, true, false);
+            $count = 3;
+            foreach ($linkDB as $link) {
+                $this->assertEquals($nextId + $count, $link['id']);
+                $this->assertEquals('http://'. $count, $link['url']);
+                if (--$count < 0) {
+                    break;
+                }
+            }
+        }
+    }
 }