From 8a4dc77cfa1fb537063617b0636cbd8996107f30 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Mon, 13 Mar 2023 11:05:05 +0100 Subject: [PATCH] Fixed order event --- src/OrderController.php | 2 ++ tests/Feature/ReorderTest.php | 5 +++++ tests/Models/Post.php | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/OrderController.php b/src/OrderController.php index 05a60c3..ad939d7 100644 --- a/src/OrderController.php +++ b/src/OrderController.php @@ -29,6 +29,8 @@ class OrderController Media::setNewOrder($request->order); + $model->getMediaCollection($collectionName)->runCallback('after', $model->fresh()); + return MediaData::collection($model->getMedia($collectionName)); } } diff --git a/tests/Feature/ReorderTest.php b/tests/Feature/ReorderTest.php index 4338563..d331f5d 100644 --- a/tests/Feature/ReorderTest.php +++ b/tests/Feature/ReorderTest.php @@ -2,7 +2,11 @@ namespace Zoomyboy\MedialibraryHelper\Tests\Feature; +use Illuminate\Support\Facades\Event; +use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange; + test('it can reorder media', function () { + Event::fake(); $this->auth()->registerModel(); $post = $this->newPost(); $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('2.id', $order->get(2)); $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 () { diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 1dfcbaa..5a3943e 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -29,7 +29,7 @@ class Post extends Model implements HasMedia $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) { return $name.' '.now()->format('Y-m-d');