X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ee24635e06a8c01d751f80caba47c57f76e8989d..HEAD:/tests/Entity/PageEditorTest.php diff --git a/tests/Entity/PageEditorTest.php b/tests/Entity/PageEditorTest.php index e5aa549b3..ad753c966 100644 --- a/tests/Entity/PageEditorTest.php +++ b/tests/Entity/PageEditorTest.php @@ -2,20 +2,19 @@ namespace Tests\Entity; -use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Page; +use BookStack\Entities\Tools\PageEditorType; use Tests\TestCase; class PageEditorTest extends TestCase { - /** @var Page */ - protected $page; + protected Page $page; protected function setUp(): void { parent::setUp(); - $this->page = Page::query()->first(); + $this->page = $this->entities->page(); } public function test_default_editor_is_wysiwyg_for_new_pages() @@ -25,9 +24,24 @@ class PageEditorTest extends TestCase $this->withHtml($this->followRedirects($resp))->assertElementExists('#html-editor'); } + public function test_editor_set_for_new_pages() + { + $book = $this->page->book; + + $this->asEditor()->get($book->getUrl('/create-page')); + $newPage = $book->pages()->orderBy('id', 'desc')->first(); + $this->assertEquals('wysiwyg', $newPage->editor); + + $this->setSettings(['app-editor' => PageEditorType::Markdown->value]); + + $this->asEditor()->get($book->getUrl('/create-page')); + $newPage = $book->pages()->orderBy('id', 'desc')->first(); + $this->assertEquals('markdown', $newPage->editor); + } + public function test_markdown_setting_shows_markdown_editor_for_new_pages() { - $this->setSettings(['app-editor' => 'markdown']); + $this->setSettings(['app-editor' => PageEditorType::Markdown->value]); $resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page')); $this->withHtml($this->followRedirects($resp)) @@ -39,7 +53,7 @@ class PageEditorTest extends TestCase { $mdContent = '# hello. This is a test'; $this->page->markdown = $mdContent; - $this->page->editor = 'markdown'; + $this->page->editor = PageEditorType::Markdown; $this->page->save(); $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); @@ -58,8 +72,7 @@ class PageEditorTest extends TestCase public function test_empty_markdown_still_saves_without_error() { $this->setSettings(['app-editor' => 'markdown']); - /** @var Book $book */ - $book = Book::query()->first(); + $book = $this->entities->book(); $this->asEditor()->get($book->getUrl('/create-page')); $draft = Page::query()->where('book_id', '=', $book->id) @@ -81,8 +94,7 @@ class PageEditorTest extends TestCase public function test_back_link_in_editor_has_correct_url() { - /** @var Book $book */ - $book = Book::query()->whereHas('pages')->whereHas('chapters')->firstOrFail(); + $book = $this->entities->bookHasChaptersAndPages(); $this->asEditor()->get($book->getUrl('/create-page')); /** @var Chapter $chapter */ $chapter = $book->chapters()->firstOrFail(); @@ -108,8 +120,7 @@ class PageEditorTest extends TestCase public function test_switching_from_html_to_clean_markdown_works() { - /** @var Page $page */ - $page = Page::query()->first(); + $page = $this->entities->page(); $page->html = '

A Header

Some bold content.

'; $page->save(); @@ -121,8 +132,7 @@ class PageEditorTest extends TestCase public function test_switching_from_html_to_stable_markdown_works() { - /** @var Page $page */ - $page = Page::query()->first(); + $page = $this->entities->page(); $page->html = '

A Header

Some bold content.

'; $page->save(); @@ -134,14 +144,26 @@ class PageEditorTest extends TestCase public function test_switching_from_markdown_to_wysiwyg_works() { - /** @var Page $page */ - $page = Page::query()->first(); + $page = $this->entities->page(); $page->html = ''; $page->markdown = "## A Header\n\nSome content with **bold** text!"; $page->save(); $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=wysiwyg')); $resp->assertStatus(200); + $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor-tinymce"]'); + $resp->assertSee("

A Header

\n

Some content with bold text!

", true); + } + + public function test_switching_from_markdown_to_wysiwyg2024_works() + { + $page = $this->entities->page(); + $page->html = ''; + $page->markdown = "## A Header\n\nSome content with **bold** text!"; + $page->save(); + + $resp = $this->asAdmin()->get($page->getUrl('/edit?editor=wysiwyg2024')); + $resp->assertStatus(200); $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]'); $resp->assertSee("

