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