Merge pull request #1350 from ArthurHoaro/hotfix/sort-consistency

Make sure that bookmark sort is consistent, even with equal timestamps
This commit is contained in:
ArthurHoaro 2019-08-10 12:07:55 +02:00 committed by GitHub
commit 14a7d73c2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View file

@ -533,6 +533,9 @@ public function reorder($order = 'DESC')
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;
});

View file

@ -619,4 +619,39 @@ public function testCountLinkPerTagPrivateWithFilter()
$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;
}
}
}
}
}