diff --git a/lib/Db/View.php b/lib/Db/View.php index 910cf0b211..70c2c24036 100644 --- a/lib/Db/View.php +++ b/lib/Db/View.php @@ -150,7 +150,7 @@ public function getColumnsArray(): array { $columnSettings = $this->getColumnsSettingsArray(); usort($columnSettings, static fn (ViewColumnInformation $a, ViewColumnInformation $b) => $a->getOrder() - $b->getOrder()); - return array_column($columnSettings, ViewColumnInformation::KEY_ID); + return array_map(static fn (ViewColumnInformation $column): int => $column->getId(), $columnSettings); } /** diff --git a/tests/unit/Db/ViewTest.php b/tests/unit/Db/ViewTest.php index 3860de4e7e..0931443610 100644 --- a/tests/unit/Db/ViewTest.php +++ b/tests/unit/Db/ViewTest.php @@ -70,4 +70,37 @@ public static function provideUnusableViewSettings(): array { 'sources of the wrong type' => ['{"cardBackgroundSource":"3","cardTitleSource":[]}'], ]; } + + private function makeView(string $columns): View { + $view = new View(); + $view->setColumns($columns); + return $view; + } + + public function testGetColumnsArrayEmptyReturnsEmpty(): void { + $view = $this->makeView(''); + $this->assertSame([], $view->getColumnsArray()); + } + + public function testGetColumnsArrayLegacyPlainIdFormat(): void { + // Legacy format: plain array of integer column IDs — order is derived from index + $view = $this->makeView(\json_encode([10, 20, 30])); + $this->assertSame([10, 20, 30], $view->getColumnsArray()); + } + + public function testGetColumnsArrayObjectFormatIsSortedByOrder(): void { + $view = $this->makeView(\json_encode([ + ['columnId' => 5, 'order' => 2, 'readonly' => false, 'mandatory' => false], + ['columnId' => 3, 'order' => 1, 'readonly' => false, 'mandatory' => false], + ])); + $this->assertSame([3, 5], $view->getColumnsArray()); + } + + public function testJsonSerializeReturnsColumnIds(): void { + $view = $this->makeView(\json_encode([ + ['columnId' => 5, 'order' => 2, 'readonly' => false, 'mandatory' => false], + ['columnId' => 3, 'order' => 1, 'readonly' => false, 'mandatory' => false], + ])); + $this->assertSame([3, 5], $view->jsonSerialize()['columns']); + } }