]> BookStack Code Mirror - bookstack/commitdiff
Added deletion of revisions on page delete
authorDan Brown <redacted>
Wed, 26 May 2021 15:40:56 +0000 (16:40 +0100)
committerDan Brown <redacted>
Wed, 26 May 2021 15:40:56 +0000 (16:40 +0100)
Added testing to cover.
Closes #2668

app/Entities/Models/Page.php
app/Entities/Tools/TrashCan.php
tests/Entity/PageTest.php

index 7e397894de35fec52bd6692bf74017eed54cd45a..93fb218932cf6e9f2796fce7f3aaac8586c43490 100644 (file)
@@ -75,11 +75,23 @@ class Page extends BookChild
 
     /**
      * Get the associated page revisions, ordered by created date.
-     * @return mixed
+     * Only provides actual saved page revision instances, Not drafts.
+     */
+    public function revisions(): HasMany
+    {
+        return $this->allRevisions()
+            ->where('type', '=', 'version')
+            ->orderBy('created_at', 'desc')
+            ->orderBy('id', 'desc');
+    }
+
+    /**
+     * Get all revision instances assigned to this page.
+     * Includes all types of revisions.
      */
-    public function revisions()
+    public function allRevisions(): HasMany
     {
-        return $this->hasMany(PageRevision::class)->where('type', '=', 'version')->orderBy('created_at', 'desc')->orderBy('id', 'desc');
+        return $this->hasMany(PageRevision::class);
     }
 
     /**
index bf379283521929ef9db87b33742aaa6f7c454825..0b6081ae46a1208cc9d74b384b61a1c15f5efde3 100644 (file)
@@ -151,6 +151,7 @@ class TrashCan
     protected function destroyPage(Page $page): int
     {
         $this->destroyCommonRelations($page);
+        $page->allRevisions()->delete();
 
         // Delete Attached Files
         $attachmentService = app(AttachmentService::class);
index e1dffb61faf05ae078502ed0a7f5bd2de9ea30a9..a6f6f9d508e070b0c5a41b007a6ad72a72a1ee58 100644 (file)
@@ -71,6 +71,33 @@ class PageTest extends TestCase
         $redirectReq->assertNotificationContains('Page Successfully Deleted');
     }
 
+    public function test_page_full_delete_removes_all_revisions()
+    {
+        /** @var Page $page */
+        $page = Page::query()->first();
+        $page->revisions()->create([
+            'html' => '<p>ducks</p>',
+            'name' => 'my page revision',
+            'type' => 'draft',
+        ]);
+        $page->revisions()->create([
+            'html' => '<p>ducks</p>',
+            'name' => 'my page revision',
+            'type' => 'revision',
+        ]);
+
+        $this->assertDatabaseHas('page_revisions', [
+            'page_id' => $page->id,
+        ]);
+
+        $this->asEditor()->delete($page->getUrl());
+        $this->asAdmin()->post('/settings/recycle-bin/empty');
+
+        $this->assertDatabaseMissing('page_revisions', [
+            'page_id' => $page->id,
+        ]);
+    }
+
     public function test_page_copy()
     {
         $page = Page::first();
Morty Proxy This is a proxified and sanitized view of the page, visit original site.