]> BookStack Code Mirror - bookstack/blobdiff - tests/References/ReferencesTest.php
API Docs: Add Missing Fields in Example Responses
[bookstack] / tests / References / ReferencesTest.php
index 82cd1668075218c08283dbe7190de742a00d17a7..a19e1b9015709d6fd8443d6fb83c3c677fc60006 100644 (file)
@@ -2,45 +2,38 @@
 
 namespace Tests\References;
 
-use BookStack\Entities\Models\Book;
-use BookStack\Entities\Models\Page;
+use BookStack\App\Model;
 use BookStack\Entities\Repos\PageRepo;
 use BookStack\Entities\Tools\TrashCan;
-use BookStack\Model;
 use BookStack\References\Reference;
 use Tests\TestCase;
 
 class ReferencesTest extends TestCase
 {
-
     public function test_references_created_on_page_update()
     {
-        /** @var Page $pageA */
-        /** @var Page $pageB */
-        $pageA = Page::query()->first();
-        $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
+        $pageA = $this->entities->page();
+        $pageB = $this->entities->page();
 
         $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
 
         $this->asEditor()->put($pageA->getUrl(), [
             'name' => 'Reference test',
-            'html' => '<a href="' . $pageB->getUrl() . '">Testing</a>'
+            'html' => '<a href="' . $pageB->getUrl() . '">Testing</a>',
         ]);
 
         $this->assertDatabaseHas('references', [
-            'from_id' => $pageA->id,
+            'from_id'   => $pageA->id,
             'from_type' => $pageA->getMorphClass(),
-            'to_id' => $pageB->id,
-            'to_type' => $pageB->getMorphClass(),
+            'to_id'     => $pageB->id,
+            'to_type'   => $pageB->getMorphClass(),
         ]);
     }
 
     public function test_references_deleted_on_entity_delete()
     {
-        /** @var Page $pageA */
-        /** @var Page $pageB */
-        $pageA = Page::query()->first();
-        $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
+        $pageA = $this->entities->page();
+        $pageB = $this->entities->page();
 
         $this->createReference($pageA, $pageB);
         $this->createReference($pageB, $pageA);
@@ -57,9 +50,8 @@ class ReferencesTest extends TestCase
 
     public function test_references_to_count_visible_on_entity_show_view()
     {
-        $entities = $this->getEachEntityType();
-        /** @var Page $otherPage */
-        $otherPage = Page::query()->where('id', '!=', $entities['page']->id)->first();
+        $entities = $this->entities->all();
+        $otherPage = $this->entities->page();
 
         $this->asEditor();
         foreach ($entities as $entity) {
@@ -79,7 +71,7 @@ class ReferencesTest extends TestCase
 
     public function test_references_to_visible_on_references_page()
     {
-        $entities = $this->getEachEntityType();
+        $entities = $this->entities->all();
         $this->asEditor();
         foreach ($entities as $entity) {
             $this->createReference($entities['page'], $entity);
@@ -95,13 +87,11 @@ class ReferencesTest extends TestCase
 
     public function test_reference_not_visible_if_view_permission_does_not_permit()
     {
-        /** @var Page $page */
-        /** @var Page $pageB */
-        $page = Page::query()->first();
-        $pageB = Page::query()->where('id', '!=', $page->id)->first();
+        $page = $this->entities->page();
+        $pageB = $this->entities->page();
         $this->createReference($pageB, $page);
 
-        $this->setEntityRestrictions($pageB);
+        $this->permissions->setEntityPermissions($pageB);
 
         $this->asEditor()->get($page->getUrl('/references'))->assertDontSee($pageB->name);
         $this->asAdmin()->get($page->getUrl('/references'))->assertSee($pageB->name);
@@ -109,8 +99,7 @@ class ReferencesTest extends TestCase
 
     public function test_reference_page_shows_empty_state_with_no_references()
     {
-        /** @var Page $page */
-        $page = Page::query()->first();
+        $page = $this->entities->page();
 
         $this->asEditor()
             ->get($page->getUrl('/references'))
@@ -119,12 +108,9 @@ class ReferencesTest extends TestCase
 
     public function test_pages_leading_to_entity_updated_on_url_change()
     {
-        /** @var Page $pageA */
-        /** @var Page $pageB */
-        /** @var Book $book */
-        $pageA = Page::query()->first();
-        $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
-        $book = Book::query()->first();
+        $pageA = $this->entities->page();
+        $pageB = $this->entities->page();
+        $book = $this->entities->book();
 
         foreach ([$pageA, $pageB] as $page) {
             $page->html = '<a href="' . $book->getUrl() . '">Link</a>';
@@ -141,17 +127,59 @@ class ReferencesTest extends TestCase
             $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo"', $page->html);
             $this->assertDatabaseHas('page_revisions', [
                 'page_id' => $page->id,
-                'summary' => 'System auto-update of internal links'
+                'summary' => 'System auto-update of internal links',
             ]);
         }
     }
 
+    public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
+    {
+        $bookPage = $this->entities->page();
+        $otherPage = $this->entities->page();
+        $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()
+    {
+        $bookChapter = $this->entities->chapter();
+        $otherPage = $this->entities->page();
+        $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 */
-        /** @var Book $book */
-        $page = Page::query()->first();
-        $book = Book::query()->first();
+        $page = $this->entities->page();
+        $book = $this->entities->book();
 
         $bookUrl = $book->getUrl();
         $markdown = '
@@ -179,10 +207,9 @@ class ReferencesTest extends TestCase
     {
         (new Reference())->forceFill([
             'from_type' => $from->getMorphClass(),
-            'from_id' => $from->id,
-            'to_type' => $to->getMorphClass(),
-            'to_id' => $to->id,
+            'from_id'   => $from->id,
+            'to_type'   => $to->getMorphClass(),
+            'to_id'     => $to->id,
         ])->save();
     }
-
-}
\ No newline at end of file
+}