]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageEditorTest.php
Merge branch 'master' of https://github.com/theodor-franke/BookStack into theodor...
[bookstack] / tests / Entity / PageEditorTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use Tests\TestCase;
8
9 class PageEditorTest extends TestCase
10 {
11     /** @var Page */
12     protected $page;
13
14     public function setUp(): void
15     {
16         parent::setUp();
17         $this->page = Page::query()->first();
18     }
19
20     public function test_default_editor_is_wysiwyg()
21     {
22         $this->assertEquals('wysiwyg', setting('app-editor'));
23         $this->asAdmin()->get($this->page->getUrl() . '/edit')
24             ->assertElementExists('#html-editor');
25     }
26
27     public function test_markdown_setting_shows_markdown_editor()
28     {
29         $this->setSettings(['app-editor' => 'markdown']);
30         $this->asAdmin()->get($this->page->getUrl() . '/edit')
31             ->assertElementNotExists('#html-editor')
32             ->assertElementExists('#markdown-editor');
33     }
34
35     public function test_markdown_content_given_to_editor()
36     {
37         $this->setSettings(['app-editor' => 'markdown']);
38
39         $mdContent = '# hello. This is a test';
40         $this->page->markdown = $mdContent;
41         $this->page->save();
42
43         $this->asAdmin()->get($this->page->getUrl() . '/edit')
44             ->assertElementContains('[name="markdown"]', $mdContent);
45     }
46
47     public function test_html_content_given_to_editor_if_no_markdown()
48     {
49         $this->setSettings(['app-editor' => 'markdown']);
50         $this->asAdmin()->get($this->page->getUrl() . '/edit')
51             ->assertElementContains('[name="markdown"]', $this->page->html);
52     }
53
54     public function test_empty_markdown_still_saves_without_error()
55     {
56         $this->setSettings(['app-editor' => 'markdown']);
57         /** @var Book $book */
58         $book = Book::query()->first();
59
60         $this->asEditor()->get($book->getUrl('/create-page'));
61         $draft = Page::query()->where('book_id', '=', $book->id)
62             ->where('draft', '=', true)->first();
63
64         $details = [
65             'name'     => 'my page',
66             'markdown' => '',
67         ];
68         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
69         $resp->assertRedirect();
70
71         $this->assertDatabaseHas('pages', [
72             'markdown' => $details['markdown'],
73             'id'       => $draft->id,
74             'draft'    => false,
75         ]);
76     }
77 }