]> BookStack Code Mirror - bookstack/blob - tests/Api/RecycleBinApiTest.php
Updated version and assets for release v25.05.1
[bookstack] / tests / Api / RecycleBinApiTest.php
1 <?php
2
3 namespace Tests\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Deletion;
7 use Illuminate\Support\Collection;
8 use Tests\TestCase;
9
10 class RecycleBinApiTest extends TestCase
11 {
12     use TestsApi;
13
14     protected string $baseEndpoint = '/api/recycle-bin';
15
16     protected array $endpointMap = [
17         ['get', '/api/recycle-bin'],
18         ['put', '/api/recycle-bin/1'],
19         ['delete', '/api/recycle-bin/1'],
20     ];
21
22     public function test_settings_manage_permission_needed_for_all_endpoints()
23     {
24         $editor = $this->users->editor();
25         $this->permissions->grantUserRolePermissions($editor, ['settings-manage']);
26         $this->actingAs($editor);
27
28         foreach ($this->endpointMap as [$method, $uri]) {
29             $resp = $this->json($method, $uri);
30             $resp->assertStatus(403);
31             $resp->assertJson($this->permissionErrorResponse());
32         }
33     }
34
35     public function test_restrictions_manage_all_permission_needed_for_all_endpoints()
36     {
37         $editor = $this->users->editor();
38         $this->permissions->grantUserRolePermissions($editor, ['restrictions-manage-all']);
39         $this->actingAs($editor);
40
41         foreach ($this->endpointMap as [$method, $uri]) {
42             $resp = $this->json($method, $uri);
43             $resp->assertStatus(403);
44             $resp->assertJson($this->permissionErrorResponse());
45         }
46     }
47
48     public function test_index_endpoint_returns_expected_page()
49     {
50         $admin = $this->users->admin();
51
52         $page = $this->entities->page();
53         $book = $this->entities->book();
54         $this->actingAs($admin)->delete($page->getUrl());
55         $this->delete($book->getUrl());
56
57         $deletions = Deletion::query()->orderBy('id')->get();
58
59         $resp = $this->getJson($this->baseEndpoint);
60
61         $expectedData = $deletions
62             ->zip([$page, $book])
63             ->map(function (Collection $data) use ($admin) {
64                 return [
65                     'id'                => $data[0]->id,
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,
71                     'deletable'         => [
72                         'name' => $data[1]->name,
73                     ],
74                 ];
75             });
76
77         $resp->assertJson([
78             'data'  => $expectedData->values()->all(),
79             'total' => 2,
80         ]);
81     }
82
83     public function test_index_endpoint_returns_children_count()
84     {
85         $admin = $this->users->admin();
86
87         $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
88         $this->actingAs($admin)->delete($book->getUrl());
89
90         $deletion = Deletion::query()->orderBy('id')->first();
91
92         $resp = $this->getJson($this->baseEndpoint);
93
94         $expectedData = [
95             [
96                 'id'             => $deletion->id,
97                 'deletable'      => [
98                     'pages_count'    => $book->pages_count,
99                     'chapters_count' => $book->chapters_count,
100                 ],
101             ],
102         ];
103
104         $resp->assertJson([
105             'data'  => $expectedData,
106             'total' => 1,
107         ]);
108     }
109
110     public function test_index_endpoint_returns_parent()
111     {
112         $admin = $this->users->admin();
113         $page = $this->entities->pageWithinChapter();
114
115         $this->actingAs($admin)->delete($page->getUrl());
116         $deletion = Deletion::query()->orderBy('id')->first();
117
118         $resp = $this->getJson($this->baseEndpoint);
119
120         $expectedData = [
121             [
122                 'id'             => $deletion->id,
123                 'deletable'      => [
124                     'parent' => [
125                         'id'   => $page->chapter->id,
126                         'name' => $page->chapter->name,
127                         'type' => 'chapter',
128                     ],
129                 ],
130             ],
131         ];
132
133         $resp->assertJson([
134             'data'  => $expectedData,
135             'total' => 1,
136         ]);
137     }
138
139     public function test_restore_endpoint()
140     {
141         $page = $this->entities->page();
142         $this->asAdmin()->delete($page->getUrl());
143         $page->refresh();
144
145         $deletion = Deletion::query()->orderBy('id')->first();
146
147         $this->assertDatabaseHas('pages', [
148             'id'            => $page->id,
149             'deleted_at'    => $page->deleted_at,
150         ]);
151
152         $resp = $this->putJson($this->baseEndpoint . '/' . $deletion->id);
153         $resp->assertJson([
154             'restore_count' => 1,
155         ]);
156
157         $this->assertDatabaseHas('pages', [
158             'id'            => $page->id,
159             'deleted_at'    => null,
160         ]);
161     }
162
163     public function test_destroy_endpoint()
164     {
165         $page = $this->entities->page();
166         $this->asAdmin()->delete($page->getUrl());
167         $page->refresh();
168
169         $deletion = Deletion::query()->orderBy('id')->first();
170
171         $this->assertDatabaseHas('pages', [
172             'id'            => $page->id,
173             'deleted_at'    => $page->deleted_at,
174         ]);
175
176         $resp = $this->deleteJson($this->baseEndpoint . '/' . $deletion->id);
177         $resp->assertJson([
178             'delete_count' => 1,
179         ]);
180
181         $this->assertDatabaseMissing('pages', ['id' => $page->id]);
182     }
183 }