A Header

\n

Some content with bold text!

", true); } @@ -149,7 +171,7 @@ class PageEditorTest extends TestCase public function test_page_editor_changes_with_editor_property() { $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); - $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]'); + $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor-tinymce"]'); $this->page->markdown = "## A Header\n\nSome content with **bold** text!"; $this->page->editor = 'markdown'; @@ -157,44 +179,49 @@ class PageEditorTest extends TestCase $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); $this->withHtml($resp)->assertElementExists('[component="markdown-editor"]'); + + $this->page->editor = 'wysiwyg2024'; + $this->page->save(); + + $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); + $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]'); } public function test_editor_type_switch_options_show() { $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); $editLink = $this->page->getUrl('/edit') . '?editor='; - $this->withHtml($resp)->assertElementContains("a[href=\"${editLink}markdown-clean\"]", '(Clean Content)'); - $this->withHtml($resp)->assertElementContains("a[href=\"${editLink}markdown-stable\"]", '(Stable Content)'); + $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}markdown-clean\"]", '(Clean Content)'); + $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}markdown-stable\"]", '(Stable Content)'); + $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}wysiwyg2024\"]", '(In Beta Testing)'); $resp = $this->asAdmin()->get($this->page->getUrl('/edit?editor=markdown-stable')); $editLink = $this->page->getUrl('/edit') . '?editor='; - $this->withHtml($resp)->assertElementContains("a[href=\"${editLink}wysiwyg\"]", 'Switch to WYSIWYG Editor'); + $this->withHtml($resp)->assertElementContains("a[href=\"{$editLink}wysiwyg\"]", 'Switch to WYSIWYG Editor'); } public function test_editor_type_switch_options_dont_show_if_without_change_editor_permissions() { $resp = $this->asEditor()->get($this->page->getUrl('/edit')); $editLink = $this->page->getUrl('/edit') . '?editor='; - $this->withHtml($resp)->assertElementNotExists("a[href*=\"${editLink}\"]"); + $this->withHtml($resp)->assertElementNotExists("a[href*=\"{$editLink}\"]"); } public function test_page_editor_type_switch_does_not_work_without_change_editor_permissions() { - /** @var Page $page */ - $page = Page::query()->first(); + $page = $this->entities->page(); $page->html = '

A Header

Some bold content.

'; $page->save(); $resp = $this->asEditor()->get($page->getUrl('/edit?editor=markdown-stable')); $resp->assertStatus(200); - $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]'); + $this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor-tinymce"]'); $this->withHtml($resp)->assertElementNotExists('[component="markdown-editor"]'); } public function test_page_save_does_not_change_active_editor_without_change_editor_permissions() { - /** @var Page $page */ - $page = Page::query()->first(); + $page = $this->entities->page(); $page->html = '

A Header

Some bold content.

'; $page->editor = 'wysiwyg'; $page->save(); @@ -202,4 +229,40 @@ class PageEditorTest extends TestCase $this->asEditor()->put($page->getUrl(), ['name' => $page->name, 'markdown' => '## Updated content abc']); $this->assertEquals('wysiwyg', $page->refresh()->editor); } + + public function test_editor_type_change_to_wysiwyg_infers_type_from_request_or_uses_system_default() + { + $tests = [ + [ + 'setting' => 'wysiwyg', + 'request' => 'wysiwyg2024', + 'expected' => 'wysiwyg2024', + ], + [ + 'setting' => 'wysiwyg2024', + 'request' => 'wysiwyg', + 'expected' => 'wysiwyg', + ], + [ + 'setting' => 'wysiwyg', + 'request' => null, + 'expected' => 'wysiwyg', + ], + [ + 'setting' => 'wysiwyg2024', + 'request' => null, + 'expected' => 'wysiwyg2024', + ] + ]; + + $page = $this->entities->page(); + foreach ($tests as $test) { + $page->editor = 'markdown'; + $page->save(); + + $this->setSettings(['app-editor' => $test['setting']]); + $this->asAdmin()->put($page->getUrl(), ['name' => $page->name, 'html' => '

Hello

', 'editor' => $test['request']]); + $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"); + } + } }