]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageEditorTest.php
Added 404 response for non-existing setting categories
[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()
22     {
23         $this->assertEquals('wysiwyg', setting('app-editor'));
24         $this->asAdmin()->get($this->page->getUrl() . '/edit')
25             ->assertElementExists('#html-editor');
26     }
27
28     public function test_markdown_setting_shows_markdown_editor()
29     {
30         $this->setSettings(['app-editor' => 'markdown']);
31         $this->asAdmin()->get($this->page->getUrl() . '/edit')
32             ->assertElementNotExists('#html-editor')
33             ->assertElementExists('#markdown-editor');
34     }
35
36     public function test_markdown_content_given_to_editor()
37     {
38         $this->setSettings(['app-editor' => 'markdown']);
39
40         $mdContent = '# hello. This is a test';
41         $this->page->markdown = $mdContent;
42         $this->page->save();
43
44         $this->asAdmin()->get($this->page->getUrl() . '/edit')
45             ->assertElementContains('[name="markdown"]', $mdContent);
46     }
47
48     public function test_html_content_given_to_editor_if_no_markdown()
49     {
50         $this->setSettings(['app-editor' => 'markdown']);
51         $this->asAdmin()->get($this->page->getUrl() . '/edit')
52             ->assertElementContains('[name="markdown"]', $this->page->html);
53     }
54
55     public function test_empty_markdown_still_saves_without_error()
56     {
57         $this->setSettings(['app-editor' => 'markdown']);
58         /** @var Book $book */
59         $book = Book::query()->first();
60
61         $this->asEditor()->get($book->getUrl('/create-page'));
62         $draft = Page::query()->where('book_id', '=', $book->id)
63             ->where('draft', '=', true)->first();
64
65         $details = [
66             'name'     => 'my page',
67             'markdown' => '',
68         ];
69         $resp = $this->post($book->getUrl("/draft/{$draft->id}"), $details);
70         $resp->assertRedirect();
71
72         $this->assertDatabaseHas('pages', [
73             'markdown' => $details['markdown'],
74             'id'       => $draft->id,
75             'draft'    => false,
76         ]);
77     }
78
79     public function test_back_link_in_editor_has_correct_url()
80     {
81         /** @var Book $book */
82         $book = Book::query()->whereHas('pages')->whereHas('chapters')->firstOrFail();
83         $this->asEditor()->get($book->getUrl('/create-page'));
84         /** @var Chapter $chapter */
85         $chapter = $book->chapters()->firstOrFail();
86         /** @var Page $draft */
87         $draft = $book->pages()->where('draft', '=', true)->firstOrFail();
88
89         // Book draft goes back to book
90         $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
91         $resp->assertElementContains('a[href="' . $book->getUrl() . '"]', 'Back');
92
93         // Chapter draft goes back to chapter
94         $draft->chapter_id = $chapter->id;
95         $draft->save();
96         $resp = $this->get($book->getUrl("/draft/{$draft->id}"));
97         $resp->assertElementContains('a[href="' . $chapter->getUrl() . '"]', 'Back');
98
99         // Saved page goes back to page
100         $this->post($book->getUrl("/draft/{$draft->id}"), ['name' => 'Updated', 'html' => 'Updated']);
101         $draft->refresh();
102         $resp = $this->get($draft->getUrl('/edit'));
103         $resp->assertElementContains('a[href="' . $draft->getUrl() . '"]', 'Back');
104     }
105 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.