medialibrary-helper/tests/Feature/ReorderTest.php

59 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2023-03-12 21:43:57 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
2023-03-13 11:05:05 +01:00
use Illuminate\Support\Facades\Event;
use Zoomyboy\MedialibraryHelper\Tests\Events\MediaChange;
2023-03-12 21:43:57 +01:00
test('it can reorder media', function () {
2023-03-13 11:05:05 +01:00
Event::fake();
2023-03-12 21:43:57 +01:00
$this->auth()->registerModel();
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$order = $post->getMedia('images')->pluck('id');
$order->prepend($order->pop());
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
'order' => $order,
]);
$response->assertStatus(200);
$response->assertJsonPath('0.id', $order->get(0));
$response->assertJsonPath('1.id', $order->get(1));
$response->assertJsonPath('2.id', $order->get(2));
$this->assertEquals($order, $post->fresh()->getMedia('images')->pluck('id'));
2023-03-13 11:05:05 +01:00
Event::assertDispatched(MediaChange::class, fn ($event) => $event->model->is($post));
2023-03-12 21:43:57 +01:00
});
test('images should belong to same model', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$post = $this->newPost();
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$thirdMedia = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
'order' => [$firstMedia->id, $secondMedia->id, $thirdMedia->id],
]);
$response->assertJsonValidationErrors('order');
});
2023-03-13 09:32:09 +01:00
test('it should authorize', function () {
$this->auth(['listMedia' => false])->registerModel();
$post = $this->newPost();
$media = $post->addMedia($this->pdfFile()->getPathname())->preservingOriginal()->toMediaCollection('images');
$response = $this->patchJson("/mediaupload/post/{$post->id}/images", [
'order' => [$media->id],
]);
$response->assertStatus(403);
});