5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Deletion;
7 use Illuminate\Support\Collection;
10 class RecycleBinApiTest extends TestCase
14 protected string $baseEndpoint = '/api/recycle-bin';
16 protected array $endpointMap = [
17 ['get', '/api/recycle-bin'],
18 ['put', '/api/recycle-bin/1'],
19 ['delete', '/api/recycle-bin/1'],
22 public function test_settings_manage_permission_needed_for_all_endpoints()
24 $editor = $this->users->editor();
25 $this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
26 $this->actingAs($editor);
28 foreach ($this->endpointMap as [$method, $uri]) {
29 $resp = $this->json($method, $uri);
30 $resp->assertStatus(403);
31 $resp->assertJson($this->permissionErrorResponse());
35 public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
37 $editor = $this->users->editor();
38 $this->permissions->grantUserRolePermissions($editor, ['restrictions-manage-all']);
39 $this->actingAs($editor);
41 foreach ($this->endpointMap as [$method, $uri]) {
42 $resp = $this->json($method, $uri);
43 $resp->assertStatus(403);
44 $resp->assertJson($this->permissionErrorResponse());
48 public function test_index_endpoint_returns_expected_page()
50 $admin = $this->users->admin();
52 $page = $this->entities->page();
53 $book = $this->entities->book();
54 $this->actingAs($admin)->delete($page->getUrl());
55 $this->delete($book->getUrl());
57 $deletions = Deletion::query()->orderBy('id')->get();
59 $resp = $this->getJson($this->baseEndpoint);
61 $expectedData = $deletions
63 ->map(function (Collection $data) use ($admin) {
66 'deleted_by' => $admin->id,
67 'created_at' => $data[0]->created_at->toJson(),
68 'updated_at' => $data[0]->updated_at->toJson(),
69 'deletable_type' => $data[1]->getMorphClass(),
70 'deletable_id' => $data[1]->id,
72 'name' => $data[1]->name,
78 'data' => $expectedData->values()->all(),
83 public function test_index_endpoint_returns_children_count()
85 $admin = $this->users->admin();
87 $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
88 $this->actingAs($admin)->delete($book->getUrl());
90 $deletion = Deletion::query()->orderBy('id')->first();
92 $resp = $this->getJson($this->baseEndpoint);
96 'id' => $deletion->id,
98 'pages_count' => $book->pages_count,
99 'chapters_count' => $book->chapters_count,
105 'data' => $expectedData,
110 public function test_index_endpoint_returns_parent()
112 $admin = $this->users->admin();
113 $page = $this->entities->pageWithinChapter();
115 $this->actingAs($admin)->delete($page->getUrl());
116 $deletion = Deletion::query()->orderBy('id')->first();
118 $resp = $this->getJson($this->baseEndpoint);
122 'id' => $deletion->id,
125 'id' => $page->chapter->id,
126 'name' => $page->chapter->name,
134 'data' => $expectedData,
139 public function test_restore_endpoint()
141 $page = $this->entities->page();
142 $this->asAdmin()->delete($page->getUrl());
145 $deletion = Deletion::query()->orderBy('id')->first();
147 $this->assertDatabaseHas('pages', [
149 'deleted_at' => $page->deleted_at,
152 $resp = $this->putJson($this->baseEndpoint . '/' . $deletion->id);
154 'restore_count' => 1,
157 $this->assertDatabaseHas('pages', [
159 'deleted_at' => null,
163 public function test_destroy_endpoint()
165 $page = $this->entities->page();
166 $this->asAdmin()->delete($page->getUrl());
169 $deletion = Deletion::query()->orderBy('id')->first();
171 $this->assertDatabaseHas('pages', [
173 'deleted_at' => $page->deleted_at,
176 $resp = $this->deleteJson($this->baseEndpoint . '/' . $deletion->id);
181 $this->assertDatabaseMissing('pages', ['id' => $page->id]);