]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageTest.php
Merge pull request #5917 from BookStackApp/copy_references
[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_within_chapter_deletion_returns_to_chapter()
182     {
183         $chapter = $this->entities->chapter();
184         $page = $chapter->pages()->first();
185
186         $this->asEditor()->delete($page->getUrl())
187             ->assertRedirect($chapter->getUrl());
188     }
189
190     public function test_recently_updated_pages_view()
191     {
192         $user = $this->users->editor();
193         $content = $this->entities->createChainBelongingToUser($user);
194
195         $resp = $this->asAdmin()->get('/pages/recently-updated');
196         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
197     }
198
199     public function test_recently_updated_pages_view_shows_updated_by_details()
200     {
201         $user = $this->users->editor();
202         $page = $this->entities->page();
203
204         $this->actingAs($user)->put($page->getUrl(), [
205             'name' => 'Updated title',
206             'html' => '<p>Updated content</p>',
207         ]);
208
209         $resp = $this->asAdmin()->get('/pages/recently-updated');
210         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1) small', 'by ' . $user->name);
211     }
212
213     public function test_recently_updated_pages_view_shows_parent_chain()
214     {
215         $user = $this->users->editor();
216         $page = $this->entities->pageWithinChapter();
217
218         $this->actingAs($user)->put($page->getUrl(), [
219             'name' => 'Updated title',
220             'html' => '<p>Updated content</p>',
221         ]);
222
223         $resp = $this->asAdmin()->get('/pages/recently-updated');
224         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->chapter->getShortName(42));
225         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->book->getShortName(42));
226     }
227
228     public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible()
229     {
230         $user = $this->users->editor();
231         $page = $this->entities->pageWithinChapter();
232
233         $this->actingAs($user)->put($page->getUrl(), [
234             'name' => 'Updated title',
235             'html' => '<p>Updated content</p>',
236         ]);
237
238         $this->permissions->setEntityPermissions($page->book);
239         $this->permissions->setEntityPermissions($page, ['view'], [$user->roles->first()]);
240
241         $resp = $this->get('/pages/recently-updated');
242         $resp->assertDontSee($page->book->getShortName(42));
243         $resp->assertDontSee($page->chapter->getShortName(42));
244         $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated title');
245     }
246
247     public function test_recently_updated_pages_on_home()
248     {
249         /** @var Page $page */
250         $page = Page::query()->orderBy('updated_at', 'asc')->first();
251         Page::query()->where('id', '!=', $page->id)->update([
252             'updated_at' => Carbon::now()->subSecond(1),
253         ]);
254
255         $resp = $this->asAdmin()->get('/');
256         $this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name);
257
258         $this->put($page->getUrl(), [
259             'name' => $page->name,
260             'html' => $page->html,
261         ]);
262
263         $resp = $this->get('/');
264         $this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
265     }
266
267     public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
268     {
269         $page = $this->entities->page();
270
271         $resp = $this->asViewer()->get($page->getUrl('/edit'));
272         $resp->assertRedirect($page->getUrl());
273
274         $resp->assertSessionHas('error', 'You do not have permission to access the requested page.');
275     }
276 }