]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageTest.php
Moved more tests out of EntityTest
[bookstack] / tests / Entity / PageTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use Carbon\Carbon;
9 use Tests\TestCase;
10
11 class PageTest extends TestCase
12 {
13     public function test_page_view_when_creator_is_deleted_but_owner_exists()
14     {
15         $page = Page::query()->first();
16         $user = $this->getViewer();
17         $owner = $this->getEditor();
18         $page->created_by = $user->id;
19         $page->owned_by = $owner->id;
20         $page->save();
21         $user->delete();
22
23         $resp = $this->asAdmin()->get($page->getUrl());
24         $resp->assertStatus(200);
25         $resp->assertSeeText('Owned by ' . $owner->name);
26     }
27
28     public function test_page_creation_with_markdown_content()
29     {
30         $this->setSettings(['app-editor' => 'markdown']);
31         $book = Book::query()->first();
32
33         $this->asEditor()->get($book->getUrl('/create-page'));
34         $draft = Page::query()->where('book_id', '=', $book->id)
35             ->where('draft', '=', true)->first();
36
37         $details = [
38             'markdown' => '# a title',
39             'html'     => '<h1>a title</h1>',
40             'name'     => 'my page',
41         ];
42         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
43         $resp->assertRedirect();
44
45         $this->assertDatabaseHas('pages', [
46             'markdown' => $details['markdown'],
47             'name'     => $details['name'],
48             'id'       => $draft->id,
49             'draft'    => false,
50         ]);
51
52         $draft->refresh();
53         $resp = $this->get($draft->getUrl('/edit'));
54         $resp->assertSee('# a title');
55     }
56
57     public function test_page_delete()
58     {
59         $page = Page::query()->first();
60         $this->assertNull($page->deleted_at);
61
62         $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
63         $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
64
65         $deleteReq = $this->delete($page->getUrl());
66         $deleteReq->assertRedirect($page->getParent()->getUrl());
67         $this->assertActivityExists('page_delete', $page);
68
69         $page->refresh();
70         $this->assertNotNull($page->deleted_at);
71         $this->assertTrue($page->deletions()->count() === 1);
72
73         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
74         $redirectReq->assertNotificationContains('Page Successfully Deleted');
75     }
76
77     public function test_page_full_delete_removes_all_revisions()
78     {
79         /** @var Page $page */
80         $page = Page::query()->first();
81         $page->revisions()->create([
82             'html' => '<p>ducks</p>',
83             'name' => 'my page revision',
84             'type' => 'draft',
85         ]);
86         $page->revisions()->create([
87             'html' => '<p>ducks</p>',
88             'name' => 'my page revision',
89             'type' => 'revision',
90         ]);
91
92         $this->assertDatabaseHas('page_revisions', [
93             'page_id' => $page->id,
94         ]);
95
96         $this->asEditor()->delete($page->getUrl());
97         $this->asAdmin()->post('/settings/recycle-bin/empty');
98
99         $this->assertDatabaseMissing('page_revisions', [
100             'page_id' => $page->id,
101         ]);
102     }
103
104     public function test_page_copy()
105     {
106         $page = Page::first();
107         $page->html = '<p>This is some test content</p>';
108         $page->save();
109
110         $currentBook = $page->book;
111         $newBook = Book::where('id', '!=', $currentBook->id)->first();
112
113         $resp = $this->asEditor()->get($page->getUrl('/copy'));
114         $resp->assertSee('Copy Page');
115
116         $movePageResp = $this->post($page->getUrl('/copy'), [
117             'entity_selection' => 'book:' . $newBook->id,
118             'name'             => 'My copied test page',
119         ]);
120         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
121
122         $movePageResp->assertRedirect($pageCopy->getUrl());
123         $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
124         $this->assertStringContainsString('This is some test content', $pageCopy->html);
125     }
126
127     public function test_page_copy_with_markdown_has_both_html_and_markdown()
128     {
129         $page = Page::first();
130         $page->html = '<h1>This is some test content</h1>';
131         $page->markdown = '# This is some test content';
132         $page->save();
133         $newBook = Book::where('id', '!=', $page->book->id)->first();
134
135         $this->asEditor()->post($page->getUrl('/copy'), [
136             'entity_selection' => 'book:' . $newBook->id,
137             'name'             => 'My copied test page',
138         ]);
139         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
140
141         $this->assertStringContainsString('This is some test content', $pageCopy->html);
142         $this->assertEquals('# This is some test content', $pageCopy->markdown);
143     }
144
145     public function test_page_copy_with_no_destination()
146     {
147         $page = Page::first();
148         $currentBook = $page->book;
149
150         $resp = $this->asEditor()->get($page->getUrl('/copy'));
151         $resp->assertSee('Copy Page');
152
153         $movePageResp = $this->post($page->getUrl('/copy'), [
154             'name' => 'My copied test page',
155         ]);
156
157         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
158
159         $movePageResp->assertRedirect($pageCopy->getUrl());
160         $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
161         $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
162     }
163
164     public function test_page_can_be_copied_without_edit_permission()
165     {
166         $page = Page::first();
167         $currentBook = $page->book;
168         $newBook = Book::where('id', '!=', $currentBook->id)->first();
169         $viewer = $this->getViewer();
170
171         $resp = $this->actingAs($viewer)->get($page->getUrl());
172         $resp->assertDontSee($page->getUrl('/copy'));
173
174         $newBook->owned_by = $viewer->id;
175         $newBook->save();
176         $this->giveUserPermissions($viewer, ['page-create-own']);
177         $this->regenEntityPermissions($newBook);
178
179         $resp = $this->actingAs($viewer)->get($page->getUrl());
180         $resp->assertSee($page->getUrl('/copy'));
181
182         $movePageResp = $this->post($page->getUrl('/copy'), [
183             'entity_selection' => 'book:' . $newBook->id,
184             'name'             => 'My copied test page',
185         ]);
186         $movePageResp->assertRedirect();
187
188         $this->assertDatabaseHas('pages', [
189             'name'       => 'My copied test page',
190             'created_by' => $viewer->id,
191             'book_id'    => $newBook->id,
192         ]);
193     }
194
195     public function test_old_page_slugs_redirect_to_new_pages()
196     {
197         /** @var Page $page */
198         $page = Page::query()->first();
199
200         // Need to save twice since revisions are not generated in seeder.
201         $this->asAdmin()->put($page->getUrl(), [
202             'name' => 'super test',
203             'html' => '<p></p>'
204         ]);
205
206         $page->refresh();
207         $pageUrl = $page->getUrl();
208
209         $this->put($pageUrl, [
210             'name' => 'super test page',
211             'html' => '<p></p>'
212         ]);
213
214         $this->get($pageUrl)
215             ->assertRedirect("/books/{$page->book->slug}/page/super-test-page");
216     }
217
218     public function test_page_within_chapter_deletion_returns_to_chapter()
219     {
220         /** @var Chapter $chapter */
221         $chapter = Chapter::query()->first();
222         $page = $chapter->pages()->first();
223
224         $this->asEditor()->delete($page->getUrl())
225             ->assertRedirect($chapter->getUrl());
226     }
227
228     public function test_recently_updated_pages_view()
229     {
230         $user = $this->getEditor();
231         $content = $this->createEntityChainBelongingToUser($user);
232
233         $this->asAdmin()->get('/pages/recently-updated')
234             ->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
235     }
236
237     public function test_recently_updated_pages_on_home()
238     {
239         /** @var Page $page */
240         $page = Page::query()->orderBy('updated_at', 'asc')->first();
241         Page::query()->where('id', '!=', $page->id)->update([
242             'updated_at' => Carbon::now()->subSecond(1),
243         ]);
244
245         $this->asAdmin()->get('/')
246             ->assertElementNotContains('#recently-updated-pages', $page->name);
247
248         $this->put($page->getUrl(), [
249             'name' => $page->name,
250             'html' => $page->html,
251         ]);
252
253         $this->get('/')
254             ->assertElementContains('#recently-updated-pages', $page->name);
255     }
256
257 }