1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

ZIP Imports: Added API test cases

This commit is contained in:
Dan Brown
2025-07-18 14:05:32 +01:00
parent d55684531f
commit 73025719a4
7 changed files with 214 additions and 11 deletions

View File

@@ -26,9 +26,11 @@ class ImportApiController extends ApiController
*/
public function list(): JsonResponse
{
$imports = $this->imports->getVisibleImports()->all();
$query = $this->imports->queryVisible();
return response()->json($imports);
return $this->apiListingResponse($query, [
'id', 'name', 'size', 'type', 'created_by', 'created_at', 'updated_at'
]);
}
/**
@@ -44,7 +46,7 @@ class ImportApiController extends ApiController
try {
$import = $this->imports->storeFromUpload($file);
} catch (ZipValidationException $exception) {
$message = "ZIP upload failed with the following validation errors: \n" . implode("\n", $exception->errors);
$message = "ZIP upload failed with the following validation errors: \n" . $this->formatErrors($exception->errors);
return $this->jsonError($message, 422);
}
@@ -53,11 +55,15 @@ class ImportApiController extends ApiController
/**
* Read details of a pending ZIP import.
* The "details" property contains high-level metadata regarding the ZIP import content,
* and the structure of this will change depending on import "type".
*/
public function read(int $id): JsonResponse
{
$import = $this->imports->findVisible($id);
$import->setAttribute('details', $import->decodeMetadata());
return response()->json($import);
}
@@ -82,7 +88,7 @@ class ImportApiController extends ApiController
try {
$entity = $this->imports->runImport($import, $parent);
} catch (ZipImportException $exception) {
$message = "ZIP import failed with the following errors: \n" . implode("\n", $exception->errors);
$message = "ZIP import failed with the following errors: \n" . $this->formatErrors($exception->errors);
return $this->jsonError($message);
}
@@ -112,4 +118,17 @@ class ImportApiController extends ApiController
],
];
}
protected function formatErrors(array $errors): string
{
$parts = [];
foreach ($errors as $key => $error) {
if (is_string($key)) {
$parts[] = "[{$key}] {$error}";
} else {
$parts[] = $error;
}
}
return implode("\n", $parts);
}
}