]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageDraftTest.php
Skip intermediate login page with single provider
[bookstack] / tests / Entity / PageDraftTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Models\PageRevision;
8 use BookStack\Entities\Repos\PageRepo;
9 use Tests\TestCase;
10
11 class PageDraftTest extends TestCase
12 {
13     /**
14      * @var Page
15      */
16     protected $page;
17
18     /**
19      * @var PageRepo
20      */
21     protected $pageRepo;
22
23     protected function setUp(): void
24     {
25         parent::setUp();
26         $this->page = Page::query()->first();
27         $this->pageRepo = app()->make(PageRepo::class);
28     }
29
30     public function test_draft_content_shows_if_available()
31     {
32         $addedContent = '<p>test message content</p>';
33
34         $this->asAdmin()->get($this->page->getUrl('/edit'))
35             ->assertElementNotContains('[name="html"]', $addedContent);
36
37         $newContent = $this->page->html . $addedContent;
38         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
39         $this->asAdmin()->get($this->page->getUrl('/edit'))
40             ->assertElementContains('[name="html"]', $newContent);
41     }
42
43     public function test_draft_not_visible_by_others()
44     {
45         $addedContent = '<p>test message content</p>';
46         $this->asAdmin()->get($this->page->getUrl('/edit'))
47             ->assertElementNotContains('[name="html"]', $addedContent);
48
49         $newContent = $this->page->html . $addedContent;
50         $newUser = $this->getEditor();
51         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
52
53         $this->actingAs($newUser)->get($this->page->getUrl('/edit'))
54             ->assertElementNotContains('[name="html"]', $newContent);
55     }
56
57     public function test_alert_message_shows_if_editing_draft()
58     {
59         $this->asAdmin();
60         $this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']);
61         $this->asAdmin()->get($this->page->getUrl('/edit'))
62             ->assertSee('You are currently editing a draft');
63     }
64
65     public function test_alert_message_shows_if_someone_else_editing()
66     {
67         $nonEditedPage = Page::query()->take(10)->get()->last();
68         $addedContent = '<p>test message content</p>';
69         $this->asAdmin()->get($this->page->getUrl('/edit'))
70             ->assertElementNotContains('[name="html"]', $addedContent);
71
72         $newContent = $this->page->html . $addedContent;
73         $newUser = $this->getEditor();
74         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
75
76         $this->actingAs($newUser)
77             ->get($this->page->getUrl('/edit'))
78             ->assertSee('Admin has started editing this page');
79         $this->flushSession();
80         $this->get($nonEditedPage->getUrl() . '/edit')
81             ->assertElementNotContains('.notification', 'Admin has started editing this page');
82     }
83
84     public function test_draft_save_shows_alert_if_draft_older_than_last_page_update()
85     {
86         $admin = $this->getAdmin();
87         $editor = $this->getEditor();
88         /** @var Page $page */
89         $page = Page::query()->first();
90
91         $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
92             'name' => $page->name,
93             'html' => '<p>updated draft</p>',
94         ]);
95
96         /** @var PageRevision $draft */
97         $draft = $page->allRevisions()
98             ->where('type', '=', 'update_draft')
99             ->where('created_by', '=', $editor->id)
100             ->first();
101         $draft->created_at = now()->subMinute(1);
102         $draft->save();
103
104         $this->actingAs($admin)->put($page->refresh()->getUrl(), [
105             'name' => $page->name,
106             'html' => '<p>admin update</p>',
107         ]);
108
109         $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
110             'name' => $page->name,
111             'html' => '<p>updated draft again</p>',
112         ]);
113
114         $resp->assertJson([
115             '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.',
116         ]);
117     }
118
119     public function test_draft_save_shows_alert_if_draft_edit_started_by_someone_else()
120     {
121         $admin = $this->getAdmin();
122         $editor = $this->getEditor();
123         /** @var Page $page */
124         $page = Page::query()->first();
125
126         $this->actingAs($admin)->put('/ajax/page/' . $page->id . '/save-draft', [
127             'name' => $page->name,
128             'html' => '<p>updated draft</p>',
129         ]);
130
131         $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
132             'name' => $page->name,
133             'html' => '<p>updated draft again</p>',
134         ]);
135
136         $resp->assertJson([
137             'warning' => 'Admin has started editing this page in the last 60 minutes. Take care not to overwrite each other\'s updates!',
138         ]);
139     }
140
141     public function test_draft_pages_show_on_homepage()
142     {
143         /** @var Book $book */
144         $book = Book::query()->first();
145         $this->asAdmin()->get('/')
146             ->assertElementNotContains('#recent-drafts', 'New Page');
147
148         $this->get($book->getUrl() . '/create-page');
149
150         $this->get('/')->assertElementContains('#recent-drafts', 'New Page');
151     }
152
153     public function test_draft_pages_not_visible_by_others()
154     {
155         /** @var Book $book */
156         $book = Book::query()->first();
157         $chapter = $book->chapters->first();
158         $newUser = $this->getEditor();
159
160         $this->actingAs($newUser)->get($book->getUrl('/create-page'));
161         $this->get($chapter->getUrl('/create-page'));
162         $this->get($book->getUrl())
163             ->assertElementContains('.book-contents', 'New Page');
164
165         $this->asAdmin()->get($book->getUrl())
166             ->assertElementNotContains('.book-contents', 'New Page');
167         $this->get($chapter->getUrl())
168             ->assertElementNotContains('.book-contents', 'New Page');
169     }
170
171     public function test_page_html_in_ajax_fetch_response()
172     {
173         $this->asAdmin();
174         /** @var Page $page */
175         $page = Page::query()->first();
176
177         $this->getJson('/ajax/page/' . $page->id)->assertJson([
178             'html' => $page->html,
179         ]);
180     }
181
182     public function test_updating_page_draft_with_markdown_retains_markdown_content()
183     {
184         /** @var Book $book */
185         $book = Book::query()->first();
186         $this->asEditor()->get($book->getUrl('/create-page'));
187         /** @var Page $draft */
188         $draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail();
189
190         $resp = $this->put('/ajax/page/' . $draft->id . '/save-draft', [
191             'name'     => 'My updated draft',
192             'markdown' => "# My markdown page\n\n[A link](https://example.com)",
193             'html'     => '<p>checking markdown takes priority over this</p>',
194         ]);
195         $resp->assertOk();
196
197         $this->assertDatabaseHas('pages', [
198             'id'       => $draft->id,
199             'draft'    => true,
200             'name'     => 'My updated draft',
201             'markdown' => "# My markdown page\n\n[A link](https://example.com)",
202         ]);
203
204         $draft->refresh();
205         $this->assertStringContainsString('href="https://example.com"', $draft->html);
206     }
207 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.