]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageDraftTest.php
Create additional test helper classes
[bookstack] / tests / Entity / PageDraftTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Models\PageRevision;
7 use BookStack\Entities\Repos\PageRepo;
8 use Tests\TestCase;
9
10 class PageDraftTest extends TestCase
11 {
12     protected Page $page;
13     protected PageRepo $pageRepo;
14
15     protected function setUp(): void
16     {
17         parent::setUp();
18         $this->page = $this->entities->page();
19         $this->pageRepo = app()->make(PageRepo::class);
20     }
21
22     public function test_draft_content_shows_if_available()
23     {
24         $addedContent = '<p>test message content</p>';
25
26         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
27         $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent);
28
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);
33     }
34
35     public function test_draft_not_visible_by_others()
36     {
37         $addedContent = '<p>test message content</p>';
38         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
39         $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent);
40
41         $newContent = $this->page->html . $addedContent;
42         $newUser = $this->users->editor();
43         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
44
45         $resp = $this->actingAs($newUser)->get($this->page->getUrl('/edit'));
46         $this->withHtml($resp)->assertElementNotContains('[name="html"]', $newContent);
47     }
48
49     public function test_alert_message_shows_if_editing_draft()
50     {
51         $this->asAdmin();
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');
55     }
56
57     public function test_alert_message_shows_if_someone_else_editing()
58     {
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);
63
64         $newContent = $this->page->html . $addedContent;
65         $newUser = $this->users->editor();
66         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
67
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');
74     }
75
76     public function test_draft_save_shows_alert_if_draft_older_than_last_page_update()
77     {
78         $admin = $this->users->admin();
79         $editor = $this->users->editor();
80         $page = $this->entities->page();
81
82         $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
83             'name' => $page->name,
84             'html' => '<p>updated draft</p>',
85         ]);
86
87         /** @var PageRevision $draft */
88         $draft = $page->allRevisions()
89             ->where('type', '=', 'update_draft')
90             ->where('created_by', '=', $editor->id)
91             ->first();
92         $draft->created_at = now()->subMinute(1);
93         $draft->save();
94
95         $this->actingAs($admin)->put($page->refresh()->getUrl(), [
96             'name' => $page->name,
97             'html' => '<p>admin update</p>',
98         ]);
99
100         $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
101             'name' => $page->name,
102             'html' => '<p>updated draft again</p>',
103         ]);
104
105         $resp->assertJson([
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.',
107         ]);
108     }
109
110     public function test_draft_save_shows_alert_if_draft_edit_started_by_someone_else()
111     {
112         $admin = $this->users->admin();
113         $editor = $this->users->editor();
114         $page = $this->entities->page();
115
116         $this->actingAs($admin)->put('/ajax/page/' . $page->id . '/save-draft', [
117             'name' => $page->name,
118             'html' => '<p>updated draft</p>',
119         ]);
120
121         $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
122             'name' => $page->name,
123             'html' => '<p>updated draft again</p>',
124         ]);
125
126         $resp->assertJson([
127             'warning' => 'Admin has started editing this page in the last 60 minutes. Take care not to overwrite each other\'s updates!',
128         ]);
129     }
130
131     public function test_draft_pages_show_on_homepage()
132     {
133         $book = $this->entities->book();
134         $resp = $this->asAdmin()->get('/');
135         $this->withHtml($resp)->assertElementNotContains('#recent-drafts', 'New Page');
136
137         $this->get($book->getUrl() . '/create-page');
138
139         $this->withHtml($this->get('/'))->assertElementContains('#recent-drafts', 'New Page');
140     }
141
142     public function test_draft_pages_not_visible_by_others()
143     {
144         $book = $this->entities->book();
145         $chapter = $book->chapters->first();
146         $newUser = $this->users->editor();
147
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');
152
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');
157     }
158
159     public function test_page_html_in_ajax_fetch_response()
160     {
161         $this->asAdmin();
162         $page = $this->entities->page();
163
164         $this->getJson('/ajax/page/' . $page->id)->assertJson([
165             'html' => $page->html,
166         ]);
167     }
168
169     public function test_updating_page_draft_with_markdown_retains_markdown_content()
170     {
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();
175
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>',
180         ]);
181         $resp->assertOk();
182
183         $this->assertDatabaseHas('pages', [
184             'id'       => $draft->id,
185             'draft'    => true,
186             'name'     => 'My updated draft',
187             'markdown' => "# My markdown page\n\n[A link](https://example.com)",
188         ]);
189
190         $draft->refresh();
191         $this->assertStringContainsString('href="https://example.com"', $draft->html);
192     }
193
194     public function test_slug_generated_on_draft_publish_to_page_when_no_name_change()
195     {
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();
200
201         $this->put('/ajax/page/' . $draft->id . '/save-draft', [
202             'name'     => 'My page',
203             'markdown' => 'Update test',
204         ])->assertOk();
205
206         $draft->refresh();
207         $this->assertEmpty($draft->slug);
208
209         $this->post($draft->getUrl(), [
210             'name'     => 'My page',
211             'markdown' => '# My markdown page',
212         ]);
213
214         $this->assertDatabaseHas('pages', [
215             'id'    => $draft->id,
216             'draft' => false,
217             'slug'  => 'my-page',
218         ]);
219     }
220 }