]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipExportTest.php
536e23806f6afba9e60ae5bbac18f3832068d6c9
[bookstack] / tests / Exports / ZipExportTest.php
1 <?php
2
3 namespace Tests\Exports;
4
5 use Illuminate\Support\Carbon;
6 use Illuminate\Testing\TestResponse;
7 use Tests\TestCase;
8 use ZipArchive;
9
10 class ZipExportTest extends TestCase
11 {
12     public function test_export_results_in_zip_format()
13     {
14         $page = $this->entities->page();
15         $response = $this->asEditor()->get($page->getUrl("/export/zip"));
16
17         $zipData = $response->streamedContent();
18         $zipFile = tempnam(sys_get_temp_dir(), 'bstesta-');
19         file_put_contents($zipFile, $zipData);
20         $zip = new ZipArchive();
21         $zip->open($zipFile, ZipArchive::RDONLY);
22
23         $this->assertNotFalse($zip->locateName('data.json'));
24         $this->assertNotFalse($zip->locateName('files/'));
25
26         $data = json_decode($zip->getFromName('data.json'), true);
27         $this->assertIsArray($data);
28         $this->assertGreaterThan(0, count($data));
29
30         $zip->close();
31         unlink($zipFile);
32     }
33
34     public function test_export_metadata()
35     {
36         $page = $this->entities->page();
37         $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
38         $zip = $this->extractZipResponse($zipResp);
39
40         $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
41         $this->assertArrayNotHasKey('book', $zip->data);
42         $this->assertArrayNotHasKey('chapter', $zip->data);
43
44         $now = time();
45         $date = Carbon::parse($zip->data['exported_at'])->unix();
46         $this->assertLessThan($now + 2, $date);
47         $this->assertGreaterThan($now - 2, $date);
48
49         $version = trim(file_get_contents(base_path('version')));
50         $this->assertEquals($version, $zip->data['instance']['version']);
51
52         $instanceId = decrypt($zip->data['instance']['id_ciphertext']);
53         $this->assertEquals('bookstack', $instanceId);
54     }
55
56     public function test_page_export()
57     {
58         // TODO
59     }
60
61     public function test_book_export()
62     {
63         // TODO
64     }
65
66     public function test_chapter_export()
67     {
68         // TODO
69     }
70
71     protected function extractZipResponse(TestResponse $response): ZipResultData
72     {
73         $zipData = $response->streamedContent();
74         $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
75
76         file_put_contents($zipFile, $zipData);
77         $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
78         if (file_exists($extractDir)) {
79             unlink($extractDir);
80         }
81         mkdir($extractDir);
82
83         $zip = new ZipArchive();
84         $zip->open($zipFile, ZipArchive::RDONLY);
85         $zip->extractTo($extractDir);
86
87         $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
88         $data = json_decode($dataJson, true);
89
90         return new ZipResultData(
91             $zipFile,
92             $extractDir,
93             $data,
94         );
95     }
96 }