3 namespace Tests\Exports;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Exports\ZipExports\ZipImportRunner;
8 use BookStack\Uploads\Image;
11 class ZipImportRunnerTest extends TestCase
13 protected ZipImportRunner $runner;
15 protected function setUp(): void
18 $this->runner = app()->make(ZipImportRunner::class);
21 public function test_book_import()
23 $testImagePath = $this->files->testFilePath('test-image.png');
24 $testFilePath = $this->files->testFilePath('test-file.txt');
25 $import = ZipTestHelper::importFromData([], [
28 'name' => 'Import test',
29 'cover' => 'book_cover_image',
30 'description_html' => '<p><a href="[[bsexport:page:3]]">Link to chapter page</a></p>',
32 ['name' => 'Animal', 'value' => 'Cat'],
33 ['name' => 'Category', 'value' => 'Test'],
38 'name' => 'Chapter A',
39 'description_html' => '<p><a href="[[bsexport:book:5]]">Link to book</a></p>',
42 ['name' => 'Reviewed'],
43 ['name' => 'Category', 'value' => 'Test Chapter'],
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>',
55 ['name' => 'Unreviewed'],
60 'name' => 'Text attachment',
61 'file' => 'file_attachment'
65 'link' => 'https://example.com/cats',
77 'name' => 'Dog Drawing',
86 'name' => 'Chapter child B',
93 'markdown' => '[Link to text]([[bsexport:attachment:4]]?scale=big)',
99 'book_cover_image' => $testImagePath,
100 'file_attachment' => $testFilePath,
101 'cat_image' => $testImagePath,
102 'dog_image' => $testImagePath,
106 /** @var Book $book */
107 $book = $this->runner->run($import);
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());
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);
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);
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');
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);
163 // Reference in converted markdown
164 $pageC = $children[1];
165 $this->assertStringContainsString("href=\"{$textAttachmentUrl}?scale=big\"", $pageC->html);
167 ZipTestHelper::deleteZipForImport($import);
170 // TODO - Test full book import
171 // TODO - Test full chapter import
172 // TODO - Test full page import