]> BookStack Code Mirror - bookstack/blob - tests/Exports/MarkdownExportTest.php
Drawings: Added class to extract drawio data from png files
[bookstack] / tests / Exports / MarkdownExportTest.php
1 <?php
2
3 namespace Tests\Exports;
4
5 use BookStack\Entities\Models\Book;
6 use Tests\TestCase;
7
8 class MarkdownExportTest extends TestCase
9 {
10     public function test_page_markdown_export()
11     {
12         $page = $this->entities->page();
13
14         $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
15         $resp->assertStatus(200);
16         $resp->assertSee($page->name);
17         $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
18     }
19
20     public function test_page_markdown_export_uses_existing_markdown_if_apparent()
21     {
22         $page = $this->entities->page()->forceFill([
23             'markdown' => '# A header',
24             'html'     => '<h1>Dogcat</h1>',
25         ]);
26         $page->save();
27
28         $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
29         $resp->assertSee('A header');
30         $resp->assertDontSee('Dogcat');
31     }
32
33     public function test_page_markdown_export_converts_html_where_no_markdown()
34     {
35         $page = $this->entities->page()->forceFill([
36             'markdown' => '',
37             'html'     => '<h1>Dogcat</h1><p>Some <strong>bold</strong> text</p>',
38         ]);
39         $page->save();
40
41         $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
42         $resp->assertSee("# Dogcat\n\nSome **bold** text");
43     }
44
45     public function test_chapter_markdown_export()
46     {
47         $chapter = $this->entities->chapter();
48         $chapter->description_html = '<p>My <strong>chapter</strong> description</p>';
49         $chapter->save();
50         $page = $chapter->pages()->first();
51
52         $resp = $this->asEditor()->get($chapter->getUrl('/export/markdown'));
53
54         $resp->assertSee('# ' . $chapter->name);
55         $resp->assertSee('# ' . $page->name);
56         $resp->assertSee('My **chapter** description');
57     }
58
59     public function test_book_markdown_export()
60     {
61         $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
62         $book->description_html = '<p>My <strong>book</strong> description</p>';
63         $book->save();
64
65         $chapter = $book->chapters()->first();
66         $chapter->description_html = '<p>My <strong>chapter</strong> description</p>';
67         $chapter->save();
68
69         $page = $chapter->pages()->first();
70         $resp = $this->asEditor()->get($book->getUrl('/export/markdown'));
71
72         $resp->assertSee('# ' . $book->name);
73         $resp->assertSee('# ' . $chapter->name);
74         $resp->assertSee('# ' . $page->name);
75         $resp->assertSee('My **book** description');
76         $resp->assertSee('My **chapter** description');
77     }
78
79     public function test_book_markdown_export_concats_immediate_pages_with_newlines()
80     {
81         /** @var Book $book */
82         $book = Book::query()->whereHas('pages')->first();
83
84         $this->asEditor()->get($book->getUrl('/create-page'));
85         $this->get($book->getUrl('/create-page'));
86
87         [$pageA, $pageB] = $book->pages()->where('chapter_id', '=', 0)->get();
88         $pageA->html = '<p>hello tester</p>';
89         $pageA->save();
90         $pageB->name = 'The second page in this test';
91         $pageB->save();
92
93         $resp = $this->get($book->getUrl('/export/markdown'));
94         $resp->assertDontSee('hello tester# The second page in this test');
95         $resp->assertSee("hello tester\n\n# The second page in this test");
96     }
97 }