]> BookStack Code Mirror - bookstack/blob - tests/RecycleBinTest.php
Merge branch 'master' of https://github.com/jasonhoule/BookStack into jasonhoule...
[bookstack] / tests / RecycleBinTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Bookshelf;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Deletion;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\Page;
9 use DB;
10 use Illuminate\Support\Carbon;
11
12 class RecycleBinTest extends TestCase
13 {
14     public function test_recycle_bin_routes_permissions()
15     {
16         $page = Page::query()->first();
17         $editor = $this->getEditor();
18         $this->actingAs($editor)->delete($page->getUrl());
19         $deletion = Deletion::query()->firstOrFail();
20
21         $routes = [
22             'GET:/settings/recycle-bin',
23             'POST:/settings/recycle-bin/empty',
24             "GET:/settings/recycle-bin/{$deletion->id}/destroy",
25             "GET:/settings/recycle-bin/{$deletion->id}/restore",
26             "POST:/settings/recycle-bin/{$deletion->id}/restore",
27             "DELETE:/settings/recycle-bin/{$deletion->id}",
28         ];
29
30         foreach($routes as $route) {
31             [$method, $url] = explode(':', $route);
32             $resp = $this->call($method, $url);
33             $this->assertPermissionError($resp);
34         }
35
36         $this->giveUserPermissions($editor, ['restrictions-manage-all']);
37
38         foreach($routes as $route) {
39             [$method, $url] = explode(':', $route);
40             $resp = $this->call($method, $url);
41             $this->assertPermissionError($resp);
42         }
43
44         $this->giveUserPermissions($editor, ['settings-manage']);
45
46         foreach($routes as $route) {
47             DB::beginTransaction();
48             [$method, $url] = explode(':', $route);
49             $resp = $this->call($method, $url);
50             $this->assertNotPermissionError($resp);
51             DB::rollBack();
52         }
53
54     }
55
56     public function test_recycle_bin_view()
57     {
58         $page = Page::query()->first();
59         $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first();
60         $editor = $this->getEditor();
61         $this->actingAs($editor)->delete($page->getUrl());
62         $this->actingAs($editor)->delete($book->getUrl());
63
64         $viewReq = $this->asAdmin()->get('/settings/recycle-bin');
65         $viewReq->assertElementContains('table.table', $page->name);
66         $viewReq->assertElementContains('table.table', $editor->name);
67         $viewReq->assertElementContains('table.table', $book->name);
68         $viewReq->assertElementContains('table.table', $book->pages_count . ' Pages');
69         $viewReq->assertElementContains('table.table', $book->chapters_count . ' Chapters');
70     }
71
72     public function test_recycle_bin_empty()
73     {
74         $page = Page::query()->first();
75         $book = Book::query()->where('id' , '!=', $page->book_id)->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
76         $editor = $this->getEditor();
77         $this->actingAs($editor)->delete($page->getUrl());
78         $this->actingAs($editor)->delete($book->getUrl());
79
80         $this->assertTrue(Deletion::query()->count() === 2);
81         $emptyReq = $this->asAdmin()->post('/settings/recycle-bin/empty');
82         $emptyReq->assertRedirect('/settings/recycle-bin');
83
84         $this->assertTrue(Deletion::query()->count() === 0);
85         $this->assertDatabaseMissing('books', ['id' => $book->id]);
86         $this->assertDatabaseMissing('pages', ['id' => $page->id]);
87         $this->assertDatabaseMissing('pages', ['id' => $book->pages->first()->id]);
88         $this->assertDatabaseMissing('chapters', ['id' => $book->chapters->first()->id]);
89
90         $itemCount = 2 + $book->pages->count() + $book->chapters->count();
91         $redirectReq = $this->get('/settings/recycle-bin');
92         $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
93     }
94
95     public function test_entity_restore()
96     {
97         $book = Book::query()->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
98         $this->asEditor()->delete($book->getUrl());
99         $deletion = Deletion::query()->firstOrFail();
100
101         $this->assertEquals($book->pages->count(), DB::table('pages')->where('book_id', '=', $book->id)->whereNotNull('deleted_at')->count());
102         $this->assertEquals($book->chapters->count(), DB::table('chapters')->where('book_id', '=', $book->id)->whereNotNull('deleted_at')->count());
103
104         $restoreReq = $this->asAdmin()->post("/settings/recycle-bin/{$deletion->id}/restore");
105         $restoreReq->assertRedirect('/settings/recycle-bin');
106         $this->assertTrue(Deletion::query()->count() === 0);
107
108         $this->assertEquals($book->pages->count(), DB::table('pages')->where('book_id', '=', $book->id)->whereNull('deleted_at')->count());
109         $this->assertEquals($book->chapters->count(), DB::table('chapters')->where('book_id', '=', $book->id)->whereNull('deleted_at')->count());
110
111         $itemCount = 1 + $book->pages->count() + $book->chapters->count();
112         $redirectReq = $this->get('/settings/recycle-bin');
113         $redirectReq->assertNotificationContains('Restored '.$itemCount.' total items from the recycle bin');
114     }
115
116     public function test_permanent_delete()
117     {
118         $book = Book::query()->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
119         $this->asEditor()->delete($book->getUrl());
120         $deletion = Deletion::query()->firstOrFail();
121
122         $deleteReq = $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
123         $deleteReq->assertRedirect('/settings/recycle-bin');
124         $this->assertTrue(Deletion::query()->count() === 0);
125
126         $this->assertDatabaseMissing('books', ['id' => $book->id]);
127         $this->assertDatabaseMissing('pages', ['id' => $book->pages->first()->id]);
128         $this->assertDatabaseMissing('chapters', ['id' => $book->chapters->first()->id]);
129
130         $itemCount = 1 + $book->pages->count() + $book->chapters->count();
131         $redirectReq = $this->get('/settings/recycle-bin');
132         $redirectReq->assertNotificationContains('Deleted '.$itemCount.' total items from the recycle bin');
133     }
134
135     public function test_permanent_delete_for_each_type()
136     {
137         /** @var Entity $entity */
138         foreach ([new Bookshelf, new Book, new Chapter, new Page] as $entity) {
139             $entity = $entity->newQuery()->first();
140             $this->asEditor()->delete($entity->getUrl());
141             $deletion = Deletion::query()->orderBy('id', 'desc')->firstOrFail();
142
143             $deleteReq = $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
144             $deleteReq->assertRedirect('/settings/recycle-bin');
145             $this->assertDatabaseMissing('deletions', ['id' => $deletion->id]);
146             $this->assertDatabaseMissing($entity->getTable(), ['id' => $entity->id]);
147         }
148     }
149
150     public function test_permanent_entity_delete_updates_existing_activity_with_entity_name()
151     {
152         $page = Page::query()->firstOrFail();
153         $this->asEditor()->delete($page->getUrl());
154         $deletion = $page->deletions()->firstOrFail();
155
156         $this->assertDatabaseHas('activities', [
157             'type' => 'page_delete',
158             'entity_id' => $page->id,
159             'entity_type' => $page->getMorphClass(),
160         ]);
161
162         $this->asAdmin()->delete("/settings/recycle-bin/{$deletion->id}");
163
164         $this->assertDatabaseMissing('activities', [
165             'type' => 'page_delete',
166             'entity_id' => $page->id,
167             'entity_type' => $page->getMorphClass(),
168         ]);
169
170         $this->assertDatabaseHas('activities', [
171             'type' => 'page_delete',
172             'entity_id' => null,
173             'entity_type' => null,
174             'detail' => $page->name,
175         ]);
176     }
177
178     public function test_auto_clear_functionality_works()
179     {
180         config()->set('app.recycle_bin_lifetime', 5);
181         $page = Page::query()->firstOrFail();
182         $otherPage = Page::query()->where('id', '!=', $page->id)->firstOrFail();
183
184         $this->asEditor()->delete($page->getUrl());
185         $this->assertDatabaseHas('pages', ['id' => $page->id]);
186         $this->assertEquals(1, Deletion::query()->count());
187
188         Carbon::setTestNow(Carbon::now()->addDays(6));
189         $this->asEditor()->delete($otherPage->getUrl());
190         $this->assertEquals(1, Deletion::query()->count());
191
192         $this->assertDatabaseMissing('pages', ['id' => $page->id]);
193     }
194
195     public function test_auto_clear_functionality_with_negative_time_keeps_forever()
196     {
197         config()->set('app.recycle_bin_lifetime', -1);
198         $page = Page::query()->firstOrFail();
199         $otherPage = Page::query()->where('id', '!=', $page->id)->firstOrFail();
200
201         $this->asEditor()->delete($page->getUrl());
202         $this->assertEquals(1, Deletion::query()->count());
203
204         Carbon::setTestNow(Carbon::now()->addDays(6000));
205         $this->asEditor()->delete($otherPage->getUrl());
206         $this->assertEquals(2, Deletion::query()->count());
207
208         $this->assertDatabaseHas('pages', ['id' => $page->id]);
209     }
210
211     public function test_auto_clear_functionality_with_zero_time_deletes_instantly()
212     {
213         config()->set('app.recycle_bin_lifetime', 0);
214         $page = Page::query()->firstOrFail();
215
216         $this->asEditor()->delete($page->getUrl());
217         $this->assertDatabaseMissing('pages', ['id' => $page->id]);
218         $this->assertEquals(0, Deletion::query()->count());
219     }
220
221     public function test_restore_flow_when_restoring_nested_delete_first()
222     {
223         $book = Book::query()->whereHas('pages')->whereHas('chapters')->with(['pages', 'chapters'])->firstOrFail();
224         $chapter = $book->chapters->first();
225         $this->asEditor()->delete($chapter->getUrl());
226         $this->asEditor()->delete($book->getUrl());
227
228         $bookDeletion = $book->deletions()->first();
229         $chapterDeletion = $chapter->deletions()->first();
230
231         $chapterRestoreView = $this->asAdmin()->get("/settings/recycle-bin/{$chapterDeletion->id}/restore");
232         $chapterRestoreView->assertStatus(200);
233         $chapterRestoreView->assertSeeText($chapter->name);
234
235         $chapterRestore = $this->post("/settings/recycle-bin/{$chapterDeletion->id}/restore");
236         $chapterRestore->assertRedirect("/settings/recycle-bin");
237         $this->assertDatabaseMissing("deletions", ["id" => $chapterDeletion->id]);
238
239         $chapter->refresh();
240         $this->assertNotNull($chapter->deleted_at);
241
242         $bookRestoreView = $this->asAdmin()->get("/settings/recycle-bin/{$bookDeletion->id}/restore");
243         $bookRestoreView->assertStatus(200);
244         $bookRestoreView->assertSeeText($chapter->name);
245
246         $this->post("/settings/recycle-bin/{$bookDeletion->id}/restore");
247         $chapter->refresh();
248         $this->assertNull($chapter->deleted_at);
249     }
250 }