]> BookStack Code Mirror - bookstack/blob - tests/Exports/ZipImportRunnerTest.php
f07b3f41b424a1c64f2b6de6f9195df7ca1e6539
[bookstack] / tests / Exports / ZipImportRunnerTest.php
1 <?php
2
3 namespace Tests\Exports;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Exports\ZipExports\ZipImportRunner;
8 use BookStack\Uploads\Image;
9 use Tests\TestCase;
10
11 class ZipImportRunnerTest extends TestCase
12 {
13     protected ZipImportRunner $runner;
14
15     protected function setUp(): void
16     {
17         parent::setUp();
18         $this->runner = app()->make(ZipImportRunner::class);
19     }
20
21     public function test_book_import()
22     {
23         $testImagePath = $this->files->testFilePath('test-image.png');
24         $testFilePath = $this->files->testFilePath('test-file.txt');
25         $import = ZipTestHelper::importFromData([], [
26             'book' => [
27                 'id' => 5,
28                 'name' => 'Import test',
29                 'cover' => 'book_cover_image',
30                 'description_html' => '<p><a href="[[bsexport:page:3]]">Link to chapter page</a></p>',
31                 'tags' => [
32                     ['name' => 'Animal', 'value' => 'Cat'],
33                     ['name' => 'Category', 'value' => 'Test'],
34                 ],
35                 'chapters' => [
36                     [
37                         'id' => 6,
38                         'name' => 'Chapter A',
39                         'description_html' => '<p><a href="[[bsexport:book:5]]">Link to book</a></p>',
40                         'priority' => 1,
41                         'tags' => [
42                             ['name' => 'Reviewed'],
43                             ['name' => 'Category', 'value' => 'Test Chapter'],
44                         ],
45                         'pages' => [
46                             [
47                                 'id' => 3,
48                                 'name' => 'Page A',
49                                 'priority' => 6,
50                                 'html' => '
51 <p><a href="[[bsexport:page:3]]">Link to self</a></p>
52 <p><a href="[[bsexport:image:1]]">Link to cat image</a></p>
53 <p><a href="[[bsexport:attachment:4]]">Link to text attachment</a></p>',
54                                 'tags' => [
55                                     ['name' => 'Unreviewed'],
56                                 ],
57                                 'attachments' => [
58                                     [
59                                         'id' => 4,
60                                         'name' => 'Text attachment',
61                                         'file' => 'file_attachment'
62                                     ],
63                                     [
64                                         'name' => 'Cats',
65                                         'link' => 'https://example.com/cats',
66                                     ]
67                                 ],
68                                 'images' => [
69                                     [
70                                         'id' => 1,
71                                         'name' => 'Cat',
72                                         'type' => 'gallery',
73                                         'file' => 'cat_image'
74                                     ],
75                                     [
76                                         'id' => 2,
77                                         'name' => 'Dog Drawing',
78                                         'type' => 'drawio',
79                                         'file' => 'dog_image'
80                                     ]
81                                 ],
82                             ],
83                         ],
84                     ],
85                     [
86                         'name' => 'Chapter child B',
87                         'priority' => 5,
88                     ]
89                 ],
90                 'pages' => [
91                     [
92                         'name' => 'Page C',
93                         'markdown' => '[Link to text]([[bsexport:attachment:4]]?scale=big)',
94                         'priority' => 3,
95                     ]
96                 ],
97             ],
98         ], [
99             'book_cover_image' => $testImagePath,
100             'file_attachment'  => $testFilePath,
101             'cat_image' => $testImagePath,
102             'dog_image' => $testImagePath,
103         ]);
104
105         $this->asAdmin();
106         /** @var Book $book */
107         $book = $this->runner->run($import);
108
109         // Book checks
110         $this->assertEquals('Import test', $book->name);
111         $this->assertFileExists(public_path($book->cover->path));
112         $this->assertCount(2, $book->tags);
113         $this->assertEquals('Cat', $book->tags()->first()->value);
114         $this->assertCount(2, $book->chapters);
115         $this->assertEquals(1, $book->directPages()->count());
116
117         // Chapter checks
118         $chapterA = $book->chapters()->where('name', 'Chapter A')->first();
119         $this->assertCount(2, $chapterA->tags);
120         $firstChapterTag = $chapterA->tags()->first();
121         $this->assertEquals('Reviewed', $firstChapterTag->name);
122         $this->assertEquals('', $firstChapterTag->value);
123         $this->assertCount(1, $chapterA->pages);
124
125         // Page checks
126         /** @var Page $pageA */
127         $pageA = $chapterA->pages->first();
128         $this->assertEquals('Page A', $pageA->name);
129         $this->assertCount(1, $pageA->tags);
130         $firstPageTag = $pageA->tags()->first();
131         $this->assertEquals('Unreviewed', $firstPageTag->name);
132         $this->assertCount(2, $pageA->attachments);
133         $firstAttachment = $pageA->attachments->first();
134         $this->assertEquals('Text attachment', $firstAttachment->name);
135         $this->assertFileEquals($testFilePath, storage_path($firstAttachment->path));
136         $this->assertFalse($firstAttachment->external);
137         $secondAttachment = $pageA->attachments->last();
138         $this->assertEquals('Cats', $secondAttachment->name);
139         $this->assertEquals('https://example.com/cats', $secondAttachment->path);
140         $this->assertTrue($secondAttachment->external);
141         $pageAImages = Image::where('uploaded_to', '=', $pageA->id)->whereIn('type', ['gallery', 'drawio'])->get();
142         $this->assertCount(2, $pageAImages);
143         $this->assertEquals('Cat', $pageAImages[0]->name);
144         $this->assertEquals('gallery', $pageAImages[0]->type);
145         $this->assertFileEquals($testImagePath, public_path($pageAImages[0]->path));
146         $this->assertEquals('Dog Drawing', $pageAImages[1]->name);
147         $this->assertEquals('drawio', $pageAImages[1]->type);
148
149         // Book order check
150         $children = $book->getDirectVisibleChildren()->values()->all();
151         $this->assertEquals($children[0]->name, 'Chapter A');
152         $this->assertEquals($children[1]->name, 'Page C');
153         $this->assertEquals($children[2]->name, 'Chapter child B');
154
155         // Reference checks
156         $textAttachmentUrl = $firstAttachment->getUrl();
157         $this->assertStringContainsString($pageA->getUrl(), $book->description_html);
158         $this->assertStringContainsString($book->getUrl(), $chapterA->description_html);
159         $this->assertStringContainsString($pageA->getUrl(), $pageA->html);
160         $this->assertStringContainsString($pageAImages[0]->getThumb(1680, null, true), $pageA->html);
161         $this->assertStringContainsString($firstAttachment->getUrl(), $pageA->html);
162
163         // Reference in converted markdown
164         $pageC = $children[1];
165         $this->assertStringContainsString("href=\"{$textAttachmentUrl}?scale=big\"", $pageC->html);
166
167         ZipTestHelper::deleteZipForImport($import);
168     }
169
170     // TODO - Test full book import
171     // TODO - Test full chapter import
172     // TODO - Test full page import
173 }