]> BookStack Code Mirror - bookstack/blobdiff - tests/RecycleBinTest.php
Replaced embeds with images in exports
[bookstack] / tests / RecycleBinTest.php
index 60f06cfc4769a9d457fd401eb0727b6d5405ba6e..f3e30c0d07d442f894347f7599e97e3e1c9218fc 100644 (file)
@@ -1,10 +1,15 @@
-<?php namespace Tests;
+<?php
+
+namespace Tests;
 
 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;
+use Illuminate\Support\Facades\DB;
 
 class RecycleBinTest extends TestCase
 {
@@ -24,7 +29,7 @@ class RecycleBinTest extends TestCase
             "DELETE:/settings/recycle-bin/{$deletion->id}",
         ];
 
-        foreach($routes as $route) {
+        foreach ($routes as $route) {
             [$method, $url] = explode(':', $route);
             $resp = $this->call($method, $url);
             $this->assertPermissionError($resp);
@@ -32,7 +37,7 @@ class RecycleBinTest extends TestCase
 
         $this->giveUserPermissions($editor, ['restrictions-manage-all']);
 
-        foreach($routes as $route) {
+        foreach ($routes as $route) {
             [$method, $url] = explode(':', $route);
             $resp = $this->call($method, $url);
             $this->assertPermissionError($resp);
@@ -40,14 +45,13 @@ class RecycleBinTest extends TestCase
 
         $this->giveUserPermissions($editor, ['settings-manage']);
 
-        foreach($routes as $route) {
+        foreach ($routes as $route) {
             DB::beginTransaction();
             [$method, $url] = explode(':', $route);
             $resp = $this->call($method, $url);
             $this->assertNotPermissionError($resp);
             DB::rollBack();
         }
-
     }
 
     public function test_recycle_bin_view()
@@ -69,7 +73,7 @@ class RecycleBinTest extends TestCase
     public function test_recycle_bin_empty()
     {
         $page = Page::query()->first();
-        $book = Book::query()->where('id' , '!=', $page->book_id)->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
+        $book = Book::query()->where('id', '!=', $page->book_id)->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
         $editor = $this->getEditor();
         $this->actingAs($editor)->delete($page->getUrl());
         $this->actingAs($editor)->delete($book->getUrl());
@@ -86,7 +90,7 @@ class RecycleBinTest extends TestCase
 
         $itemCount = 2 + $book->pages->count() + $book->chapters->count();
         $redirectReq = $this->get('/settings/recycle-bin');
-        $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
+        $redirectReq->assertNotificationContains('Deleted ' . $itemCount . ' total items from the recycle bin');
     }
 
     public function test_entity_restore()
@@ -107,7 +111,7 @@ class RecycleBinTest extends TestCase
 
         $itemCount = 1 + $book->pages->count() + $book->chapters->count();
         $redirectReq = $this->get('/settings/recycle-bin');
-        $redirectReq->assertNotificationContains('Restored '.$itemCount.' total items from the recycle bin');
+        $redirectReq->assertNotificationContains('Restored ' . $itemCount . ' total items from the recycle bin');
     }
 
     public function test_permanent_delete()
@@ -126,7 +130,22 @@ class RecycleBinTest extends TestCase
 
         $itemCount = 1 + $book->pages->count() + $book->chapters->count();
         $redirectReq = $this->get('/settings/recycle-bin');
-        $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
+        $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()
@@ -136,24 +155,24 @@ class RecycleBinTest extends TestCase
         $deletion = $page->deletions()->firstOrFail();
 
         $this->assertDatabaseHas('activities', [
-            'type' => 'page_delete',
-            'entity_id' => $page->id,
+            'type'        => 'page_delete',
+            'entity_id'   => $page->id,
             'entity_type' => $page->getMorphClass(),
         ]);
 
         $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
 
         $this->assertDatabaseMissing('activities', [
-            'type' => 'page_delete',
-            'entity_id' => $page->id,
+            'type'        => 'page_delete',
+            'entity_id'   => $page->id,
             'entity_type' => $page->getMorphClass(),
         ]);
 
         $this->assertDatabaseHas('activities', [
-            'type' => 'page_delete',
-            'entity_id' => null,
+            'type'        => 'page_delete',
+            'entity_id'   => null,
             'entity_type' => null,
-            'detail' => $page->name,
+            'detail'      => $page->name,
         ]);
     }
 
@@ -215,8 +234,8 @@ class RecycleBinTest extends TestCase
         $chapterRestoreView->assertSeeText($chapter->name);
 
         $chapterRestore = $this->post("/settings/recycle-bin/{$chapterDeletion->id}/restore");
-        $chapterRestore->assertRedirect("/settings/recycle-bin");
-        $this->assertDatabaseMissing("deletions", ["id" => $chapterDeletion->id]);
+        $chapterRestore->assertRedirect('/settings/recycle-bin');
+        $this->assertDatabaseMissing('deletions', ['id' => $chapterDeletion->id]);
 
         $chapter->refresh();
         $this->assertNotNull($chapter->deleted_at);
@@ -229,4 +248,22 @@ class RecycleBinTest extends TestCase
         $chapter->refresh();
         $this->assertNull($chapter->deleted_at);
     }
-}
\ No newline at end of file
+
+    public function test_restore_page_shows_link_to_parent_restore_if_parent_also_deleted()
+    {
+        /** @var Book $book */
+        $book = Book::query()->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
+        $chapter = $book->chapters->first();
+        /** @var Page $page */
+        $page = $chapter->pages->first();
+        $this->asEditor()->delete($page->getUrl());
+        $this->asEditor()->delete($book->getUrl());
+
+        $bookDeletion = $book->deletions()->first();
+        $pageDeletion = $page->deletions()->first();
+
+        $pageRestoreView = $this->asAdmin()->get("/settings/recycle-bin/{$pageDeletion->id}/restore");
+        $pageRestoreView->assertSee('The parent of this item has also been deleted.');
+        $pageRestoreView->assertElementContains('a[href$="/settings/recycle-bin/' . $bookDeletion->id . '/restore"]', 'Restore Parent');
+    }
+}