]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageTest.php
Images: Added nulling of image page relation on page delete
[bookstack] / tests / Entity / PageTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Uploads\Image;
8 use Carbon\Carbon;
9 use Tests\TestCase;
10
11 class PageTest extends TestCase
12 {
13     public function test_create()
14     {
15         $chapter = $this->entities->chapter();
16         $page = Page::factory()->make([
17             'name' => 'My First Page',
18         ]);
19
20         $resp = $this->asEditor()->get($chapter->getUrl());
21         $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page');
22
23         $resp = $this->get($chapter->getUrl('/create-page'));
24         /** @var Page $draftPage */
25         $draftPage = Page::query()
26             ->where('draft', '=', true)
27             ->orderBy('created_at', 'desc')
28             ->first();
29         $resp->assertRedirect($draftPage->getUrl());
30
31         $resp = $this->get($draftPage->getUrl());
32         $this->withHtml($resp)->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page');
33
34         $resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html'));
35         $draftPage->refresh();
36         $resp->assertRedirect($draftPage->getUrl());
37     }
38
39     public function test_page_view_when_creator_is_deleted_but_owner_exists()
40     {
41         $page = $this->entities->page();
42         $user = $this->users->viewer();
43         $owner = $this->users->editor();
44         $page->created_by = $user->id;
45         $page->owned_by = $owner->id;
46         $page->save();
47         $user->delete();
48
49         $resp = $this->asAdmin()->get($page->getUrl());
50         $resp->assertStatus(200);
51         $resp->assertSeeText('Owned by ' . $owner->name);
52     }
53
54     public function test_page_show_includes_pointer_section_select_mode_button()
55     {
56         $page = $this->entities->page();
57         $resp = $this->asEditor()->get($page->getUrl());
58         $this->withHtml($resp)->assertElementContains('.content-wrap button.screen-reader-only', 'Enter section select mode');
59     }
60
61     public function test_page_creation_with_markdown_content()
62     {
63         $this->setSettings(['app-editor' => 'markdown']);
64         $book = $this->entities->book();
65
66         $this->asEditor()->get($book->getUrl('/create-page'));
67         $draft = Page::query()->where('book_id', '=', $book->id)
68             ->where('draft', '=', true)->first();
69
70         $details = [
71             'markdown' => '# a title',
72             'html'     => '<h1>a title</h1>',
73             'name'     => 'my page',
74         ];
75         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
76         $resp->assertRedirect();
77
78         $this->assertDatabaseHasEntityData('page', [
79             'markdown' => $details['markdown'],
80             'name'     => $details['name'],
81             'id'       => $draft->id,
82             'draft'    => false,
83         ]);
84
85         $draft->refresh();
86         $resp = $this->get($draft->getUrl('/edit'));
87         $resp->assertSee('# a title');
88     }
89
90     public function test_page_creation_allows_summary_to_be_set()
91     {
92         $book = $this->entities->book();
93
94         $this->asEditor()->get($book->getUrl('/create-page'));
95         $draft = Page::query()->where('book_id', '=', $book->id)
96             ->where('draft', '=', true)->first();
97
98         $details = [
99             'html'    => '<h1>a title</h1>',
100             'name'    => 'My page with summary',
101             'summary' => 'Here is my changelog message for a new page!',
102         ];
103         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
104         $resp->assertRedirect();
105
106         $this->assertDatabaseHas('page_revisions', [
107             'page_id' => $draft->id,
108             'summary' => 'Here is my changelog message for a new page!',
109         ]);
110
111         $draft->refresh();
112         $resp = $this->get($draft->getUrl('/revisions'));
113         $resp->assertSee('Here is my changelog message for a new page!');
114     }
115
116     public function test_page_delete()
117     {
118         $page = $this->entities->page();
119         $this->assertNull($page->deleted_at);
120
121         $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
122         $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
123
124         $deleteReq = $this->delete($page->getUrl());
125         $deleteReq->assertRedirect($page->getParent()->getUrl());
126         $this->assertActivityExists('page_delete', $page);
127
128         $page->refresh();
129         $this->assertNotNull($page->deleted_at);
130         $this->assertTrue($page->deletions()->count() === 1);
131
132         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
133         $this->assertNotificationContains($redirectReq, 'Page Successfully Deleted');
134     }
135
136     public function test_page_full_delete_removes_all_revisions()
137     {
138         $page = $this->entities->page();
139         $page->revisions()->create([
140             'html' => '<p>ducks</p>',
141             'name' => 'my page revision',
142             'type' => 'draft',
143         ]);
144         $page->revisions()->create([
145             'html' => '<p>ducks</p>',
146             'name' => 'my page revision',
147             'type' => 'revision',
148         ]);
149
150         $this->assertDatabaseHas('page_revisions', [
151             'page_id' => $page->id,
152         ]);
153
154         $this->asEditor()->delete($page->getUrl());
155         $this->asAdmin()->post('/settings/recycle-bin/empty');
156
157         $this->assertDatabaseMissing('page_revisions', [
158             'page_id' => $page->id,
159         ]);
160     }
161
162     public function test_page_full_delete_nulls_related_images()
163     {
164         $page = $this->entities->page();
165         $image = Image::factory()->create(['type' => 'gallery', 'uploaded_to' => $page->id]);
166
167         $this->asEditor()->delete($page->getUrl());
168         $this->asAdmin()->post('/settings/recycle-bin/empty');
169
170         $this->assertDatabaseMissing('images', [
171             'type' => 'gallery',
172             'uploaded_to' => $page->id,
173         ]);
174
175         $this->assertDatabaseHas('images', [
176             'id' => $image->id,
177             'uploaded_to' => null,
178         ]);
179     }
180
181     public function test_page_copy()
182     {
183         $page = $this->entities->page();
184         $page->html = '<p>This is some test content</p>';
185         $page->save();
186
187         $currentBook = $page->book;
188         $newBook = Book::where('id', '!=', $currentBook->id)->first();
189
190         $resp = $this->asEditor()->get($page->getUrl('/copy'));
191         $resp->assertSee('Copy Page');
192
193         $movePageResp = $this->post($page->getUrl('/copy'), [
194             'entity_selection' => 'book:' . $newBook->id,
195             'name'             => 'My copied test page',
196         ]);
197         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
198
199         $movePageResp->assertRedirect($pageCopy->getUrl());
200         $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
201         $this->assertStringContainsString('This is some test content', $pageCopy->html);
202     }
203
204     public function test_page_copy_with_markdown_has_both_html_and_markdown()
205     {
206         $page = $this->entities->page();
207         $page->html = '<h1>This is some test content</h1>';
208         $page->markdown = '# This is some test content';
209         $page->save();
210         $newBook = Book::where('id', '!=', $page->book->id)->first();
211
212         $this->asEditor()->post($page->getUrl('/copy'), [
213             'entity_selection' => 'book:' . $newBook->id,
214             'name'             => 'My copied test page',
215         ]);
216         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
217
218         $this->assertStringContainsString('This is some test content', $pageCopy->html);
219         $this->assertEquals('# This is some test content', $pageCopy->markdown);
220     }
221
222     public function test_page_copy_with_no_destination()
223     {
224         $page = $this->entities->page();
225         $currentBook = $page->book;
226
227         $resp = $this->asEditor()->get($page->getUrl('/copy'));
228         $resp->assertSee('Copy Page');
229
230         $movePageResp = $this->post($page->getUrl('/copy'), [
231             'name' => 'My copied test page',
232         ]);
233
234         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
235
236         $movePageResp->assertRedirect($pageCopy->getUrl());
237         $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
238         $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
239     }
240
241     public function test_page_can_be_copied_without_edit_permission()
242     {
243         $page = $this->entities->page();
244         $currentBook = $page->book;
245         $newBook = Book::where('id', '!=', $currentBook->id)->first();
246         $viewer = $this->users->viewer();
247
248         $resp = $this->actingAs($viewer)->get($page->getUrl());
249         $resp->assertDontSee($page->getUrl('/copy'));
250
251         $newBook->owned_by = $viewer->id;
252         $newBook->save();
253         $this->permissions->grantUserRolePermissions($viewer, ['page-create-own']);
254         $this->permissions->regenerateForEntity($newBook);
255
256         $resp = $this->actingAs($viewer)->get($page->getUrl());
257         $resp->assertSee($page->getUrl('/copy'));
258
259         $movePageResp = $this->post($page->getUrl('/copy'), [
260             'entity_selection' => 'book:' . $newBook->id,
261             'name'             => 'My copied test page',
262         ]);
263         $movePageResp->assertRedirect();
264
265         $this->assertDatabaseHasEntityData('page', [
266             'name'       => 'My copied test page',
267             'created_by' => $viewer->id,
268             'book_id'    => $newBook->id,
269         ]);
270     }
271
272     public function test_old_page_slugs_redirect_to_new_pages()
273     {
274         $page = $this->entities->page();
275
276         // Need to save twice since revisions are not generated in seeder.
277         $this->asAdmin()->put($page->getUrl(), [
278             'name' => 'super test',
279             'html' => '<p></p>',
280         ]);
281
282         $page->refresh();
283         $pageUrl = $page->getUrl();
284
285         $this->put($pageUrl, [
286             'name' => 'super test page',
287             'html' => '<p></p>',
288         ]);
289
290         $this->get($pageUrl)
291             ->assertRedirect("/books/{$page->book->slug}/page/super-test-page");
292     }
293
294     public function test_page_within_chapter_deletion_returns_to_chapter()
295     {
296         $chapter = $this->entities->chapter();
297         $page = $chapter->pages()->first();
298
299         $this->asEditor()->delete($page->getUrl())
300             ->assertRedirect($chapter->getUrl());
301     }
302
303     public function test_recently_updated_pages_view()
304     {
305         $user = $this->users->editor();
306         $content = $this->entities->createChainBelongingToUser($user);
307
308         $resp = $this->asAdmin()->get('/pages/recently-updated');
309         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
310     }
311
312     public function test_recently_updated_pages_view_shows_updated_by_details()
313     {
314         $user = $this->users->editor();
315         $page = $this->entities->page();
316
317         $this->actingAs($user)->put($page->getUrl(), [
318             'name' => 'Updated title',
319             'html' => '<p>Updated content</p>',
320         ]);
321
322         $resp = $this->asAdmin()->get('/pages/recently-updated');
323         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1) small', 'by ' . $user->name);
324     }
325
326     public function test_recently_updated_pages_view_shows_parent_chain()
327     {
328         $user = $this->users->editor();
329         $page = $this->entities->pageWithinChapter();
330
331         $this->actingAs($user)->put($page->getUrl(), [
332             'name' => 'Updated title',
333             'html' => '<p>Updated content</p>',
334         ]);
335
336         $resp = $this->asAdmin()->get('/pages/recently-updated');
337         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->chapter->getShortName(42));
338         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->book->getShortName(42));
339     }
340
341     public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible()
342     {
343         $user = $this->users->editor();
344         $page = $this->entities->pageWithinChapter();
345
346         $this->actingAs($user)->put($page->getUrl(), [
347             'name' => 'Updated title',
348             'html' => '<p>Updated content</p>',
349         ]);
350
351         $this->permissions->setEntityPermissions($page->book);
352         $this->permissions->setEntityPermissions($page, ['view'], [$user->roles->first()]);
353
354         $resp = $this->get('/pages/recently-updated');
355         $resp->assertDontSee($page->book->getShortName(42));
356         $resp->assertDontSee($page->chapter->getShortName(42));
357         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated title');
358     }
359
360     public function test_recently_updated_pages_on_home()
361     {
362         /** @var Page $page */
363         $page = Page::query()->orderBy('updated_at', 'asc')->first();
364         Page::query()->where('id', '!=', $page->id)->update([
365             'updated_at' => Carbon::now()->subSecond(1),
366         ]);
367
368         $resp = $this->asAdmin()->get('/');
369         $this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name);
370
371         $this->put($page->getUrl(), [
372             'name' => $page->name,
373             'html' => $page->html,
374         ]);
375
376         $resp = $this->get('/');
377         $this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
378     }
379
380     public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
381     {
382         $page = $this->entities->page();
383
384         $resp = $this->asViewer()->get($page->getUrl('/edit'));
385         $resp->assertRedirect($page->getUrl());
386
387         $resp->assertSessionHas('error', 'You do not have permission to access the requested page.');
388     }
389 }