3 namespace Tests\Exports;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Exports\ZipExports\ZipExportReader;
9 use BookStack\Exports\ZipExports\ZipExportValidator;
10 use BookStack\Exports\ZipExports\ZipImportRunner;
11 use BookStack\Uploads\Image;
14 class ZipExportValidatorTest extends TestCase
16 protected array $filesToRemove = [];
18 protected function tearDown(): void
20 foreach ($this->filesToRemove as $file) {
27 protected function getValidatorForData(array $zipData, array $files = []): ZipExportValidator
29 $upload = ZipTestHelper::zipUploadFromData($zipData, $files);
30 $path = $upload->getRealPath();
31 $this->filesToRemove[] = $path;
32 $reader = new ZipExportReader($path);
33 return new ZipExportValidator($reader);
36 public function test_ids_have_to_be_unique()
38 $validator = $this->getValidatorForData([
46 'markdown' => 'hello',
48 ['id' => 4, 'name' => 'Attachment A', 'link' => 'https://example.com'],
49 ['id' => 4, 'name' => 'Attachment B', 'link' => 'https://example.com']
52 ['id' => 4, 'name' => 'Image A', 'type' => 'gallery', 'file' => 'cat'],
53 ['id' => 4, 'name' => 'Image b', 'type' => 'gallery', 'file' => 'cat'],
56 ['id' => 4, 'name' => 'My page', 'markdown' => 'hello'],
59 ['id' => 4, 'name' => 'Chapter 1'],
60 ['id' => 4, 'name' => 'Chapter 2']
63 ], ['cat' => $this->files->testFilePath('test-image.png')]);
65 $results = $validator->validate();
66 $this->assertCount(4, $results);
68 $expectedMessage = 'The id must be unique for the object type within the ZIP.';
69 $this->assertEquals($expectedMessage, $results['book.pages.0.attachments.1.id']);
70 $this->assertEquals($expectedMessage, $results['book.pages.0.images.1.id']);
71 $this->assertEquals($expectedMessage, $results['book.pages.1.id']);
72 $this->assertEquals($expectedMessage, $results['book.chapters.1.id']);
75 public function test_image_files_need_to_be_a_valid_detected_image_file()
77 $validator = $this->getValidatorForData([
81 'markdown' => 'hello',
83 ['id' => 4, 'name' => 'Image A', 'type' => 'gallery', 'file' => 'cat'],
86 ], ['cat' => $this->files->testFilePath('test-file.txt')]);
88 $results = $validator->validate();
89 $this->assertCount(1, $results);
91 $this->assertEquals('The file needs to reference a file of type image/png,image/jpeg,image/gif,image/webp, found text/plain.', $results['page.images.0.file']);