Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
},
"platform": {
"php": "8.4"
}
},
"extra": {
Expand Down
22 changes: 22 additions & 0 deletions database/migrations/2025_11_24_082802_add_slug_to_images_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddSlugToImagesTable extends Migration
{
public function up()
{
Schema::table('images', function (Blueprint $table) {
$table->string('slug')->unique()->index()->nullable()->after('filename');
});
}

public function down()
{
Schema::table('images', function (Blueprint $table) {
$table->dropColumn('slug');
});
}
}
22 changes: 22 additions & 0 deletions database/migrations/2025_11_24_082803_add_slug_to_videos_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddSlugToVideosTable extends Migration
{
public function up()
{
Schema::table('videos', function (Blueprint $table) {
$table->string('slug')->unique()->index()->nullable()->after('identifier');
});
}

public function down()
{
Schema::table('videos', function (Blueprint $table) {
$table->dropColumn('slug');
});
}
}
2 changes: 2 additions & 0 deletions src/Filament/Resources/Images/Schemas/ImageForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static function configure(Schema $schema): Schema
}),
TextInput::make('description')
->columnSpanFull(),
TextInput::make('slug')
->helperText('Unique identifier for embedding in content (e.g., "my-image")'),
TextInput::make('alt'),
TextInput::make('credit'),
// @phpstan-ignore-next-line Placeholder is deprecated in newer Filament; compatible here
Expand Down
3 changes: 3 additions & 0 deletions src/Filament/Resources/Videos/Schemas/VideoForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static function configure(Schema $schema): Schema
->visibleOn('create'),

TextInput::make('name')->label('Internal Name'),
TextInput::make('slug')
->helperText('Unique identifier for embedding in content (e.g., "my-video")')
->visibleOn('edit'),
TextInput::make('credit')->visibleOn('edit'),
Select::make('image_id')
->label('Thumbnail Image')
Expand Down
11 changes: 11 additions & 0 deletions src/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* @property int $id
* @property string|null $filename
* @property string|null $slug
* @property int|null $width
* @property int|null $height
* @property string|null $description
Expand Down Expand Up @@ -63,6 +64,16 @@ public function getExtensionAttribute(): ?string
return isset($parts['extension']) ? strtolower($parts['extension']) : null;
}

public function embed(): string
{
$slug = $this->getAttribute('slug');
if (! $slug) {
return '';
}

return '{image:'.$slug.'}';
}

public function videos()
{
return $this->hasMany(Video::class);
Expand Down
11 changes: 11 additions & 0 deletions src/Models/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* @property int $id
* @property string $identifier
* @property string|null $slug
* @property string $source
* @property string|null $name
* @property string|null $title
Expand Down Expand Up @@ -77,4 +78,14 @@ public function youtubeUrl(): ?string
// Use the youtu.be short link format
return sprintf('https://youtu.be/%s', $this->identifier);
}

public function embed(): string
{
$slug = $this->getAttribute('slug');
if (! $slug) {
return '';
}

return '{video:'.$slug.'}';
}
}
121 changes: 121 additions & 0 deletions tests/Unit/ImageSlugEmbedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

use DrewRoberts\Media\Models\Image;

beforeEach(function () {
$this->user = createUser();
});

describe('Image Slug', function () {
describe('Basic Slug Functionality', function () {
test('creates image with slug', function () {
$image = createImage(['slug' => 'test-image-slug']);

expect($image)
->toBeInstanceOf(Image::class)
->slug->toBe('test-image-slug');
});

test('creates image without slug', function () {
$image = createImage(['slug' => null]);

expect($image)
->slug->toBeNull();
});

test('enforces unique slugs', function () {
createImage(['filename' => '/unique1.jpg', 'slug' => 'unique-slug']);

expect(fn () => createImage(['filename' => '/unique2.jpg', 'slug' => 'unique-slug']))
->toThrow(\Illuminate\Database\QueryException::class);
});

test('allows null slugs for multiple images', function () {
$image1 = createImage(['filename' => '/image1.jpg', 'slug' => null]);
$image2 = createImage(['filename' => '/image2.jpg', 'slug' => null]);

expect($image1->slug)->toBeNull()
->and($image2->slug)->toBeNull();
});
});

describe('Slug Queries', function () {
test('can find image by slug', function () {
$image = createImage(['slug' => 'findable-slug']);

$found = Image::where('slug', 'findable-slug')->first();

expect($found)
->toBeInstanceOf(Image::class)
->id->toBe($image->id)
->slug->toBe('findable-slug');
});

test('returns null when slug does not exist', function () {
$found = Image::where('slug', 'non-existent-slug')->first();

expect($found)->toBeNull();
});
});
});

