3 namespace Tests\Exports;
5 use Illuminate\Support\Carbon;
6 use Illuminate\Testing\TestResponse;
10 class ZipExportTest extends TestCase
12 public function test_export_results_in_zip_format()
14 $page = $this->entities->page();
15 $response = $this->asEditor()->get($page->getUrl("/export/zip"));
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);
23 $this->assertNotFalse($zip->locateName('data.json'));
24 $this->assertNotFalse($zip->locateName('files/'));
26 $data = json_decode($zip->getFromName('data.json'), true);
27 $this->assertIsArray($data);
28 $this->assertGreaterThan(0, count($data));
34 public function test_export_metadata()
36 $page = $this->entities->page();
37 $zipResp = $this->asEditor()->get($page->getUrl("/export/zip"));
38 $zip = $this->extractZipResponse($zipResp);
40 $this->assertEquals($page->id, $zip->data['page']['id'] ?? null);
41 $this->assertArrayNotHasKey('book', $zip->data);
42 $this->assertArrayNotHasKey('chapter', $zip->data);
45 $date = Carbon::parse($zip->data['exported_at'])->unix();
46 $this->assertLessThan($now + 2, $date);
47 $this->assertGreaterThan($now - 2, $date);
49 $version = trim(file_get_contents(base_path('version')));
50 $this->assertEquals($version, $zip->data['instance']['version']);
52 $instanceId = decrypt($zip->data['instance']['id_ciphertext']);
53 $this->assertEquals('bookstack', $instanceId);
56 public function test_page_export()
61 public function test_book_export()
66 public function test_chapter_export()
71 protected function extractZipResponse(TestResponse $response): ZipResultData
73 $zipData = $response->streamedContent();
74 $zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
76 file_put_contents($zipFile, $zipData);
77 $extractDir = tempnam(sys_get_temp_dir(), 'bstestextracted-');
78 if (file_exists($extractDir)) {
83 $zip = new ZipArchive();
84 $zip->open($zipFile, ZipArchive::RDONLY);
85 $zip->extractTo($extractDir);
87 $dataJson = file_get_contents($extractDir . DIRECTORY_SEPARATOR . "data.json");
88 $data = json_decode($dataJson, true);
90 return new ZipResultData(