10-04-2026
This commit is contained in:
parent
4d6b4930b2
commit
4bb89aad8c
836 changed files with 52961 additions and 5950 deletions
202
tests/Feature/DisplayMediaTest.php
Normal file
202
tests/Feature/DisplayMediaTest.php
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\DisplayMedia;
|
||||
use App\Models\User;
|
||||
use App\Services\DisplayMediaService;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
beforeEach(function () {
|
||||
Storage::fake('public');
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// MODEL TESTS
|
||||
// ========================================
|
||||
|
||||
it('creates an uploaded image media', function () {
|
||||
$media = DisplayMedia::factory()->create();
|
||||
|
||||
expect($media->isUpload())->toBeTrue()
|
||||
->and($media->isExternal())->toBeFalse()
|
||||
->and($media->isImage())->toBeTrue()
|
||||
->and($media->isVideo())->toBeFalse()
|
||||
->and($media->source_type)->toBe('upload');
|
||||
});
|
||||
|
||||
it('creates an external video media', function () {
|
||||
$media = DisplayMedia::factory()->externalVideo()->create();
|
||||
|
||||
expect($media->isExternal())->toBeTrue()
|
||||
->and($media->isUpload())->toBeFalse()
|
||||
->and($media->isVideo())->toBeTrue()
|
||||
->and($media->external_url)->toStartWith('https://');
|
||||
});
|
||||
|
||||
it('returns correct URL for uploaded media', function () {
|
||||
$media = DisplayMedia::factory()->create(['path' => 'display-media/2026/03/test.webp']);
|
||||
|
||||
expect($media->getUrl())->toContain('display-media/2026/03/test.webp');
|
||||
});
|
||||
|
||||
it('returns external URL for external media', function () {
|
||||
$url = 'https://drive.google.com/file/d/abc123/view';
|
||||
$media = DisplayMedia::factory()->external()->create(['external_url' => $url]);
|
||||
|
||||
expect($media->getUrl())->toBe($url);
|
||||
});
|
||||
|
||||
it('returns human readable file size', function () {
|
||||
$media = DisplayMedia::factory()->create(['file_size' => 1536000]);
|
||||
expect($media->getHumanFileSize())->toBe('1.5 MB');
|
||||
|
||||
$external = DisplayMedia::factory()->external()->create();
|
||||
expect($external->getHumanFileSize())->toBe('Extern');
|
||||
});
|
||||
|
||||
it('returns display name from title or filename', function () {
|
||||
$withTitle = DisplayMedia::factory()->create(['title' => 'Mein Video', 'filename' => 'file.mp4']);
|
||||
expect($withTitle->getDisplayName())->toBe('Mein Video');
|
||||
|
||||
$withoutTitle = DisplayMedia::factory()->create(['title' => null, 'filename' => 'file.mp4']);
|
||||
expect($withoutTitle->getDisplayName())->toBe('file.mp4');
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// SCOPE TESTS
|
||||
// ========================================
|
||||
|
||||
it('filters by image scope', function () {
|
||||
DisplayMedia::factory()->create(['type' => 'image']);
|
||||
DisplayMedia::factory()->video()->create();
|
||||
|
||||
expect(DisplayMedia::images()->count())->toBe(1);
|
||||
});
|
||||
|
||||
it('filters by video scope', function () {
|
||||
DisplayMedia::factory()->create(['type' => 'image']);
|
||||
DisplayMedia::factory()->video()->create();
|
||||
|
||||
expect(DisplayMedia::videos()->count())->toBe(1);
|
||||
});
|
||||
|
||||
it('filters by upload scope', function () {
|
||||
DisplayMedia::factory()->create();
|
||||
DisplayMedia::factory()->external()->create();
|
||||
|
||||
expect(DisplayMedia::uploads()->count())->toBe(1);
|
||||
});
|
||||
|
||||
it('filters by external scope', function () {
|
||||
DisplayMedia::factory()->create();
|
||||
DisplayMedia::factory()->external()->create();
|
||||
|
||||
expect(DisplayMedia::externals()->count())->toBe(1);
|
||||
});
|
||||
|
||||
it('filters by collection scope', function () {
|
||||
DisplayMedia::factory()->create(['collection' => 'immobilien']);
|
||||
DisplayMedia::factory()->create(['collection' => 'moebel']);
|
||||
|
||||
expect(DisplayMedia::inCollection('immobilien')->count())->toBe(1);
|
||||
});
|
||||
|
||||
it('filters by active scope', function () {
|
||||
DisplayMedia::factory()->create(['is_active' => true]);
|
||||
DisplayMedia::factory()->create(['is_active' => false]);
|
||||
|
||||
expect(DisplayMedia::active()->count())->toBe(1);
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// SERVICE TESTS
|
||||
// ========================================
|
||||
|
||||
it('stores an uploaded file', function () {
|
||||
$service = app(DisplayMediaService::class);
|
||||
$file = UploadedFile::fake()->image('test-photo.jpg', 800, 600);
|
||||
|
||||
$media = $service->storeUpload($file, 'immobilien');
|
||||
|
||||
expect($media)->toBeInstanceOf(DisplayMedia::class)
|
||||
->and($media->filename)->toBe('test-photo.jpg')
|
||||
->and($media->source_type)->toBe('upload')
|
||||
->and($media->type)->toBe('image')
|
||||
->and($media->collection)->toBe('immobilien')
|
||||
->and($media->path)->not->toBeNull();
|
||||
|
||||
Storage::disk('public')->assertExists($media->path);
|
||||
});
|
||||
|
||||
it('stores a video upload', function () {
|
||||
$service = app(DisplayMediaService::class);
|
||||
$file = UploadedFile::fake()->create('showroom.mp4', 10000, 'video/mp4');
|
||||
|
||||
$media = $service->storeUpload($file);
|
||||
|
||||
expect($media->type)->toBe('video')
|
||||
->and($media->mime_type)->toBe('video/mp4');
|
||||
});
|
||||
|
||||
it('creates media from external URL', function () {
|
||||
$service = app(DisplayMediaService::class);
|
||||
|
||||
$media = $service->createFromUrl(
|
||||
url: 'https://drive.google.com/file/d/abc123/view',
|
||||
type: 'video',
|
||||
title: 'Showroom Tour 4K',
|
||||
collection: 'brand',
|
||||
);
|
||||
|
||||
expect($media->source_type)->toBe('external')
|
||||
->and($media->external_url)->toBe('https://drive.google.com/file/d/abc123/view')
|
||||
->and($media->title)->toBe('Showroom Tour 4K')
|
||||
->and($media->type)->toBe('video')
|
||||
->and($media->collection)->toBe('brand')
|
||||
->and($media->file_size)->toBe(0);
|
||||
});
|
||||
|
||||
it('deletes uploaded media and its file', function () {
|
||||
$service = app(DisplayMediaService::class);
|
||||
$file = UploadedFile::fake()->image('delete-me.jpg');
|
||||
$media = $service->storeUpload($file);
|
||||
$path = $media->path;
|
||||
|
||||
Storage::disk('public')->assertExists($path);
|
||||
|
||||
$service->delete($media);
|
||||
|
||||
Storage::disk('public')->assertMissing($path);
|
||||
expect(DisplayMedia::find($media->id))->toBeNull();
|
||||
});
|
||||
|
||||
it('deletes external media record', function () {
|
||||
$service = app(DisplayMediaService::class);
|
||||
$media = $service->createFromUrl('https://example.com/video.mp4', 'video');
|
||||
|
||||
$service->delete($media);
|
||||
|
||||
expect(DisplayMedia::find($media->id))->toBeNull();
|
||||
});
|
||||
|
||||
// ========================================
|
||||
// ROUTE TESTS
|
||||
// ========================================
|
||||
|
||||
it('requires authentication for display media library', function () {
|
||||
$this->get(route('admin.cms.display-media'))
|
||||
->assertRedirect();
|
||||
});
|
||||
|
||||
it('loads display media library for authenticated admin', function () {
|
||||
Role::findOrCreate('Super-Admin', 'web');
|
||||
$user = User::factory()->create();
|
||||
$user->assignRole('Super-Admin');
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('admin.cms.display-media'))
|
||||
->assertSuccessful();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue