medialibrary-helper/tests/Feature/DestroyTest.php

53 lines
2.3 KiB
PHP
Raw Normal View History

2023-03-08 00:04:00 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
2023-03-13 10:17:14 +01:00
use Illuminate\Support\Facades\Event;
2023-03-13 10:34:52 +01:00
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
2023-03-13 10:17:14 +01:00
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaDestroyed;
2023-03-08 00:04:00 +01:00
test('it deletes multiple media', function () {
2023-03-13 10:17:14 +01:00
$this->auth()->registerModel()->withoutExceptionHandling();
2023-03-08 00:04:00 +01:00
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced');
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('multipleForced');
$media = $post->getFirstMedia('multipleForced');
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200);
$this->assertCount(1, $post->fresh()->getMedia('multipleForced'));
});
test('it deletes single media', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('defaultSingleFile');
$media = $post->getFirstMedia('defaultSingleFile');
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200);
$this->assertCount(0, $post->fresh()->getMedia('defaultSingleFile'));
});
test('it needs authorization', function () {
$this->auth(['destroyMedia' => false])->registerModel();
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('defaultSingleFile');
$media = $post->getFirstMedia('defaultSingleFile');
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(403);
});
2023-03-13 10:17:14 +01:00
test('it fires event', function () {
Event::fake();
$this->auth()->registerModel()->withoutExceptionHandling();
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('singleWithEvent');
$media = $post->getFirstMedia('singleWithEvent');
$this->deleteJson("/mediaupload/{$media->id}")->assertStatus(200);
Event::assertDispatched(MediaDestroyed::class, fn ($event) => $event->model->is($post));
2023-03-13 10:34:52 +01:00
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
2023-03-13 10:17:14 +01:00
});