3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Models\PageRevision;
7 use BookStack\Entities\Repos\PageRepo;
10 class PageDraftTest extends TestCase
13 protected PageRepo $pageRepo;
15 protected function setUp(): void
18 $this->page = $this->entities->page();
19 $this->pageRepo = app()->make(PageRepo::class);
22 public function test_draft_content_shows_if_available()
24 $addedContent = '<p>test message content</p>';
26 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
27 $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent);
29 $newContent = $this->page->html . $addedContent;
30 $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
31 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
32 $this->withHtml($resp)->assertElementContains('[name="html"]', $newContent);
35 public function test_draft_not_visible_by_others()
37 $addedContent = '<p>test message content</p>';
38 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
39 $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent);
41 $newContent = $this->page->html . $addedContent;
42 $newUser = $this->users->editor();
43 $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
45 $resp = $this->actingAs($newUser)->get($this->page->getUrl('/edit'));
46 $this->withHtml($resp)->assertElementNotContains('[name="html"]', $newContent);
49 public function test_alert_message_shows_if_editing_draft()
52 $this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']);
53 $this->asAdmin()->get($this->page->getUrl('/edit'))
54 ->assertSee('You are currently editing a draft');
57 public function test_alert_message_shows_if_someone_else_editing()
59 $nonEditedPage = Page::query()->take(10)->get()->last();
60 $addedContent = '<p>test message content</p>';
61 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
62 $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent);
64 $newContent = $this->page->html . $addedContent;
65 $newUser = $this->users->editor();
66 $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
68 $this->actingAs($newUser)
69 ->get($this->page->getUrl('/edit'))
70 ->assertSee('Admin has started editing this page');
71 $this->flushSession();
72 $resp = $this->get($nonEditedPage->getUrl() . '/edit');
73 $this->withHtml($resp)->assertElementNotContains('.notification', 'Admin has started editing this page');
76 public function test_draft_save_shows_alert_if_draft_older_than_last_page_update()
78 $admin = $this->users->admin();
79 $editor = $this->users->editor();
80 $page = $this->entities->page();
82 $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
83 'name' => $page->name,
84 'html' => '<p>updated draft</p>',
87 /** @var PageRevision $draft */
88 $draft = $page->allRevisions()
89 ->where('type', '=', 'update_draft')
90 ->where('created_by', '=', $editor->id)
92 $draft->created_at = now()->subMinute(1);
95 $this->actingAs($admin)->put($page->refresh()->getUrl(), [
96 'name' => $page->name,
97 'html' => '<p>admin update</p>',
100 $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
101 'name' => $page->name,
102 'html' => '<p>updated draft again</p>',
106 'warning' => 'This page has been updated since this draft was created. It is recommended that you discard this draft or take care not to overwrite any page changes.',
110 public function test_draft_save_shows_alert_if_draft_edit_started_by_someone_else()
112 $admin = $this->users->admin();
113 $editor = $this->users->editor();
114 $page = $this->entities->page();
116 $this->actingAs($admin)->put('/ajax/page/' . $page->id . '/save-draft', [
117 'name' => $page->name,
118 'html' => '<p>updated draft</p>',
121 $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
122 'name' => $page->name,
123 'html' => '<p>updated draft again</p>',
127 'warning' => 'Admin has started editing this page in the last 60 minutes. Take care not to overwrite each other\'s updates!',
131 public function test_draft_pages_show_on_homepage()
133 $book = $this->entities->book();
134 $resp = $this->asAdmin()->get('/');
135 $this->withHtml($resp)->assertElementNotContains('#recent-drafts', 'New Page');
137 $this->get($book->getUrl() . '/create-page');
139 $this->withHtml($this->get('/'))->assertElementContains('#recent-drafts', 'New Page');
142 public function test_draft_pages_not_visible_by_others()
144 $book = $this->entities->book();
145 $chapter = $book->chapters->first();
146 $newUser = $this->users->editor();
148 $this->actingAs($newUser)->get($book->getUrl('/create-page'));
149 $this->get($chapter->getUrl('/create-page'));
150 $resp = $this->get($book->getUrl());
151 $this->withHtml($resp)->assertElementContains('.book-contents', 'New Page');
153 $resp = $this->asAdmin()->get($book->getUrl());
154 $this->withHtml($resp)->assertElementNotContains('.book-contents', 'New Page');
155 $resp = $this->get($chapter->getUrl());
156 $this->withHtml($resp)->assertElementNotContains('.book-contents', 'New Page');
159 public function test_page_html_in_ajax_fetch_response()
162 $page = $this->entities->page();
164 $this->getJson('/ajax/page/' . $page->id)->assertJson([
165 'html' => $page->html,
169 public function test_updating_page_draft_with_markdown_retains_markdown_content()
171 $book = $this->entities->book();
172 $this->asEditor()->get($book->getUrl('/create-page'));
173 /** @var Page $draft */
174 $draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail();
176 $resp = $this->put('/ajax/page/' . $draft->id . '/save-draft', [
177 'name' => 'My updated draft',
178 'markdown' => "# My markdown page\n\n[A link](https://example.com)",
179 'html' => '<p>checking markdown takes priority over this</p>',
183 $this->assertDatabaseHas('pages', [
186 'name' => 'My updated draft',
187 'markdown' => "# My markdown page\n\n[A link](https://example.com)",
191 $this->assertStringContainsString('href="https://example.com"', $draft->html);
194 public function test_slug_generated_on_draft_publish_to_page_when_no_name_change()
196 $book = $this->entities->book();
197 $this->asEditor()->get($book->getUrl('/create-page'));
198 /** @var Page $draft */
199 $draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail();
201 $this->put('/ajax/page/' . $draft->id . '/save-draft', [
203 'markdown' => 'Update test',
207 $this->assertEmpty($draft->slug);
209 $this->post($draft->getUrl(), [
211 'markdown' => '# My markdown page',
214 $this->assertDatabaseHas('pages', [