medialibrary-helper/tests/Feature/UploadTest.php

348 lines
13 KiB
PHP

<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Carbon\Carbon;
use Illuminate\Support\Facades\Event;
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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
$this->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('is_deferred', false);
$response->assertJsonMissingPath('model_type');
$response->assertJsonMissingPath('model_id');
});
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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
$this->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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'singleForced', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
$this->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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'singleStoringHook', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
$media = $post->getFirstMedia('singleStoringHook');
$this->assertEquals('AAA', $media->getCustomProperty('use'));
$this->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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'multipleProperties', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
$media = $post->getFirstMedia('multipleProperties');
$this->assertEquals('beispielBild.jpg', $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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'multipleForced', 'id' => $post->id],
'payload' => [
[
'content' => $content,
'name' => 'beispiel bild.jpg',
],
],
]);
$response->assertStatus(201);
$this->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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'singleWithEvent', 'id' => $post->id],
'payload' => [
'content' => $content,
'name' => 'beispiel bild.jpg',
],
]);
$response->assertStatus(201);
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
});
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());
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'multipleFilesWithEvent', 'id' => $post->id],
'payload' => [
[
'content' => $content,
'name' => 'beispiel bild.jpg',
],
[
'content' => $content,
'name' => 'beispiel bild 1.jpg',
],
],
]);
$response->assertStatus(201);
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'));
});
test('it uploads multiple files', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$file = $this->pdfFile();
$post->addMedia($file->getPathname())->preservingOriginal()->toMediaCollection('images');
$response = $this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'images', 'id' => $post->id],
'payload' => [
[
'content' => base64_encode($file->getContent()),
'name' => 'aaaa.jpg',
],
[
'content' => base64_encode($file->getContent()),
'name' => 'beispiel bild.jpg',
],
],
]);
$response->assertStatus(201);
$this->assertCount(3, $post->getMedia('images'));
$media = $post->getMedia('images')->skip(1)->values();
$this->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.name', 'aaaa');
$response->assertJsonPath('0.collection_name', 'images');
$response->assertJsonPath('0.file_name', 'aaaa.jpg');
$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();
$this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [
'content' => base64_encode($this->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();
$this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg',
],
])->assertStatus(201);
$this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $otherPost->id],
'payload' => [
'content' => base64_encode($this->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();
$this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg',
],
])->assertStatus(201);
$this->postJson('/mediaupload', [
'parent' => ['model' => 'post', 'collection' => 'singleWithEvent', 'id' => $post->id],
'payload' => [
'content' => base64_encode($this->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();
$payload = [
'parent' => ['model' => 'post', 'collection' => 'defaultSingleFile', 'id' => $post->id],
'payload' => [
'content' => base64_encode($this->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg',
],
];
foreach ($payloadOverwrites as $key => $value) {
data_set($payload, $key, $value);
}
$response = $this->postJson('/mediaupload', $payload);
$response->assertStatus(422);
$response->assertJsonValidationErrors($invalidFieldName);
})->with(function () {
yield [['parent.model' => 'missingmodel'], 'parent.model'];
yield [['parent.id' => -1], 'parent.id'];
yield [['parent.collection' => 'missingcollection'], 'parent.collection'];
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'];
yield [['payload' => 'lalal'], 'payload'];
yield [['payload' => 55], 'payload'];
});
test('it needs validation for multiple files', function (array $payloadOverwrites, string $invalidFieldName) {
$this->auth()->registerModel();
$post = $this->newPost();
$payload = [
'parent' => ['model' => 'post', 'collection' => 'images', 'id' => $post->id],
'payload' => [
[
'content' => base64_encode($this->pdfFile()->getContent()),
'name' => 'beispiel bild.jpg',
],
],
];
foreach ($payloadOverwrites as $key => $value) {
data_set($payload, $key, $value);
}
$response = $this->postJson('/mediaupload', $payload);
$response->assertStatus(422);
$response->assertJsonValidationErrors($invalidFieldName);
})->with(function () {
yield [['parent.model' => 'missingmodel'], 'parent.model'];
yield [['parent.model' => 'post.missingcollection'], 'parent.model'];
yield [['parent.id' => -1], 'parent.id'];
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'];
});