Update media-library version
This commit is contained in:
parent
59d7647720
commit
b7a617aa62
|
@ -22,15 +22,14 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"spatie/laravel-medialibrary": "^10.7",
|
||||
"laravel/framework": "^9.50",
|
||||
"spatie/laravel-medialibrary": "^11.0",
|
||||
"laravel/framework": "^10.0",
|
||||
"spatie/laravel-data": "^3.1",
|
||||
"pestphp/pest": "^1.22"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.6",
|
||||
"orchestra/testbench": "^7.0",
|
||||
"illuminate/console": "^9.2"
|
||||
"orchestra/testbench": "^8.0",
|
||||
"illuminate/console": "^10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"post-autoload-dump": [
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -82,7 +82,7 @@ class CollectionExtension
|
|||
$this->convertTo = null;
|
||||
$this->customCallbacks = collect([
|
||||
'forceFileName' => fn ($model, $name) => $name,
|
||||
'convert' => fn () => null,
|
||||
'convert' => fn ($extension) => $extension,
|
||||
'maxWidth' => fn ($size) => null,
|
||||
'stored' => fn ($event) => true,
|
||||
'after' => fn ($event) => true,
|
||||
|
|
|
@ -13,6 +13,7 @@ use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
|||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
use Zoomyboy\MedialibraryHelper\Rules\ModelRule;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
class MediaController
|
||||
{
|
||||
|
@ -49,10 +50,9 @@ class MediaController
|
|||
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
||||
$file = new MediaFile($c['name']);
|
||||
$file->setBasename($collection->runCallback('forceFileName', $model, $file->getBasename()));
|
||||
$file->setExtension(MediaFile::extensionFromData($c['content']));
|
||||
|
||||
if ($collection->runCallback('convert') !== null) {
|
||||
$file->setExtension($collection->runCallback('convert'));
|
||||
}
|
||||
$file->setExtension($collection->runCallback('convert', $file->getExtension()));
|
||||
$adder = $this->fileAdderFromData($model, $c['content'], $collection)
|
||||
->usingName($file->getBasename())
|
||||
->usingFileName($file->getFilename())
|
||||
|
@ -90,11 +90,9 @@ class MediaController
|
|||
|
||||
$tempPaths = collect($content)->map(function ($c) use ($collection) {
|
||||
$file = new MediaFile($c['name']);
|
||||
|
||||
$file->setExtension(MediaFile::extensionFromData($c['content']));
|
||||
$file->setExtension($collection->runCallback('convert', $file->getExtension()));
|
||||
$tmpFile = $this->storeTemporaryFile($c['content'], $collection);
|
||||
if ($collection->runCallback('convert') !== null) {
|
||||
$file->setExtension($collection->runCallback('convert'));
|
||||
}
|
||||
Storage::disk(config('media-library.temp_disk'))->move($tmpFile, 'media-library/' . $file->getFilename());
|
||||
return 'media-library/' . $file->getFilename();
|
||||
});
|
||||
|
@ -174,17 +172,15 @@ class MediaController
|
|||
throw InvalidBase64Data::create();
|
||||
}
|
||||
|
||||
$tmpFile = 'media-library/' . str()->uuid()->toString();
|
||||
$tmpFile = 'media-library/' . str()->uuid()->toString() . '.' . $collection->runCallback('convert', MediaFile::extensionFromData($data));
|
||||
Storage::disk(config('media-library.temp_disk'))->put($tmpFile, $binaryData);
|
||||
|
||||
$image = Image::load(Storage::disk(config('media-library.temp_disk'))->path($tmpFile));
|
||||
$imagePath = Storage::disk(config('media-library.temp_disk'))->path($tmpFile);
|
||||
$image = Image::load($imagePath);
|
||||
if (null !== $maxWidth && 'image/jpeg' === Storage::disk(config('media-library.temp_disk'))->mimeType($tmpFile)) {
|
||||
$image->width($maxWidth);
|
||||
}
|
||||
|
||||
if ($collection->runCallback('convert') !== null) {
|
||||
$image->format($collection->runCallback('convert'));
|
||||
}
|
||||
$image->save();
|
||||
|
||||
return $tmpFile;
|
||||
|
|
|
@ -35,4 +35,17 @@ class MediaFile
|
|||
{
|
||||
return $this->file->{$method}(...$arguments);
|
||||
}
|
||||
|
||||
public static function extensionFromData(string $data): string
|
||||
{
|
||||
$tempFile = sys_get_temp_dir() . '/' . str()->uuid()->toString();
|
||||
|
||||
file_put_contents($tempFile, base64_decode($data));
|
||||
|
||||
$extension = (new self($tempFile))->guessExtension();
|
||||
|
||||
unlink($tempFile);
|
||||
|
||||
return $extension;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,9 @@ use Illuminate\Support\Facades\Storage;
|
|||
test('it uploads a deferred file to a collection', function () {
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
|
||||
'payload' => ['content' => base64_encode($this->pdfFile()->getContent()), 'name' => 'beispiel.pdf'],
|
||||
'payload' => ['content' => base64_encode(test()->pdfFile()->getContent()), 'name' => 'beispiel.pdf'],
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertExactJson([
|
||||
|
@ -18,7 +18,7 @@ test('it uploads a deferred file to a collection', function () {
|
|||
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
|
||||
'name' => 'beispiel',
|
||||
'collection_name' => 'defaultSingleFile',
|
||||
'size' => 3028,
|
||||
'size' => 64576,
|
||||
'file_name' => 'beispiel.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
'icon' => url('storage/filetypes/applicationpdf.svg'),
|
||||
|
@ -27,11 +27,11 @@ test('it uploads a deferred file to a collection', function () {
|
|||
});
|
||||
|
||||
test('it forces filename when uploading', function () {
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => null],
|
||||
'payload' => ['content' => base64_encode($this->pngFile()->getContent()), 'name' => 'beispiel.png'],
|
||||
'payload' => ['content' => base64_encode(test()->pngFile()->getContent()), 'name' => 'beispiel.png'],
|
||||
])
|
||||
->assertStatus(201)
|
||||
->assertExactJson([
|
||||
|
@ -39,7 +39,7 @@ test('it forces filename when uploading', function () {
|
|||
'original_url' => Storage::disk('temp')->url('media-library/beispiel.jpg'),
|
||||
'name' => 'beispiel',
|
||||
'collection_name' => 'singleJpegFile',
|
||||
'size' => 366471,
|
||||
'size' => 490278,
|
||||
'file_name' => 'beispiel.jpg',
|
||||
'mime_type' => 'image/jpeg',
|
||||
'icon' => url('storage/filetypes/imagejpeg.svg'),
|
||||
|
@ -50,10 +50,10 @@ test('it forces filename when uploading', function () {
|
|||
|
||||
test('it stores a file to media library after deferred upload', function () {
|
||||
Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00'));
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
Storage::disk('temp')->put('media-library/beispiel.pdf', test()->pdfFile()->getContent());
|
||||
|
||||
$post = $this->newPost();
|
||||
$post = test()->newPost();
|
||||
|
||||
$post->setDeferredUploads([
|
||||
'file_name' => 'beispiel.pdf',
|
||||
|
@ -61,18 +61,18 @@ test('it stores a file to media library after deferred upload', function () {
|
|||
]);
|
||||
|
||||
$media = $post->getMedia('singleForced')->first();
|
||||
$this->assertNotNull($media);
|
||||
$this->assertEquals('beispiel 2023-05-06', $media->name);
|
||||
test()->assertNotNull($media);
|
||||
test()->assertEquals('beispiel 2023-05-06', $media->name);
|
||||
Storage::disk('temp')->assertMissing('media-library/beispiel.pdf');
|
||||
});
|
||||
|
||||
test('it stores multiple files to media library after deferred upload', function () {
|
||||
Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00'));
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent());
|
||||
Storage::disk('temp')->put('media-library/beispiel2.pdf', $this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
Storage::disk('temp')->put('media-library/beispiel.pdf', test()->pdfFile()->getContent());
|
||||
Storage::disk('temp')->put('media-library/beispiel2.pdf', test()->pdfFile()->getContent());
|
||||
|
||||
$post = $this->newPost();
|
||||
$post = test()->newPost();
|
||||
|
||||
$post->setDeferredUploads([
|
||||
[
|
||||
|
@ -86,16 +86,16 @@ test('it stores multiple files to media library after deferred upload', function
|
|||
]);
|
||||
|
||||
$medias = $post->getMedia('multipleForced');
|
||||
$this->assertCount(2, $medias);
|
||||
$this->assertEquals('beispiel 2023-05-06', $medias->get(0)->name);
|
||||
$this->assertEquals('beispiel2 2023-05-06', $medias->get(1)->name);
|
||||
test()->assertCount(2, $medias);
|
||||
test()->assertEquals('beispiel 2023-05-06', $medias->get(0)->name);
|
||||
test()->assertEquals('beispiel2 2023-05-06', $medias->get(1)->name);
|
||||
Storage::disk('temp')->assertMissing('media-library/beispiel.pdf');
|
||||
Storage::disk('temp')->assertMissing('media-library/beispiel2.pdf');
|
||||
});
|
||||
|
||||
test('it uploads multiple files', function () {
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$payload = [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => null],
|
||||
|
@ -105,7 +105,7 @@ test('it uploads multiple files', function () {
|
|||
]
|
||||
];
|
||||
|
||||
$this->postJson('/mediaupload', $payload)
|
||||
test()->postJson('/mediaupload', $payload)
|
||||
->assertStatus(201)
|
||||
->assertJson([
|
||||
[
|
||||
|
@ -113,7 +113,7 @@ test('it uploads multiple files', function () {
|
|||
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
|
||||
'name' => 'beispiel',
|
||||
'collection_name' => 'multipleForced',
|
||||
'size' => 3028,
|
||||
'size' => 64576,
|
||||
'file_name' => 'beispiel.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
],
|
||||
|
@ -122,7 +122,7 @@ test('it uploads multiple files', function () {
|
|||
'original_url' => Storage::disk('temp')->url('media-library/beispiel2.pdf'),
|
||||
'name' => 'beispiel2',
|
||||
'collection_name' => 'multipleForced',
|
||||
'size' => 3028,
|
||||
'size' => 64576,
|
||||
'file_name' => 'beispiel2.pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
]
|
||||
|
@ -132,24 +132,24 @@ test('it uploads multiple files', function () {
|
|||
});
|
||||
|
||||
test('it reduces file size', function () {
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->jpgFile()->getContent()),
|
||||
'content' => base64_encode(test()->jpgFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
]);
|
||||
$size = getimagesize(Storage::disk('temp')->path('media-library/beispiel bild.jpg'));
|
||||
$this->assertEquals(250, $size[0]);
|
||||
test()->assertEquals(250, $size[0]);
|
||||
});
|
||||
|
||||
test('it handles authorization with collection', function () {
|
||||
$this->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -159,10 +159,10 @@ test('it handles authorization with collection', function () {
|
|||
});
|
||||
|
||||
test('it handles authorization with collection correctly', function () {
|
||||
$this->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -172,8 +172,8 @@ test('it handles authorization with collection correctly', function () {
|
|||
});
|
||||
|
||||
test('it needs a collection', function ($key, $value) {
|
||||
$this->auth()->registerModel();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$payload = [
|
||||
'parent' => ['model' => 'post', 'collection' => '', 'id' => null],
|
||||
|
@ -185,7 +185,7 @@ test('it needs a collection', function ($key, $value) {
|
|||
|
||||
data_set($payload, $key, $value);
|
||||
|
||||
$this->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key);
|
||||
test()->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key);
|
||||
})->with(function () {
|
||||
yield ['parent.collection_name', ''];
|
||||
yield ['parent.collection_name', -1];
|
||||
|
|
|
@ -4,42 +4,44 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
|||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
use Symfony\Component\Mime\MimeTypes;
|
||||
use Workbench\App\Events\MediaChange;
|
||||
use Workbench\App\Events\MediaStored;
|
||||
|
||||
test('it uploads a single file to a single file collection', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
'name' => 'beispiel bild.jpg',
|
||||
'name' => 'beispiel bild.pdf',
|
||||
],
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertCount(1, $post->getMedia('defaultSingleFile'));
|
||||
test()->assertCount(1, $post->getMedia('defaultSingleFile'));
|
||||
$media = $post->getFirstMedia('defaultSingleFile');
|
||||
$response->assertJsonPath('id', $media->id);
|
||||
$response->assertJsonPath('original_url', $media->getFullUrl());
|
||||
$response->assertJsonPath('size', 3028);
|
||||
$response->assertJsonPath('name', 'beispiel bild');
|
||||
$response->assertJsonPath('collection_name', 'defaultSingleFile');
|
||||
$response->assertJsonPath('file_name', 'beispiel-bild.jpg');
|
||||
$response->assertJsonPath('file_name', 'beispiel-bild.pdf');
|
||||
$response->assertJsonPath('is_deferred', false);
|
||||
$response->assertJsonMissingPath('model_type');
|
||||
$response->assertJsonMissingPath('model_id');
|
||||
});
|
||||
|
||||
test('it changes format', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pngFile()->getContent());
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->pngFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -48,17 +50,18 @@ test('it changes format', function () {
|
|||
]);
|
||||
|
||||
$response->assertStatus(201)
|
||||
->assertJsonPath('size', 366471)
|
||||
->assertJsonPath('size', 490278)
|
||||
->assertJsonPath('file_name', 'beispiel-bild.jpg')
|
||||
->assertJsonPath('mime_type', 'image/jpeg');
|
||||
$this->assertEquals('image/jpeg', MimeTypes::getDefault()->guessMimeType($post->getFirstMedia('singleJpegFile')->getPath()));
|
||||
});
|
||||
|
||||
test('it uploads a single image to a single file collection', function () {
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->jpgFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->jpgFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -67,16 +70,16 @@ test('it uploads a single image to a single file collection', function () {
|
|||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertCount(1, $post->getMedia('defaultSingleFile'));
|
||||
test()->assertCount(1, $post->getMedia('defaultSingleFile'));
|
||||
});
|
||||
|
||||
test('it forces a filename for a single collection', function () {
|
||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->jpgFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'singleForced', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -85,17 +88,17 @@ test('it forces a filename for a single collection', function () {
|
|||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name);
|
||||
test()->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name);
|
||||
$response->assertJsonPath('name', 'beispiel bild 2023-04-04');
|
||||
$response->assertJsonPath('file_name', 'beispiel-bild-2023-04-04.jpg');
|
||||
});
|
||||
|
||||
test('it sets custom title when storing', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'singleStoringHook', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -106,36 +109,36 @@ test('it sets custom title when storing', function () {
|
|||
$response->assertStatus(201);
|
||||
$media = $post->getFirstMedia('singleStoringHook');
|
||||
|
||||
$this->assertEquals('AAA', $media->getCustomProperty('use'));
|
||||
$this->assertEquals('beispiel bild', $media->getCustomProperty('ttt'));
|
||||
test()->assertEquals('AAA', $media->getCustomProperty('use'));
|
||||
test()->assertEquals('beispiel bild', $media->getCustomProperty('ttt'));
|
||||
});
|
||||
|
||||
test('it sets custom properties from properties method', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'multipleProperties', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
'name' => 'beispiel bild.jpg',
|
||||
'name' => 'beispiel bild.pdf',
|
||||
],
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$media = $post->getFirstMedia('multipleProperties');
|
||||
|
||||
$this->assertEquals('beispielBild.jpg', $media->getCustomProperty('test'));
|
||||
test()->assertEquals('beispielBild.pdf', $media->getCustomProperty('test'));
|
||||
});
|
||||
|
||||
test('it forces a filename for multiple collections', function () {
|
||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->jpgFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => $post->id],
|
||||
'payload' => [
|
||||
[
|
||||
|
@ -146,17 +149,17 @@ test('it forces a filename for multiple collections', function () {
|
|||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name);
|
||||
test()->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name);
|
||||
});
|
||||
|
||||
test('it throws event when file has been uploaded', function () {
|
||||
Event::fake();
|
||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => $content,
|
||||
|
@ -172,11 +175,11 @@ test('it throws event when file has been uploaded', function () {
|
|||
test('it throws event when multiple files uploaded', function () {
|
||||
Event::fake();
|
||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = $this->newPost();
|
||||
$content = base64_encode($this->pdfFile()->getContent());
|
||||
test()->auth()->registerModel()->withoutExceptionHandling();
|
||||
$post = test()->newPost();
|
||||
$content = base64_encode(test()->pdfFile()->getContent());
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'multipleFilesWithEvent', 'id' => $post->id],
|
||||
'payload' => [
|
||||
[
|
||||
|
@ -196,104 +199,105 @@ test('it throws event when multiple files uploaded', function () {
|
|||
});
|
||||
|
||||
test('it uploads multiple files', function () {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
$file = $this->pdfFile();
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
$file = test()->pdfFile();
|
||||
$post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images');
|
||||
|
||||
$response = $this->postJson('/mediaupload', [
|
||||
$response = test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id],
|
||||
'payload' => [
|
||||
[
|
||||
'content' => base64_encode($file->getContent()),
|
||||
'name' => 'aaaa.jpg',
|
||||
'name' => 'aaaa.pdf',
|
||||
],
|
||||
[
|
||||
'content' => base64_encode($file->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
'name' => 'beispiel bild.pdf',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$response->assertStatus(201);
|
||||
$this->assertCount(3, $post->getMedia('images'));
|
||||
test()->assertCount(3, $post->getMedia('images'));
|
||||
$this->assertEquals('application/pdf', MimeTypes::getDefault()->guessMimeType($post->getFirstMedia('images')->getPath()));
|
||||
$media = $post->getMedia('images')->skip(1)->values();
|
||||
$this->assertCount(2, $response->json());
|
||||
test()->assertCount(2, $response->json());
|
||||
$response->assertJsonPath('0.id', $media->get(0)->id);
|
||||
$response->assertJsonPath('1.id', $media->get(1)->id);
|
||||
$response->assertJsonPath('0.original_url', $media->first()->getFullUrl());
|
||||
$response->assertJsonPath('0.size', 3028);
|
||||
$response->assertJsonPath('0.size', 64576);
|
||||
$response->assertJsonPath('0.name', 'aaaa');
|
||||
$response->assertJsonPath('0.collection_name', 'images');
|
||||
$response->assertJsonPath('0.file_name', 'aaaa.jpg');
|
||||
$response->assertJsonPath('0.file_name', 'aaaa.pdf');
|
||||
$response->assertJsonMissingPath('0.model_type');
|
||||
$response->assertJsonMissingPath('0.model_id');
|
||||
});
|
||||
|
||||
test('it returns 403 when not authorized', function () {
|
||||
$this->auth(['storeMedia' => false])->registerModel();
|
||||
$post = $this->newPost();
|
||||
test()->auth(['storeMedia' => false])->registerModel();
|
||||
$post = test()->newPost();
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
])->assertStatus(403);
|
||||
});
|
||||
|
||||
test('it checks for model when running authorization', function () {
|
||||
$otherPost = $this->newPost();
|
||||
$post = $this->newPost();
|
||||
$this->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
|
||||
$otherPost = test()->newPost();
|
||||
$post = test()->newPost();
|
||||
test()->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $otherPost->id],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
])->assertStatus(403);
|
||||
});
|
||||
|
||||
test('it checks for collection when running authorization', function () {
|
||||
$post = $this->newPost();
|
||||
$this->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
|
||||
$post = test()->newPost();
|
||||
test()->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
])->assertStatus(201);
|
||||
|
||||
$this->postJson('/mediaupload', [
|
||||
test()->postJson('/mediaupload', [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
])->assertStatus(403);
|
||||
});
|
||||
|
||||
test('it needs validation for single files', function (array $payloadOverwrites, string $invalidFieldName) {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
|
||||
$payload = [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
|
||||
'payload' => [
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
];
|
||||
|
@ -302,7 +306,7 @@ test('it needs validation for single files', function (array $payloadOverwrites,
|
|||
data_set($payload, $key, $value);
|
||||
}
|
||||
|
||||
$response = $this->postJson('/mediaupload', $payload);
|
||||
$response = test()->postJson('/mediaupload', $payload);
|
||||
|
||||
$response->assertStatus(422);
|
||||
$response->assertJsonValidationErrors($invalidFieldName);
|
||||
|
@ -324,14 +328,14 @@ test('it needs validation for single files', function (array $payloadOverwrites,
|
|||
});
|
||||
|
||||
test('it needs validation for multiple files', function (array $payloadOverwrites, string $invalidFieldName) {
|
||||
$this->auth()->registerModel();
|
||||
$post = $this->newPost();
|
||||
test()->auth()->registerModel();
|
||||
$post = test()->newPost();
|
||||
|
||||
$payload = [
|
||||
'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id],
|
||||
'payload' => [
|
||||
[
|
||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||
'content' => base64_encode(test()->pdfFile()->getContent()),
|
||||
'name' => 'beispiel bild.jpg',
|
||||
],
|
||||
],
|
||||
|
@ -341,7 +345,7 @@ test('it needs validation for multiple files', function (array $payloadOverwrite
|
|||
data_set($payload, $key, $value);
|
||||
}
|
||||
|
||||
$response = $this->postJson('/mediaupload', $payload);
|
||||
$response = test()->postJson('/mediaupload', $payload);
|
||||
|
||||
|
||||
$response->assertStatus(422);
|
||||
|
|
|
@ -23,7 +23,7 @@ class Post extends Model implements HasMedia
|
|||
public function registerMediaCollections(): void
|
||||
{
|
||||
$this->addMediaCollection('defaultSingleFile')->maxWidth(fn () => 250)->singleFile();
|
||||
$this->addMediaCollection('singleJpegFile')->convert(fn () => 'jpg')->singleFile();
|
||||
$this->addMediaCollection('singleJpegFile')->convert(fn ($extension) => 'jpg')->singleFile();
|
||||
|
||||
$this->addMediaCollection('conversionsWithDefault')
|
||||
->singleFile()
|
||||
|
|
Loading…
Reference in New Issue