From faf1716e8cb660420b724977e79ce068b5be8274 Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 24 Oct 2023 01:45:25 +0200 Subject: [PATCH] feat(PageService): Clean subpageOrder from orphans before adding new ids Signed-off-by: Jonas --- lib/Service/PageService.php | 44 ++++++++++++++++++- lib/Service/SubpageOrderService.php | 19 ++++++++ .../Unit/Service/SubpageOrderServiceTest.php | 6 +++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/Service/PageService.php b/lib/Service/PageService.php index d30f1121c3..bb92180727 100644 --- a/lib/Service/PageService.php +++ b/lib/Service/PageService.php @@ -173,6 +173,28 @@ public function getFolder(int $collectiveId, int $fileId, string $userId): Folde return $file->getParent(); } + /** + * @param int $collectiveId + * @param File $file + * @param string $userId + * + * @return array + * @throws MissingDependencyException + * @throws NotFoundException + * @throws NotPermittedException + */ + private function getSubpagesFromFile(int $collectiveId, File $file, string $userId): array { + if (!NodeHelper::isIndexPage($file)) { + return []; + } + + $parentId = $file->getId(); + $folder = $this->getFolder($collectiveId, $parentId, $userId); + return array_filter($this->getPagesFromFolder($collectiveId, $folder, $userId), static function (PageInfo $pageInfo) use ($parentId) { + return $pageInfo->getParentId() === $parentId; + }); + } + /** * @param File $file * @param Node|null $parent @@ -902,6 +924,24 @@ public function setSubpageOrder(int $collectiveId, int $id, ?string $subpageOrde return $pageInfo; } + /** + * @param int $collectiveId + * @param PageInfo $pageInfo + * @param string $userId + * + * @return string + * @throws MissingDependencyException + * @throws NotFoundException + * @throws NotPermittedException + */ + private function cleanSubpageOrder(int $collectiveId, PageInfo $pageInfo, string $userId): string { + $pageFile = $this->getPageFile($collectiveId, $pageInfo->getId(), $userId); + $childIds = array_map(static function (PageInfo $pageInfo) { + return $pageInfo->getId(); + }, $this->getSubpagesFromFile($collectiveId, $pageFile, $userId)); + return SubpageOrderService::clean($pageInfo->getSubpageOrder(), $childIds); + } + /** * @param int $collectiveId * @param int $pageId @@ -918,7 +958,9 @@ private function addToSubpageOrder(int $collectiveId, int $pageId, int $addId, i $file = $this->nodeHelper->getFileById($collectiveFolder, $pageId); $pageInfo = $this->getPageByFile($file); - $newSubpageOrder = SubpageOrderService::add($pageInfo->getSubpageOrder(), $addId, $index); + $subpageOrder = $pageInfo->getSubpageOrder(); + $cleanedSubpageOrder = $this->cleanSubpageOrder($collectiveId, $pageInfo, $userId); + $newSubpageOrder = SubpageOrderService::add($cleanedSubpageOrder, $addId, $index); $pageInfo->setSubpageOrder($newSubpageOrder); $this->updatePage($collectiveId, $pageInfo->getId(), $userId, null, $newSubpageOrder); diff --git a/lib/Service/SubpageOrderService.php b/lib/Service/SubpageOrderService.php index 4f8b1893db..ba04a619dd 100644 --- a/lib/Service/SubpageOrderService.php +++ b/lib/Service/SubpageOrderService.php @@ -60,6 +60,25 @@ public static function verify(?string $subpageOrder): void { } } + /** + * @param string|null $subpageOrder + * @param array $childIds + * + * @return string + * @throws NotPermittedException + */ + public static function clean(?string $subpageOrder, array $childIds): string { + $subpageOrderArray = self::toArray($subpageOrder); + $cleanedSubpageOrderArray = []; + foreach ($subpageOrderArray as $pageId) { + if (in_array($pageId, $childIds, true)) { + $cleanedSubpageOrderArray[] = $pageId; + } + } + + return self::fromArray($cleanedSubpageOrderArray); + } + /** * @param string|null $subpageOrder * @param int $pageId diff --git a/tests/Unit/Service/SubpageOrderServiceTest.php b/tests/Unit/Service/SubpageOrderServiceTest.php index 16d4758c8a..ecca135c15 100644 --- a/tests/Unit/Service/SubpageOrderServiceTest.php +++ b/tests/Unit/Service/SubpageOrderServiceTest.php @@ -42,6 +42,12 @@ public function testVerifyInvalid3(): void { SubpageOrderService::verify('{a: b}'); } + public function testClean(): void { + $subpageOrder = '[1,2,3,5]'; + self::assertEquals('[1,3,5]', SubpageOrderService::clean($subpageOrder, [1,3,4,5])); + self::assertEquals('[1,3]', SubpageOrderService::clean($subpageOrder, [1,3])); + } + public function testAdd(): void { $subpageOrder = '[1,2,3]'; self::assertEquals('[0,1,2,3]', SubpageOrderService::add($subpageOrder, 0));