medialibrary-helper/tests/Feature/UploadTest.php

371 lines
14 KiB
PHP
Raw Normal View History

2023-03-07 15:31:24 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Carbon\Carbon;
2023-03-07 21:53:36 +01:00
use Illuminate\Support\Facades\Event;
2024-09-21 16:54:51 +02:00
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Symfony\Component\Mime\MimeTypes;
2024-01-03 11:10:59 +01:00
use Workbench\App\Events\MediaChange;
use Workbench\App\Events\MediaStored;
2023-03-07 15:31:24 +01:00
2023-03-07 23:11:35 +01:00
test('it uploads a single file to a single file collection', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
$post = test()->newPost();
$content = base64_encode(test()->pdfFile()->getContent());
2023-03-07 15:31:24 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
'content' => $content,
2024-09-21 16:54:51 +02:00
'name' => 'beispiel bild.pdf',
2023-03-07 23:11:35 +01:00
],
2023-03-07 15:31:24 +01:00
]);
$response->assertStatus(201);
2024-09-21 16:54:51 +02:00
test()->assertCount(1, $post->getMedia('defaultSingleFile'));
2023-03-07 15:31:24 +01:00
$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');
2024-09-21 16:54:51 +02:00
$response->assertJsonPath('file_name', 'beispiel-bild.pdf');
2024-01-03 15:47:41 +01:00
$response->assertJsonPath('is_deferred', false);
2023-03-07 15:31:24 +01:00
$response->assertJsonMissingPath('model_type');
$response->assertJsonMissingPath('model_id');
});
2024-07-13 19:49:31 +02:00
test('it changes format', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
$content = base64_encode(test()->pngFile()->getContent());
2024-07-13 19:49:31 +02:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-07-13 19:49:31 +02:00
'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.png',
],
]);
$response->assertStatus(201)
2024-09-21 16:54:51 +02:00
->assertJsonPath('size', 490278)
2024-07-13 19:49:31 +02:00
->assertJsonPath('file_name', 'beispiel-bild.jpg')
->assertJsonPath('mime_type', 'image/jpeg');
2024-09-21 16:54:51 +02:00
$this->assertEquals('image/jpeg', MimeTypes::getDefault()->guessMimeType($post->getFirstMedia('singleJpegFile')->getPath()));
2024-07-13 19:49:31 +02:00
});
2023-05-02 14:23:25 +02:00
test('it uploads a single image to a single file collection', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
$post = test()->newPost();
$content = base64_encode(test()->jpgFile()->getContent());
2023-05-02 14:23:25 +02:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
2023-05-02 14:23:25 +02:00
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
2024-09-21 16:54:51 +02:00
test()->assertCount(1, $post->getMedia('defaultSingleFile'));
2023-05-02 14:23:25 +02:00
});
2023-03-07 23:11:35 +01:00
test('it forces a filename for a single collection', function () {
2023-03-07 15:31:24 +01:00
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
$content = base64_encode(test()->jpgFile()->getContent());
2023-03-07 15:31:24 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'singleForced', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
2023-03-07 23:11:35 +01:00
],
2023-03-07 15:31:24 +01:00
]);
$response->assertStatus(201);
2024-09-21 16:54:51 +02:00
test()->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name);
2023-03-07 23:11:35 +01:00
$response->assertJsonPath('name', 'beispiel bild 2023-04-04');
$response->assertJsonPath('file_name', 'beispiel-bild-2023-04-04.jpg');
2023-03-07 15:31:24 +01:00
});
2023-03-07 23:11:35 +01:00
test('it sets custom title when storing', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
$content = base64_encode(test()->pdfFile()->getContent());
2023-03-07 15:31:24 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'singleStoringHook', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
2023-03-07 23:11:35 +01:00
],
2023-03-07 15:31:24 +01:00
]);
$response->assertStatus(201);
$media = $post->getFirstMedia('singleStoringHook');
2024-09-21 16:54:51 +02:00
test()->assertEquals('AAA', $media->getCustomProperty('use'));
test()->assertEquals('beispiel bild', $media->getCustomProperty('ttt'));
2023-03-07 15:31:24 +01:00
});
2023-03-07 23:11:35 +01:00
test('it sets custom properties from properties method', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
$content = base64_encode(test()->pdfFile()->getContent());
2023-03-07 23:11:35 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'multipleProperties', 'id' => $post->id],
2023-03-07 23:11:35 +01:00
'payload' => [
'content' => $content,
2024-09-21 16:54:51 +02:00
'name' => 'beispiel bild.pdf',
2023-03-07 23:11:35 +01:00
],
]);
$response->assertStatus(201);
$media = $post->getFirstMedia('multipleProperties');
2024-09-21 16:54:51 +02:00
test()->assertEquals('beispielBild.pdf', $media->getCustomProperty('test'));
2023-03-07 23:11:35 +01:00
});
test('it forces a filename for multiple collections', function () {
2023-03-07 15:31:24 +01:00
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
$content = base64_encode(test()->jpgFile()->getContent());
2023-03-07 15:31:24 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
[
'content' => $content,
'name' => 'beispiel bild.jpg',
2023-03-07 23:11:35 +01:00
],
],
2023-03-07 15:31:24 +01:00
]);
$response->assertStatus(201);
2024-09-21 16:54:51 +02:00
test()->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name);
2023-03-07 15:31:24 +01:00
});
2023-03-07 23:11:35 +01:00
test('it throws event when file has been uploaded', function () {
2023-03-07 21:53:36 +01:00
Event::fake();
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
$post = test()->newPost();
$content = base64_encode(test()->pdfFile()->getContent());
2023-03-07 21:53:36 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id],
2023-03-07 21:53:36 +01:00
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
2023-03-07 23:11:35 +01:00
],
2023-03-07 21:53:36 +01:00
]);
$response->assertStatus(201);
2023-03-07 23:11:35 +01:00
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
2023-03-13 10:34:52 +01:00
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
2023-03-07 21:53:36 +01:00
});
2023-03-07 23:11:35 +01:00
test('it throws event when multiple files uploaded', function () {
2023-03-07 21:53:36 +01:00
Event::fake();
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel()->withoutExceptionHandling();
$post = test()->newPost();
$content = base64_encode(test()->pdfFile()->getContent());
2023-03-07 21:53:36 +01:00
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'multipleFilesWithEvent', 'id' => $post->id],
2023-03-07 21:53:36 +01:00
'payload' => [
[
'content' => $content,
'name' => 'beispiel bild.jpg',
],
[
'content' => $content,
'name' => 'beispiel bild 1.jpg',
],
2023-03-07 23:11:35 +01:00
],
2023-03-07 21:53:36 +01:00
]);
$response->assertStatus(201);
2023-03-07 23:11:35 +01:00
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('0.id'));
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('1.id'));
2023-03-07 21:53:36 +01:00
});
2023-03-07 23:11:35 +01:00
test('it uploads multiple files', function () {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
$file = test()->pdfFile();
2023-03-07 15:31:24 +01:00
$post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images');
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
[
'content' => base64_encode($file->getContent()),
2024-09-21 16:54:51 +02:00
'name' => 'aaaa.pdf',
2023-03-07 15:31:24 +01:00
],
[
'content' => base64_encode($file->getContent()),
2024-09-21 16:54:51 +02:00
'name' => 'beispiel bild.pdf',
2023-03-07 23:11:35 +01:00
],
],
2023-03-07 15:31:24 +01:00
]);
$response->assertStatus(201);
2024-09-21 16:54:51 +02:00
test()->assertCount(3, $post->getMedia('images'));
$this->assertEquals('application/pdf', MimeTypes::getDefault()->guessMimeType($post->getFirstMedia('images')->getPath()));
2023-03-07 15:31:24 +01:00
$media = $post->getMedia('images')->skip(1)->values();
2024-09-21 16:54:51 +02:00
test()->assertCount(2, $response->json());
2023-03-07 15:31:24 +01:00
$response->assertJsonPath('0.id', $media->get(0)->id);
$response->assertJsonPath('1.id', $media->get(1)->id);
$response->assertJsonPath('0.original_url', $media->first()->getFullUrl());
2024-09-21 16:54:51 +02:00
$response->assertJsonPath('0.size', 64576);
2023-03-07 15:31:24 +01:00
$response->assertJsonPath('0.name', 'aaaa');
$response->assertJsonPath('0.collection_name', 'images');
2024-09-21 16:54:51 +02:00
$response->assertJsonPath('0.file_name', 'aaaa.pdf');
2023-03-07 15:31:24 +01:00
$response->assertJsonMissingPath('0.model_type');
$response->assertJsonMissingPath('0.model_id');
});
2023-03-07 23:11:35 +01:00
test('it returns 403 when not authorized', function () {
2024-09-21 16:54:51 +02:00
test()->auth(['storeMedia' => false])->registerModel();
$post = test()->newPost();
2023-03-07 15:31:24 +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' => $post->id],
2024-01-02 01:28:16 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2024-01-02 01:28:16 +01:00
'name' => 'beispiel bild.jpg',
],
2024-01-03 13:42:29 +01:00
])->assertStatus(403);
});
test('it checks for model when running authorization', function () {
2024-09-21 16:54:51 +02:00
$otherPost = test()->newPost();
$post = test()->newPost();
test()->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
2024-01-03 13:42:29 +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' => $post->id],
2024-01-03 13:42:29 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2024-01-03 13:42:29 +01:00
'name' => 'beispiel bild.jpg',
],
])->assertStatus(201);
2023-03-07 15:31:24 +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' => $otherPost->id],
2024-01-03 13:42:29 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2024-01-03 13:42:29 +01:00
'name' => 'beispiel bild.jpg',
],
])->assertStatus(403);
});
test('it checks for collection when running authorization', function () {
2024-09-21 16:54:51 +02:00
$post = test()->newPost();
test()->auth(['storeMedia' => ['id' => $post->id, 'collection' => 'defaultSingleFile']])->registerModel();
2024-01-03 13:42:29 +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' => $post->id],
2024-01-03 13:42:29 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2024-01-03 13:42:29 +01:00
'name' => 'beispiel bild.jpg',
],
])->assertStatus(201);
2024-09-21 16:54:51 +02:00
test()->postJson('/mediaupload', [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'singleWithEvent', 'id' => $post->id],
2024-01-03 13:42:29 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2024-01-03 13:42:29 +01:00
'name' => 'beispiel bild.jpg',
],
])->assertStatus(403);
2023-03-07 15:31:24 +01:00
});
2024-01-03 02:12:26 +01:00
test('it needs validation for single files', function (array $payloadOverwrites, string $invalidFieldName) {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
2023-03-07 15:31:24 +01:00
2024-01-03 02:12:26 +01:00
$payload = [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2023-03-07 15:31:24 +01:00
'name' => 'beispiel bild.jpg',
],
2024-01-03 02:12:26 +01:00
];
foreach ($payloadOverwrites as $key => $value) {
data_set($payload, $key, $value);
}
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', $payload);
2023-03-07 15:31:24 +01:00
$response->assertStatus(422);
$response->assertJsonValidationErrors($invalidFieldName);
2023-03-07 23:11:35 +01:00
})->with(function () {
2024-01-03 02:12:26 +01:00
yield [['parent.model' => 'missingmodel'], 'parent.model'];
yield [['parent.id' => -1], 'parent.id'];
2024-01-12 00:16:42 +01:00
yield [['parent.collection_name' => 'missingcollection'], 'parent.collection_name'];
2024-01-03 02:12:26 +01:00
yield [['payload.content' => []], 'payload.content'];
yield [['payload.content' => ['UU']], 'payload.content'];
yield [['payload.content' => null], 'payload.content'];
yield [['payload.content' => ''], 'payload.content'];
yield [['payload.content' => 1], 'payload.content'];
yield [['payload.name' => ''], 'payload.name'];
yield [['payload.name' => ['U']], 'payload.name'];
yield [['payload.name' => 1], 'payload.name'];
yield [['payload.name' => null], 'payload.name'];
2023-03-07 23:11:35 +01:00
yield [['payload' => 'lalal'], 'payload'];
yield [['payload' => 55], 'payload'];
2023-03-07 15:31:24 +01:00
});
2024-01-03 02:12:26 +01:00
test('it needs validation for multiple files', function (array $payloadOverwrites, string $invalidFieldName) {
2024-09-21 16:54:51 +02:00
test()->auth()->registerModel();
$post = test()->newPost();
2023-03-07 15:31:24 +01:00
2024-01-03 02:12:26 +01:00
$payload = [
2024-01-12 00:16:42 +01:00
'parent' => ['model' => 'post', 'collection_name' => 'images', 'id' => $post->id],
2023-03-07 15:31:24 +01:00
'payload' => [
[
2024-09-21 16:54:51 +02:00
'content' => base64_encode(test()->pdfFile()->getContent()),
2023-03-07 15:31:24 +01:00
'name' => 'beispiel bild.jpg',
2023-03-07 23:11:35 +01:00
],
2023-03-07 15:31:24 +01:00
],
2024-01-03 02:12:26 +01:00
];
foreach ($payloadOverwrites as $key => $value) {
data_set($payload, $key, $value);
}
2024-09-21 16:54:51 +02:00
$response = test()->postJson('/mediaupload', $payload);
2024-01-03 02:12:26 +01:00
2023-03-07 15:31:24 +01:00
$response->assertStatus(422);
$response->assertJsonValidationErrors($invalidFieldName);
2023-03-07 23:11:35 +01:00
})->with(function () {
2024-01-03 02:12:26 +01:00
yield [['parent.model' => 'missingmodel'], 'parent.model'];
yield [['parent.model' => 'post.missingcollection'], 'parent.model'];
yield [['parent.id' => -1], 'parent.id'];
2023-03-07 23:11:35 +01:00
yield [['payload' => 'lalal'], 'payload'];
yield [['payload' => []], 'payload'];
yield [['payload' => 1], 'payload'];
yield [['payload' => [['name' => 'AAA', 'content' => []]]], 'payload.0.content'];
yield [['payload' => [['name' => 'AAA', 'content' => ['UU']]]], 'payload.0.content'];
yield [['payload' => [['name' => 'AAA', 'content' => null]]], 'payload.0.content'];
yield [['payload' => [['name' => 'AAA', 'content' => '']]], 'payload.0.content'];
yield [['payload' => [['name' => 'AAA', 'content' => 1]]], 'payload.0.content'];
yield [['payload' => [['name' => '', 'content' => 'aaadfdf']]], 'payload.0.name'];
yield [['payload' => [['name' => ['U'], 'content' => 'aaadfdf']]], 'payload.0.name'];
yield [['payload' => [['name' => 1, 'content' => 'aaadfdf']]], 'payload.0.name'];
yield [['payload' => [['name' => null, 'content' => 'aaadfdf']]], 'payload.0.name'];
yield [['payload' => ['RRR']], 'payload.0'];
2023-03-07 15:31:24 +01:00
});