]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageEditorTest.php
6ce649a5420239d60dfcae79de6ae4a3387d7e2e
[bookstack] / tests / Entity / PageEditorTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use Tests\TestCase;
9
10 class PageEditorTest extends TestCase
11 {
12     /** @var Page */
13     protected $page;
14
15     protected function setUp(): void
16     {
17         parent::setUp();
18         $this->page = Page::query()->first();
19     }
20
21     public function test_default_editor_is_wysiwyg_for_new_pages()
22     {
23         $this->assertEquals('wysiwyg', setting('app-editor'));
24         $resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page'));
25         $this->withHtml($this->followRedirects($resp))->assertElementExists('#html-editor');
26     }
27
28     public function test_markdown_setting_shows_markdown_editor_for_new_pages()
29     {
30         $this->setSettings(['app-editor' => 'markdown']);
31
32         $resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page'));
33         $this->withHtml($this->followRedirects($resp))
34             ->assertElementNotExists('#html-editor')
35             ->assertElementExists('#markdown-editor');
36     }
37
38     public function test_markdown_content_given_to_editor()
39     {
40         $mdContent = '# hello. This is a test';
41         $this->page->markdown = $mdContent;
42         $this->page->editor = 'markdown';
43         $this->page->save();
44
45         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
46         $this->withHtml($resp)->assertElementContains('[name="markdown"]', $mdContent);
47     }
48
49     public function test_html_content_given_to_editor_if_no_markdown()
50     {
51         $this->page->editor = 'markdown';
52         $this->page->save();
53
54         $resp = $this->asAdmin()->get($this->page->getUrl() . '/edit');
55         $this->withHtml($resp)->assertElementContains('[name="markdown"]', $this->page->html);
56     }
57
58     public function test_empty_markdown_still_saves_without_error()
59     {
60         $this->setSettings(['app-editor' => 'markdown']);
61         $book = $this->entities->book();
62
63         $this->asEditor()->get($book->getUrl('/create-page'));
64         $draft = Page::query()->where('book_id', '=', $book->id)
65             ->where('draft', '=', true)->first();
66
67         $details = [
68             'name'     => 'my page',
69             'markdown' => '',
70         ];
71         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
72         $resp->assertRedirect();
73
74         $this->assertDatabaseHas('pages', [
75             'markdown' => $details['markdown'],
76             'id'       => $draft->id,
77             'draft'    => false,
78         ]);
79     }
80
81     public function test_back_link_in_editor_has_correct_url()
82     {
83         /** @var Book $book */
84         $book = Book::query()->whereHas('pages')->whereHas('chapters')->firstOrFail();
85         $this->asEditor()->get($book->getUrl('/create-page'));
86         /** @var Chapter $chapter */
87         $chapter = $book->chapters()->firstOrFail();
88         /** @var Page $draft */
89         $draft = $book->pages()->where('draft', '=', true)->firstOrFail();
90
91         // Book draft goes back to book
92         $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
93         $this->withHtml($resp)->assertElementContains('a[href="' . $book->getUrl() . '"]', 'Back');
94
95         // Chapter draft goes back to chapter
96         $draft->chapter_id = $chapter->id;
97         $draft->save();
98         $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
99         $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl() . '"]', 'Back');
100
101         // Saved page goes back to page
102         $this->post($book->getUrl("/draft/{$draft->id}"), ['name' => 'Updated', 'html' => 'Updated']);
103         $draft->refresh();
104         $resp = $this->get($draft->getUrl('/edit'));
105         $this->withHtml($resp)->assertElementContains('a[href="' . $draft->getUrl() . '"]', 'Back');
106     }
107
108     public function test_switching_from_html_to_clean_markdown_works()
109     {
110         $page = $this->entities->page();
111         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
112         $page->save();
113
114         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=markdown-clean'));
115         $resp->assertStatus(200);
116         $resp->assertSee("## A Header\n\nSome **bold** content.");
117         $this->withHtml($resp)->assertElementExists('#markdown-editor');
118     }
119
120     public function test_switching_from_html_to_stable_markdown_works()
121     {
122         $page = $this->entities->page();
123         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
124         $page->save();
125
126         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=markdown-stable'));
127         $resp->assertStatus(200);
128         $resp->assertSee('<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>', true);
129         $this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
130     }
131
132     public function test_switching_from_markdown_to_wysiwyg_works()
133     {
134         $page = $this->entities->page();
135         $page->html = '';
136         $page->markdown = "## A Header\n\nSome content with **bold** text!";
137         $page->save();
138
139         $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=wysiwyg'));
140         $resp->assertStatus(200);
141         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
142         $resp->assertSee("<h2>A Header</h2>\n<p>Some content with <strong>bold</strong> text!</p>", true);
143     }
144
145     public function test_page_editor_changes_with_editor_property()
146     {
147         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
148         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
149
150         $this->page->markdown = "## A Header\n\nSome content with **bold** text!";
151         $this->page->editor = 'markdown';
152         $this->page->save();
153
154         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
155         $this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
156     }
157
158     public function test_editor_type_switch_options_show()
159     {
160         $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
161         $editLink = $this->page->getUrl('/edit') . '?editor=';
162         $this->withHtml($resp)->assertElementContains("a[href=\"${editLink}markdown-clean\"]", '(Clean Content)');
163         $this->withHtml($resp)->assertElementContains("a[href=\"${editLink}markdown-stable\"]", '(Stable Content)');
164
165         $resp = $this->asAdmin()->get($this->page->getUrl('/edit?editor=markdown-stable'));
166         $editLink = $this->page->getUrl('/edit') . '?editor=';
167         $this->withHtml($resp)->assertElementContains("a[href=\"${editLink}wysiwyg\"]", 'Switch to WYSIWYG Editor');
168     }
169
170     public function test_editor_type_switch_options_dont_show_if_without_change_editor_permissions()
171     {
172         $resp = $this->asEditor()->get($this->page->getUrl('/edit'));
173         $editLink = $this->page->getUrl('/edit') . '?editor=';
174         $this->withHtml($resp)->assertElementNotExists("a[href*=\"${editLink}\"]");
175     }
176
177     public function test_page_editor_type_switch_does_not_work_without_change_editor_permissions()
178     {
179         $page = $this->entities->page();
180         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
181         $page->save();
182
183         $resp = $this->asEditor()->get($page->getUrl('/edit?editor=markdown-stable'));
184         $resp->assertStatus(200);
185         $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
186         $this->withHtml($resp)->assertElementNotExists('[component="markdown-editor"]');
187     }
188
189     public function test_page_save_does_not_change_active_editor_without_change_editor_permissions()
190     {
191         $page = $this->entities->page();
192         $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
193         $page->editor = 'wysiwyg';
194         $page->save();
195
196         $this->asEditor()->put($page->getUrl(), ['name' => $page->name, 'markdown' => '## Updated content abc']);
197         $this->assertEquals('wysiwyg', $page->refresh()->editor);
198     }
199 }