Fixed order event

This commit is contained in:
Philipp Lang 2023-03-13 11:05:05 +01:00
parent fb15678d4e
commit 8a4dc77cfa
3 changed files with 8 additions and 1 deletions

View File

@ -29,6 +29,8 @@ class OrderController
Media::setNewOrder($request->order); Media::setNewOrder($request->order);
$model->getMediaCollection($collectionName)->runCallback('after', $model->fresh());
return MediaData::collection($model->getMedia($collectionName)); return MediaData::collection($model->getMedia($collectionName));
} }
} }

View File

@ -2,7 +2,11 @@
namespace Zoomyboy\MedialibraryHelper\Tests\Feature; namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
test('it can reorder media', function () { test('it can reorder media', function () {
Event::fake();
$this->auth()->registerModel(); $this->auth()->registerModel();
$post = $this->newPost(); $post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images'); $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
@ -20,6 +24,7 @@ test('it can reorder media', function () {
$response->assertJsonPath('1.id', $order->get(1)); $response->assertJsonPath('1.id', $order->get(1));
$response->assertJsonPath('2.id', $order->get(2)); $response->assertJsonPath('2.id', $order->get(2));
$this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id')); $this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id'));
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
}); });
test('images should belong to same model', function () { test('images should belong to same model', function () {

View File

@ -29,7 +29,7 @@ class Post extends Model implements HasMedia
$this->addMediaConversion('tiny')->width(200)->height(200); $this->addMediaConversion('tiny')->width(200)->height(200);
}); });
$this->addMediaCollection('images'); $this->addMediaCollection('images')->after(fn ($model) => Event::dispatch(new MediaChange($model)));
$this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) { $this->addMediaCollection('singleForced')->singleFile()->forceFileName(function ($model, $name) {
return $name.' '.now()->format('Y-m-d'); return $name.' '.now()->format('Y-m-d');