]> BookStack Code Mirror - bookstack/blobdiff - tests/RecycleBinTest.php
Add base64 image support
[bookstack] / tests / RecycleBinTest.php
index ce8b644bd216a200503066adea829c437b00d654..55a9571de40acbdd1896cd7e5208012f8a28719d 100644 (file)
@@ -1,8 +1,11 @@
 <?php namespace Tests;
 
-use BookStack\Entities\Book;
-use BookStack\Entities\Deletion;
-use BookStack\Entities\Page;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Models\Chapter;
+use BookStack\Entities\Models\Deletion;
+use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Models\Page;
 use DB;
 use Illuminate\Support\Carbon;
 
@@ -129,6 +132,21 @@ class RecycleBinTest extends TestCase
         $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
     }
 
+    public function test_permanent_delete_for_each_type()
+    {
+        /** @var Entity $entity */
+        foreach ([new Bookshelf, new Book, new Chapter, new Page] as $entity) {
+            $entity = $entity->newQuery()->first();
+            $this->asEditor()->delete($entity->getUrl());
+            $deletion = Deletion::query()->orderBy('id', 'desc')->firstOrFail();
+
+            $deleteReq = $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
+            $deleteReq->assertRedirect('/settings/recycle-bin');
+            $this->assertDatabaseMissing('deletions', ['id' => $deletion->id]);
+            $this->assertDatabaseMissing($entity->getTable(), ['id' => $entity->id]);
+        }
+    }
+
     public function test_permanent_entity_delete_updates_existing_activity_with_entity_name()
     {
         $page = Page::query()->firstOrFail();
@@ -136,7 +154,7 @@ class RecycleBinTest extends TestCase
         $deletion = $page->deletions()->firstOrFail();
 
         $this->assertDatabaseHas('activities', [
-            'key' => 'page_delete',
+            'type' => 'page_delete',
             'entity_id' => $page->id,
             'entity_type' => $page->getMorphClass(),
         ]);
@@ -144,16 +162,16 @@ class RecycleBinTest extends TestCase
         $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
 
         $this->assertDatabaseMissing('activities', [
-            'key' => 'page_delete',
+            'type' => 'page_delete',
             'entity_id' => $page->id,
             'entity_type' => $page->getMorphClass(),
         ]);
 
         $this->assertDatabaseHas('activities', [
-            'key' => 'page_delete',
-            'entity_id' => 0,
-            'entity_type' => '',
-            'extra' => $page->name,
+            'type' => 'page_delete',
+            'entity_id' => null,
+            'entity_type' => null,
+            'detail' => $page->name,
         ]);
     }
 
@@ -199,4 +217,34 @@ class RecycleBinTest extends TestCase
         $this->assertDatabaseMissing('pages', ['id' => $page->id]);
         $this->assertEquals(0, Deletion::query()->count());
     }
+
+    public function test_restore_flow_when_restoring_nested_delete_first()
+    {
+        $book = Book::query()->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
+        $chapter = $book->chapters->first();
+        $this->asEditor()->delete($chapter->getUrl());
+        $this->asEditor()->delete($book->getUrl());
+
+        $bookDeletion = $book->deletions()->first();
+        $chapterDeletion = $chapter->deletions()->first();
+
+        $chapterRestoreView = $this->asAdmin()->get("/settings/recycle-bin/{$chapterDeletion->id}/restore");
+        $chapterRestoreView->assertStatus(200);
+        $chapterRestoreView->assertSeeText($chapter->name);
+
+        $chapterRestore = $this->post("/settings/recycle-bin/{$chapterDeletion->id}/restore");
+        $chapterRestore->assertRedirect("/settings/recycle-bin");
+        $this->assertDatabaseMissing("deletions", ["id" => $chapterDeletion->id]);
+
+        $chapter->refresh();
+        $this->assertNotNull($chapter->deleted_at);
+
+        $bookRestoreView = $this->asAdmin()->get("/settings/recycle-bin/{$bookDeletion->id}/restore");
+        $bookRestoreView->assertStatus(200);
+        $bookRestoreView->assertSeeText($chapter->name);
+
+        $this->post("/settings/recycle-bin/{$bookDeletion->id}/restore");
+        $chapter->refresh();
+        $this->assertNull($chapter->deleted_at);
+    }
 }
\ No newline at end of file