medialibrary-helper/tests/Feature/DeferredUploadTest.php

197 lines
7.4 KiB
PHP
Raw Normal View History

2024-01-03 15:47:41 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
2024-01-12 00:16:42 +01:00
use Carbon\Carbon;
2024-01-03 15:47:41 +01:00
use Illuminate\Support\Facades\Storage;
test('it uploads a deferred file to a collection', function () {
$this->auth()->registerModel()->withoutExceptionHandling();
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
2024-09-21 16:54:51 +02:00
'payload' => ['content' => base64_encode(test()->pdfFile()->getContent()), 'name' => 'beispiel.pdf'],
2024-01-12 00:16:42 +01:00
])
2024-01-03 15:47:41 +01:00
->assertStatus(201)
2024-01-12 00:16:42 +01:00
->assertExactJson([
2024-01-03 15:47:41 +01:00
'is_deferred' => true,
2024-01-03 16:45:02 +01:00
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
'name' => 'beispiel',
2024-01-03 15:47:41 +01:00
'collection_name' => 'defaultSingleFile',
2024-09-21 16:54:51 +02:00
'size' => 64576,
2024-01-03 16:45:02 +01:00
'file_name' => 'beispiel.pdf',
2024-01-03 15:47:41 +01:00
'mime_type' => 'application/pdf',
2024-01-13 21:49:17 +01:00
'icon' => url('storage/filetypes/applicationpdf.svg'),
2024-01-03 15:47:41 +01:00
]);
2024-01-03 16:45:02 +01:00
Storage::disk('temp')->assertExists('media-library/beispiel.pdf');
});
2024-07-13 19:38:51 +02:00
test('it forces filename when uploading', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
2024-07-13 19:38:51 +02:00
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', [
2024-07-13 19:38:51 +02:00
'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => null],
2024-09-21 16:54:51 +02:00
'payload' => ['content' => base64_encode(test()->pngFile()->getContent()), 'name' => 'beispiel.png'],
2024-07-13 19:38:51 +02:00
])
->assertStatus(201)
->assertExactJson([
'is_deferred' => true,
'original_url' => Storage::disk('temp')->url('media-library/beispiel.jpg'),
'name' => 'beispiel',
'collection_name' => 'singleJpegFile',
2024-09-21 16:54:51 +02:00
'size' => 490278,
2024-07-13 19:38:51 +02:00
'file_name' => 'beispiel.jpg',
'mime_type' => 'image/jpeg',
'icon' => url('storage/filetypes/imagejpeg.svg'),
]);
Storage::disk('temp')->assertExists('media-library/beispiel.jpg');
Storage::disk('temp')->assertMissing('media-library/beispiel.png');
});
2024-01-12 00:16:42 +01:00
test('it stores a file to media library after deferred upload', function () {
Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00'));
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
Storage::disk('temp')->put('media-library/beispiel.pdf', test()->pdfFile()->getContent());
2024-01-12 00:16:42 +01:00
2024-09-21 16:54:51 +02:00
$post = test()->newPost();
2024-01-12 00:16:42 +01:00
$post->setDeferredUploads([
'file_name' => 'beispiel.pdf',
'collection_name' => 'singleForced',
]);
$media = $post->getMedia('singleForced')->first();
2024-09-21 16:54:51 +02:00
test()->assertNotNull($media);
test()->assertEquals('beispiel 2023-05-06', $media->name);
2024-01-12 00:16:42 +01:00
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'));
2024-09-21 16:54:51 +02:00
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());
2024-09-21 16:54:51 +02:00
$post = test()->newPost();
$post->setDeferredUploads([
[
'file_name' => 'beispiel.pdf',
'collection_name' => 'multipleForced',
],
[
'file_name' => 'beispiel2.pdf',
'collection_name' => 'multipleForced',
]
]);
$medias = $post->getMedia('multipleForced');
2024-09-21 16:54:51 +02:00
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');
});
2024-01-03 16:45:02 +01:00
test('it uploads multiple files', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
$content = base64_encode(test()->pdfFile()->getContent());
2024-01-03 16:45:02 +01:00
$payload = [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => null],
2024-01-03 16:45:02 +01:00
'payload' => [
['content' => $content, 'name' => 'beispiel.pdf'],
['content' => $content, 'name' => 'beispiel2.pdf'],
]
];
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', $payload)
2024-01-03 16:45:02 +01:00
->assertStatus(201)
->assertJson([
[
'is_deferred' => true,
'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'),
'name' => 'beispiel',
'collection_name' => 'multipleForced',
2024-09-21 16:54:51 +02:00
'size' => 64576,
2024-01-03 16:45:02 +01:00
'file_name' => 'beispiel.pdf',
'mime_type' => 'application/pdf',
],
[
'is_deferred' => true,
'original_url' => Storage::disk('temp')->url('media-library/beispiel2.pdf'),
'name' => 'beispiel2',
'collection_name' => 'multipleForced',
2024-09-21 16:54:51 +02:00
'size' => 64576,
2024-01-03 16:45:02 +01:00
'file_name' => 'beispiel2.pdf',
'mime_type' => 'application/pdf',
]
]);
Storage::disk('temp')->assertExists('media-library/beispiel.pdf');
Storage::disk('temp')->assertExists('media-library/beispiel2.pdf');
});
test('it reduces file size', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
2024-01-03 16:45:02 +01:00
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
2024-01-03 16:45:02 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->jpgFile()->getContent()),
2024-01-03 16:45:02 +01:00
'name' => 'beispiel bild.jpg',
],
]);
$size = getimagesize(Storage::disk('temp')->path('media-library/beispiel bild.jpg'));
2024-09-21 16:54:51 +02:00
test()->assertEquals(250, $size[0]);
2024-01-03 15:47:41 +01:00
});
test('it handles authorization with collection', function () {
2024-09-21 16:54:51 +02:00
test()->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel();
$content = base64_encode(test()->pdfFile()->getContent());
2024-01-03 15:47:41 +01:00
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
2024-01-03 15:47:41 +01:00
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
])->assertStatus(403);
});
test('it handles authorization with collection correctly', function () {
2024-09-21 16:54:51 +02:00
test()->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel();
$content = base64_encode(test()->pdfFile()->getContent());
2024-01-03 15:47:41 +01:00
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null],
2024-01-03 15:47:41 +01:00
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
])->assertStatus(201);
});
test('it needs a collection', function ($key, $value) {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$content = base64_encode(test()->pdfFile()->getContent());
2024-01-03 15:47:41 +01:00
$payload = [
'parent' => ['model' => 'post', 'collection' => '', 'id' => null],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
];
data_set($payload, $key, $value);
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key);
2024-01-03 15:47:41 +01:00
})->with(function () {
2024-01-12 00:16:42 +01:00
yield ['parent.collection_name', ''];
yield ['parent.collection_name', -1];
yield ['parent.collection_name', 'missingcollection'];
2024-01-03 15:47:41 +01:00
yield ['parent.model', 'lalala'];
yield ['parent.model', -1];
yield ['parent.model', ''];
});