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 Illuminate\Support\Carbon;
11 use Illuminate\Testing\TestResponse;
15 class ZipExportTest extends TestCase
17 public function test_export_results_in_zip_format()
19 $page = $this->entities->page();
20 $response = $this->asEditor()->get($page->getUrl("/export/zip"));
22 $zipData = $response->streamedContent();
23 $zipFile = tempnam(sys_get_temp_dir(), 'bstesta-');
24 file_put_contents($zipFile, $zipData);
25 $zip = new ZipArchive();
26 $zip->open($zipFile, ZipArchive::RDONLY);
28 $this->assertNotFalse($zip->locateName('data.json'));
29 $this->assertNotFalse($zip->locateName('files/'));
31 $data = json_decode($zip->getFromName('data.json'), true);
32 $this->assertIsArray($data);
33 $this->assertGreaterThan(0, count($data));
39 public function test_export_metadata()
41 $page = $this->entities->page();
42 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
43 $zip = $this->extractZipResponse($zipResp);
45 $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
46 $this->assertArrayNotHasKey('book', $zip->data);
47 $this->assertArrayNotHasKey('chapter', $zip->data);
50 $date = Carbon::parse($zip->data['exported_at'])->unix();
51 $this->assertLessThan($now + 2, $date);
52 $this->assertGreaterThan($now - 2, $date);
54 $version = trim(file_get_contents(base_path('version')));
55 $this->assertEquals($version, $zip->data['instance']['version']);
57 $instanceId = decrypt($zip->data['instance']['id_ciphertext']);
58 $this->assertEquals('bookstack', $instanceId);
61 public function test_page_export()
63 $page = $this->entities->page();
64 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
65 $zip = $this->extractZipResponse($zipResp);
67 $pageData = $zip->data['page'];
70 'name' => $page->name,
71 'html' => (new PageContent($page))->render(),
72 'priority' => $page->priority,
79 public function test_page_export_with_markdown()
81 $page = $this->entities->page();
82 $markdown = "# My page\n\nwritten in markdown for export\n";
83 $page->markdown = $markdown;
86 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
87 $zip = $this->extractZipResponse($zipResp);
89 $pageData = $zip->data['page'];
90 $this->assertEquals($markdown, $pageData['markdown']);
91 $this->assertNotEmpty($pageData['html']);
94 public function test_page_export_with_tags()
96 $page = $this->entities->page();
97 $page->tags()->saveMany([
98 new Tag(['name' => 'Exporty', 'value' => 'Content', 'order' => 1]),
99 new Tag(['name' => 'Another', 'value' => '', 'order' => 2]),
102 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
103 $zip = $this->extractZipResponse($zipResp);
105 $pageData = $zip->data['page'];
106 $this->assertEquals([
109 'value' => 'Content',
115 ], $pageData['tags']);
118 public function test_page_export_with_images()
121 $page = $this->entities->page();
122 $result = $this->files->uploadGalleryImageToPage($this, $page);
123 $displayThumb = $result['response']->thumbs->gallery ?? '';
124 $page->html = '<p><img src="' . $displayThumb . '" alt="My image"></p>';
126 $image = Image::findOrFail($result['response']->id);
128 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
129 $zip = $this->extractZipResponse($zipResp);
130 $pageData = $zip->data['page'];
132 $this->assertCount(1, $pageData['images']);
133 $imageData = $pageData['images'][0];
134 $this->assertEquals($image->id, $imageData['id']);
135 $this->assertEquals($image->name, $imageData['name']);
136 $this->assertEquals('gallery', $imageData['type']);
137 $this->assertNotEmpty($imageData['file']);
139 $filePath = $zip->extractPath("files/{$imageData['file']}");
140 $this->assertFileExists($filePath);
141 $this->assertEquals(file_get_contents(public_path($image->path)), file_get_contents($filePath));
143 $this->assertEquals('<p><img src="[[bsexport:image:' . $imageData['id'] . ']]" alt="My image"></p>', $pageData['html']);
146 public function test_page_export_file_attachments()
148 $contents = 'My great attachment content!';
150 $page = $this->entities->page();
152 $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
154 $zipResp = $this->get($page->getUrl("/export/zip"));
155 $zip = $this->extractZipResponse($zipResp);
157 $pageData = $zip->data['page'];
158 $this->assertCount(1, $pageData['attachments']);
160 $attachmentData = $pageData['attachments'][0];
161 $this->assertEquals('PageAttachmentExport.txt', $attachmentData['name']);
162 $this->assertEquals($attachment->id, $attachmentData['id']);
163 $this->assertArrayNotHasKey('link', $attachmentData);
164 $this->assertNotEmpty($attachmentData['file']);
166 $fileRef = $attachmentData['file'];
167 $filePath = $zip->extractPath("/files/$fileRef");
168 $this->assertFileExists($filePath);
169 $this->assertEquals($contents, file_get_contents($filePath));
172 public function test_page_export_link_attachments()
174 $page = $this->entities->page();
176 $attachment = Attachment::factory()->create([
177 'name' => 'My link attachment for export',
178 'path' => 'https://example.com/cats',
180 'uploaded_to' => $page->id,
184 $zipResp = $this->get($page->getUrl("/export/zip"));
185 $zip = $this->extractZipResponse($zipResp);
187 $pageData = $zip->data['page'];
188 $this->assertCount(1, $pageData['attachments']);
190 $attachmentData = $pageData['attachments'][0];
191 $this->assertEquals('My link attachment for export', $attachmentData['name']);
192 $this->assertEquals($attachment->id, $attachmentData['id']);
193 $this->assertEquals('https://example.com/cats', $attachmentData['link']);
194 $this->assertArrayNotHasKey('file', $attachmentData);
197 public function test_book_export()
199 $book = $this->entities->book();
200 $book->tags()->saveMany(Tag::factory()->count(2)->make());
202 $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
203 $zip = $this->extractZipResponse($zipResp);
204 $this->assertArrayHasKey('book', $zip->data);
206 $bookData = $zip->data['book'];
207 $this->assertEquals($book->id, $bookData['id']);
208 $this->assertEquals($book->name, $bookData['name']);
209 $this->assertEquals($book->descriptionHtml(), $bookData['description_html']);
210 $this->assertCount(2, $bookData['tags']);
211 $this->assertCount($book->directPages()->count(), $bookData['pages']);
212 $this->assertCount($book->chapters()->count(), $bookData['chapters']);
213 $this->assertArrayNotHasKey('cover', $bookData);
216 public function test_book_export_with_cover_image()
218 $book = $this->entities->book();
219 $bookRepo = $this->app->make(BookRepo::class);
220 $coverImageFile = $this->files->uploadedImage('cover.png');
221 $bookRepo->updateCoverImage($book, $coverImageFile);
222 $coverImage = $book->cover()->first();
224 $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
225 $zip = $this->extractZipResponse($zipResp);
227 $this->assertArrayHasKey('cover', $zip->data['book']);
228 $coverRef = $zip->data['book']['cover'];
229 $coverPath = $zip->extractPath("/files/$coverRef");
230 $this->assertFileExists($coverPath);
231 $this->assertEquals(file_get_contents(public_path($coverImage->path)), file_get_contents($coverPath));
234 public function test_chapter_export()
236 $chapter = $this->entities->chapter();
237 $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
239 $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
240 $zip = $this->extractZipResponse($zipResp);
241 $this->assertArrayHasKey('chapter', $zip->data);
243 $chapterData = $zip->data['chapter'];
244 $this->assertEquals($chapter->id, $chapterData['id']);
245 $this->assertEquals($chapter->name, $chapterData['name']);
246 $this->assertEquals($chapter->descriptionHtml(), $chapterData['description_html']);
247 $this->assertCount(2, $chapterData['tags']);
248 $this->assertEquals($chapter->priority, $chapterData['priority']);
249 $this->assertCount($chapter->pages()->count(), $chapterData['pages']);
253 public function test_cross_reference_links_are_converted()
255 $book = $this->entities->bookHasChaptersAndPages();
256 $chapter = $book->chapters()->first();
257 $page = $chapter->pages()->first();
259 $book->description_html = '<p><a href="' . $chapter->getUrl() . '">Link to chapter</a></p>';
261 $chapter->description_html = '<p><a href="' . $page->getUrl() . '#section2">Link to page</a></p>';
263 $page->html = '<p><a href="' . $book->getUrl() . '?view=true">Link to book</a></p>';
266 $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
267 $zip = $this->extractZipResponse($zipResp);
268 $bookData = $zip->data['book'];
269 $chapterData = $bookData['chapters'][0];
270 $pageData = $chapterData['pages'][0];
272 $this->assertStringContainsString('href="[[bsexport:chapter:' . $chapter->id . ']]"', $bookData['description_html']);
273 $this->assertStringContainsString('href="[[bsexport:page:' . $page->id . ']]#section2"', $chapterData['description_html']);
274 $this->assertStringContainsString('href="[[bsexport:book:' . $book->id . ']]?view=true"', $pageData['html']);
277 public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
279 $book = $this->entities->bookHasChaptersAndPages();
280 $chapter = $book->chapters()->first();
281 $page = $chapter->pages()->first();
284 $this->files->uploadGalleryImageToPage($this, $page);
285 /** @var Image $image */
286 $image = Image::query()->where('type', '=', 'gallery')
287 ->where('uploaded_to', '=', $page->id)->first();
289 $book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
291 $chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
294 $zipResp = $this->get($book->getUrl("/export/zip"));
295 $zip = $this->extractZipResponse($zipResp);
296 $bookData = $zip->data['book'];
297 $chapterData = $bookData['chapters'][0];
299 $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
300 $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
303 public function test_image_links_are_handled_when_using_external_storage_url()
305 $page = $this->entities->page();
308 $this->files->uploadGalleryImageToPage($this, $page);
309 /** @var Image $image */
310 $image = Image::query()->where('type', '=', 'gallery')
311 ->where('uploaded_to', '=', $page->id)->first();
313 config()->set('filesystems.url', 'https://i.example.com/content');
315 $storageUrl = 'https://i.example.com/content/' . ltrim($image->path, '/');
316 $page->html = '<p><a href="' . $image->url . '">Original URL</a><a href="' . $storageUrl . '">Storage URL</a></p>';
319 $zipResp = $this->get($page->getUrl("/export/zip"));
320 $zip = $this->extractZipResponse($zipResp);
321 $pageData = $zip->data['page'];
323 $ref = '[[bsexport:image:' . $image->id . ']]';
324 $this->assertStringContainsString("<a href=\"{$ref}\">Original URL</a><a href=\"{$ref}\">Storage URL</a>", $pageData['html']);
327 public function test_cross_reference_links_external_to_export_are_not_converted()
329 $page = $this->entities->page();
330 $page->html = '<p><a href="' . $page->book->getUrl() . '">Link to book</a></p>';
333 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
334 $zip = $this->extractZipResponse($zipResp);
335 $pageData = $zip->data['page'];
337 $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
340 public function test_attachments_links_are_converted()
342 $page = $this->entities->page();
343 $attachment = Attachment::factory()->create([
344 'name' => 'My link attachment for export reference',
345 'path' => 'https://example.com/cats/ref',
347 'uploaded_to' => $page->id,
351 $page->html = '<p><a href="' . url("/attachments/{$attachment->id}") . '?open=true">Link to attachment</a></p>';
354 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
355 $zip = $this->extractZipResponse($zipResp);
356 $pageData = $zip->data['page'];
358 $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
361 public function test_links_in_markdown_are_parsed()
363 $chapter = $this->entities->chapterHasPages();
364 $page = $chapter->pages()->first();
366 $page->markdown = "[Link to chapter]({$chapter->getUrl()})";
369 $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
370 $zip = $this->extractZipResponse($zipResp);
371 $pageData = $zip->data['chapter']['pages'][0];
373 $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
376 protected function extractZipResponse(TestResponse $response): ZipResultData
378 $zipData = $response->streamedContent();
379 $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
381 file_put_contents($zipFile, $zipData);
382 $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
383 if (file_exists($extractDir)) {
388 $zip = new ZipArchive();
389 $zip->open($zipFile, ZipArchive::RDONLY);
390 $zip->extractTo($extractDir);
392 $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
393 $data = json_decode($dataJson, true);
395 return new ZipResultData(