Skip to content
Closed
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
98 changes: 98 additions & 0 deletions tests/RichTextModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tonysm\RichTextLaravel\Tests;

use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Tonysm\RichTextLaravel\Content;
use Tonysm\RichTextLaravel\Exceptions\RichTextException;
use Tonysm\RichTextLaravel\Models\RichText;
Expand Down Expand Up @@ -222,6 +224,64 @@ public function can_update_content(): void
HTML, "{$post->body}");
}

#[\PHPUnit\Framework\Attributes\Test]
public function can_delete_attachments(): void
{
Storage::fake('public');

$firstImage = '/images/attachments/post-01.png';
$secondImage = '/images/attachments/post-02.png';

Storage::disk('public')->put($firstImage, '');
Storage::disk('public')->put($secondImage, '');

$encodeImage = fn (string $imageUrl) => e($imageUrl);

$post = PostForDeleting::create([
'title' => 'Has attachments',
'body' => <<<HTML
<div>
<figure data-trix-attachment='{"contentType":"image\/jpeg","url":"{$encodeImage($firstImage)}","href":"{$encodeImage($firstImage)}","filename":"first-image.jpg","filesize":47665,"width":880,"height":660}' data-trix-attributes='{"presentation":"gallery","caption":"First Image"}'></figure>
<figure data-trix-attachment='{"contentType":"image\/jpeg","url":"{$encodeImage($secondImage)}","href":"{$encodeImage($secondImage)}","filename":"first-image.jpg","filesize":47665,"width":880,"height":660}' data-trix-attributes='{"presentation":"gallery","caption":"First Image"}'></figure>
</div>
HTML,
]);

$post->delete();

Storage::disk('public')->assertMissing($firstImage);
Storage::disk('public')->assertMissing($secondImage);
}

#[\PHPUnit\Framework\Attributes\Test]
public function can_delete_via_observers(): void
{
Storage::fake('public');

$firstImage = '/images/attachments/post-01.png';
$secondImage = '/images/attachments/post-02.png';

Storage::disk('public')->put($firstImage, '');
Storage::disk('public')->put($secondImage, '');

$encodeImage = fn (string $imageUrl) => e($imageUrl);

$post = PostForDeletingViaObserver::create([
'title' => 'Has attachments',
'body' => <<<HTML
<div>
<figure data-trix-attachment='{"contentType":"image\/jpeg","url":"{$encodeImage($firstImage)}","href":"{$encodeImage($firstImage)}","filename":"first-image.jpg","filesize":47665,"width":880,"height":660}' data-trix-attributes='{"presentation":"gallery","caption":"First Image"}'></figure>
<figure data-trix-attachment='{"contentType":"image\/jpeg","url":"{$encodeImage($secondImage)}","href":"{$encodeImage($secondImage)}","filename":"first-image.jpg","filesize":47665,"width":880,"height":660}' data-trix-attributes='{"presentation":"gallery","caption":"First Image"}'></figure>
</div>
HTML,
]);

$post->delete();

Storage::disk('public')->assertMissing($firstImage);
Storage::disk('public')->assertMissing($secondImage);
}

private function createPost(?string $body = null, ?string $notes = null): PostWithNotes
{
return PostWithNotes::create(PostFactory::new()->raw([
Expand All @@ -248,3 +308,41 @@ class PostWithNotes extends Post
'notes',
];
}

class PostForDeleting extends Post
{
protected $table = 'posts';

protected $richTextAttributes = [
'body',
];

public static function booted(): void
{
static::deleted(function (Post $post) {
foreach ($post->body->attachments() as $attachment) {
Storage::disk('public')->delete($attachment->attachable->url);
}
});
}
}

#[ObservedBy([DeleteAttachmentsObserver::class])]
class PostForDeletingViaObserver extends Post
{
protected $table = 'posts';

protected $richTextAttributes = [
'body',
];
}

class DeleteAttachmentsObserver
{
public function deleted($model): void
{
foreach ($model->body->attachments() as $attachment) {
Storage::disk('public')->delete($attachment->attachable->url);
}
}
}
Loading