describe('Image Embed', function () {
describe('Embed Method', function () {
test('returns formatted embed string with slug', function () {
$image = createImage(['slug' => 'my-image-slug']);

expect($image->embed())
->toBe('{image:my-image-slug}');
});

test('returns empty string when slug is null', function () {
$image = createImage(['slug' => null]);

expect($image->embed())
->toBe('');
});

test('returns empty string when slug is empty', function () {
$image = createImage(['slug' => '']);

expect($image->embed())
->toBe('');
});

test('handles slug with special characters', function () {
$image = createImage(['slug' => 'image-with-123']);

expect($image->embed())
->toBe('{image:image-with-123}');
});

test('handles slug with hyphens', function () {
$image = createImage(['slug' => 'my-awesome-image']);

expect($image->embed())
->toBe('{image:my-awesome-image}');
});
});

describe('Embed Integration', function () {
test('embed works after updating slug', function () {
$image = createImage(['slug' => 'original-slug']);
$image->update(['slug' => 'updated-slug']);

expect($image->embed())
->toBe('{image:updated-slug}');
});

test('multiple images have different embed strings', function () {
$image1 = createImage(['filename' => '/image1.jpg', 'slug' => 'first-image']);
$image2 = createImage(['filename' => '/image2.jpg', 'slug' => 'second-image']);

expect($image1->embed())
->toBe('{image:first-image}')
->and($image2->embed())
->toBe('{image:second-image}')
->and($image1->embed())
->not->toBe($image2->embed());
});
});
});
130 changes: 130 additions & 0 deletions tests/Unit/VideoSlugEmbedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

use DrewRoberts\Media\Models\Video;

beforeEach(function () {
$this->user = createUser();
});

describe('Video Slug', function () {
describe('Basic Slug Functionality', function () {
test('creates video with slug', function () {
$video = createVideo(['slug' => 'test-video-slug']);

expect($video)
->toBeInstanceOf(Video::class)
->slug->toBe('test-video-slug');
});

test('creates video without slug', function () {
$video = createVideo(['slug' => null]);

expect($video)
->slug->toBeNull();
});

test('enforces unique slugs', function () {
createVideo(['identifier' => 'unique-vid-1', 'slug' => 'unique-slug']);

expect(fn () => createVideo(['identifier' => 'unique-vid-2', 'slug' => 'unique-slug']))
->toThrow(\Illuminate\Database\QueryException::class);
});

test('allows null slugs for multiple videos', function () {
$video1 = createVideo(['identifier' => 'vid1', 'slug' => null]);
$video2 = createVideo(['identifier' => 'vid2', 'slug' => null]);

expect($video1->slug)->toBeNull()
->and($video2->slug)->toBeNull();
});
});

describe('Slug Queries', function () {
test('can find video by slug', function () {
$video = createVideo(['slug' => 'findable-video-slug']);

$found = Video::where('slug', 'findable-video-slug')->first();

expect($found)
->toBeInstanceOf(Video::class)
->id->toBe($video->id)
->slug->toBe('findable-video-slug');
});

test('returns null when slug does not exist', function () {
$found = Video::where('slug', 'non-existent-slug')->first();

expect($found)->toBeNull();
});
});
});

describe('Video Embed', function () {
describe('Embed Method', function () {
test('returns formatted embed string with slug', function () {
$video = createVideo(['slug' => 'my-video-slug']);

expect($video->embed())
->toBe('{video:my-video-slug}');
});

test('returns empty string when slug is null', function () {
$video = createVideo(['slug' => null]);

expect($video->embed())
->toBe('');
});

test('returns empty string when slug is empty', function () {
$video = createVideo(['slug' => '']);

expect($video->embed())
->toBe('');
});

test('handles slug with special characters', function () {
$video = createVideo(['slug' => 'video-with-123']);

expect($video->embed())
->toBe('{video:video-with-123}');
});

test('handles slug with hyphens', function () {
$video = createVideo(['slug' => 'my-awesome-video']);

expect($video->embed())
->toBe('{video:my-awesome-video}');
});
});

describe('Embed Integration', function () {
test('embed works after updating slug', function () {
$video = createVideo(['slug' => 'original-slug']);
$video->update(['slug' => 'updated-slug']);

expect($video->embed())
->toBe('{video:updated-slug}');
});

test('multiple videos have different embed strings', function () {
$video1 = createVideo(['identifier' => 'vid1', 'slug' => 'first-video']);
$video2 = createVideo(['identifier' => 'vid2', 'slug' => 'second-video']);

expect($video1->embed())
->toBe('{video:first-video}')
->and($video2->embed())
->toBe('{video:second-video}')
->and($video1->embed())
->not->toBe($video2->embed());
});

test('embed is independent of youtube url', function () {
$video = createVideo(['identifier' => 'dQw4w9WgXcQ', 'slug' => 'rick-roll']);

expect($video->embed())
->toBe('{video:rick-roll}')
->and($video->youtubeUrl())
->toBe('https://youtu.be/dQw4w9WgXcQ');
});
});
});