]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipExportTest.php
ZIP Exports: Prevent book child page drafts from being included
[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         $zipInstanceId = $zip->data['instance']['id'];
58         $instanceId = setting('instance-id');
59         $this->assertNotEmpty($instanceId);
60         $this->assertEquals($instanceId, $zipInstanceId);
61     }
62
63     public function test_page_export()
64     {
65         $page = $this->entities->page();
66         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
67         $zip = $this->extractZipResponse($zipResp);
68
69         $pageData = $zip->data['page'];
70         $this->assertEquals([
71             'id' => $page->id,
72             'name' => $page->name,
73             'html' => (new PageContent($page))->render(),
74             'priority' => $page->priority,
75             'attachments' => [],
76             'images' => [],
77             'tags' => [],
78         ], $pageData);
79     }
80
81     public function test_page_export_with_markdown()
82     {
83         $page = $this->entities->page();
84         $markdown = "# My page\n\nwritten in markdown for export\n";
85         $page->markdown = $markdown;
86         $page->save();
87
88         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
89         $zip = $this->extractZipResponse($zipResp);
90
91         $pageData = $zip->data['page'];
92         $this->assertEquals($markdown, $pageData['markdown']);
93         $this->assertNotEmpty($pageData['html']);
94     }
95
96     public function test_page_export_with_tags()
97     {
98         $page = $this->entities->page();
99         $page->tags()->saveMany([
100             new Tag(['name' => 'Exporty', 'value' => 'Content', 'order' => 1]),
101             new Tag(['name' => 'Another', 'value' => '', 'order' => 2]),
102         ]);
103
104         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
105         $zip = $this->extractZipResponse($zipResp);
106
107         $pageData = $zip->data['page'];
108         $this->assertEquals([
109             [
110                 'name' => 'Exporty',
111                 'value' => 'Content',
112             ],
113             [
114                 'name' => 'Another',
115                 'value' => '',
116             ]
117         ], $pageData['tags']);
118     }
119
120     public function test_page_export_with_images()
121     {
122         $this->asEditor();
123         $page = $this->entities->page();
124         $result = $this->files->uploadGalleryImageToPage($this, $page);
125         $displayThumb = $result['response']->thumbs->gallery ?? '';
126         $page->html = '<p><img src="' . $displayThumb . '" alt="My image"></p>';
127         $page->save();
128         $image = Image::findOrFail($result['response']->id);
129
130         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
131         $zip = $this->extractZipResponse($zipResp);
132         $pageData = $zip->data['page'];
133
134         $this->assertCount(1, $pageData['images']);
135         $imageData = $pageData['images'][0];
136         $this->assertEquals($image->id, $imageData['id']);
137         $this->assertEquals($image->name, $imageData['name']);
138         $this->assertEquals('gallery', $imageData['type']);
139         $this->assertNotEmpty($imageData['file']);
140
141         $filePath = $zip->extractPath("files/{$imageData['file']}");
142         $this->assertFileExists($filePath);
143         $this->assertEquals(file_get_contents(public_path($image->path)), file_get_contents($filePath));
144
145         $this->assertEquals('<p><img src="[[bsexport:image:' . $imageData['id'] . ']]" alt="My image"></p>', $pageData['html']);
146     }
147
148     public function test_page_export_file_attachments()
149     {
150         $contents = 'My great attachment content!';
151
152         $page = $this->entities->page();
153         $this->asAdmin();
154         $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
155
156         $zipResp = $this->get($page->getUrl("/export/zip"));
157         $zip = $this->extractZipResponse($zipResp);
158
159         $pageData = $zip->data['page'];
160         $this->assertCount(1, $pageData['attachments']);
161
162         $attachmentData = $pageData['attachments'][0];
163         $this->assertEquals('PageAttachmentExport.txt', $attachmentData['name']);
164         $this->assertEquals($attachment->id, $attachmentData['id']);
165         $this->assertArrayNotHasKey('link', $attachmentData);
166         $this->assertNotEmpty($attachmentData['file']);
167
168         $fileRef = $attachmentData['file'];
169         $filePath = $zip->extractPath("/files/$fileRef");
170         $this->assertFileExists($filePath);
171         $this->assertEquals($contents, file_get_contents($filePath));
172     }
173
174     public function test_page_export_link_attachments()
175     {
176         $page = $this->entities->page();
177         $this->asEditor();
178         $attachment = Attachment::factory()->create([
179             'name' => 'My link attachment for export',
180             'path' => 'https://example.com/cats',
181             'external' => true,
182             'uploaded_to' => $page->id,
183             'order' => 1,
184         ]);
185
186         $zipResp = $this->get($page->getUrl("/export/zip"));
187         $zip = $this->extractZipResponse($zipResp);
188
189         $pageData = $zip->data['page'];
190         $this->assertCount(1, $pageData['attachments']);
191
192         $attachmentData = $pageData['attachments'][0];
193         $this->assertEquals('My link attachment for export', $attachmentData['name']);
194         $this->assertEquals($attachment->id, $attachmentData['id']);
195         $this->assertEquals('https://example.com/cats', $attachmentData['link']);
196         $this->assertArrayNotHasKey('file', $attachmentData);
197     }
198
199     public function test_book_export()
200     {
201         $book = $this->entities->bookHasChaptersAndPages();
202         $book->tags()->saveMany(Tag::factory()->count(2)->make());
203
204         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
205         $zip = $this->extractZipResponse($zipResp);
206         $this->assertArrayHasKey('book', $zip->data);
207
208         $bookData = $zip->data['book'];
209         $this->assertEquals($book->id, $bookData['id']);
210         $this->assertEquals($book->name, $bookData['name']);
211         $this->assertEquals($book->descriptionHtml(), $bookData['description_html']);
212         $this->assertCount(2, $bookData['tags']);
213         $this->assertCount($book->directPages()->count(), $bookData['pages']);
214         $this->assertCount($book->chapters()->count(), $bookData['chapters']);
215         $this->assertArrayNotHasKey('cover', $bookData);
216     }
217
218     public function test_book_export_with_cover_image()
219     {
220         $book = $this->entities->book();
221         $bookRepo = $this->app->make(BookRepo::class);
222         $coverImageFile = $this->files->uploadedImage('cover.png');
223         $bookRepo->updateCoverImage($book, $coverImageFile);
224         $coverImage = $book->cover()->first();
225
226         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
227         $zip = $this->extractZipResponse($zipResp);
228
229         $this->assertArrayHasKey('cover', $zip->data['book']);
230         $coverRef = $zip->data['book']['cover'];
231         $coverPath = $zip->extractPath("/files/$coverRef");
232         $this->assertFileExists($coverPath);
233         $this->assertEquals(file_get_contents(public_path($coverImage->path)), file_get_contents($coverPath));
234     }
235
236     public function test_chapter_export()
237     {
238         $chapter = $this->entities->chapter();
239         $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
240
241         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
242         $zip = $this->extractZipResponse($zipResp);
243         $this->assertArrayHasKey('chapter', $zip->data);
244
245         $chapterData = $zip->data['chapter'];
246         $this->assertEquals($chapter->id, $chapterData['id']);
247         $this->assertEquals($chapter->name, $chapterData['name']);
248         $this->assertEquals($chapter->descriptionHtml(), $chapterData['description_html']);
249         $this->assertCount(2, $chapterData['tags']);
250         $this->assertEquals($chapter->priority, $chapterData['priority']);
251         $this->assertCount($chapter->pages()->count(), $chapterData['pages']);
252     }
253
254     public function test_draft_pages_are_not_included()
255     {
256         $editor = $this->users->editor();
257         $entities = $this->entities->createChainBelongingToUser($editor);
258         $book = $entities['book'];
259         $page = $entities['page'];
260         $chapter = $entities['chapter'];
261         $book->tags()->saveMany(Tag::factory()->count(2)->make());
262
263         $page->created_by = $editor->id;
264         $page->draft = true;
265         $page->save();
266
267         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
268         $zip = $this->extractZipResponse($zipResp);
269         $this->assertCount(0, $zip->data['book']['chapters'][0]['pages'] ?? ['cat']);
270
271         $zipResp = $this->actingAs($editor)->get($chapter->getUrl("/export/zip"));
272         $zip = $this->extractZipResponse($zipResp);
273         $this->assertCount(0, $zip->data['chapter']['pages'] ?? ['cat']);
274
275         $page->chapter_id = 0;
276         $page->save();
277
278         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
279         $zip = $this->extractZipResponse($zipResp);
280         $this->assertCount(0, $zip->data['book']['pages'] ?? ['cat']);
281     }
282
283
284     public function test_cross_reference_links_are_converted()
285     {
286         $book = $this->entities->bookHasChaptersAndPages();
287         $chapter = $book->chapters()->first();
288         $page = $chapter->pages()->first();
289
290         $book->description_html = '<p><a href="' . $chapter->getUrl() . '">Link to chapter</a></p>';
291         $book->save();
292         $chapter->description_html = '<p><a href="' . $page->getUrl() . '#section2">Link to page</a></p>';
293         $chapter->save();
294         $page->html = '<p><a href="' . $book->getUrl() . '?view=true">Link to book</a></p>';
295         $page->save();
296
297         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
298         $zip = $this->extractZipResponse($zipResp);
299         $bookData = $zip->data['book'];
300         $chapterData = $bookData['chapters'][0];
301         $pageData = $chapterData['pages'][0];
302
303         $this->assertStringContainsString('href="[[bsexport:chapter:' . $chapter->id . ']]"', $bookData['description_html']);
304         $this->assertStringContainsString('href="[[bsexport:page:' . $page->id . ']]#section2"', $chapterData['description_html']);
305         $this->assertStringContainsString('href="[[bsexport:book:' . $book->id . ']]?view=true"', $pageData['html']);
306     }
307
308     public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
309     {
310         $book = $this->entities->bookHasChaptersAndPages();
311         $chapter = $book->chapters()->first();
312         $page = $chapter->pages()->first();
313
314         $this->asEditor();
315         $this->files->uploadGalleryImageToPage($this, $page);
316         /** @var Image $image */
317         $image = Image::query()->where('type', '=', 'gallery')
318             ->where('uploaded_to', '=', $page->id)->first();
319
320         $book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
321         $book->save();
322         $chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
323         $chapter->save();
324
325         $zipResp = $this->get($book->getUrl("/export/zip"));
326         $zip = $this->extractZipResponse($zipResp);
327         $bookData = $zip->data['book'];
328         $chapterData = $bookData['chapters'][0];
329
330         $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
331         $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
332     }
333
334     public function test_image_links_are_handled_when_using_external_storage_url()
335     {
336         $page = $this->entities->page();
337
338         $this->asEditor();
339         $this->files->uploadGalleryImageToPage($this, $page);
340         /** @var Image $image */
341         $image = Image::query()->where('type', '=', 'gallery')
342             ->where('uploaded_to', '=', $page->id)->first();
343
344         config()->set('filesystems.url', 'https://i.example.com/content');
345
346         $storageUrl = 'https://i.example.com/content/' . ltrim($image->path, '/');
347         $page->html = '<p><a href="' . $image->url . '">Original URL</a><a href="' . $storageUrl . '">Storage URL</a></p>';
348         $page->save();
349
350         $zipResp = $this->get($page->getUrl("/export/zip"));
351         $zip = $this->extractZipResponse($zipResp);
352         $pageData = $zip->data['page'];
353
354         $ref = '[[bsexport:image:' . $image->id . ']]';
355         $this->assertStringContainsString("<a href=\"{$ref}\">Original URL</a><a href=\"{$ref}\">Storage URL</a>", $pageData['html']);
356     }
357
358     public function test_cross_reference_links_external_to_export_are_not_converted()
359     {
360         $page = $this->entities->page();
361         $page->html = '<p><a href="' . $page->book->getUrl() . '">Link to book</a></p>';
362         $page->save();
363
364         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
365         $zip = $this->extractZipResponse($zipResp);
366         $pageData = $zip->data['page'];
367
368         $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
369     }
370
371     public function test_attachments_links_are_converted()
372     {
373         $page = $this->entities->page();
374         $attachment = Attachment::factory()->create([
375             'name' => 'My link attachment for export reference',
376             'path' => 'https://example.com/cats/ref',
377             'external' => true,
378             'uploaded_to' => $page->id,
379             'order' => 1,
380         ]);
381
382         $page->html = '<p><a href="' . url("/attachments/{$attachment->id}") . '?open=true">Link to attachment</a></p>';
383         $page->save();
384
385         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
386         $zip = $this->extractZipResponse($zipResp);
387         $pageData = $zip->data['page'];
388
389         $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
390     }
391
392     public function test_links_in_markdown_are_parsed()
393     {
394         $chapter = $this->entities->chapterHasPages();
395         $page = $chapter->pages()->first();
396
397         $page->markdown = "[Link to chapter]({$chapter->getUrl()})";
398         $page->save();
399
400         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
401         $zip = $this->extractZipResponse($zipResp);
402         $pageData = $zip->data['chapter']['pages'][0];
403
404         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
405     }
406
407     protected function extractZipResponse(TestResponse $response): ZipResultData
408     {
409         $zipData = $response->streamedContent();
410         $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
411
412         file_put_contents($zipFile, $zipData);
413         $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
414         if (file_exists($extractDir)) {
415             unlink($extractDir);
416         }
417         mkdir($extractDir);
418
419         $zip = new ZipArchive();
420         $zip->open($zipFile, ZipArchive::RDONLY);
421         $zip->extractTo($extractDir);
422
423         $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
424         $data = json_decode($dataJson, true);
425
426         return new ZipResultData(
427             $zipFile,
428             $extractDir,
429             $data,
430         );
431     }
432 }