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