3 namespace Tests\Exports;
5 use BookStack\Entities\Models\Book;
8 class MarkdownExportTest extends TestCase
10 public function test_page_markdown_export()
12 $page = $this->entities->page();
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"');
20 public function test_page_markdown_export_uses_existing_markdown_if_apparent()
22 $page = $this->entities->page()->forceFill([
23 'markdown' => '# A header',
24 'html' => '<h1>Dogcat</h1>',
28 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
29 $resp->assertSee('A header');
30 $resp->assertDontSee('Dogcat');
33 public function test_page_markdown_export_converts_html_where_no_markdown()
35 $page = $this->entities->page()->forceFill([
37 'html' => '<h1>Dogcat</h1><p>Some <strong>bold</strong> text</p>',
41 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
42 $resp->assertSee("# Dogcat\n\nSome **bold** text");
45 public function test_chapter_markdown_export()
47 $chapter = $this->entities->chapter();
48 $chapter->description_html = '<p>My <strong>chapter</strong> description</p>';
50 $page = $chapter->pages()->first();
52 $resp = $this->asEditor()->get($chapter->getUrl('/export/markdown'));
54 $resp->assertSee('# ' . $chapter->name);
55 $resp->assertSee('# ' . $page->name);
56 $resp->assertSee('My **chapter** description');
59 public function test_book_markdown_export()
61 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
62 $book->description_html = '<p>My <strong>book</strong> description</p>';
65 $chapter = $book->chapters()->first();
66 $chapter->description_html = '<p>My <strong>chapter</strong> description</p>';
69 $page = $chapter->pages()->first();
70 $resp = $this->asEditor()->get($book->getUrl('/export/markdown'));
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');
79 public function test_book_markdown_export_concats_immediate_pages_with_newlines()
81 /** @var Book $book */
82 $book = Book::query()->whereHas('pages')->first();
84 $this->asEditor()->get($book->getUrl('/create-page'));
85 $this->get($book->getUrl('/create-page'));
87 [$pageA, $pageB] = $book->pages()->where('chapter_id', '=', 0)->get();
88 $pageA->html = '<p>hello tester</p>';
90 $pageB->name = 'The second page in this test';
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");