]> BookStack Code Mirror - bookstack/commitdiff
Added book child reference handling on book url change
authorDan Brown <redacted>
Tue, 30 Aug 2022 21:00:32 +0000 (22:00 +0100)
committerDan Brown <redacted>
Tue, 30 Aug 2022 21:00:32 +0000 (22:00 +0100)
Closes #3683

app/References/ReferenceUpdater.php
tests/References/ReferencesTest.php

index d90591ab6e4922fff16966c94376f79eb799de2f..2f7b70a87ec0884aa3fd6dc4dd38db45e557af0e 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace BookStack\References;
 
+use BookStack\Entities\Models\Book;
 use BookStack\Entities\Models\Entity;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Repos\RevisionRepo;
@@ -21,7 +22,7 @@ class ReferenceUpdater
 
     public function updateEntityPageReferences(Entity $entity, string $oldLink)
     {
-        $references = $this->referenceFetcher->getPageReferencesToEntity($entity);
+        $references = $this->getReferencesToUpdate($entity);
         $newLink = $entity->getUrl();
 
         /** @var Reference $reference */
@@ -32,6 +33,33 @@ class ReferenceUpdater
         }
     }
 
+    /**
+     * @return Reference[]
+     */
+    protected function getReferencesToUpdate(Entity $entity): array
+    {
+        /** @var Reference[] $references */
+        $references = $this->referenceFetcher->getPageReferencesToEntity($entity)->values()->all();
+
+        if ($entity instanceof Book) {
+            $pages = $entity->pages()->get(['id']);
+            $chapters = $entity->chapters()->get(['id']);
+            $children = $pages->concat($chapters);
+            foreach ($children as $bookChild) {
+                $childRefs = $this->referenceFetcher->getPageReferencesToEntity($bookChild)->values()->all();
+                array_push($references, ...$childRefs);
+            }
+        }
+
+        $deduped = [];
+        foreach ($references as $reference) {
+            $key = $reference->from_id . ':' . $reference->from_type;
+            $deduped[$key] = $reference;
+        }
+
+        return array_values($deduped);
+    }
+
     protected function updateReferencesWithinPage(Page $page, string $oldLink, string $newLink)
     {
         $page = (clone $page)->refresh();
index a067aadfa7015b6d7f3761f0d28f8402d46246f0..3fd68d64750218c0ad483731eee170cecec997a8 100644 (file)
@@ -3,6 +3,7 @@
 namespace Tests\References;
 
 use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Repos\PageRepo;
 use BookStack\Entities\Tools\TrashCan;
@@ -145,6 +146,56 @@ class ReferencesTest extends TestCase
         }
     }
 
+    public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
+    {
+        /** @var Page $bookPage */
+        /** @var Page $otherPage */
+        /** @var Book $book */
+        $bookPage = Page::query()->first();
+        $otherPage = Page::query()->where('id', '!=', $bookPage->id)->first();
+        $book = $bookPage->book;
+
+        $otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
+        $otherPage->save();
+        $this->createReference($otherPage, $bookPage);
+
+        $this->asEditor()->put($book->getUrl(), [
+            'name' => 'my updated book slugaroo',
+        ]);
+
+        $otherPage->refresh();
+        $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/page/' . $bookPage->slug . '"', $otherPage->html);
+        $this->assertDatabaseHas('page_revisions', [
+            'page_id' => $otherPage->id,
+            'summary' => 'System auto-update of internal links',
+        ]);
+    }
+
+    public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
+    {
+        /** @var Chapter $bookChapter */
+        /** @var Page $otherPage */
+        /** @var Book $book */
+        $bookChapter = Chapter::query()->first();
+        $otherPage = Page::query()->first();
+        $book = $bookChapter->book;
+
+        $otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
+        $otherPage->save();
+        $this->createReference($otherPage, $bookChapter);
+
+        $this->asEditor()->put($book->getUrl(), [
+            'name' => 'my updated book slugaroo',
+        ]);
+
+        $otherPage->refresh();
+        $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/chapter/' . $bookChapter->slug . '"', $otherPage->html);
+        $this->assertDatabaseHas('page_revisions', [
+            'page_id' => $otherPage->id,
+            'summary' => 'System auto-update of internal links',
+        ]);
+    }
+
     public function test_markdown_links_leading_to_entity_updated_on_url_change()
     {
         /** @var Page $page */