3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
10 class PageEditorTest extends TestCase
15 protected function setUp(): void
18 $this->page = Page::query()->first();
21 public function test_default_editor_is_wysiwyg_for_new_pages()
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');
28 public function test_markdown_setting_shows_markdown_editor_for_new_pages()
30 $this->setSettings(['app-editor' => 'markdown']);
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');
38 public function test_markdown_content_given_to_editor()
40 $mdContent = '# hello. This is a test';
41 $this->page->markdown = $mdContent;
42 $this->page->editor = 'markdown';
45 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
46 $this->withHtml($resp)->assertElementContains('[name="markdown"]', $mdContent);
49 public function test_html_content_given_to_editor_if_no_markdown()
51 $this->page->editor = 'markdown';
54 $resp = $this->asAdmin()->get($this->page->getUrl() . '/edit');
55 $this->withHtml($resp)->assertElementContains('[name="markdown"]', $this->page->html);
58 public function test_empty_markdown_still_saves_without_error()
60 $this->setSettings(['app-editor' => 'markdown']);
61 $book = $this->entities->book();
63 $this->asEditor()->get($book->getUrl('/create-page'));
64 $draft = Page::query()->where('book_id', '=', $book->id)
65 ->where('draft', '=', true)->first();
71 $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
72 $resp->assertRedirect();
74 $this->assertDatabaseHas('pages', [
75 'markdown' => $details['markdown'],
81 public function test_back_link_in_editor_has_correct_url()
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();
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');
95 // Chapter draft goes back to chapter
96 $draft->chapter_id = $chapter->id;
98 $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
99 $this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl() . '"]', 'Back');
101 // Saved page goes back to page
102 $this->post($book->getUrl("/draft/{$draft->id}"), ['name' => 'Updated', 'html' => 'Updated']);
104 $resp = $this->get($draft->getUrl('/edit'));
105 $this->withHtml($resp)->assertElementContains('a[href="' . $draft->getUrl() . '"]', 'Back');
108 public function test_switching_from_html_to_clean_markdown_works()
110 $page = $this->entities->page();
111 $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
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');
120 public function test_switching_from_html_to_stable_markdown_works()
122 $page = $this->entities->page();
123 $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
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"]');
132 public function test_switching_from_markdown_to_wysiwyg_works()
134 $page = $this->entities->page();
136 $page->markdown = "## A Header\n\nSome content with **bold** text!";
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);
145 public function test_page_editor_changes_with_editor_property()
147 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
148 $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
150 $this->page->markdown = "## A Header\n\nSome content with **bold** text!";
151 $this->page->editor = 'markdown';
154 $resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
155 $this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
158 public function test_editor_type_switch_options_show()
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)');
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');
170 public function test_editor_type_switch_options_dont_show_if_without_change_editor_permissions()
172 $resp = $this->asEditor()->get($this->page->getUrl('/edit'));
173 $editLink = $this->page->getUrl('/edit') . '?editor=';
174 $this->withHtml($resp)->assertElementNotExists("a[href*=\"${editLink}\"]");
177 public function test_page_editor_type_switch_does_not_work_without_change_editor_permissions()
179 $page = $this->entities->page();
180 $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
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"]');
189 public function test_page_save_does_not_change_active_editor_without_change_editor_permissions()
191 $page = $this->entities->page();
192 $page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
193 $page->editor = 'wysiwyg';
196 $this->asEditor()->put($page->getUrl(), ['name' => $page->name, 'markdown' => '## Updated content abc']);
197 $this->assertEquals('wysiwyg', $page->refresh()->editor);