mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-08-09 10:22:51 +03:00
ZIP Imports: Added high level import run tests
This commit is contained in:
47
tests/Exports/ZipTestHelper.php
Normal file
47
tests/Exports/ZipTestHelper.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Exports;
|
||||
|
||||
use BookStack\Exports\Import;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use ZipArchive;
|
||||
|
||||
class ZipTestHelper
|
||||
{
|
||||
public static function importFromData(array $importData, array $zipData): Import
|
||||
{
|
||||
if (isset($zipData['book'])) {
|
||||
$importData['type'] = 'book';
|
||||
} else if (isset($zipData['chapter'])) {
|
||||
$importData['type'] = 'chapter';
|
||||
} else if (isset($zipData['page'])) {
|
||||
$importData['type'] = 'page';
|
||||
}
|
||||
|
||||
$import = Import::factory()->create($importData);
|
||||
$zip = static::zipUploadFromData($zipData);
|
||||
rename($zip->getRealPath(), storage_path($import->path));
|
||||
|
||||
return $import;
|
||||
}
|
||||
|
||||
public static function deleteZipForImport(Import $import): void
|
||||
{
|
||||
$path = storage_path($import->path);
|
||||
if (file_exists($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
public static function zipUploadFromData(array $data): UploadedFile
|
||||
{
|
||||
$zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
|
||||
|
||||
$zip = new ZipArchive();
|
||||
$zip->open($zipFile, ZipArchive::CREATE);
|
||||
$zip->addFromString('data.json', json_encode($data));
|
||||
$zip->close();
|
||||
|
||||
return new UploadedFile($zipFile, 'upload.zip', 'application/zip', null, true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user