diff --git a/lib/Database.php b/lib/Database.php index 812012f..1373c9a 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -1193,12 +1193,10 @@ class Database { } $id = $this->articleValidateId($user, $id)['article']; $out = $this->db->prepare("SELECT id,name from arsse_labels where owner = ? and exists(select id from arsse_label_members where article = ? and label = arsse_labels.id and assigned = 1)", "str", "int")->run($user, $id)->getAll(); - if (!$out) { - return $out; - } else { - // flatten the result to return just the label ID or name - return array_column($out, !$byName ? "id" : "name"); - } + // flatten the result to return just the label ID or name, sorted + $out = $out ? array_column($out, !$byName ? "id" : "name") : []; + sort($out); + return $out; } public function articleCategoriesGet(string $user, $id): array { @@ -1444,7 +1442,7 @@ class Database { $this->labelValidateId($user, $id, $byName, false); $field = !$byName ? "id" : "name"; $type = !$byName ? "int" : "str"; - $out = $this->db->prepare("SELECT article from arsse_label_members join arsse_labels on label = id where assigned = 1 and $field = ? and owner = ?", $type, "str")->run($id, $user)->getAll(); + $out = $this->db->prepare("SELECT article from arsse_label_members join arsse_labels on label = id where assigned = 1 and $field = ? and owner = ? order by article", $type, "str")->run($id, $user)->getAll(); if (!$out) { // if no results were returned, do a full validation on the label ID $this->labelValidateId($user, $id, $byName, true, true); diff --git a/lib/Db/PDOError.php b/lib/Db/PDOError.php index 5aee4df..7e8252d 100644 --- a/lib/Db/PDOError.php +++ b/lib/Db/PDOError.php @@ -19,6 +19,7 @@ trait PDOError { return [ExceptionInput::class, 'engineTypeViolation', $err[2]]; case "23000": case "23502": + case "23505": return [ExceptionInput::class, "constraintViolation", $err[2]]; case "55P03": case "57014": diff --git a/tests/cases/Database/SeriesArticle.php b/tests/cases/Database/SeriesArticle.php index 05a8ded..2147d64 100644 --- a/tests/cases/Database/SeriesArticle.php +++ b/tests/cases/Database/SeriesArticle.php @@ -957,7 +957,7 @@ trait SeriesArticle { } public function testListTheLabelsOfAnArticle() { - $this->assertEquals([2,1], Arsse::$db->articleLabelsGet("john.doe@example.com", 1)); + $this->assertEquals([1,2], Arsse::$db->articleLabelsGet("john.doe@example.com", 1)); $this->assertEquals([2], Arsse::$db->articleLabelsGet("john.doe@example.com", 5)); $this->assertEquals([], Arsse::$db->articleLabelsGet("john.doe@example.com", 2)); $this->assertEquals(["Fascinating","Interesting"], Arsse::$db->articleLabelsGet("john.doe@example.com", 1, true)); diff --git a/tests/cases/Db/PostgreSQL/TestDatabase.php b/tests/cases/Db/PostgreSQL/TestDatabase.php index 2b6e7fb..91e326f 100644 --- a/tests/cases/Db/PostgreSQL/TestDatabase.php +++ b/tests/cases/Db/PostgreSQL/TestDatabase.php @@ -14,7 +14,7 @@ class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base { protected static $implementation = "PDO PostgreSQL"; protected function nextID(string $table): int { - return ((int) static::$drv->query("SELECT last_value from pg_sequences where sequencename = '{$table}_id_seq'")->getValue()) + 1; + return (int) static::$drv->query("SELECT coalesce(last_value, (select max(id) from $table)) + 1 from pg_sequences where sequencename = '{$table}_id_seq'")->getValue(); } public function setUp() { @@ -30,7 +30,7 @@ class TestDatabase extends \JKingWeb\Arsse\TestCase\Database\Base { and column_default like 'nextval(%' "; foreach(static::$drv->query($seqList) as $r) { - $num = static::$drv->query("SELECT max({$r['col']}) from {$r['table']}")->getValue(); + $num = (int) static::$drv->query("SELECT max({$r['col']}) from {$r['table']}")->getValue(); if (!$num) { continue; }