]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipExportTest.php
Sorting: Fixes during testing of sort rules
[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 FilesystemIterator;
11 use Illuminate\Support\Carbon;
12 use Illuminate\Testing\TestResponse;
13 use Tests\TestCase;
14 use ZipArchive;
15
16 class ZipExportTest extends TestCase
17 {
18     public function test_export_results_in_zip_format()
19     {
20         $page = $this->entities->page();
21         $response = $this->asEditor()->get($page->getUrl("/export/zip"));
22
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);
28
29         $this->assertNotFalse($zip->locateName('data.json'));
30         $this->assertNotFalse($zip->locateName('files/'));
31
32         $data = json_decode($zip->getFromName('data.json'), true);
33         $this->assertIsArray($data);
34         $this->assertGreaterThan(0, count($data));
35
36         $zip->close();
37         unlink($zipFile);
38     }
39
40     public function test_export_metadata()
41     {
42         $page = $this->entities->page();
43         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
44         $zip = $this->extractZipResponse($zipResp);
45
46         $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
47         $this->assertArrayNotHasKey('book', $zip->data);
48         $this->assertArrayNotHasKey('chapter', $zip->data);
49
50         $now = time();
51         $date = Carbon::parse($zip->data['exported_at'])->unix();
52         $this->assertLessThan($now + 2, $date);
53         $this->assertGreaterThan($now - 2, $date);
54
55         $version = trim(file_get_contents(base_path('version')));
56         $this->assertEquals($version, $zip->data['instance']['version']);
57
58         $zipInstanceId = $zip->data['instance']['id'];
59         $instanceId = setting('instance-id');
60         $this->assertNotEmpty($instanceId);
61         $this->assertEquals($instanceId, $zipInstanceId);
62     }
63
64     public function test_export_leaves_no_temp_files()
65     {
66         $tempDir = sys_get_temp_dir();
67         $startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
68
69         $page = $this->entities->pageWithinChapter();
70         $this->asEditor();
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();
76
77         $afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
78
79         $this->assertEquals($startTempFileCount, $afterTempFileCount);
80     }
81
82     public function test_page_export()
83     {
84         $page = $this->entities->page();
85         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
86         $zip = $this->extractZipResponse($zipResp);
87
88         $pageData = $zip->data['page'];
89         $this->assertEquals([
90             'id' => $page->id,
91             'name' => $page->name,
92             'html' => (new PageContent($page))->render(),
93             'priority' => $page->priority,
94             'attachments' => [],
95             'images' => [],
96             'tags' => [],
97         ], $pageData);
98     }
99
100     public function test_page_export_with_markdown()
101     {
102         $page = $this->entities->page();
103         $markdown = "# My page\n\nwritten in markdown for export\n";
104         $page->markdown = $markdown;
105         $page->save();
106
107         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
108         $zip = $this->extractZipResponse($zipResp);
109
110         $pageData = $zip->data['page'];
111         $this->assertEquals($markdown, $pageData['markdown']);
112         $this->assertNotEmpty($pageData['html']);
113     }
114
115     public function test_page_export_with_tags()
116     {
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]),
121         ]);
122
123         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
124         $zip = $this->extractZipResponse($zipResp);
125
126         $pageData = $zip->data['page'];
127         $this->assertEquals([
128             [
129                 'name' => 'Exporty',
130                 'value' => 'Content',
131             ],
132             [
133                 'name' => 'Another',
134                 'value' => '',
135             ]
136         ], $pageData['tags']);
137     }
138
139     public function test_page_export_with_images()
140     {
141         $this->asEditor();
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>';
146         $page->save();
147         $image = Image::findOrFail($result['response']->id);
148
149         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
150         $zip = $this->extractZipResponse($zipResp);
151         $pageData = $zip->data['page'];
152
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']);
159
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));
163
164         $this->assertEquals('<p><img src="[[bsexport:image:' . $imageData['id'] . ']]" alt="My image"></p>', $pageData['html']);
165     }
166
167     public function test_page_export_file_attachments()
168     {
169         $contents = 'My great attachment content!';
170
171         $page = $this->entities->page();
172         $this->asAdmin();
173         $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'PageAttachmentExport.txt', $contents, 'text/plain');
174
175         $zipResp = $this->get($page->getUrl("/export/zip"));
176         $zip = $this->extractZipResponse($zipResp);
177
178         $pageData = $zip->data['page'];
179         $this->assertCount(1, $pageData['attachments']);
180
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']);
186
187         $fileRef = $attachmentData['file'];
188         $filePath = $zip->extractPath("/files/$fileRef");
189         $this->assertFileExists($filePath);
190         $this->assertEquals($contents, file_get_contents($filePath));
191     }
192
193     public function test_page_export_link_attachments()
194     {
195         $page = $this->entities->page();
196         $this->asEditor();
197         $attachment = Attachment::factory()->create([
198             'name' => 'My link attachment for export',
199             'path' => 'https://example.com/cats',
200             'external' => true,
201             'uploaded_to' => $page->id,
202             'order' => 1,
203         ]);
204
205         $zipResp = $this->get($page->getUrl("/export/zip"));
206         $zip = $this->extractZipResponse($zipResp);
207
208         $pageData = $zip->data['page'];
209         $this->assertCount(1, $pageData['attachments']);
210
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);
216     }
217
218     public function test_book_export()
219     {
220         $book = $this->entities->bookHasChaptersAndPages();
221         $book->tags()->saveMany(Tag::factory()->count(2)->make());
222
223         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
224         $zip = $this->extractZipResponse($zipResp);
225         $this->assertArrayHasKey('book', $zip->data);
226
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);
235     }
236
237     public function test_book_export_with_cover_image()
238     {
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();
244
245         $zipResp = $this->asEditor()->get($book->getUrl("/export/zip"));
246         $zip = $this->extractZipResponse($zipResp);
247
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));
253     }
254
255     public function test_chapter_export()
256     {
257         $chapter = $this->entities->chapter();
258         $chapter->tags()->saveMany(Tag::factory()->count(2)->make());
259
260         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
261         $zip = $this->extractZipResponse($zipResp);
262         $this->assertArrayHasKey('chapter', $zip->data);
263
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']);
271     }
272
273     public function test_draft_pages_are_not_included()
274     {
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());
281
282         $page->created_by = $editor->id;
283         $page->draft = true;
284         $page->save();
285
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']);
289
290         $zipResp = $this->actingAs($editor)->get($chapter->getUrl("/export/zip"));
291         $zip = $this->extractZipResponse($zipResp);
292         $this->assertCount(0, $zip->data['chapter']['pages'] ?? ['cat']);
293
294         $page->chapter_id = 0;
295         $page->save();
296
297         $zipResp = $this->actingAs($editor)->get($book->getUrl("/export/zip"));
298         $zip = $this->extractZipResponse($zipResp);
299         $this->assertCount(0, $zip->data['book']['pages'] ?? ['cat']);
300     }
301
302
303     public function test_cross_reference_links_are_converted()
304     {
305         $book = $this->entities->bookHasChaptersAndPages();
306         $chapter = $book->chapters()->first();
307         $page = $chapter->pages()->first();
308
309         $book->description_html = '<p><a href="' . $chapter->getUrl() . '">Link to chapter</a></p>';
310         $book->save();
311         $chapter->description_html = '<p><a href="' . $page->getUrl() . '#section2">Link to page</a></p>';
312         $chapter->save();
313         $page->html = '<p><a href="' . $book->getUrl() . '?view=true">Link to book</a></p>';
314         $page->save();
315
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];
321
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']);
325     }
326
327     public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
328     {
329         $book = $this->entities->bookHasChaptersAndPages();
330         $chapter = $book->chapters()->first();
331         $page = $chapter->pages()->first();
332
333         $this->asEditor();
334         $this->files->uploadGalleryImageToPage($this, $page);
335         /** @var Image $image */
336         $image = Image::query()->where('type', '=', 'gallery')
337             ->where('uploaded_to', '=', $page->id)->first();
338
339         $book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
340         $book->save();
341         $chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
342         $chapter->save();
343
344         $zipResp = $this->get($book->getUrl("/export/zip"));
345         $zip = $this->extractZipResponse($zipResp);
346         $bookData = $zip->data['book'];
347         $chapterData = $bookData['chapters'][0];
348
349         $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
350         $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
351     }
352
353     public function test_image_links_are_handled_when_using_external_storage_url()
354     {
355         $page = $this->entities->page();
356
357         $this->asEditor();
358         $this->files->uploadGalleryImageToPage($this, $page);
359         /** @var Image $image */
360         $image = Image::query()->where('type', '=', 'gallery')
361             ->where('uploaded_to', '=', $page->id)->first();
362
363         config()->set('filesystems.url', 'https://i.example.com/content');
364
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>';
367         $page->save();
368
369         $zipResp = $this->get($page->getUrl("/export/zip"));
370         $zip = $this->extractZipResponse($zipResp);
371         $pageData = $zip->data['page'];
372
373         $ref = '[[bsexport:image:' . $image->id . ']]';
374         $this->assertStringContainsString("<a href=\"{$ref}\">Original URL</a><a href=\"{$ref}\">Storage URL</a>", $pageData['html']);
375     }
376
377     public function test_cross_reference_links_external_to_export_are_not_converted()
378     {
379         $page = $this->entities->page();
380         $page->html = '<p><a href="' . $page->book->getUrl() . '">Link to book</a></p>';
381         $page->save();
382
383         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
384         $zip = $this->extractZipResponse($zipResp);
385         $pageData = $zip->data['page'];
386
387         $this->assertStringContainsString('href="' . $page->book->getUrl() . '"', $pageData['html']);
388     }
389
390     public function test_attachments_links_are_converted()
391     {
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',
396             'external' => true,
397             'uploaded_to' => $page->id,
398             'order' => 1,
399         ]);
400
401         $page->html = '<p><a href="' . url("/attachments/{$attachment->id}") . '?open=true">Link to attachment</a></p>';
402         $page->save();
403
404         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
405         $zip = $this->extractZipResponse($zipResp);
406         $pageData = $zip->data['page'];
407
408         $this->assertStringContainsString('href="[[bsexport:attachment:' . $attachment->id . ']]?open=true"', $pageData['html']);
409     }
410
411     public function test_links_in_markdown_are_parsed()
412     {
413         $chapter = $this->entities->chapterHasPages();
414         $page = $chapter->pages()->first();
415
416         $page->markdown = "[Link to chapter]({$chapter->getUrl()})";
417         $page->save();
418
419         $zipResp = $this->asEditor()->get($chapter->getUrl("/export/zip"));
420         $zip = $this->extractZipResponse($zipResp);
421         $pageData = $zip->data['chapter']['pages'][0];
422
423         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
424     }
425
426     public function test_exports_rate_limited_low_for_guest_viewers()
427     {
428         $this->setSettings(['app-public' => 'true']);
429
430         $page = $this->entities->page();
431         for ($i = 0; $i < 4; $i++) {
432             $this->get($page->getUrl("/export/zip"))->assertOk();
433         }
434         $this->get($page->getUrl("/export/zip"))->assertStatus(429);
435     }
436
437     public function test_exports_rate_limited_higher_for_logged_in_viewers()
438     {
439         $this->asAdmin();
440
441         $page = $this->entities->page();
442         for ($i = 0; $i < 10; $i++) {
443             $this->get($page->getUrl("/export/zip"))->assertOk();
444         }
445         $this->get($page->getUrl("/export/zip"))->assertStatus(429);
446     }
447
448     protected function extractZipResponse(TestResponse $response): ZipResultData
449     {
450         $zipData = $response->streamedContent();
451         $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
452
453         file_put_contents($zipFile, $zipData);
454         $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
455         if (file_exists($extractDir)) {
456             unlink($extractDir);
457         }
458         mkdir($extractDir);
459
460         $zip = new ZipArchive();
461         $zip->open($zipFile, ZipArchive::RDONLY);
462         $zip->extractTo($extractDir);
463
464         $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
465         $data = json_decode($dataJson, true);
466
467         return new ZipResultData(
468             $zipFile,
469             $extractDir,
470             $data,
471         );
472     }
473 }