]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipExportTest.php
ZIP Imports: Added image type validation/handling
[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_cross_reference_links_external_to_export_are_not_converted()
278     {
279         $page = $this->entities->page();
280         $page->html = '<p><a href="' . $page->book->getUrl() . '">Link to book</a></p>';
281         $page->save();
282
283         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
284         $zip = $this->extractZipResponse($zipResp);
285         $pageData = $zip->data['page'];
286
287         $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
288     }
289
290     public function test_attachments_links_are_converted()
291     {
292         $page = $this->entities->page();
293         $attachment = Attachment::factory()->create([
294             'name' => 'My link attachment for export reference',
295             'path' => 'https://example.com/cats/ref',
296             'external' => true,
297             'uploaded_to' => $page->id,
298             'order' => 1,
299         ]);
300
301         $page->html = '<p><a href="' . url("/attachments/{$attachment->id}") . '?open=true">Link to attachment</a></p>';
302         $page->save();
303
304         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
305         $zip = $this->extractZipResponse($zipResp);
306         $pageData = $zip->data['page'];
307
308         $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
309     }
310
311     public function test_links_in_markdown_are_parsed()
312     {
313         $chapter = $this->entities->chapterHasPages();
314         $page = $chapter->pages()->first();
315
316         $page->markdown = "[Link to chapter]({$chapter->getUrl()})";
317         $page->save();
318
319         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
320         $zip = $this->extractZipResponse($zipResp);
321         $pageData = $zip->data['chapter']['pages'][0];
322
323         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
324     }
325
326     protected function extractZipResponse(TestResponse $response): ZipResultData
327     {
328         $zipData = $response->streamedContent();
329         $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
330
331         file_put_contents($zipFile, $zipData);
332         $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
333         if (file_exists($extractDir)) {
334             unlink($extractDir);
335         }
336         mkdir($extractDir);
337
338         $zip = new ZipArchive();
339         $zip->open($zipFile, ZipArchive::RDONLY);
340         $zip->extractTo($extractDir);
341
342         $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
343         $data = json_decode($dataJson, true);
344
345         return new ZipResultData(
346             $zipFile,
347             $extractDir,
348             $data,
349         );
350     }
351 }