]> BookStack Code Mirror - bookstack/blobdiff - tests/Api/RecycleBinApiTest.php
Added language list favourites sorting, updated styles
[bookstack] / tests / Api / RecycleBinApiTest.php
index 4849080b9489902bd90bb8cb3ce691ae92b79570..83cd82480310a137c5e7313c2eab41f6320ca6d2 100644 (file)
@@ -12,12 +12,12 @@ class RecycleBinApiTest extends TestCase
 {
     use TestsApi;
 
-    protected string $baseEndpoint = '/api/recycle_bin';
+    protected string $baseEndpoint = '/api/recycle-bin';
 
     protected array $endpointMap = [
-        ['get', '/api/recycle_bin'],
-        ['put', '/api/recycle_bin/1'],
-        ['delete', '/api/recycle_bin/1'],
+        ['get', '/api/recycle-bin'],
+        ['put', '/api/recycle-bin/1'],
+        ['delete', '/api/recycle-bin/1'],
     ];
 
     public function test_settings_manage_permission_needed_for_all_endpoints()
@@ -48,13 +48,12 @@ class RecycleBinApiTest extends TestCase
 
     public function test_index_endpoint_returns_expected_page()
     {
-        $this->actingAsAuthorizedUser();
+        $admin = $this->getAdmin();
 
         $page = Page::query()->first();
-        $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
-        $editor = $this->getEditor();
-        $this->actingAs($editor)->delete($page->getUrl());
-        $this->actingAs($editor)->delete($book->getUrl());
+        $book = Book::query()->first();
+        $this->actingAs($admin)->delete($page->getUrl());
+        $this->delete($book->getUrl());
 
         $deletions = Deletion::query()->orderBy('id')->get();
 
@@ -62,14 +61,17 @@ class RecycleBinApiTest extends TestCase
 
         $expectedData = $deletions
             ->zip([$page, $book])
-            ->map(function (Collection $data) use ($editor) {
+            ->map(function (Collection $data) use ($admin) {
                 return [
                     'id'                => $data[0]->id,
-                    'deleted_by'        => $editor->getKey(),
+                    'deleted_by'        => $admin->id,
                     '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]->id,
+                    'deletable'         => [
+                        'name' => $data[1]->name,
+                    ],
                 ];
             });
 
@@ -79,13 +81,12 @@ class RecycleBinApiTest extends TestCase
         ]);
     }
 
-    public function test_index_endpoint_returns_children()
+    public function test_index_endpoint_returns_children_count()
     {
-        $this->actingAsAuthorizedUser();
+        $admin = $this->getAdmin();
 
         $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
-        $editor = $this->getEditor();
-        $this->actingAs($editor)->delete($book->getUrl());
+        $this->actingAs($admin)->delete($book->getUrl());
 
         $deletion = Deletion::query()->orderBy('id')->first();
 
@@ -93,17 +94,11 @@ class RecycleBinApiTest extends TestCase
 
         $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,
+                'id'             => $deletion->id,
+                'deletable'      => [
+                    'pages_count'    => $book->pages_count,
+                    'chapters_count' => $book->chapters_count,
                 ],
-                'parent' => null,
             ],
         ];
 
@@ -115,30 +110,24 @@ class RecycleBinApiTest extends TestCase
 
     public function test_index_endpoint_returns_parent()
     {
-        $this->actingAsAuthorizedUser();
-
+        $admin = $this->getAdmin();
         $page = Page::query()->whereHas('chapter')->with('chapter')->first();
 
-        $editor = $this->getEditor();
-        $this->actingAs($editor)->delete($page->getUrl());
-
+        $this->actingAs($admin)->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(),
+                'id'             => $deletion->id,
+                'deletable'      => [
+                    'parent' => [
+                        'id'   => $page->chapter->id,
+                        'name' => $page->chapter->name,
+                        'type' => 'chapter',
+                    ],
                 ],
-                'children' => null,
             ],
         ];
 
@@ -150,53 +139,46 @@ class RecycleBinApiTest extends TestCase
 
     public function test_restore_endpoint()
     {
-        $this->actingAsAuthorizedUser();
-
         $page = Page::query()->first();
-        $editor = $this->getEditor();
-        $this->actingAs($editor)->delete($page->getUrl());
+        $this->asAdmin()->delete($page->getUrl());
         $page->refresh();
 
         $deletion = Deletion::query()->orderBy('id')->first();
 
         $this->assertDatabaseHas('pages', [
-            'id'            => $page->getKey(),
+            'id'            => $page->id,
             'deleted_at'    => $page->deleted_at,
         ]);
 
-        $this->putJson($this->baseEndpoint . '/' . $deletion->getKey());
+        $resp = $this->putJson($this->baseEndpoint . '/' . $deletion->id);
+        $resp->assertJson([
+            'restore_count' => 1,
+        ]);
 
         $this->assertDatabaseHas('pages', [
-            'id'            => $page->getKey(),
+            'id'            => $page->id,
             'deleted_at'    => null,
         ]);
     }
 
     public function test_destroy_endpoint()
     {
-        $this->actingAsAuthorizedUser();
-
         $page = Page::query()->first();
-        $editor = $this->getEditor();
-        $this->actingAs($editor)->delete($page->getUrl());
+        $this->asAdmin()->delete($page->getUrl());
         $page->refresh();
 
         $deletion = Deletion::query()->orderBy('id')->first();
 
         $this->assertDatabaseHas('pages', [
-            'id'            => $page->getKey(),
+            'id'            => $page->id,
             'deleted_at'    => $page->deleted_at,
         ]);
 
-        $this->deleteJson($this->baseEndpoint . '/' . $deletion->getKey());
-        $this->assertDatabaseMissing('pages', ['id' => $page->getKey()]);
-    }
+        $resp = $this->deleteJson($this->baseEndpoint . '/' . $deletion->id);
+        $resp->assertJson([
+            'delete_count' => 1,
+        ]);
 
-    private function actingAsAuthorizedUser()
-    {
-        $editor = $this->getEditor();
-        $this->giveUserPermissions($editor, ['restrictions-manage-all']);
-        $this->giveUserPermissions($editor, ['settings-manage']);
-        $this->actingAs($editor);
+        $this->assertDatabaseMissing('pages', ['id' => $page->id]);
     }
 }