3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Uploads\Image;
11 class PageTest extends TestCase
13 public function test_create()
15 $chapter = $this->entities->chapter();
16 $page = Page::factory()->make([
17 'name' => 'My First Page',
20 $resp = $this->asEditor()->get($chapter->getUrl());
21 $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl('/create-page') . '"]', 'New Page');
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')
29 $resp->assertRedirect($draftPage->getUrl());
31 $resp = $this->get($draftPage->getUrl());
32 $this->withHtml($resp)->assertElementContains('form[action="' . $draftPage->getUrl() . '"][method="POST"]', 'Save Page');
34 $resp = $this->post($draftPage->getUrl(), $draftPage->only('name', 'html'));
35 $draftPage->refresh();
36 $resp->assertRedirect($draftPage->getUrl());
39 public function test_page_view_when_creator_is_deleted_but_owner_exists()
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;
49 $resp = $this->asAdmin()->get($page->getUrl());
50 $resp->assertStatus(200);
51 $resp->assertSeeText('Owned by ' . $owner->name);
54 public function test_page_show_includes_pointer_section_select_mode_button()
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');
61 public function test_page_creation_with_markdown_content()
63 $this->setSettings(['app-editor' => 'markdown']);
64 $book = $this->entities->book();
66 $this->asEditor()->get($book->getUrl('/create-page'));
67 $draft = Page::query()->where('book_id', '=', $book->id)
68 ->where('draft', '=', true)->first();
71 'markdown' => '# a title',
72 'html' => '<h1>a title</h1>',
75 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
76 $resp->assertRedirect();
78 $this->assertDatabaseHasEntityData('page', [
79 'markdown' => $details['markdown'],
80 'name' => $details['name'],
86 $resp = $this->get($draft->getUrl('/edit'));
87 $resp->assertSee('# a title');
90 public function test_page_creation_allows_summary_to_be_set()
92 $book = $this->entities->book();
94 $this->asEditor()->get($book->getUrl('/create-page'));
95 $draft = Page::query()->where('book_id', '=', $book->id)
96 ->where('draft', '=', true)->first();
99 'html' => '<h1>a title</h1>',
100 'name' => 'My page with summary',
101 'summary' => 'Here is my changelog message for a new page!',
103 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
104 $resp->assertRedirect();
106 $this->assertDatabaseHas('page_revisions', [
107 'page_id' => $draft->id,
108 'summary' => 'Here is my changelog message for a new page!',
112 $resp = $this->get($draft->getUrl('/revisions'));
113 $resp->assertSee('Here is my changelog message for a new page!');
116 public function test_page_delete()
118 $page = $this->entities->page();
119 $this->assertNull($page->deleted_at);
121 $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
122 $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
124 $deleteReq = $this->delete($page->getUrl());
125 $deleteReq->assertRedirect($page->getParent()->getUrl());
126 $this->assertActivityExists('page_delete', $page);
129 $this->assertNotNull($page->deleted_at);
130 $this->assertTrue($page->deletions()->count() === 1);
132 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
133 $this->assertNotificationContains($redirectReq, 'Page Successfully Deleted');
136 public function test_page_full_delete_removes_all_revisions()
138 $page = $this->entities->page();
139 $page->revisions()->create([
140 'html' => '<p>ducks</p>',
141 'name' => 'my page revision',
144 $page->revisions()->create([
145 'html' => '<p>ducks</p>',
146 'name' => 'my page revision',
147 'type' => 'revision',
150 $this->assertDatabaseHas('page_revisions', [
151 'page_id' => $page->id,
154 $this->asEditor()->delete($page->getUrl());
155 $this->asAdmin()->post('/settings/recycle-bin/empty');
157 $this->assertDatabaseMissing('page_revisions', [
158 'page_id' => $page->id,
162 public function test_page_full_delete_nulls_related_images()
164 $page = $this->entities->page();
165 $image = Image::factory()->create(['type' => 'gallery', 'uploaded_to' => $page->id]);
167 $this->asEditor()->delete($page->getUrl());
168 $this->asAdmin()->post('/settings/recycle-bin/empty');
170 $this->assertDatabaseMissing('images', [
172 'uploaded_to' => $page->id,
175 $this->assertDatabaseHas('images', [
177 'uploaded_to' => null,
181 public function test_page_copy()
183 $page = $this->entities->page();
184 $page->html = '<p>This is some test content</p>';
187 $currentBook = $page->book;
188 $newBook = Book::where('id', '!=', $currentBook->id)->first();
190 $resp = $this->asEditor()->get($page->getUrl('/copy'));
191 $resp->assertSee('Copy Page');
193 $movePageResp = $this->post($page->getUrl('/copy'), [
194 'entity_selection' => 'book:' . $newBook->id,
195 'name' => 'My copied test page',
197 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
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);
204 public function test_page_copy_with_markdown_has_both_html_and_markdown()
206 $page = $this->entities->page();
207 $page->html = '<h1>This is some test content</h1>';
208 $page->markdown = '# This is some test content';
210 $newBook = Book::where('id', '!=', $page->book->id)->first();
212 $this->asEditor()->post($page->getUrl('/copy'), [
213 'entity_selection' => 'book:' . $newBook->id,
214 'name' => 'My copied test page',
216 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
218 $this->assertStringContainsString('This is some test content', $pageCopy->html);
219 $this->assertEquals('# This is some test content', $pageCopy->markdown);
222 public function test_page_copy_with_no_destination()
224 $page = $this->entities->page();
225 $currentBook = $page->book;
227 $resp = $this->asEditor()->get($page->getUrl('/copy'));
228 $resp->assertSee('Copy Page');
230 $movePageResp = $this->post($page->getUrl('/copy'), [
231 'name' => 'My copied test page',
234 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
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');
241 public function test_page_can_be_copied_without_edit_permission()
243 $page = $this->entities->page();
244 $currentBook = $page->book;
245 $newBook = Book::where('id', '!=', $currentBook->id)->first();
246 $viewer = $this->users->viewer();
248 $resp = $this->actingAs($viewer)->get($page->getUrl());
249 $resp->assertDontSee($page->getUrl('/copy'));
251 $newBook->owned_by = $viewer->id;
253 $this->permissions->grantUserRolePermissions($viewer, ['page-create-own']);
254 $this->permissions->regenerateForEntity($newBook);
256 $resp = $this->actingAs($viewer)->get($page->getUrl());
257 $resp->assertSee($page->getUrl('/copy'));
259 $movePageResp = $this->post($page->getUrl('/copy'), [
260 'entity_selection' => 'book:' . $newBook->id,
261 'name' => 'My copied test page',
263 $movePageResp->assertRedirect();
265 $this->assertDatabaseHasEntityData('page', [
266 'name' => 'My copied test page',
267 'created_by' => $viewer->id,
268 'book_id' => $newBook->id,
272 public function test_old_page_slugs_redirect_to_new_pages()
274 $page = $this->entities->page();
276 // Need to save twice since revisions are not generated in seeder.
277 $this->asAdmin()->put($page->getUrl(), [
278 'name' => 'super test',
283 $pageUrl = $page->getUrl();
285 $this->put($pageUrl, [
286 'name' => 'super test page',
291 ->assertRedirect("/books/{$page->book->slug}/page/super-test-page");
294 public function test_page_within_chapter_deletion_returns_to_chapter()
296 $chapter = $this->entities->chapter();
297 $page = $chapter->pages()->first();
299 $this->asEditor()->delete($page->getUrl())
300 ->assertRedirect($chapter->getUrl());
303 public function test_recently_updated_pages_view()
305 $user = $this->users->editor();
306 $content = $this->entities->createChainBelongingToUser($user);
308 $resp = $this->asAdmin()->get('/pages/recently-updated');
309 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
312 public function test_recently_updated_pages_view_shows_updated_by_details()
314 $user = $this->users->editor();
315 $page = $this->entities->page();
317 $this->actingAs($user)->put($page->getUrl(), [
318 'name' => 'Updated title',
319 'html' => '<p>Updated content</p>',
322 $resp = $this->asAdmin()->get('/pages/recently-updated');
323 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1) small', 'by ' . $user->name);
326 public function test_recently_updated_pages_view_shows_parent_chain()
328 $user = $this->users->editor();
329 $page = $this->entities->pageWithinChapter();
331 $this->actingAs($user)->put($page->getUrl(), [
332 'name' => 'Updated title',
333 'html' => '<p>Updated content</p>',
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));
341 public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible()
343 $user = $this->users->editor();
344 $page = $this->entities->pageWithinChapter();
346 $this->actingAs($user)->put($page->getUrl(), [
347 'name' => 'Updated title',
348 'html' => '<p>Updated content</p>',
351 $this->permissions->setEntityPermissions($page->book);
352 $this->permissions->setEntityPermissions($page, ['view'], [$user->roles->first()]);
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');
360 public function test_recently_updated_pages_on_home()
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),
368 $resp = $this->asAdmin()->get('/');
369 $this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name);
371 $this->put($page->getUrl(), [
372 'name' => $page->name,
373 'html' => $page->html,
376 $resp = $this->get('/');
377 $this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
380 public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
382 $page = $this->entities->page();
384 $resp = $this->asViewer()->get($page->getUrl('/edit'));
385 $resp->assertRedirect($page->getUrl());
387 $resp->assertSessionHas('error', 'You do not have permission to access the requested page.');