1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Page;
7 class PageTest extends TestCase
10 public function test_page_view_when_creator_is_deleted_but_owner_exists()
12 $page = Page::query()->first();
13 $user = $this->getViewer();
14 $owner = $this->getEditor();
15 $page->created_by = $user->id;
16 $page->owned_by = $owner->id;
20 $resp = $this->asAdmin()->get($page->getUrl());
21 $resp->assertStatus(200);
22 $resp->assertSeeText('Owned by ' . $owner->name);
25 public function test_page_creation_with_markdown_content()
27 $this->setSettings(['app-editor' => 'markdown']);
28 $book = Book::query()->first();
30 $this->asEditor()->get($book->getUrl('/create-page'));
31 $draft = Page::query()->where('book_id', '=', $book->id)
32 ->where('draft', '=', true)->first();
35 'markdown' => '# a title',
36 'html' => '<h1>a title</h1>',
39 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
40 $resp->assertRedirect();
42 $this->assertDatabaseHas('pages', [
43 'markdown' => $details['markdown'],
44 'name' => $details['name'],
50 $resp = $this->get($draft->getUrl("/edit"));
51 $resp->assertSee("# a title");
54 public function test_page_delete()
56 $page = Page::query()->first();
57 $this->assertNull($page->deleted_at);
59 $deleteViewReq = $this->asEditor()->get($page->getUrl('/delete'));
60 $deleteViewReq->assertSeeText('Are you sure you want to delete this page?');
62 $deleteReq = $this->delete($page->getUrl());
63 $deleteReq->assertRedirect($page->getParent()->getUrl());
64 $this->assertActivityExists('page_delete', $page);
67 $this->assertNotNull($page->deleted_at);
68 $this->assertTrue($page->deletions()->count() === 1);
70 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
71 $redirectReq->assertNotificationContains('Page Successfully Deleted');
74 public function test_page_full_delete_removes_all_revisions()
76 /** @var Page $page */
77 $page = Page::query()->first();
78 $page->revisions()->create([
79 'html' => '<p>ducks</p>',
80 'name' => 'my page revision',
83 $page->revisions()->create([
84 'html' => '<p>ducks</p>',
85 'name' => 'my page revision',
89 $this->assertDatabaseHas('page_revisions', [
90 'page_id' => $page->id,
93 $this->asEditor()->delete($page->getUrl());
94 $this->asAdmin()->post('/settings/recycle-bin/empty');
96 $this->assertDatabaseMissing('page_revisions', [
97 'page_id' => $page->id,
101 public function test_page_copy()
103 $page = Page::first();
104 $page->html = '<p>This is some test content</p>';
107 $currentBook = $page->book;
108 $newBook = Book::where('id', '!=', $currentBook->id)->first();
110 $resp = $this->asEditor()->get($page->getUrl('/copy'));
111 $resp->assertSee('Copy Page');
113 $movePageResp = $this->post($page->getUrl('/copy'), [
114 'entity_selection' => 'book:' . $newBook->id,
115 'name' => 'My copied test page'
117 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
119 $movePageResp->assertRedirect($pageCopy->getUrl());
120 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
121 $this->assertStringContainsString('This is some test content', $pageCopy->html);
124 public function test_page_copy_with_markdown_has_both_html_and_markdown()
126 $page = Page::first();
127 $page->html = '<h1>This is some test content</h1>';
128 $page->markdown = '# This is some test content';
130 $newBook = Book::where('id', '!=', $page->book->id)->first();
132 $this->asEditor()->post($page->getUrl('/copy'), [
133 'entity_selection' => 'book:' . $newBook->id,
134 'name' => 'My copied test page'
136 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
138 $this->assertStringContainsString('This is some test content', $pageCopy->html);
139 $this->assertEquals('# This is some test content', $pageCopy->markdown);
142 public function test_page_copy_with_no_destination()
144 $page = Page::first();
145 $currentBook = $page->book;
147 $resp = $this->asEditor()->get($page->getUrl('/copy'));
148 $resp->assertSee('Copy Page');
150 $movePageResp = $this->post($page->getUrl('/copy'), [
151 'name' => 'My copied test page'
154 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
156 $movePageResp->assertRedirect($pageCopy->getUrl());
157 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
158 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
161 public function test_page_can_be_copied_without_edit_permission()
163 $page = Page::first();
164 $currentBook = $page->book;
165 $newBook = Book::where('id', '!=', $currentBook->id)->first();
166 $viewer = $this->getViewer();
168 $resp = $this->actingAs($viewer)->get($page->getUrl());
169 $resp->assertDontSee($page->getUrl('/copy'));
171 $newBook->owned_by = $viewer->id;
173 $this->giveUserPermissions($viewer, ['page-create-own']);
174 $this->regenEntityPermissions($newBook);
176 $resp = $this->actingAs($viewer)->get($page->getUrl());
177 $resp->assertSee($page->getUrl('/copy'));
179 $movePageResp = $this->post($page->getUrl('/copy'), [
180 'entity_selection' => 'book:' . $newBook->id,
181 'name' => 'My copied test page'
183 $movePageResp->assertRedirect();
185 $this->assertDatabaseHas('pages', [
186 'name' => 'My copied test page',
187 'created_by' => $viewer->id,
188 'book_id' => $newBook->id,
192 public function test_empty_markdown_still_saves_without_error()
194 $this->setSettings(['app-editor' => 'markdown']);
195 $book = Book::query()->first();
197 $this->asEditor()->get($book->getUrl('/create-page'));
198 $draft = Page::query()->where('book_id', '=', $book->id)
199 ->where('draft', '=', true)->first();
205 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
206 $resp->assertRedirect();
208 $this->assertDatabaseHas('pages', [
209 'markdown' => $details['markdown'],