5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Deletion;
7 use BookStack\Entities\Models\Page;
8 use Illuminate\Support\Collection;
11 class RecycleBinApiTest extends TestCase
15 protected string $baseEndpoint = '/api/recycle-bin';
17 protected array $endpointMap = [
18 ['get', '/api/recycle-bin'],
19 ['put', '/api/recycle-bin/1'],
20 ['delete', '/api/recycle-bin/1'],
23 public function test_settings_manage_permission_needed_for_all_endpoints()
25 $editor = $this->getEditor();
26 $this->giveUserPermissions($editor, ['settings-manage']);
27 $this->actingAs($editor);
29 foreach ($this->endpointMap as [$method, $uri]) {
30 $resp = $this->json($method, $uri);
31 $resp->assertStatus(403);
32 $resp->assertJson($this->permissionErrorResponse());
36 public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
38 $editor = $this->getEditor();
39 $this->giveUserPermissions($editor, ['restrictions-manage-all']);
40 $this->actingAs($editor);
42 foreach ($this->endpointMap as [$method, $uri]) {
43 $resp = $this->json($method, $uri);
44 $resp->assertStatus(403);
45 $resp->assertJson($this->permissionErrorResponse());
49 public function test_index_endpoint_returns_expected_page()
51 $admin = $this->getAdmin();
53 $page = Page::query()->first();
54 $book = Book::query()->first();
55 $this->actingAs($admin)->delete($page->getUrl());
56 $this->delete($book->getUrl());
58 $deletions = Deletion::query()->orderBy('id')->get();
60 $resp = $this->getJson($this->baseEndpoint);
62 $expectedData = $deletions
64 ->map(function (Collection $data) use ($admin) {
67 'deleted_by' => $admin->id,
68 'created_at' => $data[0]->created_at->toJson(),
69 'updated_at' => $data[0]->updated_at->toJson(),
70 'deletable_type' => $data[1]->getMorphClass(),
71 'deletable_id' => $data[1]->id,
73 'name' => $data[1]->name,
79 'data' => $expectedData->values()->all(),
84 public function test_index_endpoint_returns_children_count()
86 $admin = $this->getAdmin();
88 $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
89 $this->actingAs($admin)->delete($book->getUrl());
91 $deletion = Deletion::query()->orderBy('id')->first();
93 $resp = $this->getJson($this->baseEndpoint);
97 'id' => $deletion->id,
99 'pages_count' => $book->pages_count,
100 'chapters_count' => $book->chapters_count,
106 'data' => $expectedData,
111 public function test_index_endpoint_returns_parent()
113 $admin = $this->getAdmin();
114 $page = Page::query()->whereHas('chapter')->with('chapter')->first();
116 $this->actingAs($admin)->delete($page->getUrl());
117 $deletion = Deletion::query()->orderBy('id')->first();
119 $resp = $this->getJson($this->baseEndpoint);
123 'id' => $deletion->id,
126 'id' => $page->chapter->id,
127 'name' => $page->chapter->name,
135 'data' => $expectedData,
140 public function test_restore_endpoint()
142 $page = Page::query()->first();
143 $this->asAdmin()->delete($page->getUrl());
146 $deletion = Deletion::query()->orderBy('id')->first();
148 $this->assertDatabaseHas('pages', [
150 'deleted_at' => $page->deleted_at,
153 $resp = $this->putJson($this->baseEndpoint . '/' . $deletion->id);
155 'restore_count' => 1,
158 $this->assertDatabaseHas('pages', [
160 'deleted_at' => null,
164 public function test_destroy_endpoint()
166 $page = Page::query()->first();
167 $this->asAdmin()->delete($page->getUrl());
170 $deletion = Deletion::query()->orderBy('id')->first();
172 $this->assertDatabaseHas('pages', [
174 'deleted_at' => $page->deleted_at,
177 $resp = $this->deleteJson($this->baseEndpoint . '/' . $deletion->id);
182 $this->assertDatabaseMissing('pages', ['id' => $page->id]);