2023-03-07 23:11:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
|
|
|
|
|
2023-03-13 10:34:52 +01:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2024-01-03 11:10:59 +01:00
|
|
|
use Workbench\App\Events\MediaChange;
|
2023-03-13 10:34:52 +01:00
|
|
|
|
2023-03-07 23:11:35 +01:00
|
|
|
test('it updates a single files properties', function () {
|
2023-03-13 10:34:52 +01:00
|
|
|
Event::fake();
|
2023-03-07 23:11:35 +01:00
|
|
|
$this->auth()->registerModel();
|
|
|
|
$post = $this->newPost();
|
|
|
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
|
|
|
|
$media = $post->getFirstMedia('multipleProperties');
|
|
|
|
|
|
|
|
$response = $this->patchJson("/mediaupload/{$media->id}", [
|
|
|
|
'properties' => [
|
|
|
|
'test' => 'new',
|
2023-03-07 23:14:11 +01:00
|
|
|
'missing' => 'value',
|
2023-03-07 23:11:35 +01:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$this->assertEquals('new', $media->fresh()->getCustomProperty('test'));
|
2023-03-07 23:14:11 +01:00
|
|
|
$this->assertEquals(null, $media->fresh()->getCustomProperty('missing'));
|
2023-03-07 23:11:35 +01:00
|
|
|
$response->assertJsonPath('properties.test', 'new');
|
2023-03-07 23:14:11 +01:00
|
|
|
$response->assertJsonMissingPath('properties.missing');
|
2023-03-13 10:34:52 +01:00
|
|
|
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
|
2023-03-07 23:11:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('it validates a single files properties', function () {
|
|
|
|
$this->auth()->registerModel();
|
|
|
|
$post = $this->newPost();
|
|
|
|
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleProperties');
|
|
|
|
$media = $post->getFirstMedia('multipleProperties');
|
|
|
|
|
|
|
|
$response = $this->patchJson("/mediaupload/{$media->id}", [
|
|
|
|
'properties' => [
|
|
|
|
'test' => 'new feswfewfwewefew wewe ew ewewf wefwfwefwefwefwewefewwedw sad fd',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(422);
|
|
|
|
$response->assertJsonValidationErrors('properties.test');
|
|
|
|
});
|
2023-03-08 00:04:00 +01:00
|
|
|
|
|
|
|
test('it checks for authorization', function () {
|
|
|
|
$this->auth(['updateMedia' => false])->registerModel();
|
|
|
|
$post = $this->newPost();
|
|
|
|
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('multipleProperties');
|
|
|
|
$media = $post->getFirstMedia('multipleProperties');
|
|
|
|
|
|
|
|
$response = $this->patchJson("/mediaupload/{$media->id}", [
|
|
|
|
'properties' => [
|
|
|
|
'test' => 'new',
|
|
|
|
'missing' => 'value',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(403);
|
|
|
|
});
|