]> BookStack Code Mirror - bookstack/blobdiff - tests/Api/RecycleBinApiTest.php
do some cleanup and add doc
[bookstack] / tests / Api / RecycleBinApiTest.php
index 9371e06e81841131a97001cdd34e605e7af1afb1..4849080b9489902bd90bb8cb3ce691ae92b79570 100644 (file)
@@ -3,12 +3,9 @@
 namespace Tests\Api;
 
 use BookStack\Entities\Models\Book;
-use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Deletion;
 use BookStack\Entities\Models\Page;
-use Carbon\Carbon;
 use Illuminate\Support\Collection;
-use Illuminate\Support\Facades\DB;
 use Tests\TestCase;
 
 class RecycleBinApiTest extends TestCase
@@ -36,12 +33,12 @@ class RecycleBinApiTest extends TestCase
         }
     }
 
-    public function test_restrictions_manage_all_permission_neeed_for_all_endpoints()
+    public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
     {
         $editor = $this->getEditor();
         $this->giveUserPermissions($editor, ['restrictions-manage-all']);
         $this->actingAs($editor);
-        
+
         foreach ($this->endpointMap as [$method, $uri]) {
             $resp = $this->json($method, $uri);
             $resp->assertStatus(403);
@@ -52,7 +49,7 @@ class RecycleBinApiTest extends TestCase
     public function test_index_endpoint_returns_expected_page()
     {
         $this->actingAsAuthorizedUser();
-        
+
         $page = Page::query()->first();
         $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
         $editor = $this->getEditor();
@@ -72,20 +69,89 @@ class RecycleBinApiTest extends TestCase
                     'created_at'        => $data[0]->created_at->toJson(),
                     'updated_at'        => $data[0]->updated_at->toJson(),
                     'deletable_type'    => $data[1]->getMorphClass(),
-                    'deletable_id'      => $data[1]->getKey()
+                    'deletable_id'      => $data[1]->getKey(),
                 ];
             });
 
         $resp->assertJson([
-            'data' => $expectedData->values()->all(), 
-            'total' => 2
+            'data'  => $expectedData->values()->all(),
+            'total' => 2,
+        ]);
+    }
+
+    public function test_index_endpoint_returns_children()
+    {
+        $this->actingAsAuthorizedUser();
+
+        $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
+        $editor = $this->getEditor();
+        $this->actingAs($editor)->delete($book->getUrl());
+
+        $deletion = Deletion::query()->orderBy('id')->first();
+
+        $resp = $this->getJson($this->baseEndpoint);
+
+        $expectedData = [
+            [
+                'id'                => $deletion->getKey(),
+                'deleted_by'        => $editor->getKey(),
+                'created_at'        => $deletion->created_at->toJson(),
+                'updated_at'        => $deletion->updated_at->toJson(),
+                'deletable_type'    => $book->getMorphClass(),
+                'deletable_id'      => $book->getKey(),
+                'children'          => [
+                    'BookStack\Page'    => $book->pages_count,
+                    'BookStack\Chapter' => $book->chapters_count,
+                ],
+                'parent' => null,
+            ],
+        ];
+
+        $resp->assertJson([
+            'data'  => $expectedData,
+            'total' => 1,
+        ]);
+    }
+
+    public function test_index_endpoint_returns_parent()
+    {
+        $this->actingAsAuthorizedUser();
+
+        $page = Page::query()->whereHas('chapter')->with('chapter')->first();
+
+        $editor = $this->getEditor();
+        $this->actingAs($editor)->delete($page->getUrl());
+
+        $deletion = Deletion::query()->orderBy('id')->first();
+
+        $resp = $this->getJson($this->baseEndpoint);
+
+        $expectedData = [
+            [
+                'id'                => $deletion->getKey(),
+                'deleted_by'        => $editor->getKey(),
+                'created_at'        => $deletion->created_at->toJson(),
+                'updated_at'        => $deletion->updated_at->toJson(),
+                'deletable_type'    => $page->getMorphClass(),
+                'deletable_id'      => $page->getKey(),
+                'parent'            => [
+                    'type'  => 'BookStack\Chapter',
+                    'id'    => $page->chapter->getKey(),
+                ],
+                'children' => null,
+            ],
+        ];
+
+        $resp->assertJson([
+            'data'  => $expectedData,
+            'total' => 1,
         ]);
     }
 
     public function test_restore_endpoint()
     {
         $this->actingAsAuthorizedUser();
-        
+
         $page = Page::query()->first();
         $editor = $this->getEditor();
         $this->actingAs($editor)->delete($page->getUrl());
@@ -94,22 +160,22 @@ class RecycleBinApiTest extends TestCase
         $deletion = Deletion::query()->orderBy('id')->first();
 
         $this->assertDatabaseHas('pages', [
-            'id' => $page->getKey(),
-            'deleted_at' => $page->deleted_at
+            'id'            => $page->getKey(),
+            'deleted_at'    => $page->deleted_at,
         ]);
 
         $this->putJson($this->baseEndpoint . '/' . $deletion->getKey());
 
         $this->assertDatabaseHas('pages', [
-            'id' => $page->getKey(),
-            'deleted_at' => null
+            'id'            => $page->getKey(),
+            'deleted_at'    => null,
         ]);
     }
 
     public function test_destroy_endpoint()
     {
         $this->actingAsAuthorizedUser();
-        
+
         $page = Page::query()->first();
         $editor = $this->getEditor();
         $this->actingAs($editor)->delete($page->getUrl());
@@ -118,8 +184,8 @@ class RecycleBinApiTest extends TestCase
         $deletion = Deletion::query()->orderBy('id')->first();
 
         $this->assertDatabaseHas('pages', [
-            'id' => $page->getKey(),
-            'deleted_at' => $page->deleted_at
+            'id'            => $page->getKey(),
+            'deleted_at'    => $page->deleted_at,
         ]);
 
         $this->deleteJson($this->baseEndpoint . '/' . $deletion->getKey());