3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Exceptions\PdfExportException;
9 use BookStack\Exports\PdfGenerator;
10 use Illuminate\Support\Facades\Storage;
13 class ExportTest extends TestCase
15 public function test_page_text_export()
17 $page = $this->entities->page();
20 $resp = $this->get($page->getUrl('/export/plaintext'));
21 $resp->assertStatus(200);
22 $resp->assertSee($page->name);
23 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.txt"');
26 public function test_page_pdf_export()
28 $page = $this->entities->page();
31 $resp = $this->get($page->getUrl('/export/pdf'));
32 $resp->assertStatus(200);
33 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.pdf"');
36 public function test_page_html_export()
38 $page = $this->entities->page();
41 $resp = $this->get($page->getUrl('/export/html'));
42 $resp->assertStatus(200);
43 $resp->assertSee($page->name);
44 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.html"');
47 public function test_book_text_export()
49 $book = $this->entities->bookHasChaptersAndPages();
50 $directPage = $book->directPages()->first();
51 $chapter = $book->chapters()->first();
52 $chapterPage = $chapter->pages()->first();
53 $this->entities->updatePage($directPage, ['html' => '<p>My awesome page</p>']);
54 $this->entities->updatePage($chapterPage, ['html' => '<p>My little nested page</p>']);
57 $resp = $this->get($book->getUrl('/export/plaintext'));
58 $resp->assertStatus(200);
59 $resp->assertSee($book->name);
60 $resp->assertSee($chapterPage->name);
61 $resp->assertSee($chapter->name);
62 $resp->assertSee($directPage->name);
63 $resp->assertSee('My awesome page');
64 $resp->assertSee('My little nested page');
65 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
68 public function test_book_text_export_format()
70 $entities = $this->entities->createChainBelongingToUser($this->users->viewer());
71 $this->entities->updatePage($entities['page'], ['html' => '<p>My great page</p><p>Full of <strong>great</strong> stuff</p>', 'name' => 'My wonderful page!']);
72 $entities['chapter']->name = 'Export chapter';
73 $entities['chapter']->description = "A test chapter to be exported\nIt has loads of info within";
74 $entities['book']->name = 'Export Book';
75 $entities['book']->description = "This is a book with stuff to export";
76 $entities['chapter']->save();
77 $entities['book']->save();
79 $resp = $this->asEditor()->get($entities['book']->getUrl('/export/plaintext'));
81 $expected = "Export Book\nThis is a book with stuff to export\n\nExport chapter\nA test chapter to be exported\nIt has loads of info within\n\n";
82 $expected .= "My wonderful page!\nMy great page Full of great stuff";
83 $resp->assertSee($expected);
86 public function test_book_pdf_export()
88 $page = $this->entities->page();
92 $resp = $this->get($book->getUrl('/export/pdf'));
93 $resp->assertStatus(200);
94 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
97 public function test_book_html_export()
99 $page = $this->entities->page();
103 $resp = $this->get($book->getUrl('/export/html'));
104 $resp->assertStatus(200);
105 $resp->assertSee($book->name);
106 $resp->assertSee($page->name);
107 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
110 public function test_book_html_export_shows_html_descriptions()
112 $book = $this->entities->bookHasChaptersAndPages();
113 $chapter = $book->chapters()->first();
114 $book->description_html = '<p>A description with <strong>HTML</strong> within!</p>';
115 $chapter->description_html = '<p>A chapter description with <strong>HTML</strong> within!</p>';
119 $resp = $this->asEditor()->get($book->getUrl('/export/html'));
120 $resp->assertSee($book->description_html, false);
121 $resp->assertSee($chapter->description_html, false);
124 public function test_chapter_text_export()
126 $chapter = $this->entities->chapter();
127 $page = $chapter->pages[0];
128 $this->entities->updatePage($page, ['html' => '<p>This is content within the page!</p>']);
131 $resp = $this->get($chapter->getUrl('/export/plaintext'));
132 $resp->assertStatus(200);
133 $resp->assertSee($chapter->name);
134 $resp->assertSee($page->name);
135 $resp->assertSee('This is content within the page!');
136 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
139 public function test_chapter_text_export_format()
141 $entities = $this->entities->createChainBelongingToUser($this->users->viewer());
142 $this->entities->updatePage($entities['page'], ['html' => '<p>My great page</p><p>Full of <strong>great</strong> stuff</p>', 'name' => 'My wonderful page!']);
143 $entities['chapter']->name = 'Export chapter';
144 $entities['chapter']->description = "A test chapter to be exported\nIt has loads of info within";
145 $entities['chapter']->save();
147 $resp = $this->asEditor()->get($entities['book']->getUrl('/export/plaintext'));
149 $expected = "Export chapter\nA test chapter to be exported\nIt has loads of info within\n\n";
150 $expected .= "My wonderful page!\nMy great page Full of great stuff";
151 $resp->assertSee($expected);
154 public function test_chapter_pdf_export()
156 $chapter = $this->entities->chapter();
159 $resp = $this->get($chapter->getUrl('/export/pdf'));
160 $resp->assertStatus(200);
161 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
164 public function test_chapter_html_export()
166 $chapter = $this->entities->chapter();
167 $page = $chapter->pages[0];
170 $resp = $this->get($chapter->getUrl('/export/html'));
171 $resp->assertStatus(200);
172 $resp->assertSee($chapter->name);
173 $resp->assertSee($page->name);
174 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
177 public function test_chapter_html_export_shows_html_descriptions()
179 $chapter = $this->entities->chapter();
180 $chapter->description_html = '<p>A description with <strong>HTML</strong> within!</p>';
183 $resp = $this->asEditor()->get($chapter->getUrl('/export/html'));
184 $resp->assertSee($chapter->description_html, false);
187 public function test_page_html_export_contains_custom_head_if_set()
189 $page = $this->entities->page();
191 $customHeadContent = '<style>p{color: red;}</style>';
192 $this->setSettings(['app-custom-head' => $customHeadContent]);
194 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
195 $resp->assertSee($customHeadContent, false);
198 public function test_page_html_export_does_not_break_with_only_comments_in_custom_head()
200 $page = $this->entities->page();
202 $customHeadContent = '<!-- A comment -->';
203 $this->setSettings(['app-custom-head' => $customHeadContent]);
205 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
206 $resp->assertStatus(200);
207 $resp->assertSee($customHeadContent, false);
210 public function test_page_html_export_use_absolute_dates()
212 $page = $this->entities->page();
214 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
215 $resp->assertSee($page->created_at->isoFormat('D MMMM Y HH:mm:ss'));
216 $resp->assertDontSee($page->created_at->diffForHumans());
217 $resp->assertSee($page->updated_at->isoFormat('D MMMM Y HH:mm:ss'));
218 $resp->assertDontSee($page->updated_at->diffForHumans());
221 public function test_page_export_does_not_include_user_or_revision_links()
223 $page = $this->entities->page();
225 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
226 $resp->assertDontSee($page->getUrl('/revisions'));
227 $resp->assertDontSee($page->createdBy->getProfileUrl());
228 $resp->assertSee($page->createdBy->name);
231 public function test_page_export_sets_right_data_type_for_svg_embeds()
233 $page = $this->entities->page();
234 Storage::disk('local')->makeDirectory('uploads/images/gallery');
235 Storage::disk('local')->put('uploads/images/gallery/svg_test.svg', '<svg></svg>');
236 $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg">';
240 $resp = $this->get($page->getUrl('/export/html'));
241 Storage::disk('local')->delete('uploads/images/gallery/svg_test.svg');
243 $resp->assertStatus(200);
244 $resp->assertSee('<img src="data:image/svg+xml;base64', false);
247 public function test_page_image_containment_works_on_multiple_images_within_a_single_line()
249 $page = $this->entities->page();
250 Storage::disk('local')->makeDirectory('uploads/images/gallery');
251 Storage::disk('local')->put('uploads/images/gallery/svg_test.svg', '<svg></svg>');
252 Storage::disk('local')->put('uploads/images/gallery/svg_test2.svg', '<svg></svg>');
253 $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg" class="a"><img src="http://localhost/uploads/images/gallery/svg_test2.svg" class="b">';
256 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
257 Storage::disk('local')->delete('uploads/images/gallery/svg_test.svg');
258 Storage::disk('local')->delete('uploads/images/gallery/svg_test2.svg');
260 $resp->assertDontSee('http://localhost/uploads/images/gallery/svg_test');
263 public function test_page_export_contained_html_image_fetches_only_run_when_url_points_to_image_upload_folder()
265 $page = $this->entities->page();
266 $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg"/>'
267 . '<img src="http://localhost/uploads/svg_test.svg"/>'
268 . '<img src="/uploads/svg_test.svg"/>';
269 $storageDisk = Storage::disk('local');
270 $storageDisk->makeDirectory('uploads/images/gallery');
271 $storageDisk->put('uploads/images/gallery/svg_test.svg', '<svg>good</svg>');
272 $storageDisk->put('uploads/svg_test.svg', '<svg>bad</svg>');
275 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
277 $storageDisk->delete('uploads/images/gallery/svg_test.svg');
278 $storageDisk->delete('uploads/svg_test.svg');
280 $resp->assertDontSee('http://localhost/uploads/images/gallery/svg_test.svg', false);
281 $resp->assertSee('http://localhost/uploads/svg_test.svg');
282 $resp->assertSee('src="/uploads/svg_test.svg"', false);
285 public function test_page_export_contained_html_does_not_allow_upward_traversal_with_local()
287 $contents = file_get_contents(public_path('.htaccess'));
288 config()->set('filesystems.images', 'local');
290 $page = $this->entities->page();
291 $page->html = '<img src="http://localhost/uploads/images/../../.htaccess"/>';
294 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
295 $resp->assertDontSee(base64_encode($contents));
298 public function test_page_export_contained_html_does_not_allow_upward_traversal_with_local_secure()
300 $testFilePath = storage_path('logs/test.txt');
301 config()->set('filesystems.images', 'local_secure');
302 file_put_contents($testFilePath, 'I am a cat');
304 $page = $this->entities->page();
305 $page->html = '<img src="http://localhost/uploads/images/../../logs/test.txt"/>';
308 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
309 $resp->assertDontSee(base64_encode('I am a cat'));
310 unlink($testFilePath);
313 public function test_exports_removes_scripts_from_custom_head()
316 Page::query()->first(), Chapter::query()->first(), Book::query()->first(),
318 setting()->put('app-custom-head', '<script>window.donkey = "cat";</script><style>.my-test-class { color: red; }</style>');
320 foreach ($entities as $entity) {
321 $resp = $this->asEditor()->get($entity->getUrl('/export/html'));
322 $resp->assertDontSee('window.donkey');
323 $resp->assertDontSee('<script', false);
324 $resp->assertSee('.my-test-class { color: red; }');
328 public function test_page_export_with_deleted_creator_and_updater()
330 $user = $this->users->viewer(['name' => 'ExportWizardTheFifth']);
331 $page = $this->entities->page();
332 $page->created_by = $user->id;
333 $page->updated_by = $user->id;
336 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
337 $resp->assertSee('ExportWizardTheFifth');
340 $resp = $this->get($page->getUrl('/export/html'));
341 $resp->assertStatus(200);
342 $resp->assertDontSee('ExportWizardTheFifth');
345 public function test_page_pdf_export_converts_iframes_to_links()
347 $page = Page::query()->first()->forceFill([
348 'html' => '<iframe width="560" height="315" src="//www.youtube.com/embed/ShqUjt33uOs"></iframe>',
353 $mockPdfGenerator = $this->mock(PdfGenerator::class);
354 $mockPdfGenerator->shouldReceive('fromHtml')
355 ->with(\Mockery::capture($pdfHtml))
357 $mockPdfGenerator->shouldReceive('getActiveEngine')->andReturn(PdfGenerator::ENGINE_DOMPDF);
359 $this->asEditor()->get($page->getUrl('/export/pdf'));
360 $this->assertStringNotContainsString('iframe>', $pdfHtml);
361 $this->assertStringContainsString('<p><a href="https://www.youtube.com/embed/ShqUjt33uOs">https://www.youtube.com/embed/ShqUjt33uOs</a></p>', $pdfHtml);
364 public function test_page_pdf_export_opens_details_blocks()
366 $page = $this->entities->page()->forceFill([
367 'html' => '<details><summary>Hello</summary><p>Content!</p></details>',
372 $mockPdfGenerator = $this->mock(PdfGenerator::class);
373 $mockPdfGenerator->shouldReceive('fromHtml')
374 ->with(\Mockery::capture($pdfHtml))
376 $mockPdfGenerator->shouldReceive('getActiveEngine')->andReturn(PdfGenerator::ENGINE_DOMPDF);
378 $this->asEditor()->get($page->getUrl('/export/pdf'));
379 $this->assertStringContainsString('<details open="open"', $pdfHtml);
382 public function test_page_markdown_export()
384 $page = $this->entities->page();
386 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
387 $resp->assertStatus(200);
388 $resp->assertSee($page->name);
389 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
392 public function test_page_markdown_export_uses_existing_markdown_if_apparent()
394 $page = $this->entities->page()->forceFill([
395 'markdown' => '# A header',
396 'html' => '<h1>Dogcat</h1>',
400 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
401 $resp->assertSee('A header');
402 $resp->assertDontSee('Dogcat');
405 public function test_page_markdown_export_converts_html_where_no_markdown()
407 $page = $this->entities->page()->forceFill([
409 'html' => '<h1>Dogcat</h1><p>Some <strong>bold</strong> text</p>',
413 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
414 $resp->assertSee("# Dogcat\n\nSome **bold** text");
417 public function test_chapter_markdown_export()
419 $chapter = $this->entities->chapter();
420 $page = $chapter->pages()->first();
421 $resp = $this->asEditor()->get($chapter->getUrl('/export/markdown'));
423 $resp->assertSee('# ' . $chapter->name);
424 $resp->assertSee('# ' . $page->name);
427 public function test_book_markdown_export()
429 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
430 $chapter = $book->chapters()->first();
431 $page = $chapter->pages()->first();
432 $resp = $this->asEditor()->get($book->getUrl('/export/markdown'));
434 $resp->assertSee('# ' . $book->name);
435 $resp->assertSee('# ' . $chapter->name);
436 $resp->assertSee('# ' . $page->name);
439 public function test_book_markdown_export_concats_immediate_pages_with_newlines()
441 /** @var Book $book */
442 $book = Book::query()->whereHas('pages')->first();
444 $this->asEditor()->get($book->getUrl('/create-page'));
445 $this->get($book->getUrl('/create-page'));
447 [$pageA, $pageB] = $book->pages()->where('chapter_id', '=', 0)->get();
448 $pageA->html = '<p>hello tester</p>';
450 $pageB->name = 'The second page in this test';
453 $resp = $this->get($book->getUrl('/export/markdown'));
454 $resp->assertDontSee('hello tester# The second page in this test');
455 $resp->assertSee("hello tester\n\n# The second page in this test");
458 public function test_export_option_only_visible_and_accessible_with_permission()
460 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
461 $chapter = $book->chapters()->first();
462 $page = $chapter->pages()->first();
463 $entities = [$book, $chapter, $page];
464 $user = $this->users->viewer();
465 $this->actingAs($user);
467 foreach ($entities as $entity) {
468 $resp = $this->get($entity->getUrl());
469 $resp->assertSee('/export/pdf');
472 $this->permissions->removeUserRolePermissions($user, ['content-export']);
474 foreach ($entities as $entity) {
475 $resp = $this->get($entity->getUrl());
476 $resp->assertDontSee('/export/pdf');
477 $resp = $this->get($entity->getUrl('/export/pdf'));
478 $this->assertPermissionError($resp);
482 public function test_wkhtmltopdf_only_used_when_allow_untrusted_is_true()
484 $page = $this->entities->page();
486 config()->set('exports.snappy.pdf_binary', '/abc123');
487 config()->set('app.allow_untrusted_server_fetching', false);
489 $resp = $this->asEditor()->get($page->getUrl('/export/pdf'));
490 $resp->assertStatus(200); // Sucessful response with invalid snappy binary indicates dompdf usage.
492 config()->set('app.allow_untrusted_server_fetching', true);
493 $resp = $this->get($page->getUrl('/export/pdf'));
494 $resp->assertStatus(500); // Bad response indicates wkhtml usage
497 public function test_pdf_command_option_used_if_set()
499 $page = $this->entities->page();
500 $command = 'cp {input_html_path} {output_pdf_path}';
501 config()->set('exports.pdf_command', $command);
503 $resp = $this->asEditor()->get($page->getUrl('/export/pdf'));
504 $download = $resp->getContent();
506 $this->assertStringContainsString(e($page->name), $download);
507 $this->assertStringContainsString('<html lang=', $download);
510 public function test_pdf_command_option_errors_if_output_path_not_written_to()
512 $page = $this->entities->page();
513 $command = 'echo "hi"';
514 config()->set('exports.pdf_command', $command);
516 $this->assertThrows(function () use ($page) {
517 $this->withoutExceptionHandling()->asEditor()->get($page->getUrl('/export/pdf'));
518 }, PdfExportException::class);
521 public function test_pdf_command_option_errors_if_command_returns_error_status()
523 $page = $this->entities->page();
525 config()->set('exports.pdf_command', $command);
527 $this->assertThrows(function () use ($page) {
528 $this->withoutExceptionHandling()->asEditor()->get($page->getUrl('/export/pdf'));
529 }, PdfExportException::class);
532 public function test_pdf_command_timout_option_limits_export_time()
534 $page = $this->entities->page();
535 $command = 'php -r \'sleep(4);\'';
536 config()->set('exports.pdf_command', $command);
537 config()->set('exports.pdf_command_timeout', 1);
539 $this->assertThrows(function () use ($page) {
541 $this->withoutExceptionHandling()->asEditor()->get($page->getUrl('/export/pdf'));
543 $this->assertTrue(time() < ($start + 3));
544 }, PdfExportException::class,
545 "PDF Export via command failed due to timeout at 1 second(s)");
548 public function test_html_exports_contain_csp_meta_tag()
551 $this->entities->page(),
552 $this->entities->book(),
553 $this->entities->chapter(),
556 foreach ($entities as $entity) {
557 $resp = $this->asEditor()->get($entity->getUrl('/export/html'));
558 $this->withHtml($resp)->assertElementExists('head meta[http-equiv="Content-Security-Policy"][content*="script-src "]');
562 public function test_html_exports_contain_body_classes_for_export_identification()
564 $page = $this->entities->page();
566 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
567 $this->withHtml($resp)->assertElementExists('body.export.export-format-html.export-engine-none');