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_within_chapter_deletion_returns_to_chapter()
183 $chapter = $this->entities->chapter();
184 $page = $chapter->pages()->first();
186 $this->asEditor()->delete($page->getUrl())
187 ->assertRedirect($chapter->getUrl());
190 public function test_recently_updated_pages_view()
192 $user = $this->users->editor();
193 $content = $this->entities->createChainBelongingToUser($user);
195 $resp = $this->asAdmin()->get('/pages/recently-updated');
196 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1)', $content['page']->name);
199 public function test_recently_updated_pages_view_shows_updated_by_details()
201 $user = $this->users->editor();
202 $page = $this->entities->page();
204 $this->actingAs($user)->put($page->getUrl(), [
205 'name' => 'Updated title',
206 'html' => '<p>Updated content</p>',
209 $resp = $this->asAdmin()->get('/pages/recently-updated');
210 $this->withHtml($resp)->assertElementContains('.entity-list .page:nth-child(1) small', 'by ' . $user->name);
213 public function test_recently_updated_pages_view_shows_parent_chain()
215 $user = $this->users->editor();
216 $page = $this->entities->pageWithinChapter();
218 $this->actingAs($user)->put($page->getUrl(), [
219 'name' => 'Updated title',
220 'html' => '<p>Updated content</p>',
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));
228 public function test_recently_updated_pages_view_does_not_show_parent_if_not_visible()
230 $user = $this->users->editor();
231 $page = $this->entities->pageWithinChapter();
233 $this->actingAs($user)->put($page->getUrl(), [
234 'name' => 'Updated title',
235 'html' => '<p>Updated content</p>',
238 $this->permissions->setEntityPermissions($page->book);
239 $this->permissions->setEntityPermissions($page, ['view'], [$user->roles->first()]);
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');
247 public function test_recently_updated_pages_on_home()
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),
255 $resp = $this->asAdmin()->get('/');
256 $this->withHtml($resp)->assertElementNotContains('#recently-updated-pages', $page->name);
258 $this->put($page->getUrl(), [
259 'name' => $page->name,
260 'html' => $page->html,
263 $resp = $this->get('/');
264 $this->withHtml($resp)->assertElementContains('#recently-updated-pages', $page->name);
267 public function test_page_edit_without_update_permissions_but_with_view_redirects_to_page()
269 $page = $this->entities->page();
271 $resp = $this->asViewer()->get($page->getUrl('/edit'));
272 $resp->assertRedirect($page->getUrl());
274 $resp->assertSessionHas('error', 'You do not have permission to access the requested page.');