Skip to content

Commit 9ecc919

Browse files
committed
ZIP Import & Exports: Addressed issues during testing
- 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.
1 parent f79c6ae commit 9ecc919

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

app/Exports/Controllers/ImportController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public function run(int $id, Request $request)
8989
try {
9090
$entity = $this->imports->runImport($import, $parent);
9191
} catch (ZipImportException $exception) {
92+
session()->flush();
93+
$this->showErrorNotification(trans('errors.import_zip_failed_notification'));
9294
return redirect($import->getUrl())->with('import_errors', $exception->errors);
9395
}
9496

app/Exports/ZipExports/ZipExportReferences.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ protected function handleModelReference(Model $model, ZipExportModel $exportMode
127127
return null;
128128
}
129129

130-
// We don't expect images to be part of book/chapter content
131-
if (!($exportModel instanceof ZipExportPage)) {
132-
return null;
130+
// Handle simple links outside of page content
131+
if (!($exportModel instanceof ZipExportPage) && isset($this->images[$model->id])) {
132+
return "[[bsexport:image:{$model->id}]]";
133133
}
134134

135+
// Find and include images if in visibility
135136
$page = $model->getPage();
136137
if ($page && userCan('view', $page)) {
137138
if (!isset($this->images[$model->id])) {

lang/en/errors.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
'import_zip_cant_decode_data' => 'Could not find and decode ZIP data.json content.',
111111
'import_zip_no_data' => 'ZIP file data has no expected book, chapter or page content.',
112112
'import_validation_failed' => 'Import ZIP failed to validate with errors:',
113+
'import_zip_failed_notification' => 'Failed to import ZIP file.',
113114
'import_perms_books' => 'You are lacking the required permissions to create books.',
114115
'import_perms_chapters' => 'You are lacking the required permissions to create chapters.',
115116
'import_perms_pages' => 'You are lacking the required permissions to create pages.',

tests/Exports/ZipExportTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,32 @@ public function test_cross_reference_links_are_converted()
274274
$this->assertStringContainsString('href="[[bsexport:book:' . $book->id . ']]?view=true"', $pageData['html']);
275275
}
276276

277+
public function test_book_and_chapter_description_links_to_images_in_pages_are_converted()
278+
{
279+
$book = $this->entities->bookHasChaptersAndPages();
280+
$chapter = $book->chapters()->first();
281+
$page = $chapter->pages()->first();
282+
283+
$this->asEditor();
284+
$this->files->uploadGalleryImageToPage($this, $page);
285+
/** @var Image $image */
286+
$image = Image::query()->where('type', '=', 'gallery')
287+
->where('uploaded_to', '=', $page->id)->first();
288+
289+
$book->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
290+
$book->save();
291+
$chapter->description_html = '<p><a href="' . $image->url . '">Link to image</a></p>';
292+
$chapter->save();
293+
294+
$zipResp = $this->get($book->getUrl("/export/zip"));
295+
$zip = $this->extractZipResponse($zipResp);
296+
$bookData = $zip->data['book'];
297+
$chapterData = $bookData['chapters'][0];
298+
299+
$this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $bookData['description_html']);
300+
$this->assertStringContainsString('href="[[bsexport:image:' . $image->id . ']]"', $chapterData['description_html']);
301+
}
302+
277303
public function test_cross_reference_links_external_to_export_are_not_converted()
278304
{
279305
$page = $this->entities->page();

0 commit comments

Comments
 (0)