diff --git a/src/MediaController.php b/src/MediaController.php index 552c387..0f25106 100644 --- a/src/MediaController.php +++ b/src/MediaController.php @@ -22,8 +22,8 @@ class MediaController public function store(Request $request) { $request->validate([ - 'name' => 'string', 'model' => ['required', 'string', Rule::in(app('media-library-helpers')->keys())], + 'id' => 'required', ]); $model = $this->validateModel($request); @@ -34,13 +34,13 @@ class MediaController $request->validate($isSingle ? [ 'payload' => 'array', 'payload.*' => '', - 'payload.name' => 'required|string|max:255', + 'payload.name' => 'required|string|regex:/\..*$/|max:255', 'payload.content' => 'required|string', ] : [ 'payload' => 'required|array|min:1', 'payload.*' => 'array', - 'payload.*.name' => 'string', - 'payload.*.content' => 'string', + 'payload.*.name' => 'required|string|regex:/\..*$/|max:255', + 'payload.*.content' => 'required|string', ]); $content = $isSingle ? [$request->input('payload')] : $request->input('payload'); diff --git a/tests/Feature/UploadTest.php b/tests/Feature/UploadTest.php index 4f5934c..bcb3088 100644 --- a/tests/Feature/UploadTest.php +++ b/tests/Feature/UploadTest.php @@ -35,6 +35,79 @@ test('it uploads a single file to a single file collection', function () { $response->assertJsonMissingPath('model_id'); }); +test('test validation', function (array $attributes, string $messages) { + $this->auth()->registerModel(); + $post = $this->newPost(); + $content = base64_encode($this->pdfFile()->getContent()); + + $this->postJson('/mediaupload', [ + 'model' => 'post', + 'id' => $post->id, + 'collection' => 'defaultSingleFile', + 'payload' => [ + 'content' => $content, + 'name' => 'beispiel bild.jpg', + ], + ...$attributes + ])->assertJsonValidationErrors($messages); +})->with([ + 'missing collection' => [ + ['collection' => ''], + 'collection' + ], + 'missing id' => [ + ['id' => ''], + 'id' + ], +]); + +test('test validation for payload', function () { + $this->auth()->registerModel(); + $post = $this->newPost(); + + $this->postJson('/mediaupload', [ + 'model' => 'post', + 'id' => $post->id, + 'collection' => 'defaultSingleFile', + 'payload' => [ + 'content' => '', + 'name' => 'beispiel bild.jpg', + ], + ])->assertJsonValidationErrors('payload.content'); +}); + +test('test validation for name', function () { + $this->auth()->registerModel(); + $post = $this->newPost(); + $content = base64_encode($this->pdfFile()->getContent()); + + $this->postJson('/mediaupload', [ + 'model' => 'post', + 'id' => $post->id, + 'collection' => 'defaultSingleFile', + 'payload' => [ + 'content' => $content, + 'name' => '', + ], + ])->assertJsonValidationErrors('payload.name'); +}); + +test('test validation for extension', function () { + $this->auth()->registerModel(); + $post = $this->newPost(); + $content = base64_encode($this->pdfFile()->getContent()); + + $this->postJson('/mediaupload', [ + 'model' => 'post', + 'id' => $post->id, + 'collection' => 'defaultSingleFile', + 'payload' => [ + 'content' => $content, + 'name' => 'aaa', + ], + ])->assertJsonValidationErrors('payload.name'); +}); + test('it uploads a single image to a single file collection', function () { $this->auth()->registerModel(); $post = $this->newPost();