2021-09-13 17:54:21 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Entity;
|
|
|
|
|
2021-09-15 17:18:37 -04:00
|
|
|
use BookStack\Entities\Models\Book;
|
2021-11-15 06:17:27 -05:00
|
|
|
use BookStack\Entities\Models\Chapter;
|
2021-09-13 17:54:21 -04:00
|
|
|
use BookStack\Entities\Models\Page;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class PageEditorTest extends TestCase
|
|
|
|
{
|
2021-09-18 16:21:44 -04:00
|
|
|
/** @var Page */
|
2021-09-13 17:54:21 -04:00
|
|
|
protected $page;
|
|
|
|
|
2021-10-30 16:29:59 -04:00
|
|
|
protected function setUp(): void
|
2021-09-13 17:54:21 -04:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->page = Page::query()->first();
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
public function test_default_editor_is_wysiwyg_for_new_pages()
|
2021-09-13 17:54:21 -04:00
|
|
|
{
|
|
|
|
$this->assertEquals('wysiwyg', setting('app-editor'));
|
2022-04-23 18:20:46 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($this->followRedirects($resp))->assertElementExists('#html-editor');
|
2021-09-13 17:54:21 -04:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
public function test_markdown_setting_shows_markdown_editor_for_new_pages()
|
2021-09-13 17:54:21 -04:00
|
|
|
{
|
|
|
|
$this->setSettings(['app-editor' => 'markdown']);
|
2022-04-23 18:20:46 -04:00
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get($this->page->book->getUrl('/create-page'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($this->followRedirects($resp))
|
2021-09-13 17:54:21 -04:00
|
|
|
->assertElementNotExists('#html-editor')
|
|
|
|
->assertElementExists('#markdown-editor');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_markdown_content_given_to_editor()
|
|
|
|
{
|
|
|
|
$mdContent = '# hello. This is a test';
|
|
|
|
$this->page->markdown = $mdContent;
|
2022-04-23 18:20:46 -04:00
|
|
|
$this->page->editor = 'markdown';
|
2021-09-13 17:54:21 -04:00
|
|
|
$this->page->save();
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
|
|
|
|
$this->withHtml($resp)->assertElementContains('[name="markdown"]', $mdContent);
|
2021-09-13 17:54:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_html_content_given_to_editor_if_no_markdown()
|
|
|
|
{
|
2022-04-23 18:20:46 -04:00
|
|
|
$this->page->editor = 'markdown';
|
|
|
|
$this->page->save();
|
|
|
|
|
2022-07-23 10:10:18 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->getUrl() . '/edit');
|
|
|
|
$this->withHtml($resp)->assertElementContains('[name="markdown"]', $this->page->html);
|
2021-09-13 17:54:21 -04:00
|
|
|
}
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
public function test_empty_markdown_still_saves_without_error()
|
|
|
|
{
|
|
|
|
$this->setSettings(['app-editor' => 'markdown']);
|
2022-09-29 12:31:38 -04:00
|
|
|
$book = $this->entities->book();
|
2021-09-15 17:18:37 -04:00
|
|
|
|
|
|
|
$this->asEditor()->get($book->getUrl('/create-page'));
|
|
|
|
$draft = Page::query()->where('book_id', '=', $book->id)
|
|
|
|
->where('draft', '=', true)->first();
|
|
|
|
|
|
|
|
$details = [
|
|
|
|
'name' => 'my page',
|
|
|
|
'markdown' => '',
|
|
|
|
];
|
|
|
|
$resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
|
|
|
|
$resp->assertRedirect();
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('pages', [
|
|
|
|
'markdown' => $details['markdown'],
|
|
|
|
'id' => $draft->id,
|
|
|
|
'draft' => false,
|
|
|
|
]);
|
|
|
|
}
|
2021-11-15 06:17:27 -05:00
|
|
|
|
|
|
|
public function test_back_link_in_editor_has_correct_url()
|
|
|
|
{
|
|
|
|
/** @var Book $book */
|
|
|
|
$book = Book::query()->whereHas('pages')->whereHas('chapters')->firstOrFail();
|
|
|
|
$this->asEditor()->get($book->getUrl('/create-page'));
|
|
|
|
/** @var Chapter $chapter */
|
|
|
|
$chapter = $book->chapters()->firstOrFail();
|
|
|
|
/** @var Page $draft */
|
|
|
|
$draft = $book->pages()->where('draft', '=', true)->firstOrFail();
|
|
|
|
|
|
|
|
// Book draft goes back to book
|
|
|
|
$resp = $this->get($book->getUrl("/draft/{$draft->id}"));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href="' . $book->getUrl() . '"]', 'Back');
|
2021-11-15 06:17:27 -05:00
|
|
|
|
|
|
|
// Chapter draft goes back to chapter
|
|
|
|
$draft->chapter_id = $chapter->id;
|
|
|
|
$draft->save();
|
|
|
|
$resp = $this->get($book->getUrl("/draft/{$draft->id}"));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href="' . $chapter->getUrl() . '"]', 'Back');
|
2021-11-15 06:17:27 -05:00
|
|
|
|
|
|
|
// Saved page goes back to page
|
|
|
|
$this->post($book->getUrl("/draft/{$draft->id}"), ['name' => 'Updated', 'html' => 'Updated']);
|
|
|
|
$draft->refresh();
|
|
|
|
$resp = $this->get($draft->getUrl('/edit'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains('a[href="' . $draft->getUrl() . '"]', 'Back');
|
2021-11-15 06:17:27 -05:00
|
|
|
}
|
2022-04-23 09:22:04 -04:00
|
|
|
|
|
|
|
public function test_switching_from_html_to_clean_markdown_works()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-04-23 09:22:04 -04:00
|
|
|
$page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
|
|
|
|
$page->save();
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get($page->getUrl('/edit?editor=markdown-clean'));
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSee("## A Header\n\nSome **bold** content.");
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('#markdown-editor');
|
2022-04-23 09:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_switching_from_html_to_stable_markdown_works()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-04-23 09:22:04 -04:00
|
|
|
$page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
|
|
|
|
$page->save();
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get($page->getUrl('/edit?editor=markdown-stable'));
|
|
|
|
$resp->assertStatus(200);
|
2022-04-24 13:22:40 -04:00
|
|
|
$resp->assertSee('<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>', true);
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
|
2022-04-23 09:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_switching_from_markdown_to_wysiwyg_works()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-04-23 09:22:04 -04:00
|
|
|
$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);
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
|
2022-04-23 09:22:04 -04:00
|
|
|
$resp->assertSee("<h2>A Header</h2>\n<p>Some content with <strong>bold</strong> text!</p>", true);
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
public function test_page_editor_changes_with_editor_property()
|
2022-04-23 09:22:04 -04:00
|
|
|
{
|
2022-04-23 18:20:46 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
|
2022-04-23 09:22:04 -04:00
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
$this->page->markdown = "## A Header\n\nSome content with **bold** text!";
|
|
|
|
$this->page->editor = 'markdown';
|
|
|
|
$this->page->save();
|
2022-04-23 09:22:04 -04:00
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('[component="markdown-editor"]');
|
2022-04-23 09:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_editor_type_switch_options_show()
|
|
|
|
{
|
2022-04-23 18:20:46 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->getUrl('/edit'));
|
|
|
|
$editLink = $this->page->getUrl('/edit') . '?editor=';
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains("a[href=\"${editLink}markdown-clean\"]", '(Clean Content)');
|
|
|
|
$this->withHtml($resp)->assertElementContains("a[href=\"${editLink}markdown-stable\"]", '(Stable Content)');
|
2022-04-23 09:22:04 -04:00
|
|
|
|
2022-04-23 18:20:46 -04:00
|
|
|
$resp = $this->asAdmin()->get($this->page->getUrl('/edit?editor=markdown-stable'));
|
|
|
|
$editLink = $this->page->getUrl('/edit') . '?editor=';
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementContains("a[href=\"${editLink}wysiwyg\"]", 'Switch to WYSIWYG Editor');
|
2022-04-23 09:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_editor_type_switch_options_dont_show_if_without_change_editor_permissions()
|
|
|
|
{
|
2022-04-23 18:20:46 -04:00
|
|
|
$resp = $this->asEditor()->get($this->page->getUrl('/edit'));
|
|
|
|
$editLink = $this->page->getUrl('/edit') . '?editor=';
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementNotExists("a[href*=\"${editLink}\"]");
|
2022-04-23 09:22:04 -04:00
|
|
|
}
|
|
|
|
|
2022-04-23 18:34:15 -04:00
|
|
|
public function test_page_editor_type_switch_does_not_work_without_change_editor_permissions()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-04-23 18:34:15 -04:00
|
|
|
$page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
|
|
|
|
$page->save();
|
|
|
|
|
|
|
|
$resp = $this->asEditor()->get($page->getUrl('/edit?editor=markdown-stable'));
|
|
|
|
$resp->assertStatus(200);
|
2022-07-23 10:10:18 -04:00
|
|
|
$this->withHtml($resp)->assertElementExists('[component="wysiwyg-editor"]');
|
|
|
|
$this->withHtml($resp)->assertElementNotExists('[component="markdown-editor"]');
|
2022-04-23 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_page_save_does_not_change_active_editor_without_change_editor_permissions()
|
|
|
|
{
|
2022-09-29 12:31:38 -04:00
|
|
|
$page = $this->entities->page();
|
2022-04-23 18:34:15 -04:00
|
|
|
$page->html = '<h2>A Header</h2><p>Some <strong>bold</strong> content.</p>';
|
|
|
|
$page->editor = 'wysiwyg';
|
|
|
|
$page->save();
|
|
|
|
|
|
|
|
$this->asEditor()->put($page->getUrl(), ['name' => $page->name, 'markdown' => '## Updated content abc']);
|
|
|
|
$this->assertEquals('wysiwyg', $page->refresh()->editor);
|
|
|
|
}
|
2021-09-18 16:21:44 -04:00
|
|
|
}
|