From f5e9aff0bec60e3250807e34458a683e2e59b2d1 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 7 Mar 2023 23:30:05 +0100 Subject: [PATCH] abort if single media not found --- src/MediaController.php | 2 ++ tests/Feature/IndexTest.php | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/src/MediaController.php b/src/MediaController.php index ab5a685..b72bc10 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -78,6 +78,8 @@ class MediaController $model = $model::find($parentId); $isSingle = 1 === $model->getMediaCollection($collection)->collectionSizeLimit; + abort_if($isSingle && !$model->getFirstMedia($collection), 404); + return $isSingle ? MediaData::from($model->getFirstMedia($collection)) : MediaData::collection($model->getMedia($collection)); diff --git a/tests/Feature/IndexTest.php b/tests/Feature/IndexTest.php index 857b161..9f6d456 100644 --- a/tests/Feature/IndexTest.php +++ b/tests/Feature/IndexTest.php @@ -27,3 +27,12 @@ test('it gets media for single', function () { $response->assertJsonPath('id', $media->id); $response->assertJsonPath('properties.test', 'old'); }); + +test('it returns 404 when media not found', function () { + $this->auth()->registerModel(); + $post = $this->newPost(); + + $response = $this->getJson("/mediaupload/post/{$post->id}/defaultSingleFile"); + + $response->assertStatus(404); +});