mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-12 21:43:48 +08:00
95d62e7f57
Some checks failed
analyse-php / build (push) Waiting to run
lint-php / build (push) Waiting to run
test-migrations / build (8.1) (push) Waiting to run
test-migrations / build (8.2) (push) Waiting to run
test-migrations / build (8.3) (push) Waiting to run
test-php / build (8.1) (push) Waiting to run
test-php / build (8.2) (push) Waiting to run
test-php / build (8.3) (push) Waiting to run
lint-js / build (push) Has been cancelled
test-js / build (push) Has been cancelled
- Updated test handling to create imports folder when required. - Updated some tests to delete created import zip files.
60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Exports;
|
|
|
|
use BookStack\Exports\Import;
|
|
use Illuminate\Http\UploadedFile;
|
|
use ZipArchive;
|
|
|
|
class ZipTestHelper
|
|
{
|
|
public static function importFromData(array $importData, array $zipData, array $files = []): 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, $files);
|
|
$targetPath = storage_path($import->path);
|
|
$targetDir = dirname($targetPath);
|
|
|
|
if (!file_exists($targetDir)) {
|
|
mkdir($targetDir);
|
|
}
|
|
|
|
rename($zip->getRealPath(), $targetPath);
|
|
|
|
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, array $files = []): UploadedFile
|
|
{
|
|
$zipFile = tempnam(sys_get_temp_dir(), 'bstest-');
|
|
|
|
$zip = new ZipArchive();
|
|
$zip->open($zipFile, ZipArchive::CREATE);
|
|
$zip->addFromString('data.json', json_encode($data));
|
|
|
|
foreach ($files as $name => $file) {
|
|
$zip->addFile($file, "files/$name");
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
return new UploadedFile($zipFile, 'upload.zip', 'application/zip', null, true);
|
|
}
|
|
}
|