auth()->registerModel()->withoutExceptionHandling(); $this->postJson('/mediaupload', [ 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'payload' => ['content' => base64_encode($this->pdfFile()->getContent()), 'name' => 'beispiel.pdf'], ]) ->assertStatus(201) ->assertExactJson([ 'is_deferred' => true, 'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'), 'name' => 'beispiel', 'collection_name' => 'defaultSingleFile', 'size' => 3028, 'file_name' => 'beispiel.pdf', 'mime_type' => 'application/pdf', 'icon' => url('storage/filetypes/applicationpdf.svg'), ]); Storage::disk('temp')->assertExists('media-library/beispiel.pdf'); }); test('it forces filename when uploading', function () { $this->auth()->registerModel()->withoutExceptionHandling(); $this->postJson('/mediaupload', [ 'parent' => ['model' => 'post', 'collection_name' => 'singleJpegFile', 'id' => null], 'payload' => ['content' => base64_encode($this->pngFile()->getContent()), 'name' => 'beispiel.png'], ]) ->assertStatus(201) ->assertExactJson([ 'is_deferred' => true, 'original_url' => Storage::disk('temp')->url('media-library/beispiel.jpg'), 'name' => 'beispiel', 'collection_name' => 'singleJpegFile', 'size' => 366471, 'file_name' => 'beispiel.jpg', 'mime_type' => 'image/jpeg', 'icon' => url('storage/filetypes/imagejpeg.svg'), ]); Storage::disk('temp')->assertExists('media-library/beispiel.jpg'); Storage::disk('temp')->assertMissing('media-library/beispiel.png'); }); test('it stores a file to media library after deferred upload', function () { Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00')); $this->auth()->registerModel()->withoutExceptionHandling(); Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent()); $post = $this->newPost(); $post->setDeferredUploads([ 'file_name' => 'beispiel.pdf', 'collection_name' => 'singleForced', ]); $media = $post->getMedia('singleForced')->first(); $this->assertNotNull($media); $this->assertEquals('beispiel 2023-05-06', $media->name); Storage::disk('temp')->assertMissing('media-library/beispiel.pdf'); }); test('it stores multiple files to media library after deferred upload', function () { Carbon::setTestNow(Carbon::parse('2023-05-06 06:00:00')); $this->auth()->registerModel()->withoutExceptionHandling(); Storage::disk('temp')->put('media-library/beispiel.pdf', $this->pdfFile()->getContent()); Storage::disk('temp')->put('media-library/beispiel2.pdf', $this->pdfFile()->getContent()); $post = $this->newPost(); $post->setDeferredUploads([ [ 'file_name' => 'beispiel.pdf', 'collection_name' => 'multipleForced', ], [ 'file_name' => 'beispiel2.pdf', 'collection_name' => 'multipleForced', ] ]); $medias = $post->getMedia('multipleForced'); $this->assertCount(2, $medias); $this->assertEquals('beispiel 2023-05-06', $medias->get(0)->name); $this->assertEquals('beispiel2 2023-05-06', $medias->get(1)->name); Storage::disk('temp')->assertMissing('media-library/beispiel.pdf'); Storage::disk('temp')->assertMissing('media-library/beispiel2.pdf'); }); test('it uploads multiple files', function () { $this->auth()->registerModel()->withoutExceptionHandling(); $content = base64_encode($this->pdfFile()->getContent()); $payload = [ 'parent' => ['model' => 'post', 'collection_name' => 'multipleForced', 'id' => null], 'payload' => [ ['content' => $content, 'name' => 'beispiel.pdf'], ['content' => $content, 'name' => 'beispiel2.pdf'], ] ]; $this->postJson('/mediaupload', $payload) ->assertStatus(201) ->assertJson([ [ 'is_deferred' => true, 'original_url' => Storage::disk('temp')->url('media-library/beispiel.pdf'), 'name' => 'beispiel', 'collection_name' => 'multipleForced', 'size' => 3028, 'file_name' => 'beispiel.pdf', 'mime_type' => 'application/pdf', ], [ 'is_deferred' => true, 'original_url' => Storage::disk('temp')->url('media-library/beispiel2.pdf'), 'name' => 'beispiel2', 'collection_name' => 'multipleForced', 'size' => 3028, 'file_name' => 'beispiel2.pdf', 'mime_type' => 'application/pdf', ] ]); Storage::disk('temp')->assertExists('media-library/beispiel.pdf'); Storage::disk('temp')->assertExists('media-library/beispiel2.pdf'); }); test('it reduces file size', function () { $this->auth()->registerModel()->withoutExceptionHandling(); $this->postJson('/mediaupload', [ 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'payload' => [ 'content' => base64_encode($this->jpgFile()->getContent()), 'name' => 'beispiel bild.jpg', ], ]); $size = getimagesize(Storage::disk('temp')->path('media-library/beispiel bild.jpg')); $this->assertEquals(250, $size[0]); }); test('it handles authorization with collection', function () { $this->auth(['storeMedia' => ['collection' => 'rtrt']])->registerModel(); $content = base64_encode($this->pdfFile()->getContent()); $this->postJson('/mediaupload', [ 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'payload' => [ 'content' => $content, 'name' => 'beispiel bild.jpg', ], ])->assertStatus(403); }); test('it handles authorization with collection correctly', function () { $this->auth(['storeMedia' => ['collection' => 'defaultSingleFile']])->registerModel(); $content = base64_encode($this->pdfFile()->getContent()); $this->postJson('/mediaupload', [ 'parent' => ['model' => 'post', 'collection_name' => 'defaultSingleFile', 'id' => null], 'payload' => [ 'content' => $content, 'name' => 'beispiel bild.jpg', ], ])->assertStatus(201); }); test('it needs a collection', function ($key, $value) { $this->auth()->registerModel(); $content = base64_encode($this->pdfFile()->getContent()); $payload = [ 'parent' => ['model' => 'post', 'collection' => '', 'id' => null], 'payload' => [ 'content' => $content, 'name' => 'beispiel bild.jpg', ], ]; data_set($payload, $key, $value); $this->postJson('/mediaupload', $payload)->assertJsonValidationErrors($key); })->with(function () { yield ['parent.collection_name', '']; yield ['parent.collection_name', -1]; yield ['parent.collection_name', 'missingcollection']; yield ['parent.model', 'lalala']; yield ['parent.model', -1]; yield ['parent.model', '']; });