]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageEditorTest.php
bb3d7419306c5179a7ea4cf9d0802b0c41e2448c
[bookstack] / tests / Entity / PageEditorTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Tools\PageEditorType;
8 use Tests\TestCase;
9
10 class PageEditorTest extends TestCase
11 {
12     protected Page $page;
13
14     protected function setUp(): void
15     {
16         parent::setUp();
17         $this->page = $this->entities->page();
18     }
19
20     public function test_default_editor_is_wysiwyg_for_new_pages()
21     {
22         $this->assertEquals('wysiwyg', setting('app-editor'));
23         $resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page'));
24         $this->withHtml($this->followRedirects($resp))->assertElementExists('#html-editor');
25     }
26
27     public function test_editor_set_for_new_pages()
28     {
29         $book = $this->page->book;
30
31         $this->asEditor()->get($book->getUrl('/create-page'));
32         $newPage = $book->pages()->orderBy('id', 'desc')->first();
33         $this->assertEquals('wysiwyg', $newPage->editor);
34
35         $this->setSettings(['app-editor' => PageEditorType::Markdown->value]);
36
37         $this->asEditor()->get($book->getUrl('/create-page'));
38         $newPage = $book->pages()->orderBy('id', 'desc')->first();
39         $this->assertEquals('markdown', $newPage->editor);
40     }
41
42     public function test_markdown_setting_shows_markdown_editor_for_new_pages()
43     {
44         $this->setSettings(['app-editor' => PageEditorType::Markdown->value]);
45
46         $resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page'));
47         $this->withHtml($this->followRedirects($resp))
48             ->assertElementNotExists('#html-editor')
49             ->assertElementExists('#markdown-editor');
50     }
51
52     public function test_markdown_content_given_to_editor()
53     {
54         $mdContent = '# hello. This is a test';
55         $this->page->markdown = $mdContent;
56         $this->page->editor = PageEditorType::Markdown;
57         $this->page->save();
58
59         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
60         $this->withHtml($resp)->assertElementContains('[name="markdown"]', $mdContent);
61     }
62
63     public function test_html_content_given_to_editor_if_no_markdown()
64     {
65         $this->page->editor = 'markdown';
66         $this->page->save();
67
68         $resp = $this->asAdmin()->get($this->page->getUrl() . '/edit');
69         $this->withHtml($resp)->assertElementContains('[name="markdown"]', $this->page->html);
70     }
71
72     public function test_empty_markdown_still_saves_without_error()
73     {
74         $this->setSettings(['app-editor' => 'markdown']);
75         $book = $this->entities->book();
76
77         $this->asEditor()->get($book->getUrl('/create-page'));
78         $draft = Page::query()->where('book_id', '=', $book->id)
79             ->where('draft', '=', true)->first();
80
81         $details = [
82             'name'     => 'my page',
83             'markdown' => '',
84         ];
85         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
86         $resp->assertRedirect();
87
88         $this->assertDatabaseHas('pages', [
89             'markdown' => $details['markdown'],
90             'id'       => $draft->id,
91             'draft'    => false,
92         ]);
93     }
94
95     public function test_back_link_in_editor_has_correct_url()
96     {
97         $book = $this->entities->bookHasChaptersAndPages();
98         $this->asEditor()->get($book->getUrl('/create-page'));
99         /** @var Chapter $chapter */
100         $chapter = $book->chapters()->firstOrFail();
101         /** @var Page $draft */
102         $draft = $book->pages()->where('draft', '=', true)->firstOrFail();
103
104         // Book draft goes back to book
105         $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
106         $this->withHtml($resp)->assertElementContains('a[href="' . $book->getUrl() . '"]', 'Back');
107
108         // Chapter draft goes back to chapter
109         $draft->chapter_id = $chapter->id;
110         $draft->save();
111         $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
112         $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl() . '"]', 'Back');
113
114         // Saved page goes back to page
115         $this->post($book->getUrl("/draft/{$draft->id}"), ['name' => 'Updated', 'html' => 'Updated']);
116         $draft->refresh();
117         $resp = $this->get($draft->getUrl('/edit'));
118         $this->withHtml($resp)->assertElementContains('a[href="' . $draft->getUrl() . '"]', 'Back');
119     }
120
121     public function test_switching_from_html_to_clean_markdown_works()
122     {
123         $page = $this->entities->page();
124         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
125         $page->save();
126
127         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=markdown-clean'));
128         $resp->assertStatus(200);
129         $resp->assertSee("## A Header\n\nSome **bold** content.");
130         $this->withHtml($resp)->assertElementExists('#markdown-editor');
131     }
132
133     public function test_switching_from_html_to_stable_markdown_works()
134     {
135         $page = $this->entities->page();
136         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
137         $page->save();
138
139         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=markdown-stable'));
140         $resp->assertStatus(200);
141         $resp->assertSee('<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>', true);
142         $this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
143     }
144
145     public function test_switching_from_markdown_to_wysiwyg_works()
146     {
147         $page = $this->entities->page();
148         $page->html = '';
149         $page->markdown = "## A Header\n\nSome content with **bold** text!";
150         $page->save();
151
152         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=wysiwyg'));
153         $resp->assertStatus(200);
154         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor-tinymce"]');
155         $resp->assertSee("<h2>A Header</h2>\n<p>Some content with <strong>bold</strong> text!</p>", true);
156     }
157
158     public function test_switching_from_markdown_to_wysiwyg2024_works()
159     {
160         $page = $this->entities->page();
161         $page->html = '';
162         $page->markdown = "## A Header\n\nSome content with **bold** text!";
163         $page->save();
164
165         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=wysiwyg2024'));
166         $resp->assertStatus(200);
167         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
168         $resp->assertSee("<h2>A Header</h2>\n<p>Some content with <strong>bold</strong> text!</p>", true);
169     }
170
171     public function test_page_editor_changes_with_editor_property()
172     {
173         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
174         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor-tinymce"]');
175
176         $this->page->markdown = "## A Header\n\nSome content with **bold** text!";
177         $this->page->editor = 'markdown';
178         $this->page->save();
179
180         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
181         $this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
182
183         $this->page->editor = 'wysiwyg2024';
184         $this->page->save();
185
186         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
187         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
188     }
189
190     public function test_editor_type_switch_options_show()
191     {
192         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
193         $editLink = $this->page->getUrl('/edit') . '?editor=';
194         $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}markdown-clean\"]", '(Clean Content)');
195         $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}markdown-stable\"]", '(Stable Content)');
196         $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}wysiwyg2024\"]", '(In Alpha Testing)');
197
198         $resp = $this->asAdmin()->get($this->page->getUrl('/edit?editor=markdown-stable'));
199         $editLink = $this->page->getUrl('/edit') . '?editor=';
200         $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}wysiwyg\"]", 'Switch to WYSIWYG Editor');
201     }
202
203     public function test_editor_type_switch_options_dont_show_if_without_change_editor_permissions()
204     {
205         $resp = $this->asEditor()->get($this->page->getUrl('/edit'));
206         $editLink = $this->page->getUrl('/edit') . '?editor=';
207         $this->withHtml($resp)->assertElementNotExists("a[href*=\"{$editLink}\"]");
208     }
209
210     public function test_page_editor_type_switch_does_not_work_without_change_editor_permissions()
211     {
212         $page = $this->entities->page();
213         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
214         $page->save();
215
216         $resp = $this->asEditor()->get($page->getUrl('/edit?editor=markdown-stable'));
217         $resp->assertStatus(200);
218         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor-tinymce"]');
219         $this->withHtml($resp)->assertElementNotExists('[component="markdown-editor"]');
220     }
221
222     public function test_page_save_does_not_change_active_editor_without_change_editor_permissions()
223     {
224         $page = $this->entities->page();
225         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
226         $page->editor = 'wysiwyg';
227         $page->save();
228
229         $this->asEditor()->put($page->getUrl(), ['name' => $page->name, 'markdown' => '## Updated content abc']);
230         $this->assertEquals('wysiwyg', $page->refresh()->editor);
231     }
232
233     public function test_editor_type_change_to_wysiwyg_infers_type_from_request_or_uses_system_default()
234     {
235         $tests = [
236             [
237                 'setting' => 'wysiwyg',
238                 'request' => 'wysiwyg2024',
239                 'expected' => 'wysiwyg2024',
240             ],
241             [
242                 'setting' => 'wysiwyg2024',
243                 'request' => 'wysiwyg',
244                 'expected' => 'wysiwyg',
245             ],
246             [
247                 'setting' => 'wysiwyg',
248                 'request' => null,
249                 'expected' => 'wysiwyg',
250             ],
251             [
252                 'setting' => 'wysiwyg2024',
253                 'request' => null,
254                 'expected' => 'wysiwyg2024',
255             ]
256         ];
257
258         $page = $this->entities->page();
259         foreach ($tests as $test) {
260             $page->editor = 'markdown';
261             $page->save();
262
263             $this->setSettings(['app-editor' => $test['setting']]);
264             $this->asAdmin()->put($page->getUrl(), ['name' => $page->name, 'html' => '<p>Hello</p>', 'editor' => $test['request']]);
265             $this->assertEquals($test['expected'], $page->refresh()->editor, "Failed asserting global editor {$test['setting']} with request editor {$test['request']} results in {$test['expected']} set for the page");
266         }
267     }
268 }