3 namespace Tests\Entity;
5 use BookStack\Auth\Role;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Page;
9 use Illuminate\Support\Facades\Storage;
10 use Illuminate\Support\Str;
13 class ExportTest extends TestCase
15 public function test_page_text_export()
17 $page = Page::query()->first();
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 = Page::query()->first();
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 = Page::query()->first();
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 $page = Page::query()->first();
53 $resp = $this->get($book->getUrl('/export/plaintext'));
54 $resp->assertStatus(200);
55 $resp->assertSee($book->name);
56 $resp->assertSee($page->name);
57 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.txt"');
60 public function test_book_pdf_export()
62 $page = Page::query()->first();
66 $resp = $this->get($book->getUrl('/export/pdf'));
67 $resp->assertStatus(200);
68 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.pdf"');
71 public function test_book_html_export()
73 $page = Page::query()->first();
77 $resp = $this->get($book->getUrl('/export/html'));
78 $resp->assertStatus(200);
79 $resp->assertSee($book->name);
80 $resp->assertSee($page->name);
81 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $book->slug . '.html"');
84 public function test_book_html_export_shows_chapter_descriptions()
86 $chapterDesc = 'My custom test chapter description ' . Str::random(12);
87 $chapter = Chapter::query()->first();
88 $chapter->description = $chapterDesc;
91 $book = $chapter->book;
94 $resp = $this->get($book->getUrl('/export/html'));
95 $resp->assertSee($chapterDesc);
98 public function test_chapter_text_export()
100 $chapter = Chapter::query()->first();
101 $page = $chapter->pages[0];
104 $resp = $this->get($chapter->getUrl('/export/plaintext'));
105 $resp->assertStatus(200);
106 $resp->assertSee($chapter->name);
107 $resp->assertSee($page->name);
108 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.txt"');
111 public function test_chapter_pdf_export()
113 $chapter = Chapter::query()->first();
116 $resp = $this->get($chapter->getUrl('/export/pdf'));
117 $resp->assertStatus(200);
118 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.pdf"');
121 public function test_chapter_html_export()
123 $chapter = Chapter::query()->first();
124 $page = $chapter->pages[0];
127 $resp = $this->get($chapter->getUrl('/export/html'));
128 $resp->assertStatus(200);
129 $resp->assertSee($chapter->name);
130 $resp->assertSee($page->name);
131 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $chapter->slug . '.html"');
134 public function test_page_html_export_contains_custom_head_if_set()
136 $page = Page::query()->first();
138 $customHeadContent = '<style>p{color: red;}</style>';
139 $this->setSettings(['app-custom-head' => $customHeadContent]);
141 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
142 $resp->assertSee($customHeadContent);
145 public function test_page_html_export_does_not_break_with_only_comments_in_custom_head()
147 $page = Page::query()->first();
149 $customHeadContent = '<!-- A comment -->';
150 $this->setSettings(['app-custom-head' => $customHeadContent]);
152 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
153 $resp->assertStatus(200);
154 $resp->assertSee($customHeadContent);
157 public function test_page_html_export_use_absolute_dates()
159 $page = Page::query()->first();
161 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
162 $resp->assertSee($page->created_at->formatLocalized('%e %B %Y %H:%M:%S'));
163 $resp->assertDontSee($page->created_at->diffForHumans());
164 $resp->assertSee($page->updated_at->formatLocalized('%e %B %Y %H:%M:%S'));
165 $resp->assertDontSee($page->updated_at->diffForHumans());
168 public function test_page_export_does_not_include_user_or_revision_links()
170 $page = Page::query()->first();
172 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
173 $resp->assertDontSee($page->getUrl('/revisions'));
174 $resp->assertDontSee($page->createdBy->getProfileUrl());
175 $resp->assertSee($page->createdBy->name);
178 public function test_page_export_sets_right_data_type_for_svg_embeds()
180 $page = Page::query()->first();
181 Storage::disk('local')->makeDirectory('uploads/images/gallery');
182 Storage::disk('local')->put('uploads/images/gallery/svg_test.svg', '<svg></svg>');
183 $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg">';
187 $resp = $this->get($page->getUrl('/export/html'));
188 Storage::disk('local')->delete('uploads/images/gallery/svg_test.svg');
190 $resp->assertStatus(200);
191 $resp->assertSee('<img src="data:image/svg+xml;base64');
194 public function test_page_image_containment_works_on_multiple_images_within_a_single_line()
196 $page = Page::query()->first();
197 Storage::disk('local')->makeDirectory('uploads/images/gallery');
198 Storage::disk('local')->put('uploads/images/gallery/svg_test.svg', '<svg></svg>');
199 Storage::disk('local')->put('uploads/images/gallery/svg_test2.svg', '<svg></svg>');
200 $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">';
203 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
204 Storage::disk('local')->delete('uploads/images/gallery/svg_test.svg');
205 Storage::disk('local')->delete('uploads/images/gallery/svg_test2.svg');
207 $resp->assertDontSee('http://localhost/uploads/images/gallery/svg_test');
210 public function test_page_export_contained_html_image_fetches_only_run_when_url_points_to_image_upload_folder()
212 $page = Page::query()->first();
213 $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg"/>'
214 . '<img src="http://localhost/uploads/svg_test.svg"/>'
215 . '<img src="/uploads/svg_test.svg"/>';
216 $storageDisk = Storage::disk('local');
217 $storageDisk->makeDirectory('uploads/images/gallery');
218 $storageDisk->put('uploads/images/gallery/svg_test.svg', '<svg>good</svg>');
219 $storageDisk->put('uploads/svg_test.svg', '<svg>bad</svg>');
222 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
224 $storageDisk->delete('uploads/images/gallery/svg_test.svg');
225 $storageDisk->delete('uploads/svg_test.svg');
227 $resp->assertDontSee('http://localhost/uploads/images/gallery/svg_test.svg');
228 $resp->assertSee('http://localhost/uploads/svg_test.svg');
229 $resp->assertSee('src="/uploads/svg_test.svg"');
232 public function test_exports_removes_scripts_from_custom_head()
235 Page::query()->first(), Chapter::query()->first(), Book::query()->first(),
237 setting()->put('app-custom-head', '<script>window.donkey = "cat";</script><style>.my-test-class { color: red; }</style>');
239 foreach ($entities as $entity) {
240 $resp = $this->asEditor()->get($entity->getUrl('/export/html'));
241 $resp->assertDontSee('window.donkey');
242 $resp->assertDontSee('script');
243 $resp->assertSee('.my-test-class { color: red; }');
247 public function test_page_export_with_deleted_creator_and_updater()
249 $user = $this->getViewer(['name' => 'ExportWizardTheFifth']);
250 $page = Page::query()->first();
251 $page->created_by = $user->id;
252 $page->updated_by = $user->id;
255 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
256 $resp->assertSee('ExportWizardTheFifth');
259 $resp = $this->get($page->getUrl('/export/html'));
260 $resp->assertStatus(200);
261 $resp->assertDontSee('ExportWizardTheFifth');
264 public function test_page_markdown_export()
266 $page = Page::query()->first();
268 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
269 $resp->assertStatus(200);
270 $resp->assertSee($page->name);
271 $resp->assertHeader('Content-Disposition', 'attachment; filename="' . $page->slug . '.md"');
274 public function test_page_markdown_export_uses_existing_markdown_if_apparent()
276 $page = Page::query()->first()->forceFill([
277 'markdown' => '# A header',
278 'html' => '<h1>Dogcat</h1>',
282 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
283 $resp->assertSee('A header');
284 $resp->assertDontSee('Dogcat');
287 public function test_page_markdown_export_converts_html_where_no_markdown()
289 $page = Page::query()->first()->forceFill([
291 'html' => '<h1>Dogcat</h1><p>Some <strong>bold</strong> text</p>',
295 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
296 $resp->assertSee("# Dogcat\n\nSome **bold** text");
299 public function test_page_markdown_export_does_not_convert_callouts()
301 $page = Page::query()->first()->forceFill([
303 'html' => '<h1>Dogcat</h1><p class="callout info">Some callout text</p><p>Another line</p>',
307 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
308 $resp->assertSee("# Dogcat\n\n<p class=\"callout info\">Some callout text</p>\n\nAnother line");
311 public function test_page_markdown_export_handles_bookstacks_wysiwyg_codeblock_format()
313 $page = Page::query()->first()->forceFill([
315 'html' => '<h1>Dogcat</h1>' . "\r\n" . '<pre id="bkmrk-var-a-%3D-%27cat%27%3B"><code class="language-JavaScript">var a = \'cat\';</code></pre><p>Another line</p>',
319 $resp = $this->asEditor()->get($page->getUrl('/export/markdown'));
320 $resp->assertSee("# Dogcat\n\n```JavaScript\nvar a = 'cat';\n```\n\nAnother line");
323 public function test_chapter_markdown_export()
325 $chapter = Chapter::query()->first();
326 $page = $chapter->pages()->first();
327 $resp = $this->asEditor()->get($chapter->getUrl('/export/markdown'));
329 $resp->assertSee('# ' . $chapter->name);
330 $resp->assertSee('# ' . $page->name);
333 public function test_book_markdown_export()
335 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
336 $chapter = $book->chapters()->first();
337 $page = $chapter->pages()->first();
338 $resp = $this->asEditor()->get($book->getUrl('/export/markdown'));
340 $resp->assertSee('# ' . $book->name);
341 $resp->assertSee('# ' . $chapter->name);
342 $resp->assertSee('# ' . $page->name);
345 public function test_export_option_only_visible_and_accessible_with_permission()
347 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
348 $chapter = $book->chapters()->first();
349 $page = $chapter->pages()->first();
350 $entities = [$book, $chapter, $page];
351 $user = $this->getViewer();
352 $this->actingAs($user);
354 foreach ($entities as $entity) {
355 $resp = $this->get($entity->getUrl());
356 $resp->assertSee("/export/pdf");
359 /** @var Role $role */
360 $this->removePermissionFromUser($user, 'content-export');
362 foreach ($entities as $entity) {
363 $resp = $this->get($entity->getUrl());
364 $resp->assertDontSee("/export/pdf");
365 $resp = $this->get($entity->getUrl("/export/pdf"));
366 $this->assertPermissionError($resp);