]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipExportValidatorTest.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / tests / Exports / ZipExportValidatorTest.php
1 <?php
2
3 namespace Tests\Exports;
4
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;
12 use Tests\TestCase;
13
14 class ZipExportValidatorTest extends TestCase
15 {
16     protected array $filesToRemove = [];
17
18     protected function tearDown(): void
19     {
20         foreach ($this->filesToRemove as $file) {
21             unlink($file);
22         }
23
24         parent::tearDown();
25     }
26
27     protected function getValidatorForData(array $zipData, array $files = []): ZipExportValidator
28     {
29         $upload = ZipTestHelper::zipUploadFromData($zipData, $files);
30         $path = $upload->getRealPath();
31         $this->filesToRemove[] = $path;
32         $reader = new ZipExportReader($path);
33         return new ZipExportValidator($reader);
34     }
35
36     public function test_ids_have_to_be_unique()
37     {
38         $validator = $this->getValidatorForData([
39             'book' => [
40                 'id' => 4,
41                 'name' => 'My book',
42                 'pages' => [
43                     [
44                         'id' => 4,
45                         'name' => 'My page',
46                         'markdown' => 'hello',
47                         'attachments' => [
48                             ['id' => 4, 'name' => 'Attachment A', 'link' => 'https://example.com'],
49                             ['id' => 4, 'name' => 'Attachment B', 'link' => 'https://example.com']
50                         ],
51                         'images' => [
52                             ['id' => 4, 'name' => 'Image A', 'type' => 'gallery', 'file' => 'cat'],
53                             ['id' => 4, 'name' => 'Image b', 'type' => 'gallery', 'file' => 'cat'],
54                         ],
55                     ],
56                     ['id' => 4, 'name' => 'My page', 'markdown' => 'hello'],
57                 ],
58                 'chapters' => [
59                     ['id' => 4, 'name' => 'Chapter 1'],
60                     ['id' => 4, 'name' => 'Chapter 2']
61                 ]
62             ]
63         ], ['cat' => $this->files->testFilePath('test-image.png')]);
64
65         $results = $validator->validate();
66         $this->assertCount(4, $results);
67
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']);
73     }
74
75     public function test_image_files_need_to_be_a_valid_detected_image_file()
76     {
77         $validator = $this->getValidatorForData([
78             'page' => [
79                 'id' => 4,
80                 'name' => 'My page',
81                 'markdown' => 'hello',
82                 'images' => [
83                     ['id' => 4, 'name' => 'Image A', 'type' => 'gallery', 'file' => 'cat'],
84                 ],
85             ]
86         ], ['cat' => $this->files->testFilePath('test-file.txt')]);
87
88         $results = $validator->validate();
89         $this->assertCount(1, $results);
90
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']);
92     }
93 }