medialibrary-helper/tests/Feature/IndexTest.php

100 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2023-03-07 23:28:11 +01:00
<?php
namespace Zoomyboy\MedialibraryHelper\Tests\Feature;
2023-03-08 00:58:15 +01:00
use Illuminate\Support\Facades\Storage;
2023-03-10 14:52:17 +01:00
use Spatie\MediaLibrary\MediaCollections\Models\Media;
2023-03-08 00:58:15 +01:00
2023-03-07 23:28:11 +01:00
test('it gets all medias', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$response = $this->getJson("/mediaupload/post/{$post->id}/images");
$response->assertStatus(200);
$response->assertJsonPath('0.id', $firstMedia->id);
$response->assertJsonPath('1.id', $secondMedia->id);
$response->assertJsonPath('1.properties.test', 'old');
2023-03-10 12:47:14 +01:00
$response->assertJsonPath('1.icon', url('storage/filetypes/applicationpdf.svg'));
2023-03-07 23:28:11 +01:00
});
2023-03-10 14:52:17 +01:00
test('it gets media in order', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$firstMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$secondMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$thirdMedia = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$order = $post->getMedia('images')->pluck('id');
$order->prepend($order->pop());
Media::setNewOrder($order->toArray());
$response = $this->getJson("/mediaupload/post/{$post->id}/images");
$response->assertStatus(200);
$response->assertJsonPath('0.id', $thirdMedia->id);
$response->assertJsonPath('1.id', $firstMedia->id);
$response->assertJsonPath('2.id', $secondMedia->id);
});
2023-03-07 23:28:11 +01:00
test('it gets media for single', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$media = $post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('defaultSingleFile');
$response = $this->getJson("/mediaupload/post/{$post->id}/defaultSingleFile");
$response->assertStatus(200);
$response->assertJsonPath('id', $media->id);
$response->assertJsonPath('properties.test', 'old');
2023-03-08 02:04:13 +01:00
$response->assertJsonPath('fallback', false);
2023-03-08 15:50:40 +01:00
$response->assertJsonPath('mime_type', 'application/pdf');
2023-03-07 23:28:11 +01:00
});
2023-03-07 23:30:05 +01:00
2023-03-08 00:04:00 +01:00
test('it checks for authorization', function () {
$this->auth(['listMedia' => false])->registerModel();
$post = $this->newPost();
$post->addMedia($this->pdfFile()->getPathname())->withCustomProperties(['test' => 'old'])->preservingOriginal()->toMediaCollection('images');
$response = $this->getJson("/mediaupload/post/{$post->id}/images");
$response->assertStatus(403);
});
2023-03-07 23:30:05 +01:00
test('it returns 404 when media not found', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$response = $this->getJson("/mediaupload/post/{$post->id}/defaultSingleFile");
$response->assertStatus(404);
});
2023-03-08 00:58:15 +01:00
test('test it gets conversions for single media', function () {
$this->auth()->registerModel();
$post = $this->newPost();
$media = $post->addMedia($this->jpgFile()->getPathname())->preservingOriginal()->toMediaCollection('conversionsWithDefault');
$response = $this->getJson("/mediaupload/post/{$post->id}/conversionsWithDefault");
$response->assertStatus(200);
$response->assertJsonPath('conversions.tiny.original_url', $media->getFullUrl('tiny'));
});
test('test it gets default single media', function () {
$this->auth()->registerModel();
$post = $this->newPost();
2023-03-08 15:50:40 +01:00
Storage::disk('public')->put('default.jpg', $this->jpgFile()->getContent());
2023-03-08 00:58:15 +01:00
$response = $this->getJson("/mediaupload/post/{$post->id}/conversionsWithDefault");
$response->assertStatus(200);
$response->assertJsonPath('id', null);
$response->assertJsonPath('original_url', Storage::disk('public')->url('default.jpg'));
$response->assertJsonPath('name', 'default');
$response->assertJsonPath('file_name', 'default.jpg');
2023-03-08 02:04:13 +01:00
$response->assertJsonPath('fallback', true);
2023-03-08 15:50:40 +01:00
$response->assertJsonPath('mime_type', 'image/jpeg');
2023-03-08 00:58:15 +01:00
});