]> BookStack Code Mirror - bookstack/commitdiff
Input WYSIWYG: Added testing for description references
authorDan Brown <redacted>
Tue, 19 Dec 2023 12:55:51 +0000 (12:55 +0000)
committerDan Brown <redacted>
Tue, 19 Dec 2023 12:55:51 +0000 (12:55 +0000)
tests/References/ReferencesTest.php

index a19e1b9015709d6fd8443d6fb83c3c677fc60006..bc691b0cd56adcde5684a33fe012770de31e6bde 100644 (file)
@@ -30,7 +30,30 @@ class ReferencesTest extends TestCase
         ]);
     }
 
-    public function test_references_deleted_on_entity_delete()
+    public function test_references_created_on_book_chapter_bookshelf_update()
+    {
+        $entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->shelf()];
+        $shelf = $this->entities->shelf();
+
+        foreach ($entities as $entity) {
+            $entity->refresh();
+            $this->assertDatabaseMissing('references', ['from_id' => $entity->id, 'from_type' => $entity->getMorphClass()]);
+
+            $this->asEditor()->put($entity->getUrl(), [
+                'name' => 'Reference test',
+                'description_html' => '<a href="' . $shelf->getUrl() . '">Testing</a>',
+            ]);
+
+            $this->assertDatabaseHas('references', [
+                'from_id'   => $entity->id,
+                'from_type' => $entity->getMorphClass(),
+                'to_id'     => $shelf->id,
+                'to_type'   => $shelf->getMorphClass(),
+            ]);
+        }
+    }
+
+    public function test_references_deleted_on_page_delete()
     {
         $pageA = $this->entities->page();
         $pageB = $this->entities->page();
@@ -48,6 +71,25 @@ class ReferencesTest extends TestCase
         $this->assertDatabaseMissing('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
     }
 
+    public function test_references_from_deleted_on_book_chapter_shelf_delete()
+    {
+        $entities = [$this->entities->chapter(), $this->entities->book(), $this->entities->shelf()];
+        $shelf = $this->entities->shelf();
+
+        foreach ($entities as $entity) {
+            $this->createReference($entity, $shelf);
+            $this->assertDatabaseHas('references', ['from_id' => $entity->id, 'from_type' => $entity->getMorphClass()]);
+
+            $this->asEditor()->delete($entity->getUrl());
+            app(TrashCan::class)->empty();
+
+            $this->assertDatabaseMissing('references', [
+                'from_id'   => $entity->id,
+                'from_type' => $entity->getMorphClass()
+            ]);
+        }
+    }
+
     public function test_references_to_count_visible_on_entity_show_view()
     {
         $entities = $this->entities->all();
@@ -203,6 +245,32 @@ class ReferencesTest extends TestCase
         $this->assertEquals($expected, $page->markdown);
     }
 
+    public function test_description_links_from_book_chapter_shelf_updated_on_url_change()
+    {
+        $entities = [$this->entities->chapter(), $this->entities->book(), $this->entities->shelf()];
+        $shelf = $this->entities->shelf();
+        $this->asEditor();
+
+        foreach ($entities as $entity) {
+            $this->put($entity->getUrl(), [
+                'name' => 'Reference test',
+                'description_html' => '<a href="' . $shelf->getUrl() . '">Testing</a>',
+            ]);
+        }
+
+        $oldUrl = $shelf->getUrl();
+        $this->put($shelf->getUrl(), ['name' => 'My updated shelf link']);
+        $shelf->refresh();
+        $this->assertNotEquals($oldUrl, $shelf->getUrl());
+
+        foreach ($entities as $entity) {
+            $oldHtml = $entity->description_html;
+            $entity->refresh();
+            $this->assertNotEquals($oldHtml, $entity->description_html);
+            $this->assertStringContainsString($shelf->getUrl(), $entity->description_html);
+        }
+    }
+
     protected function createReference(Model $from, Model $to)
     {
         (new Reference())->forceFill([