5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Deletion;
8 use BookStack\Entities\Models\Page;
10 use Illuminate\Support\Collection;
11 use Illuminate\Support\Facades\DB;
14 class RecycleBinApiTest extends TestCase
18 protected string $baseEndpoint = '/api/recycle_bin';
20 protected array $endpointMap = [
21 ['get', '/api/recycle_bin'],
22 ['put', '/api/recycle_bin/1'],
23 ['delete', '/api/recycle_bin/1'],
26 public function test_settings_manage_permission_needed_for_all_endpoints()
28 $editor = $this->getEditor();
29 $this->giveUserPermissions($editor, ['settings-manage']);
30 $this->actingAs($editor);
32 foreach ($this->endpointMap as [$method, $uri]) {
33 $resp = $this->json($method, $uri);
34 $resp->assertStatus(403);
35 $resp->assertJson($this->permissionErrorResponse());
39 public function test_restrictions_manage_all_permission_neeed_for_all_endpoints()
41 $editor = $this->getEditor();
42 $this->giveUserPermissions($editor, ['restrictions-manage-all']);
43 $this->actingAs($editor);
45 foreach ($this->endpointMap as [$method, $uri]) {
46 $resp = $this->json($method, $uri);
47 $resp->assertStatus(403);
48 $resp->assertJson($this->permissionErrorResponse());
52 public function test_index_endpoint_returns_expected_page()
54 $this->actingAsAuthorizedUser();
56 $page = Page::query()->first();
57 $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
58 $editor = $this->getEditor();
59 $this->actingAs($editor)->delete($page->getUrl());
60 $this->actingAs($editor)->delete($book->getUrl());
62 $deletions = Deletion::query()->orderBy('id')->get();
64 $resp = $this->getJson($this->baseEndpoint);
66 $expectedData = $deletions
68 ->map(function (Collection $data) use ($editor) {
71 'deleted_by' => $editor->getKey(),
72 'created_at' => $data[0]->created_at->toJson(),
73 'updated_at' => $data[0]->updated_at->toJson(),
74 'deletable_type' => $data[1]->getMorphClass(),
75 'deletable_id' => $data[1]->getKey()
80 'data' => $expectedData->values()->all(),
85 public function test_restore_endpoint()
87 $this->actingAsAuthorizedUser();
89 $page = Page::query()->first();
90 $editor = $this->getEditor();
91 $this->actingAs($editor)->delete($page->getUrl());
94 $deletion = Deletion::query()->orderBy('id')->first();
96 $this->assertDatabaseHas('pages', [
97 'id' => $page->getKey(),
98 'deleted_at' => $page->deleted_at
101 $this->putJson($this->baseEndpoint . '/' . $deletion->getKey());
103 $this->assertDatabaseHas('pages', [
104 'id' => $page->getKey(),
109 public function test_destroy_endpoint()
111 $this->actingAsAuthorizedUser();
113 $page = Page::query()->first();
114 $editor = $this->getEditor();
115 $this->actingAs($editor)->delete($page->getUrl());
118 $deletion = Deletion::query()->orderBy('id')->first();
120 $this->assertDatabaseHas('pages', [
121 'id' => $page->getKey(),
122 'deleted_at' => $page->deleted_at
125 $this->deleteJson($this->baseEndpoint . '/' . $deletion->getKey());
126 $this->assertDatabaseMissing('pages', ['id' => $page->getKey()]);
129 private function actingAsAuthorizedUser()
131 $editor = $this->getEditor();
132 $this->giveUserPermissions($editor, ['restrictions-manage-all']);
133 $this->giveUserPermissions($editor, ['settings-manage']);
134 $this->actingAs($editor);