]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipExportTest.php
17891c73d73f171318b3712bb36730e5bbdeae52
[bookstack] / tests / Exports / ZipExportTest.php
1 <?php
2
3 namespace Tests\Exports;
4
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;
12 use Tests\TestCase;
13 use ZipArchive;
14
15 class ZipExportTest extends TestCase
16 {
17     public function test_export_results_in_zip_format()
18     {
19         $page = $this->entities->page();
20         $response = $this->asEditor()->get($page->getUrl("/export/zip"));
21
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);
27
28         $this->assertNotFalse($zip->locateName('data.json'));
29         $this->assertNotFalse($zip->locateName('files/'));
30
31         $data = json_decode($zip->getFromName('data.json'), true);
32         $this->assertIsArray($data);
33         $this->assertGreaterThan(0, count($data));
34
35         $zip->close();
36         unlink($zipFile);
37     }
38
39     public function test_export_metadata()
40     {
41         $page = $this->entities->page();
42         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
43         $zip = $this->extractZipResponse($zipResp);
44
45         $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
46         $this->assertArrayNotHasKey('book', $zip->data);
47         $this->assertArrayNotHasKey('chapter', $zip->data);
48
49         $now = time();
50         $date = Carbon::parse($zip->data['exported_at'])->unix();
51         $this->assertLessThan($now + 2, $date);
52         $this->assertGreaterThan($now - 2, $date);
53
54         $version = trim(file_get_contents(base_path('version')));
55         $this->assertEquals($version, $zip->data['instance']['version']);
56
57         $instanceId = decrypt($zip->data['instance']['id_ciphertext']);
58         $this->assertEquals('bookstack', $instanceId);
59     }
60
61     public function test_page_export()
62     {
63         $page = $this->entities->page();
64         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
65         $zip = $this->extractZipResponse($zipResp);
66
67         $pageData = $zip->data['page'];
68         $this->assertEquals([
69             'id' => $page->id,
70             'name' => $page->name,
71             'html' => (new PageContent($page))->render(),
72             'priority' => $page->priority,
73             'attachments' => [],
74             'images' => [],
75             'tags' => [],
76         ], $pageData);
77     }
78
79     public function test_page_export_with_markdown()
80     {
81         $page = $this->entities->page();
82         $markdown = "# My page\n\nwritten in markdown for export\n";
83         $page->markdown = $markdown;
84         $page->save();
85
86         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
87         $zip = $this->extractZipResponse($zipResp);
88
89         $pageData = $zip->data['page'];
90         $this->assertEquals($markdown, $pageData['markdown']);
91         $this->assertNotEmpty($pageData['html']);
92     }
93
94     public function test_page_export_with_tags()
95     {
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]),
100         ]);
101
102         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
103         $zip = $this->extractZipResponse($zipResp);
104
105         $pageData = $zip->data['page'];
106         $this->assertEquals([
107             [
108                 'name' => 'Exporty',
109                 'value' => 'Content',
110             ],
111             [
112                 'name' => 'Another',
113                 'value' => '',
114             ]
115         ], $pageData['tags']);
116     }
117
118     public function test_page_export_with_images()
119     {
120         $this->asEditor();
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>';
125         $page->save();
126         $image = Image::findOrFail($result['response']->id);
127
128         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
129         $zip = $this->extractZipResponse($zipResp);
130         $pageData = $zip->data['page'];
131
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']);
138
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));
142
143         $this->assertEquals('<p><img src="[[bsexport:image:' . $imageData['id'] . ']]" alt="My image"></p>', $pageData['html']);
144     }
145
146     public function test_page_export_file_attachments()
147     {
148         $contents = 'My great attachment content!';
149
150         $page = $this->entities->page();
151         $this->asAdmin();
152         $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
153
154         $zipResp = $this->get($page->getUrl("/export/zip"));
155         $zip = $this->extractZipResponse($zipResp);
156
157         $pageData = $zip->data['page'];
158         $this->assertCount(1, $pageData['attachments']);
159
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']);
165
166         $fileRef = $attachmentData['file'];
167         $filePath = $zip->extractPath("/files/$fileRef");
168         $this->assertFileExists($filePath);
169         $this->assertEquals($contents, file_get_contents($filePath));
170     }
171
172     public function test_page_export_link_attachments()
173     {
174         $page = $this->entities->page();
175         $this->asEditor();
176         $attachment = Attachment::factory()->create([
177             'name' => 'My link attachment for export',
178             'path' => 'https://example.com/cats',
179             'external' => true,
180             'uploaded_to' => $page->id,
181             'order' => 1,
182         ]);
183
184         $zipResp = $this->get($page->getUrl("/export/zip"));
185         $zip = $this->extractZipResponse($zipResp);
186
187         $pageData = $zip->data['page'];
188         $this->assertCount(1, $pageData['attachments']);
189
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);
195     }
196
197     public function test_book_export()
198     {
199         $book = $this->entities->book();
200         $book->tags()->saveMany(Tag::factory()->count(2)->make());
201
202         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
203         $zip = $this->extractZipResponse($zipResp);
204         $this->assertArrayHasKey('book', $zip->data);
205
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);
214     }
215
216     public function test_book_export_with_cover_image()
217     {
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();
223
224         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
225         $zip = $this->extractZipResponse($zipResp);
226
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));
232     }
233
234     public function test_chapter_export()
235     {
236         $chapter = $this->entities->chapter();
237         $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
238
239         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
240         $zip = $this->extractZipResponse($zipResp);
241         $this->assertArrayHasKey('chapter', $zip->data);
242
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']);
250     }
251
252
253     public function test_cross_reference_links_are_converted()
254     {
255         $book = $this->entities->bookHasChaptersAndPages();
256         $chapter = $book->chapters()->first();
257         $page = $chapter->pages()->first();
258
259         $book->description_html = '<p><a href="' . $chapter->getUrl() . '">Link to chapter</a></p>';
260         $book->save();
261         $chapter->description_html = '<p><a href="' . $page->getUrl() . '#section2">Link to page</a></p>';
262         $chapter->save();
263         $page->html = '<p><a href="' . $book->getUrl() . '?view=true">Link to book</a></p>';
264         $page->save();
265
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];
271
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']);
275     }
276
277     public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
278     {
279         $book = $this->entities->bookHasChaptersAndPages();
280         $chapter = $book->chapters()->first();
281         $page = $chapter->pages()->first();
282
283         $this->asEditor();
284         $this->files->uploadGalleryImageToPage($this, $page);
285         /** @var Image $image */
286         $image = Image::query()->where('type', '=', 'gallery')
287             ->where('uploaded_to', '=', $page->id)->first();
288
289         $book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
290         $book->save();
291         $chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
292         $chapter->save();
293
294         $zipResp = $this->get($book->getUrl("/export/zip"));
295         $zip = $this->extractZipResponse($zipResp);
296         $bookData = $zip->data['book'];
297         $chapterData = $bookData['chapters'][0];
298
299         $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
300         $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
301     }
302
303     public function test_image_links_are_handled_when_using_external_storage_url()
304     {
305         $page = $this->entities->page();
306
307         $this->asEditor();
308         $this->files->uploadGalleryImageToPage($this, $page);
309         /** @var Image $image */
310         $image = Image::query()->where('type', '=', 'gallery')
311             ->where('uploaded_to', '=', $page->id)->first();
312
313         config()->set('filesystems.url', 'https://i.example.com/content');
314
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>';
317         $page->save();
318
319         $zipResp = $this->get($page->getUrl("/export/zip"));
320         $zip = $this->extractZipResponse($zipResp);
321         $pageData = $zip->data['page'];
322
323         $ref = '[[bsexport:image:' . $image->id . ']]';
324         $this->assertStringContainsString("<a href=\"{$ref}\">Original URL</a><a href=\"{$ref}\">Storage URL</a>", $pageData['html']);
325     }
326
327     public function test_cross_reference_links_external_to_export_are_not_converted()
328     {
329         $page = $this->entities->page();
330         $page->html = '<p><a href="' . $page->book->getUrl() . '">Link to book</a></p>';
331         $page->save();
332
333         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
334         $zip = $this->extractZipResponse($zipResp);
335         $pageData = $zip->data['page'];
336
337         $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
338     }
339
340     public function test_attachments_links_are_converted()
341     {
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',
346             'external' => true,
347             'uploaded_to' => $page->id,
348             'order' => 1,
349         ]);
350
351         $page->html = '<p><a href="' . url("/attachments/{$attachment->id}") . '?open=true">Link to attachment</a></p>';
352         $page->save();
353
354         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
355         $zip = $this->extractZipResponse($zipResp);
356         $pageData = $zip->data['page'];
357
358         $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
359     }
360
361     public function test_links_in_markdown_are_parsed()
362     {
363         $chapter = $this->entities->chapterHasPages();
364         $page = $chapter->pages()->first();
365
366         $page->markdown = "[Link to chapter]({$chapter->getUrl()})";
367         $page->save();
368
369         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
370         $zip = $this->extractZipResponse($zipResp);
371         $pageData = $zip->data['chapter']['pages'][0];
372
373         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
374     }
375
376     protected function extractZipResponse(TestResponse $response): ZipResultData
377     {
378         $zipData = $response->streamedContent();
379         $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
380
381         file_put_contents($zipFile, $zipData);
382         $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
383         if (file_exists($extractDir)) {
384             unlink($extractDir);
385         }
386         mkdir($extractDir);
387
388         $zip = new ZipArchive();
389         $zip->open($zipFile, ZipArchive::RDONLY);
390         $zip->extractTo($extractDir);
391
392         $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
393         $data = json_decode($dataJson, true);
394
395         return new ZipResultData(
396             $zipFile,
397             $extractDir,
398             $data,
399         );
400     }
401 }