]> BookStack Code Mirror - bookstack/commitdiff
ZIP Import & Exports: Addressed issues during testing
authorDan Brown <redacted>
Mon, 25 Nov 2024 15:54:15 +0000 (15:54 +0000)
committerDan Brown <redacted>
Mon, 25 Nov 2024 15:54:15 +0000 (15:54 +0000)
- Handled links to within-zip page images found in chapter/book
  descriptions; Added test to cover.
- Fixed session showing unrelated success on failed import.

Tested import file-create undo on failure as part of this testing.

app/Exports/Controllers/ImportController.php
app/Exports/ZipExports/ZipExportReferences.php
lang/en/errors.php
tests/Exports/ZipExportTest.php

index a20c341fb02424299c75f3423f7ddc97ea20ee27..b938dac8e29191b045eab9e8022afcf1018da678 100644 (file)
@@ -89,6 +89,8 @@ class ImportController extends Controller
         try {
             $entity = $this->imports->runImport($import, $parent);
         } catch (ZipImportException $exception) {
         try {
             $entity = $this->imports->runImport($import, $parent);
         } catch (ZipImportException $exception) {
+            session()->flush();
+            $this->showErrorNotification(trans('errors.import_zip_failed_notification'));
             return redirect($import->getUrl())->with('import_errors', $exception->errors);
         }
 
             return redirect($import->getUrl())->with('import_errors', $exception->errors);
         }
 
index 0de409fa19aaa0f35153f6a6243c5eae10265b77..bf5e02133ef3d48939aa79af309ddb9e0a2de74a 100644 (file)
@@ -127,11 +127,12 @@ class ZipExportReferences
                 return null;
             }
 
                 return null;
             }
 
-            // We don't expect images to be part of book/chapter content
-            if (!($exportModel instanceof ZipExportPage)) {
-                return null;
+            // Handle simple links outside of page content
+            if (!($exportModel instanceof ZipExportPage) && isset($this->images[$model->id])) {
+                return "[[bsexport:image:{$model->id}]]";
             }
 
             }
 
+            // Find and include images if in visibility
             $page = $model->getPage();
             if ($page && userCan('view', $page)) {
                 if (!isset($this->images[$model->id])) {
             $page = $model->getPage();
             if ($page && userCan('view', $page)) {
                 if (!isset($this->images[$model->id])) {
index ced80a32c1f53f0f842aa6d53febe3c62376da47..9d738379648d26fd0b59ea0a0c82f53edf1e4b38 100644 (file)
@@ -110,6 +110,7 @@ return [
     'import_zip_cant_decode_data' => 'Could not find and decode ZIP data.json content.',
     'import_zip_no_data' => 'ZIP file data has no expected book, chapter or page content.',
     'import_validation_failed' => 'Import ZIP failed to validate with errors:',
     'import_zip_cant_decode_data' => 'Could not find and decode ZIP data.json content.',
     'import_zip_no_data' => 'ZIP file data has no expected book, chapter or page content.',
     'import_validation_failed' => 'Import ZIP failed to validate with errors:',
+    'import_zip_failed_notification' => 'Failed to import ZIP file.',
     'import_perms_books' => 'You are lacking the required permissions to create books.',
     'import_perms_chapters' => 'You are lacking the required permissions to create chapters.',
     'import_perms_pages' => 'You are lacking the required permissions to create pages.',
     'import_perms_books' => 'You are lacking the required permissions to create books.',
     'import_perms_chapters' => 'You are lacking the required permissions to create chapters.',
     'import_perms_pages' => 'You are lacking the required permissions to create pages.',
index 12531239ff3c39cf888cb46becaa797312de4955..6e8462f596f7ebc3b85f369471e88e1949c0b484 100644 (file)
@@ -274,6 +274,32 @@ class ZipExportTest extends TestCase
         $this->assertStringContainsString('href="[[bsexport:book:' . $book->id . ']]?view=true"', $pageData['html']);
     }
 
         $this->assertStringContainsString('href="[[bsexport:book:' . $book->id . ']]?view=true"', $pageData['html']);
     }
 
+    public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
+    {
+        $book = $this->entities->bookHasChaptersAndPages();
+        $chapter = $book->chapters()->first();
+        $page = $chapter->pages()->first();
+
+        $this->asEditor();
+        $this->files->uploadGalleryImageToPage($this, $page);
+        /** @var Image $image */
+        $image = Image::query()->where('type', '=', 'gallery')
+            ->where('uploaded_to', '=', $page->id)->first();
+
+        $book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
+        $book->save();
+        $chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
+        $chapter->save();
+
+        $zipResp = $this->get($book->getUrl("/export/zip"));
+        $zip = $this->extractZipResponse($zipResp);
+        $bookData = $zip->data['book'];
+        $chapterData = $bookData['chapters'][0];
+
+        $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
+        $this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
+    }
+
     public function test_cross_reference_links_external_to_export_are_not_converted()
     {
         $page = $this->entities->page();
     public function test_cross_reference_links_external_to_export_are_not_converted()
     {
         $page = $this->entities->page();