3 namespace Tests\Exports;
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Repos\BookRepo;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Uploads\Attachment;
9 use BookStack\Uploads\Image;
10 use FilesystemIterator;
11 use Illuminate\Support\Carbon;
12 use Illuminate\Testing\TestResponse;
16 class ZipExportTest extends TestCase
18 public function test_export_results_in_zip_format()
20 $page = $this->entities->page();
21 $response = $this->asEditor()->get($page->getUrl("/export/zip"));
23 $zipData = $response->streamedContent();
24 $zipFile = tempnam(sys_get_temp_dir(), 'bstesta-');
25 file_put_contents($zipFile, $zipData);
26 $zip = new ZipArchive();
27 $zip->open($zipFile, ZipArchive::RDONLY);
29 $this->assertNotFalse($zip->locateName('data.json'));
30 $this->assertNotFalse($zip->locateName('files/'));
32 $data = json_decode($zip->getFromName('data.json'), true);
33 $this->assertIsArray($data);
34 $this->assertGreaterThan(0, count($data));
40 public function test_export_metadata()
42 $page = $this->entities->page();
43 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
44 $zip = $this->extractZipResponse($zipResp);
46 $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
47 $this->assertArrayNotHasKey('book', $zip->data);
48 $this->assertArrayNotHasKey('chapter', $zip->data);
51 $date = Carbon::parse($zip->data['exported_at'])->unix();
52 $this->assertLessThan($now + 2, $date);
53 $this->assertGreaterThan($now - 2, $date);
55 $version = trim(file_get_contents(base_path('version')));
56 $this->assertEquals($version, $zip->data['instance']['version']);
58 $zipInstanceId = $zip->data['instance']['id'];
59 $instanceId = setting('instance-id');
60 $this->assertNotEmpty($instanceId);
61 $this->assertEquals($instanceId, $zipInstanceId);
64 public function test_export_leaves_no_temp_files()
66 $tempDir = sys_get_temp_dir();
67 $startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
69 $page = $this->entities->pageWithinChapter();
71 $pageResp = $this->get($page->getUrl("/export/zip"));
72 $pageResp->streamedContent();
73 $pageResp->assertOk();
74 $this->get($page->chapter->getUrl("/export/zip"))->assertOk();
75 $this->get($page->book->getUrl("/export/zip"))->assertOk();
77 $afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
79 $this->assertEquals($startTempFileCount, $afterTempFileCount);
82 public function test_page_export()
84 $page = $this->entities->page();
85 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
86 $zip = $this->extractZipResponse($zipResp);
88 $pageData = $zip->data['page'];
91 'name' => $page->name,
92 'html' => (new PageContent($page))->render(),
93 'priority' => $page->priority,
100 public function test_page_export_with_markdown()
102 $page = $this->entities->page();
103 $markdown = "# My page\n\nwritten in markdown for export\n";
104 $page->markdown = $markdown;
107 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
108 $zip = $this->extractZipResponse($zipResp);
110 $pageData = $zip->data['page'];
111 $this->assertEquals($markdown, $pageData['markdown']);
112 $this->assertNotEmpty($pageData['html']);
115 public function test_page_export_with_tags()
117 $page = $this->entities->page();
118 $page->tags()->saveMany([
119 new Tag(['name' => 'Exporty', 'value' => 'Content', 'order' => 1]),
120 new Tag(['name' => 'Another', 'value' => '', 'order' => 2]),
123 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
124 $zip = $this->extractZipResponse($zipResp);
126 $pageData = $zip->data['page'];
127 $this->assertEquals([
130 'value' => 'Content',
136 ], $pageData['tags']);
139 public function test_page_export_with_images()
142 $page = $this->entities->page();
143 $result = $this->files->uploadGalleryImageToPage($this, $page);
144 $displayThumb = $result['response']->thumbs->gallery ?? '';
145 $page->html = '<p><img src="' . $displayThumb . '" alt="My image"></p>';
147 $image = Image::findOrFail($result['response']->id);
149 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
150 $zip = $this->extractZipResponse($zipResp);
151 $pageData = $zip->data['page'];
153 $this->assertCount(1, $pageData['images']);
154 $imageData = $pageData['images'][0];
155 $this->assertEquals($image->id, $imageData['id']);
156 $this->assertEquals($image->name, $imageData['name']);
157 $this->assertEquals('gallery', $imageData['type']);
158 $this->assertNotEmpty($imageData['file']);
160 $filePath = $zip->extractPath("files/{$imageData['file']}");
161 $this->assertFileExists($filePath);
162 $this->assertEquals(file_get_contents(public_path($image->path)), file_get_contents($filePath));
164 $this->assertEquals('<p><img src="[[bsexport:image:' . $imageData['id'] . ']]" alt="My image"></p>', $pageData['html']);
167 public function test_page_export_file_attachments()
169 $contents = 'My great attachment content!';
171 $page = $this->entities->page();
173 $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
175 $zipResp = $this->get($page->getUrl("/export/zip"));
176 $zip = $this->extractZipResponse($zipResp);
178 $pageData = $zip->data['page'];
179 $this->assertCount(1, $pageData['attachments']);
181 $attachmentData = $pageData['attachments'][0];
182 $this->assertEquals('PageAttachmentExport.txt', $attachmentData['name']);
183 $this->assertEquals($attachment->id, $attachmentData['id']);
184 $this->assertArrayNotHasKey('link', $attachmentData);
185 $this->assertNotEmpty($attachmentData['file']);
187 $fileRef = $attachmentData['file'];
188 $filePath = $zip->extractPath("/files/$fileRef");
189 $this->assertFileExists($filePath);
190 $this->assertEquals($contents, file_get_contents($filePath));
193 public function test_page_export_link_attachments()
195 $page = $this->entities->page();
197 $attachment = Attachment::factory()->create([
198 'name' => 'My link attachment for export',
199 'path' => 'https://example.com/cats',
201 'uploaded_to' => $page->id,
205 $zipResp = $this->get($page->getUrl("/export/zip"));
206 $zip = $this->extractZipResponse($zipResp);
208 $pageData = $zip->data['page'];
209 $this->assertCount(1, $pageData['attachments']);
211 $attachmentData = $pageData['attachments'][0];
212 $this->assertEquals('My link attachment for export', $attachmentData['name']);
213 $this->assertEquals($attachment->id, $attachmentData['id']);
214 $this->assertEquals('https://example.com/cats', $attachmentData['link']);
215 $this->assertArrayNotHasKey('file', $attachmentData);
218 public function test_book_export()
220 $book = $this->entities->bookHasChaptersAndPages();
221 $book->tags()->saveMany(Tag::factory()->count(2)->make());
223 $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
224 $zip = $this->extractZipResponse($zipResp);
225 $this->assertArrayHasKey('book', $zip->data);
227 $bookData = $zip->data['book'];
228 $this->assertEquals($book->id, $bookData['id']);
229 $this->assertEquals($book->name, $bookData['name']);
230 $this->assertEquals($book->descriptionHtml(), $bookData['description_html']);
231 $this->assertCount(2, $bookData['tags']);
232 $this->assertCount($book->directPages()->count(), $bookData['pages']);
233 $this->assertCount($book->chapters()->count(), $bookData['chapters']);
234 $this->assertArrayNotHasKey('cover', $bookData);
237 public function test_book_export_with_cover_image()
239 $book = $this->entities->book();
240 $bookRepo = $this->app->make(BookRepo::class);
241 $coverImageFile = $this->files->uploadedImage('cover.png');
242 $bookRepo->updateCoverImage($book, $coverImageFile);
243 $coverImage = $book->cover()->first();
245 $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
246 $zip = $this->extractZipResponse($zipResp);
248 $this->assertArrayHasKey('cover', $zip->data['book']);
249 $coverRef = $zip->data['book']['cover'];
250 $coverPath = $zip->extractPath("/files/$coverRef");
251 $this->assertFileExists($coverPath);
252 $this->assertEquals(file_get_contents(public_path($coverImage->path)), file_get_contents($coverPath));
255 public function test_chapter_export()
257 $chapter = $this->entities->chapter();
258 $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
260 $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
261 $zip = $this->extractZipResponse($zipResp);
262 $this->assertArrayHasKey('chapter', $zip->data);
264 $chapterData = $zip->data['chapter'];
265 $this->assertEquals($chapter->id, $chapterData['id']);
266 $this->assertEquals($chapter->name, $chapterData['name']);
267 $this->assertEquals($chapter->descriptionHtml(), $chapterData['description_html']);
268 $this->assertCount(2, $chapterData['tags']);
269 $this->assertEquals($chapter->priority, $chapterData['priority']);
270 $this->assertCount($chapter->pages()->count(), $chapterData['pages']);
273 public function test_draft_pages_are_not_included()
275 $editor = $this->users->editor();
276 $entities = $this->entities->createChainBelongingToUser($editor);
277 $book = $entities['book'];
278 $page = $entities['page'];
279 $chapter = $entities['chapter'];
280 $book->tags()->saveMany(Tag::factory()->count(2)->make());
282 $page->created_by = $editor->id;
286 $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
287 $zip = $this->extractZipResponse($zipResp);
288 $this->assertCount(0, $zip->data['book']['chapters'][0]['pages'] ?? ['cat']);
290 $zipResp = $this->actingAs($editor)->get($chapter->getUrl("/export/zip"));
291 $zip = $this->extractZipResponse($zipResp);
292 $this->assertCount(0, $zip->data['chapter']['pages'] ?? ['cat']);
294 $page->chapter_id = 0;
297 $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
298 $zip = $this->extractZipResponse($zipResp);
299 $this->assertCount(0, $zip->data['book']['pages'] ?? ['cat']);
303 public function test_cross_reference_links_are_converted()
305 $book = $this->entities->bookHasChaptersAndPages();
306 $chapter = $book->chapters()->first();
307 $page = $chapter->pages()->first();
309 $book->description_html = '<p><a href="' . $chapter->getUrl() . '">Link to chapter</a></p>';
311 $chapter->description_html = '<p><a href="' . $page->getUrl() . '#section2">Link to page</a></p>';
313 $page->html = '<p><a href="' . $book->getUrl() . '?view=true">Link to book</a></p>';
316 $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
317 $zip = $this->extractZipResponse($zipResp);
318 $bookData = $zip->data['book'];
319 $chapterData = $bookData['chapters'][0];
320 $pageData = $chapterData['pages'][0];
322 $this->assertStringContainsString('href="[[bsexport:chapter:' . $chapter->id . ']]"', $bookData['description_html']);
323 $this->assertStringContainsString('href="[[bsexport:page:' . $page->id . ']]#section2"', $chapterData['description_html']);
324 $this->assertStringContainsString('href="[[bsexport:book:' . $book->id . ']]?view=true"', $pageData['html']);
327 public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
329 $book = $this->entities->bookHasChaptersAndPages();
330 $chapter = $book->chapters()->first();
331 $page = $chapter->pages()->first();
334 $this->files->uploadGalleryImageToPage($this, $page);
335 /** @var Image $image */
336 $image = Image::query()->where('type', '=', 'gallery')
337 ->where('uploaded_to', '=', $page->id)->first();
339 $book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
341 $chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
344 $zipResp = $this->get($book->getUrl("/export/zip"));
345 $zip = $this->extractZipResponse($zipResp);
346 $bookData = $zip->data['book'];
347 $chapterData = $bookData['chapters'][0];
349 $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
350 $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
353 public function test_image_links_are_handled_when_using_external_storage_url()
355 $page = $this->entities->page();
358 $this->files->uploadGalleryImageToPage($this, $page);
359 /** @var Image $image */
360 $image = Image::query()->where('type', '=', 'gallery')
361 ->where('uploaded_to', '=', $page->id)->first();
363 config()->set('filesystems.url', 'https://i.example.com/content');
365 $storageUrl = 'https://i.example.com/content/' . ltrim($image->path, '/');
366 $page->html = '<p><a href="' . $image->url . '">Original URL</a><a href="' . $storageUrl . '">Storage URL</a></p>';
369 $zipResp = $this->get($page->getUrl("/export/zip"));
370 $zip = $this->extractZipResponse($zipResp);
371 $pageData = $zip->data['page'];
373 $ref = '[[bsexport:image:' . $image->id . ']]';
374 $this->assertStringContainsString("<a href=\"{$ref}\">Original URL</a><a href=\"{$ref}\">Storage URL</a>", $pageData['html']);
377 public function test_cross_reference_links_external_to_export_are_not_converted()
379 $page = $this->entities->page();
380 $page->html = '<p><a href="' . $page->book->getUrl() . '">Link to book</a></p>';
383 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
384 $zip = $this->extractZipResponse($zipResp);
385 $pageData = $zip->data['page'];
387 $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
390 public function test_attachments_links_are_converted()
392 $page = $this->entities->page();
393 $attachment = Attachment::factory()->create([
394 'name' => 'My link attachment for export reference',
395 'path' => 'https://example.com/cats/ref',
397 'uploaded_to' => $page->id,
401 $page->html = '<p><a href="' . url("/attachments/{$attachment->id}") . '?open=true">Link to attachment</a></p>';
404 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
405 $zip = $this->extractZipResponse($zipResp);
406 $pageData = $zip->data['page'];
408 $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
411 public function test_links_in_markdown_are_parsed()
413 $chapter = $this->entities->chapterHasPages();
414 $page = $chapter->pages()->first();
416 $page->markdown = "[Link to chapter]({$chapter->getUrl()})";
419 $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
420 $zip = $this->extractZipResponse($zipResp);
421 $pageData = $zip->data['chapter']['pages'][0];
423 $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
426 protected function extractZipResponse(TestResponse $response): ZipResultData
428 $zipData = $response->streamedContent();
429 $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
431 file_put_contents($zipFile, $zipData);
432 $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
433 if (file_exists($extractDir)) {
438 $zip = new ZipArchive();
439 $zip->open($zipFile, ZipArchive::RDONLY);
440 $zip->extractTo($extractDir);
442 $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
443 $data = json_decode($dataJson, true);
445 return new ZipResultData(