|
3 | 3 | namespace Tests\Exports;
|
4 | 4 |
|
5 | 5 | use BookStack\Entities\Models\Book;
|
| 6 | +use BookStack\Entities\Models\Chapter; |
6 | 7 | use BookStack\Entities\Models\Page;
|
7 | 8 | use BookStack\Exports\ZipExports\ZipImportRunner;
|
8 | 9 | use BookStack\Uploads\Image;
|
@@ -167,7 +168,194 @@ public function test_book_import()
|
167 | 168 | ZipTestHelper::deleteZipForImport($import);
|
168 | 169 | }
|
169 | 170 |
|
170 |
| - // TODO - Test full book import |
171 |
| - // TODO - Test full chapter import |
172 |
| - // TODO - Test full page import |
| 171 | + public function test_chapter_import() |
| 172 | + { |
| 173 | + $testImagePath = $this->files->testFilePath('test-image.png'); |
| 174 | + $testFilePath = $this->files->testFilePath('test-file.txt'); |
| 175 | + $parent = $this->entities->book(); |
| 176 | + |
| 177 | + $import = ZipTestHelper::importFromData([], [ |
| 178 | + 'chapter' => [ |
| 179 | + 'id' => 6, |
| 180 | + 'name' => 'Chapter A', |
| 181 | + 'description_html' => '<p><a href="[[bsexport:page:3]]">Link to page</a></p>', |
| 182 | + 'priority' => 1, |
| 183 | + 'tags' => [ |
| 184 | + ['name' => 'Reviewed', 'value' => '2024'], |
| 185 | + ], |
| 186 | + 'pages' => [ |
| 187 | + [ |
| 188 | + 'id' => 3, |
| 189 | + 'name' => 'Page A', |
| 190 | + 'priority' => 6, |
| 191 | + 'html' => '<p><a href="[[bsexport:chapter:6]]">Link to chapter</a></p> |
| 192 | +<p><a href="[[bsexport:image:2]]">Link to dog drawing</a></p> |
| 193 | +<p><a href="[[bsexport:attachment:4]]">Link to text attachment</a></p>', |
| 194 | + 'tags' => [ |
| 195 | + ['name' => 'Unreviewed'], |
| 196 | + ], |
| 197 | + 'attachments' => [ |
| 198 | + [ |
| 199 | + 'id' => 4, |
| 200 | + 'name' => 'Text attachment', |
| 201 | + 'file' => 'file_attachment' |
| 202 | + ] |
| 203 | + ], |
| 204 | + 'images' => [ |
| 205 | + [ |
| 206 | + 'id' => 2, |
| 207 | + 'name' => 'Dog Drawing', |
| 208 | + 'type' => 'drawio', |
| 209 | + 'file' => 'dog_image' |
| 210 | + ] |
| 211 | + ], |
| 212 | + ], |
| 213 | + [ |
| 214 | + 'name' => 'Page B', |
| 215 | + 'markdown' => '[Link to page A]([[bsexport:page:3]])', |
| 216 | + 'priority' => 9, |
| 217 | + ], |
| 218 | + ], |
| 219 | + ], |
| 220 | + ], [ |
| 221 | + 'file_attachment' => $testFilePath, |
| 222 | + 'dog_image' => $testImagePath, |
| 223 | + ]); |
| 224 | + |
| 225 | + $this->asAdmin(); |
| 226 | + /** @var Chapter $chapter */ |
| 227 | + $chapter = $this->runner->run($import, $parent); |
| 228 | + |
| 229 | + // Chapter checks |
| 230 | + $this->assertEquals('Chapter A', $chapter->name); |
| 231 | + $this->assertEquals($parent->id, $chapter->book_id); |
| 232 | + $this->assertCount(1, $chapter->tags); |
| 233 | + $firstChapterTag = $chapter->tags()->first(); |
| 234 | + $this->assertEquals('Reviewed', $firstChapterTag->name); |
| 235 | + $this->assertEquals('2024', $firstChapterTag->value); |
| 236 | + $this->assertCount(2, $chapter->pages); |
| 237 | + |
| 238 | + // Page checks |
| 239 | + /** @var Page $pageA */ |
| 240 | + $pageA = $chapter->pages->first(); |
| 241 | + $this->assertEquals('Page A', $pageA->name); |
| 242 | + $this->assertCount(1, $pageA->tags); |
| 243 | + $this->assertCount(1, $pageA->attachments); |
| 244 | + $pageAImages = Image::where('uploaded_to', '=', $pageA->id)->whereIn('type', ['gallery', 'drawio'])->get(); |
| 245 | + $this->assertCount(1, $pageAImages); |
| 246 | + |
| 247 | + // Reference checks |
| 248 | + $attachment = $pageA->attachments->first(); |
| 249 | + $this->assertStringContainsString($pageA->getUrl(), $chapter->description_html); |
| 250 | + $this->assertStringContainsString($chapter->getUrl(), $pageA->html); |
| 251 | + $this->assertStringContainsString($pageAImages[0]->url, $pageA->html); |
| 252 | + $this->assertStringContainsString($attachment->getUrl(), $pageA->html); |
| 253 | + |
| 254 | + ZipTestHelper::deleteZipForImport($import); |
| 255 | + } |
| 256 | + |
| 257 | + public function test_page_import() |
| 258 | + { |
| 259 | + $testImagePath = $this->files->testFilePath('test-image.png'); |
| 260 | + $testFilePath = $this->files->testFilePath('test-file.txt'); |
| 261 | + $parent = $this->entities->chapter(); |
| 262 | + |
| 263 | + $import = ZipTestHelper::importFromData([], [ |
| 264 | + 'page' => [ |
| 265 | + 'id' => 3, |
| 266 | + 'name' => 'Page A', |
| 267 | + 'priority' => 6, |
| 268 | + 'html' => '<p><a href="[[bsexport:page:3]]">Link to self</a></p> |
| 269 | +<p><a href="[[bsexport:image:2]]">Link to dog drawing</a></p> |
| 270 | +<p><a href="[[bsexport:attachment:4]]">Link to text attachment</a></p>', |
| 271 | + 'tags' => [ |
| 272 | + ['name' => 'Unreviewed'], |
| 273 | + ], |
| 274 | + 'attachments' => [ |
| 275 | + [ |
| 276 | + 'id' => 4, |
| 277 | + 'name' => 'Text attachment', |
| 278 | + 'file' => 'file_attachment' |
| 279 | + ] |
| 280 | + ], |
| 281 | + 'images' => [ |
| 282 | + [ |
| 283 | + 'id' => 2, |
| 284 | + 'name' => 'Dog Drawing', |
| 285 | + 'type' => 'drawio', |
| 286 | + 'file' => 'dog_image' |
| 287 | + ] |
| 288 | + ], |
| 289 | + ], |
| 290 | + ], [ |
| 291 | + 'file_attachment' => $testFilePath, |
| 292 | + 'dog_image' => $testImagePath, |
| 293 | + ]); |
| 294 | + |
| 295 | + $this->asAdmin(); |
| 296 | + /** @var Page $page */ |
| 297 | + $page = $this->runner->run($import, $parent); |
| 298 | + |
| 299 | + // Page checks |
| 300 | + $this->assertEquals('Page A', $page->name); |
| 301 | + $this->assertCount(1, $page->tags); |
| 302 | + $this->assertCount(1, $page->attachments); |
| 303 | + $pageImages = Image::where('uploaded_to', '=', $page->id)->whereIn('type', ['gallery', 'drawio'])->get(); |
| 304 | + $this->assertCount(1, $pageImages); |
| 305 | + $this->assertFileEquals($testImagePath, public_path($pageImages[0]->path)); |
| 306 | + |
| 307 | + // Reference checks |
| 308 | + $this->assertStringContainsString($page->getUrl(), $page->html); |
| 309 | + $this->assertStringContainsString($pageImages[0]->url, $page->html); |
| 310 | + $this->assertStringContainsString($page->attachments->first()->getUrl(), $page->html); |
| 311 | + |
| 312 | + ZipTestHelper::deleteZipForImport($import); |
| 313 | + } |
| 314 | + |
| 315 | + public function test_revert_cleans_up_uploaded_files() |
| 316 | + { |
| 317 | + $testImagePath = $this->files->testFilePath('test-image.png'); |
| 318 | + $testFilePath = $this->files->testFilePath('test-file.txt'); |
| 319 | + $parent = $this->entities->chapter(); |
| 320 | + |
| 321 | + $import = ZipTestHelper::importFromData([], [ |
| 322 | + 'page' => [ |
| 323 | + 'name' => 'Page A', |
| 324 | + 'html' => '<p>Hello</p>', |
| 325 | + 'attachments' => [ |
| 326 | + [ |
| 327 | + 'name' => 'Text attachment', |
| 328 | + 'file' => 'file_attachment' |
| 329 | + ] |
| 330 | + ], |
| 331 | + 'images' => [ |
| 332 | + [ |
| 333 | + 'name' => 'Dog Image', |
| 334 | + 'type' => 'gallery', |
| 335 | + 'file' => 'dog_image' |
| 336 | + ] |
| 337 | + ], |
| 338 | + ], |
| 339 | + ], [ |
| 340 | + 'file_attachment' => $testFilePath, |
| 341 | + 'dog_image' => $testImagePath, |
| 342 | + ]); |
| 343 | + |
| 344 | + $this->asAdmin(); |
| 345 | + /** @var Page $page */ |
| 346 | + $page = $this->runner->run($import, $parent); |
| 347 | + |
| 348 | + $attachment = $page->attachments->first(); |
| 349 | + $image = Image::query()->where('uploaded_to', '=', $page->id)->where('type', '=', 'gallery')->first(); |
| 350 | + |
| 351 | + $this->assertFileExists(public_path($image->path)); |
| 352 | + $this->assertFileExists(storage_path($attachment->path)); |
| 353 | + |
| 354 | + $this->runner->revertStoredFiles(); |
| 355 | + |
| 356 | + $this->assertFileDoesNotExist(public_path($image->path)); |
| 357 | + $this->assertFileDoesNotExist(storage_path($attachment->path)); |
| 358 | + |
| 359 | + ZipTestHelper::deleteZipForImport($import); |
| 360 | + } |
173 | 361 | }
|
0 commit comments