3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
10 class PageTest extends TestCase
12 public function test_create()
14 $chapter = $this->entities->chapter();
15 $page = Page::factory()->make([
16 'name' => 'My First Page',
19 $resp = $this->asEditor()->get($chapter->getUrl());
20 $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page');
22 $resp = $this->get($chapter->getUrl('/create-page'));
23 /** @var Page $draftPage */
24 $draftPage = Page::query()
25 ->where('draft', '=', true)
26 ->orderBy('created_at', 'desc')
28 $resp->assertRedirect($draftPage->getUrl());
30 $resp = $this->get($draftPage->getUrl());
31 $this->withHtml($resp)->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page');
33 $resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html'));
34 $draftPage->refresh();
35 $resp->assertRedirect($draftPage->getUrl());
38 public function test_page_view_when_creator_is_deleted_but_owner_exists()
40 $page = $this->entities->page();
41 $user = $this->users->viewer();
42 $owner = $this->users->editor();
43 $page->created_by = $user->id;
44 $page->owned_by = $owner->id;
48 $resp = $this->asAdmin()->get($page->getUrl());
49 $resp->assertStatus(200);
50 $resp->assertSeeText('Owned by ' . $owner->name);
53 public function test_page_show_includes_pointer_section_select_mode_button()
55 $page = $this->entities->page();
56 $resp = $this->asEditor()->get($page->getUrl());
57 $this->withHtml($resp)->assertElementContains('.content-wrap button.screen-reader-only', 'Enter section select mode');
60 public function test_page_creation_with_markdown_content()
62 $this->setSettings(['app-editor' => 'markdown']);
63 $book = $this->entities->book();
65 $this->asEditor()->get($book->getUrl('/create-page'));
66 $draft = Page::query()->where('book_id', '=', $book->id)
67 ->where('draft', '=', true)->first();
70 'markdown' => '# a title',
71 'html' => '<h1>a title</h1>',
74 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
75 $resp->assertRedirect();
77 $this->assertDatabaseHas('pages', [
78 'markdown' => $details['markdown'],
79 'name' => $details['name'],
85 $resp = $this->get($draft->getUrl('/edit'));
86 $resp->assertSee('# a title');
89 public function test_page_creation_allows_summary_to_be_set()
91 $book = $this->entities->book();
93 $this->asEditor()->get($book->getUrl('/create-page'));
94 $draft = Page::query()->where('book_id', '=', $book->id)
95 ->where('draft', '=', true)->first();
98 'html' => '<h1>a title</h1>',
99 'name' => 'My page with summary',
100 'summary' => 'Here is my changelog message for a new page!',
102 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
103 $resp->assertRedirect();
105 $this->assertDatabaseHas('page_revisions', [
106 'page_id' => $draft->id,
107 'summary' => 'Here is my changelog message for a new page!',
111 $resp = $this->get($draft->getUrl('/revisions'));
112 $resp->assertSee('Here is my changelog message for a new page!');
115 public function test_page_delete()
117 $page = $this->entities->page();
118 $this->assertNull($page->deleted_at);
120 $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
121 $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
123 $deleteReq = $this->delete($page->getUrl());
124 $deleteReq->assertRedirect($page->getParent()->getUrl());
125 $this->assertActivityExists('page_delete', $page);
128 $this->assertNotNull($page->deleted_at);
129 $this->assertTrue($page->deletions()->count() === 1);
131 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
132 $this->assertNotificationContains($redirectReq, 'Page Successfully Deleted');
135 public function test_page_full_delete_removes_all_revisions()
137 $page = $this->entities->page();
138 $page->revisions()->create([
139 'html' => '<p>ducks</p>',
140 'name' => 'my page revision',
143 $page->revisions()->create([
144 'html' => '<p>ducks</p>',
145 'name' => 'my page revision',
146 'type' => 'revision',
149 $this->assertDatabaseHas('page_revisions', [
150 'page_id' => $page->id,
153 $this->asEditor()->delete($page->getUrl());
154 $this->asAdmin()->post('/settings/recycle-bin/empty');
156 $this->assertDatabaseMissing('page_revisions', [
157 'page_id' => $page->id,
161 public function test_page_copy()
163 $page = $this->entities->page();
164 $page->html = '<p>This is some test content</p>';
167 $currentBook = $page->book;
168 $newBook = Book::where('id', '!=', $currentBook->id)->first();
170 $resp = $this->asEditor()->get($page->getUrl('/copy'));
171 $resp->assertSee('Copy Page');
173 $movePageResp = $this->post($page->getUrl('/copy'), [
174 'entity_selection' => 'book:' . $newBook->id,
175 'name' => 'My copied test page',
177 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
179 $movePageResp->assertRedirect($pageCopy->getUrl());
180 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
181 $this->assertStringContainsString('This is some test content', $pageCopy->html);
184 public function test_page_copy_with_markdown_has_both_html_and_markdown()
186 $page = $this->entities->page();
187 $page->html = '<h1>This is some test content</h1>';
188 $page->markdown = '# This is some test content';
190 $newBook = Book::where('id', '!=', $page->book->id)->first();
192 $this->asEditor()->post($page->getUrl('/copy'), [
193 'entity_selection' => 'book:' . $newBook->id,
194 'name' => 'My copied test page',
196 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
198 $this->assertStringContainsString('This is some test content', $pageCopy->html);
199 $this->assertEquals('# This is some test content', $pageCopy->markdown);
202 public function test_page_copy_with_no_destination()
204 $page = $this->entities->page();
205 $currentBook = $page->book;
207 $resp = $this->asEditor()->get($page->getUrl('/copy'));
208 $resp->assertSee('Copy Page');
210 $movePageResp = $this->post($page->getUrl('/copy'), [
211 'name' => 'My copied test page',
214 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
216 $movePageResp->assertRedirect($pageCopy->getUrl());
217 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
218 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
221 public function test_page_can_be_copied_without_edit_permission()
223 $page = $this->entities->page();
224 $currentBook = $page->book;
225 $newBook = Book::where('id', '!=', $currentBook->id)->first();
226 $viewer = $this->users->viewer();
228 $resp = $this->actingAs($viewer)->get($page->getUrl());
229 $resp->assertDontSee($page->getUrl('/copy'));
231 $newBook->owned_by = $viewer->id;
233 $this->permissions->grantUserRolePermissions($viewer, ['page-create-own']);
234 $this->permissions->regenerateForEntity($newBook);
236 $resp = $this->actingAs($viewer)->get($page->getUrl());
237 $resp->assertSee($page->getUrl('/copy'));
239 $movePageResp = $this->post($page->getUrl('/copy'), [
240 'entity_selection' => 'book:' . $newBook->id,
241 'name' => 'My copied test page',
243 $movePageResp->assertRedirect();
245 $this->assertDatabaseHas('pages', [
246 'name' => 'My copied test page',
247 'created_by' => $viewer->id,
248 'book_id' => $newBook->id,
252 public function test_old_page_slugs_redirect_to_new_pages()
254 $page = $this->entities->page();
256 // Need to save twice since revisions are not generated in seeder.
257 $this->asAdmin()->put($page->getUrl(), [
258 'name' => 'super test',
263 $pageUrl = $page->getUrl();
265 $this->put($pageUrl, [
266 'name' => 'super test page',
271 ->assertRedirect("/books/{$page->book->slug}/page/super-test-page");
274 public function test_page_within_chapter_deletion_returns_to_chapter()
276 $chapter = $this->entities->chapter();
277 $page = $chapter->pages()->first();
279 $this->asEditor()->delete($page->getUrl())
280 ->assertRedirect($chapter->getUrl());
283 public function test_recently_updated_pages_view()
285 $user = $this->users->editor();
286 $content = $this->entities->createChainBelongingToUser($user);
288 $resp = $this->asAdmin()->get('/pages/recently-updated');
289 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
292 public function test_recently_updated_pages_view_shows_updated_by_details()
294 $user = $this->users->editor();
295 $page = $this->entities->page();
297 $this->actingAs($user)->put($page->getUrl(), [
298 'name' => 'Updated title',
299 'html' => '<p>Updated content</p>',
302 $resp = $this->asAdmin()->get('/pages/recently-updated');
303 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1) small', 'by ' . $user->name);
306 public function test_recently_updated_pages_view_shows_parent_chain()
308 $user = $this->users->editor();
309 $page = $this->entities->pageWithinChapter();
311 $this->actingAs($user)->put($page->getUrl(), [
312 'name' => 'Updated title',
313 'html' => '<p>Updated content</p>',
316 $resp = $this->asAdmin()->get('/pages/recently-updated');
317 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->chapter->getShortName(42));
318 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $page->book->getShortName(42));
321 public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible()
323 $user = $this->users->editor();
324 $page = $this->entities->pageWithinChapter();
326 $this->actingAs($user)->put($page->getUrl(), [
327 'name' => 'Updated title',
328 'html' => '<p>Updated content</p>',
331 $this->permissions->setEntityPermissions($page->book);
332 $this->permissions->setEntityPermissions($page, ['view'], [$user->roles->first()]);
334 $resp = $this->get('/pages/recently-updated');
335 $resp->assertDontSee($page->book->getShortName(42));
336 $resp->assertDontSee($page->chapter->getShortName(42));
337 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', 'Updated title');
340 public function test_recently_updated_pages_on_home()
342 /** @var Page $page */
343 $page = Page::query()->orderBy('updated_at', 'asc')->first();
344 Page::query()->where('id', '!=', $page->id)->update([
345 'updated_at' => Carbon::now()->subSecond(1),
348 $resp = $this->asAdmin()->get('/');
349 $this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name);
351 $this->put($page->getUrl(), [
352 'name' => $page->name,
353 'html' => $page->html,
356 $resp = $this->get('/');
357 $this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
360 public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
362 $page = $this->entities->page();
364 $resp = $this->asViewer()->get($page->getUrl('/edit'));
365 $resp->assertRedirect($page->getUrl());
367 $resp->assertSessionHas('error', 'You do not have permission to access the requested page.');