add updater
This commit is contained in:
parent
58867e87e4
commit
87265191f2
|
@ -19,11 +19,21 @@ class CollectionExtension
|
||||||
return fn ($callback) => $this->registerCustomCallback('storing', $callback);
|
return fn ($callback) => $this->registerCustomCallback('storing', $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function withDefaultProperties()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('withDefaultProperties', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
public function stored()
|
public function stored()
|
||||||
{
|
{
|
||||||
return fn ($callback) => $this->registerCustomCallback('stored', $callback);
|
return fn ($callback) => $this->registerCustomCallback('stored', $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function withPropertyValidation()
|
||||||
|
{
|
||||||
|
return fn ($callback) => $this->registerCustomCallback('withPropertyValidation', $callback);
|
||||||
|
}
|
||||||
|
|
||||||
public function runCallback()
|
public function runCallback()
|
||||||
{
|
{
|
||||||
return function (string $callback, ...$parameters) {
|
return function (string $callback, ...$parameters) {
|
||||||
|
@ -53,6 +63,8 @@ class CollectionExtension
|
||||||
'forceFileName' => fn ($name) => $name,
|
'forceFileName' => fn ($name) => $name,
|
||||||
'stored' => fn ($event) => true,
|
'stored' => fn ($event) => true,
|
||||||
'storing' => fn ($adder, $name) => $adder,
|
'storing' => fn ($adder, $name) => $adder,
|
||||||
|
'withDefaultProperties' => fn ($path) => [],
|
||||||
|
'withPropertyValidation' => fn ($path) => [],
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,26 +16,6 @@ class MediaController
|
||||||
{
|
{
|
||||||
use AuthorizesRequests;
|
use AuthorizesRequests;
|
||||||
|
|
||||||
private function validateModel(Request $request): HasMedia
|
|
||||||
{
|
|
||||||
$model = app('media-library-helpers')->get($request->input('model'));
|
|
||||||
|
|
||||||
$request->validate([
|
|
||||||
'collection' => [
|
|
||||||
'required',
|
|
||||||
'string',
|
|
||||||
Rule::in((new $model())->getRegisteredMediaCollections()->pluck('name'))
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$model = $model::find($request->input('id'));
|
|
||||||
if (!$model) {
|
|
||||||
throw ValidationException::withMessages(['model' => 'nicht gefunden']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $model;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
|
@ -62,11 +42,13 @@ class MediaController
|
||||||
|
|
||||||
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');
|
$content = $isSingle ? [$request->input('payload')] : $request->input('payload');
|
||||||
|
|
||||||
$medias = collect($content)->map(function($c) use ($collection, $model) {
|
$medias = collect($content)->map(function ($c) use ($collection, $model) {
|
||||||
$pathinfo = pathinfo($c['name']);
|
$pathinfo = pathinfo($c['name']);
|
||||||
$path = $collection->runCallback('forceFileName', $pathinfo['filename']).'.'.$pathinfo['extension'];
|
$path = $collection->runCallback('forceFileName', $pathinfo['filename']).'.'.$pathinfo['extension'];
|
||||||
Storage::disk('public')->put($path, base64_decode($c['content']));
|
Storage::disk('public')->put($path, base64_decode($c['content']));
|
||||||
$adder = $model->addMedia(Storage::disk('public')->path($path));
|
$adder = $model
|
||||||
|
->addMedia(Storage::disk('public')->path($path))
|
||||||
|
->withCustomProperties($collection->runCallback('withDefaultProperties', $path));
|
||||||
|
|
||||||
return tap(
|
return tap(
|
||||||
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
|
$collection->runCallback('storing', $adder, $path)->toMediaCollection($collection->name),
|
||||||
|
@ -77,6 +59,18 @@ class MediaController
|
||||||
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
return $isSingle ? MediaData::from($medias->first()) : MediaData::collection($medias);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function update(Request $request, Media $media): MediaData
|
||||||
|
{
|
||||||
|
$rules = collect($media->model->getMediaCollection($media->collection_name)->runCallback('withPropertyValidation', $media->file_name))
|
||||||
|
->mapWithKeys(fn ($rule, $key) => ["properties.{$key}" => $rule])->toArray();
|
||||||
|
|
||||||
|
$request->validate($rules);
|
||||||
|
|
||||||
|
$media->update(['custom_properties' => $request->properties]);
|
||||||
|
|
||||||
|
return MediaData::from($media);
|
||||||
|
}
|
||||||
|
|
||||||
public function index(Request $request, $parentModel, int $parentId, string $collection): JsonResponse
|
public function index(Request $request, $parentModel, int $parentId, string $collection): JsonResponse
|
||||||
{
|
{
|
||||||
$model = app('media-library-helpers')->get($parentModel);
|
$model = app('media-library-helpers')->get($parentModel);
|
||||||
|
@ -101,4 +95,23 @@ class MediaController
|
||||||
return property_exists($collection, $callback);
|
return property_exists($collection, $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function validateModel(Request $request): HasMedia
|
||||||
|
{
|
||||||
|
$model = app('media-library-helpers')->get($request->input('model'));
|
||||||
|
|
||||||
|
$request->validate([
|
||||||
|
'collection' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
Rule::in((new $model())->getRegisteredMediaCollections()->pluck('name')),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$model = $model::find($request->input('id'));
|
||||||
|
if (!$model) {
|
||||||
|
throw ValidationException::withMessages(['model' => 'nicht gefunden']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,8 @@ use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
|
|
||||||
#[MapInputName(SnakeCaseMapper::class)]
|
#[MapInputName(SnakeCaseMapper::class)]
|
||||||
#[MapOutputName(SnakeCaseMapper::class)]
|
#[MapOutputName(SnakeCaseMapper::class)]
|
||||||
class MediaData extends Data {
|
class MediaData extends Data
|
||||||
|
{
|
||||||
public int $id;
|
public int $id;
|
||||||
|
|
||||||
public string $originalUrl;
|
public string $originalUrl;
|
||||||
|
@ -24,9 +24,11 @@ class MediaData extends Data {
|
||||||
|
|
||||||
public string $fileName;
|
public string $fileName;
|
||||||
|
|
||||||
|
#[MapInputName('custom_properties')]
|
||||||
|
public array $properties;
|
||||||
|
|
||||||
public static function fromMedia(Media $media): self
|
public static function fromMedia(Media $media): self
|
||||||
{
|
{
|
||||||
return self::withoutMagicalCreationFrom($media);
|
return self::withoutMagicalCreationFrom($media);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace Zoomyboy\MedialibraryHelper;
|
||||||
|
|
||||||
use Illuminate\Routing\Router;
|
use Illuminate\Routing\Router;
|
||||||
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
|
||||||
use Spatie\MediaLibrary\MediaCollections\MediaCollection;
|
|
||||||
|
|
||||||
class ServiceProvider extends BaseServiceProvider
|
class ServiceProvider extends BaseServiceProvider
|
||||||
{
|
{
|
||||||
|
@ -20,10 +19,10 @@ class ServiceProvider extends BaseServiceProvider
|
||||||
$router->post('mediaupload', [MediaController::class, 'store'])->name('media.store');
|
$router->post('mediaupload', [MediaController::class, 'store'])->name('media.store');
|
||||||
$router->delete('mediaupload/{media}', [MediaController::class, 'destroy'])->name('media.destroy');
|
$router->delete('mediaupload/{media}', [MediaController::class, 'destroy'])->name('media.destroy');
|
||||||
$router->get('mediaupload/{parent_model}/{parent_id}/{collection}', [MediaController::class, 'index'])->name('media.index');
|
$router->get('mediaupload/{parent_model}/{parent_id}/{collection}', [MediaController::class, 'index'])->name('media.index');
|
||||||
|
$router->patch('mediaupload/{media}', [MediaController::class, 'update'])->name('media.update');
|
||||||
});
|
});
|
||||||
|
|
||||||
app(CollectionExtension::class)->boot();
|
app(CollectionExtension::class)->boot();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -0,0 +1,303 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
|
test('it updates a single files properties', function () {
|
||||||
|
$this->auth()->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
|
||||||
|
$media = $post->getFirstMedia('multipleProperties');
|
||||||
|
|
||||||
|
$response = $this->patchJson("/mediaupload/{$media->id}", [
|
||||||
|
'properties' => [
|
||||||
|
'test' => 'new',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$this->assertEquals('new', $media->fresh()->getCustomProperty('test'));
|
||||||
|
$response->assertJsonPath('properties.test', 'new');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it validates a single files properties', function () {
|
||||||
|
$this->auth()->registerModel();
|
||||||
|
$post = $this->newPost();
|
||||||
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
|
||||||
|
$media = $post->getFirstMedia('multipleProperties');
|
||||||
|
|
||||||
|
$response = $this->patchJson("/mediaupload/{$media->id}", [
|
||||||
|
'properties' => [
|
||||||
|
'test' => 'new feswfewfwewefew wewe ew ewewf wefwfwefwefwefwewefewwedw sad fd',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertStatus(422);
|
||||||
|
$response->assertJsonValidationErrors('properties.test');
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'singleForced',
|
||||||
|
// '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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'singleStoringHook',
|
||||||
|
// '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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'multipleProperties',
|
||||||
|
// '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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'multipleForced',
|
||||||
|
// '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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'singleWithEvent',
|
||||||
|
// 'payload' => [
|
||||||
|
// 'content' => $content,
|
||||||
|
// 'name' => 'beispiel bild.jpg',
|
||||||
|
// ],
|
||||||
|
// ]);
|
||||||
|
//
|
||||||
|
// $response->assertStatus(201);
|
||||||
|
// Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// 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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'multipleFilesWithEvent',
|
||||||
|
// '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', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'images',
|
||||||
|
// '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();
|
||||||
|
//
|
||||||
|
// $response = $this->postJson('/mediaupload', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'defaultSingleFile',
|
||||||
|
// 'payload' => [
|
||||||
|
// 'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
|
// 'name' => 'beispiel bild.jpg',
|
||||||
|
// ],
|
||||||
|
// ]);
|
||||||
|
//
|
||||||
|
// $response->assertStatus(403);
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// test('it needs validation for single files', function (array $payload, string $invalidFieldName) {
|
||||||
|
// $this->auth()->registerModel();
|
||||||
|
// $post = $this->newPost();
|
||||||
|
//
|
||||||
|
// $response = $this->postJson('/mediaupload', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'defaultSingleFile',
|
||||||
|
// 'payload' => [
|
||||||
|
// 'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
|
// 'name' => 'beispiel bild.jpg',
|
||||||
|
// ],
|
||||||
|
// ...$payload,
|
||||||
|
// ]);
|
||||||
|
//
|
||||||
|
// $response->assertStatus(422);
|
||||||
|
// $response->assertJsonValidationErrors($invalidFieldName);
|
||||||
|
// })->with(function () {
|
||||||
|
// yield [['model' => 'missingmodel'], 'model'];
|
||||||
|
// yield [['id' => -1], 'model'];
|
||||||
|
// yield [['collection' => 'missingcollection'], 'collection'];
|
||||||
|
// yield [['payload' => ['name' => 'AAA', 'content' => []]], 'payload.content'];
|
||||||
|
// yield [['payload' => ['name' => 'AAA', 'content' => ['UU']]], 'payload.content'];
|
||||||
|
// yield [['payload' => ['name' => 'AAA', 'content' => null]], 'payload.content'];
|
||||||
|
// yield [['payload' => ['name' => 'AAA', 'content' => '']], 'payload.content'];
|
||||||
|
// yield [['payload' => ['name' => 'AAA', 'content' => 1]], 'payload.content'];
|
||||||
|
// yield [['payload' => ['name' => '', 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
|
// yield [['payload' => ['name' => ['U'], 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
|
// yield [['payload' => ['name' => 1, 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
|
// yield [['payload' => ['name' => null, 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
|
// yield [['payload' => 'lalal'], 'payload'];
|
||||||
|
// yield [['payload' => 55], 'payload'];
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// test('it needs validation for multiple files', function (array $payload, string $invalidFieldName) {
|
||||||
|
// $this->auth()->registerModel();
|
||||||
|
// $post = $this->newPost();
|
||||||
|
//
|
||||||
|
// $response = $this->postJson('/mediaupload', [
|
||||||
|
// 'model' => 'post',
|
||||||
|
// 'id' => $post->id,
|
||||||
|
// 'collection' => 'images',
|
||||||
|
// 'payload' => [
|
||||||
|
// [
|
||||||
|
// 'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
|
// 'name' => 'beispiel bild.jpg',
|
||||||
|
// ],
|
||||||
|
// ],
|
||||||
|
// ...$payload,
|
||||||
|
// ]);
|
||||||
|
//
|
||||||
|
// $response->assertStatus(422);
|
||||||
|
// $response->assertJsonValidationErrors($invalidFieldName);
|
||||||
|
// })->with(function () {
|
||||||
|
// yield [['model' => 'missingmodel'], 'model'];
|
||||||
|
// yield [['id' => -1], 'model'];
|
||||||
|
// yield [['collection' => 'missingcollection'], 'collection'];
|
||||||
|
// 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'];
|
||||||
|
// });
|
|
@ -6,7 +6,7 @@ use Carbon\Carbon;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
test('it uploads a single file to a single file collection', function() {
|
test('it uploads a single file to a single file collection', function () {
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
@ -18,7 +18,7 @@ test('it uploads a single file to a single file collection', function() {
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
|
@ -34,7 +34,7 @@ test('it uploads a single file to a single file collection', function() {
|
||||||
$response->assertJsonMissingPath('model_id');
|
$response->assertJsonMissingPath('model_id');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it forces a filename for a single collection', function() {
|
test('it forces a filename for a single collection', function () {
|
||||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
@ -47,16 +47,16 @@ test('it forces a filename for a single collection', function() {
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name);
|
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('singleForced')->file_name);
|
||||||
$response->assertJsonPath('name', "beispiel bild 2023-04-04");
|
$response->assertJsonPath('name', 'beispiel bild 2023-04-04');
|
||||||
$response->assertJsonPath('file_name', "beispiel-bild-2023-04-04.jpg");
|
$response->assertJsonPath('file_name', 'beispiel-bild-2023-04-04.jpg');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it sets custom title when storing', function() {
|
test('it sets custom title when storing', function () {
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$content = base64_encode($this->pdfFile()->getContent());
|
$content = base64_encode($this->pdfFile()->getContent());
|
||||||
|
@ -68,7 +68,7 @@ test('it sets custom title when storing', function() {
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
|
@ -78,7 +78,28 @@ test('it sets custom title when storing', function() {
|
||||||
$this->assertEquals('beispiel bild', $media->getCustomProperty('ttt'));
|
$this->assertEquals('beispiel bild', $media->getCustomProperty('ttt'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it forces a filename for multiple collections', function() {
|
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', [
|
||||||
|
'model' => 'post',
|
||||||
|
'id' => $post->id,
|
||||||
|
'collection' => 'multipleProperties',
|
||||||
|
'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'));
|
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
@ -92,15 +113,15 @@ test('it forces a filename for multiple collections', function() {
|
||||||
[
|
[
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name);
|
$this->assertEquals('beispiel-bild-2023-04-04.jpg', $post->getFirstMedia('multipleForced')->file_name);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it throws event when file has been uploaded', function() {
|
test('it throws event when file has been uploaded', function () {
|
||||||
Event::fake();
|
Event::fake();
|
||||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||||
|
@ -114,14 +135,14 @@ test('it throws event when file has been uploaded', function() {
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
Event::assertDispatched(MediaStored::class, fn($event) => $event->media->id === $response->json('id'));
|
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('id'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it throws event when multiple files uploaded', function() {
|
test('it throws event when multiple files uploaded', function () {
|
||||||
Event::fake();
|
Event::fake();
|
||||||
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
Carbon::setTestNow(Carbon::parse('2023-04-04 00:00:00'));
|
||||||
$this->auth()->registerModel()->withoutExceptionHandling();
|
$this->auth()->registerModel()->withoutExceptionHandling();
|
||||||
|
@ -141,15 +162,15 @@ test('it throws event when multiple files uploaded', function() {
|
||||||
'content' => $content,
|
'content' => $content,
|
||||||
'name' => 'beispiel bild 1.jpg',
|
'name' => 'beispiel bild 1.jpg',
|
||||||
],
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$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('0.id'));
|
||||||
Event::assertDispatched(MediaStored::class, fn($event) => $event->media->id === $response->json('1.id'));
|
Event::assertDispatched(MediaStored::class, fn ($event) => $event->media->id === $response->json('1.id'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it uploads multiple files', function() {
|
test('it uploads multiple files', function () {
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
$file = $this->pdfFile();
|
$file = $this->pdfFile();
|
||||||
|
@ -167,8 +188,8 @@ test('it uploads multiple files', function() {
|
||||||
[
|
[
|
||||||
'content' => base64_encode($file->getContent()),
|
'content' => base64_encode($file->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(201);
|
$response->assertStatus(201);
|
||||||
|
@ -186,24 +207,24 @@ test('it uploads multiple files', function() {
|
||||||
$response->assertJsonMissingPath('0.model_id');
|
$response->assertJsonMissingPath('0.model_id');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it returns 403 when not authorized', function() {
|
test('it returns 403 when not authorized', function () {
|
||||||
$this->auth(['storeMedia' => false])->registerModel();
|
$this->auth(['storeMedia' => false])->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
$response = $this->postJson('/mediaupload', [
|
$response = $this->postJson('/mediaupload', [
|
||||||
'model' => 'post',
|
'model' => 'post',
|
||||||
'id' => $post->id,
|
'id' => $post->id,
|
||||||
'collection' => 'defaultSingleFile',
|
'collection' => 'defaultSingleFile',
|
||||||
'payload' => [
|
'payload' => [
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(403);
|
$response->assertStatus(403);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('it needs validation for single files', function(array $payload, string $invalidFieldName) {
|
test('it needs validation for single files', function (array $payload, string $invalidFieldName) {
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
|
@ -215,30 +236,29 @@ test('it needs validation for single files', function(array $payload, string $in
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
],
|
],
|
||||||
...$payload
|
...$payload,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(422);
|
$response->assertStatus(422);
|
||||||
$response->assertJsonValidationErrors($invalidFieldName);
|
$response->assertJsonValidationErrors($invalidFieldName);
|
||||||
})->with(function() {
|
})->with(function () {
|
||||||
yield [ ['model' => 'missingmodel'], 'model' ];
|
yield [['model' => 'missingmodel'], 'model'];
|
||||||
yield [ ['id' => -1], 'model' ];
|
yield [['id' => -1], 'model'];
|
||||||
yield [ ['collection' => 'missingcollection'], 'collection' ];
|
yield [['collection' => 'missingcollection'], 'collection'];
|
||||||
yield [ ['payload' => ['name' => 'AAA', 'content' => []]], 'payload.content' ];
|
yield [['payload' => ['name' => 'AAA', 'content' => []]], 'payload.content'];
|
||||||
yield [ ['payload' => ['name' => 'AAA', 'content' => ['UU']]], 'payload.content' ];
|
yield [['payload' => ['name' => 'AAA', 'content' => ['UU']]], 'payload.content'];
|
||||||
yield [ ['payload' => ['name' => 'AAA', 'content' => null]], 'payload.content' ];
|
yield [['payload' => ['name' => 'AAA', 'content' => null]], 'payload.content'];
|
||||||
yield [ ['payload' => ['name' => 'AAA', 'content' => '']], 'payload.content' ];
|
yield [['payload' => ['name' => 'AAA', 'content' => '']], 'payload.content'];
|
||||||
yield [ ['payload' => ['name' => 'AAA', 'content' => 1]], 'payload.content' ];
|
yield [['payload' => ['name' => 'AAA', 'content' => 1]], 'payload.content'];
|
||||||
yield [ ['payload' => ['name' => '', 'content' => 'aaadfdf']], 'payload.name' ];
|
yield [['payload' => ['name' => '', 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [ ['payload' => ['name' => ['U'], 'content' => 'aaadfdf']], 'payload.name' ];
|
yield [['payload' => ['name' => ['U'], 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [ ['payload' => ['name' => 1, 'content' => 'aaadfdf']], 'payload.name' ];
|
yield [['payload' => ['name' => 1, 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [ ['payload' => ['name' => null, 'content' => 'aaadfdf']], 'payload.name' ];
|
yield [['payload' => ['name' => null, 'content' => 'aaadfdf']], 'payload.name'];
|
||||||
yield [ ['payload' => 'lalal'], 'payload' ];
|
yield [['payload' => 'lalal'], 'payload'];
|
||||||
yield [ ['payload' => 55], 'payload' ];
|
yield [['payload' => 55], 'payload'];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('it needs validation for multiple files', function (array $payload, string $invalidFieldName) {
|
||||||
test('it needs validation for multiple files', function(array $payload, string $invalidFieldName) {
|
|
||||||
$this->auth()->registerModel();
|
$this->auth()->registerModel();
|
||||||
$post = $this->newPost();
|
$post = $this->newPost();
|
||||||
|
|
||||||
|
@ -250,31 +270,29 @@ test('it needs validation for multiple files', function(array $payload, string $
|
||||||
[
|
[
|
||||||
'content' => base64_encode($this->pdfFile()->getContent()),
|
'content' => base64_encode($this->pdfFile()->getContent()),
|
||||||
'name' => 'beispiel bild.jpg',
|
'name' => 'beispiel bild.jpg',
|
||||||
]
|
],
|
||||||
],
|
],
|
||||||
...$payload
|
...$payload,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(422);
|
$response->assertStatus(422);
|
||||||
$response->assertJsonValidationErrors($invalidFieldName);
|
$response->assertJsonValidationErrors($invalidFieldName);
|
||||||
})->with(function() {
|
})->with(function () {
|
||||||
yield [ ['model' => 'missingmodel'], 'model' ];
|
yield [['model' => 'missingmodel'], 'model'];
|
||||||
yield [ ['id' => -1], 'model' ];
|
yield [['id' => -1], 'model'];
|
||||||
yield [ ['collection' => 'missingcollection'], 'collection' ];
|
yield [['collection' => 'missingcollection'], 'collection'];
|
||||||
yield [ ['payload' => 'lalal'], 'payload' ];
|
yield [['payload' => 'lalal'], 'payload'];
|
||||||
yield [ ['payload' => []], 'payload' ];
|
yield [['payload' => []], 'payload'];
|
||||||
yield [ ['payload' => 1], 'payload' ];
|
yield [['payload' => 1], 'payload'];
|
||||||
|
|
||||||
yield [ ['payload' => [['name' => 'AAA', 'content' => []]]], 'payload.0.content' ];
|
yield [['payload' => [['name' => 'AAA', 'content' => []]]], 'payload.0.content'];
|
||||||
yield [ ['payload' => [['name' => 'AAA', 'content' => ['UU']]]], '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' => null]]], 'payload.0.content'];
|
||||||
yield [ ['payload' => [['name' => 'AAA', 'content' => '']]], 'payload.0.content' ];
|
yield [['payload' => [['name' => 'AAA', 'content' => '']]], 'payload.0.content'];
|
||||||
yield [ ['payload' => [['name' => 'AAA', 'content' => 1]]], 'payload.0.content' ];
|
yield [['payload' => [['name' => 'AAA', 'content' => 1]]], 'payload.0.content'];
|
||||||
yield [ ['payload' => [['name' => '', 'content' => 'aaadfdf']]], 'payload.0.name' ];
|
yield [['payload' => [['name' => '', 'content' => 'aaadfdf']]], 'payload.0.name'];
|
||||||
yield [ ['payload' => [['name' => ['U'], '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' => 1, 'content' => 'aaadfdf']]], 'payload.0.name'];
|
||||||
yield [ ['payload' => [['name' => null, 'content' => 'aaadfdf']]], 'payload.0.name' ];
|
yield [['payload' => [['name' => null, 'content' => 'aaadfdf']]], 'payload.0.name'];
|
||||||
yield [ ['payload' => ['RRR']], 'payload.0' ];
|
yield [['payload' => ['RRR']], 'payload.0'];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
|
@ -11,7 +12,6 @@ use Zoomyboy\MedialibraryHelper\Tests\Events\MediaStored;
|
||||||
|
|
||||||
class Post extends Model implements HasMedia
|
class Post extends Model implements HasMedia
|
||||||
{
|
{
|
||||||
|
|
||||||
use InteractsWithMedia;
|
use InteractsWithMedia;
|
||||||
|
|
||||||
public $guarded = [];
|
public $guarded = [];
|
||||||
|
@ -22,29 +22,33 @@ class Post extends Model implements HasMedia
|
||||||
|
|
||||||
$this->addMediaCollection('images');
|
$this->addMediaCollection('images');
|
||||||
|
|
||||||
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function($name) {
|
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($name) {
|
||||||
return $name.' '.now()->format('Y-m-d');
|
return $name.' '.now()->format('Y-m-d');
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('multipleForced')->forceFileName(function($name) {
|
$this->addMediaCollection('multipleForced')->forceFileName(function ($name) {
|
||||||
return $name.' '.now()->format('Y-m-d');
|
return $name.' '.now()->format('Y-m-d');
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function($adder, $fileName) {
|
$this->addMediaCollection('singleStoringHook')->singleFile()->storing(function ($adder, $fileName) {
|
||||||
return $adder->withCustomProperties([
|
return $adder->withCustomProperties([
|
||||||
'use' => 'AAA',
|
'use' => 'AAA',
|
||||||
'ttt' => pathinfo($fileName, PATHINFO_FILENAME),
|
'ttt' => pathinfo($fileName, PATHINFO_FILENAME),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function(Media $media) {
|
$this->addMediaCollection('singleWithEvent')->singleFile()->stored(function (Media $media) {
|
||||||
Event::dispatch(new MediaStored($media));
|
Event::dispatch(new MediaStored($media));
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->addMediaCollection('multipleFilesWithEvent')->stored(function(Media $media) {
|
$this->addMediaCollection('multipleFilesWithEvent')->stored(function (Media $media) {
|
||||||
Event::dispatch(new MediaStored($media));
|
Event::dispatch(new MediaStored($media));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->addMediaCollection('multipleProperties')->singleFile()->withDefaultProperties(fn ($path) => [
|
||||||
|
'test' => Str::camel($path),
|
||||||
|
])->withPropertyValidation(fn ($path) => [
|
||||||
|
'test' => 'string|max:10',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,4 +5,3 @@ use Illuminate\Support\Facades\Storage;
|
||||||
uses(Zoomyboy\MedialibraryHelper\Tests\TestCase::class)->in('Feature');
|
uses(Zoomyboy\MedialibraryHelper\Tests\TestCase::class)->in('Feature');
|
||||||
uses(Illuminate\Foundation\Testing\RefreshDatabase::class)->in('Feature');
|
uses(Illuminate\Foundation\Testing\RefreshDatabase::class)->in('Feature');
|
||||||
uses()->beforeEach(fn () => Storage::fake('media'))->in('Feature');
|
uses()->beforeEach(fn () => Storage::fake('media'))->in('Feature');
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
namespace Zoomyboy\MedialibraryHelper\Tests;
|
namespace Zoomyboy\MedialibraryHelper\Tests;
|
||||||
|
|
||||||
use Illuminate\Http\File;
|
use Illuminate\Http\File;
|
||||||
use Illuminate\Support\Facades\Config;
|
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Orchestra\Testbench\TestCase as BaseTestCase;
|
use Orchestra\Testbench\TestCase as BaseTestCase;
|
||||||
use Spatie\LaravelData\LaravelDataServiceProvider;
|
use Spatie\LaravelData\LaravelDataServiceProvider;
|
||||||
|
@ -13,15 +12,12 @@ use Zoomyboy\MedialibraryHelper\Tests\Models\Post;
|
||||||
|
|
||||||
class TestCase extends BaseTestCase
|
class TestCase extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define database migrations.
|
* Define database migrations.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected function defineDatabaseMigrations(): void
|
protected function defineDatabaseMigrations(): void
|
||||||
{
|
{
|
||||||
$this->loadMigrationsFrom(__DIR__ . '/migrations');
|
$this->loadMigrationsFrom(__DIR__.'/migrations');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPackageProviders($app): array
|
protected function getPackageProviders($app): array
|
||||||
|
@ -34,7 +30,7 @@ class TestCase extends BaseTestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a pdf file with a filename and get path
|
* Generate a pdf file with a filename and get path.
|
||||||
*/
|
*/
|
||||||
protected function pdfFile(?string $filename = null): File
|
protected function pdfFile(?string $filename = null): File
|
||||||
{
|
{
|
||||||
|
@ -77,4 +73,9 @@ class TestCase extends BaseTestCase
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function defineEnvironment($app)
|
||||||
|
{
|
||||||
|
$app['config']->set('medialibrary-helper.middleware', ['web']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue