feat(attachments): ensure 'key' is set as UUID on attachment creation and update tests for key validation
This commit is contained in:
@@ -12,6 +12,7 @@ use Illuminate\Support\Str;
|
|||||||
#[Fillable([
|
#[Fillable([
|
||||||
'attachable_type',
|
'attachable_type',
|
||||||
'attachable_id',
|
'attachable_id',
|
||||||
|
'key',
|
||||||
'path',
|
'path',
|
||||||
'filename',
|
'filename',
|
||||||
'type',
|
'type',
|
||||||
@@ -28,7 +29,9 @@ class Attachment extends Model
|
|||||||
protected static function booted(): void
|
protected static function booted(): void
|
||||||
{
|
{
|
||||||
static::creating(function (self $attachment): void {
|
static::creating(function (self $attachment): void {
|
||||||
$attachment->key = (string) Str::uuid();
|
if (! $attachment->key) {
|
||||||
|
$attachment->key = (string) Str::uuid();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use App\Domains\Attachable\Models\Attachment;
|
|||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
class AttachmentService
|
class AttachmentService
|
||||||
@@ -32,10 +33,11 @@ class AttachmentService
|
|||||||
throw new AttachmentStorageException('The attachment filename cannot be empty.');
|
throw new AttachmentStorageException('The attachment filename cannot be empty.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$key = (string) Str::uuid();
|
||||||
$storedPath = Storage::disk('s3')->putFileAs(
|
$storedPath = Storage::disk('s3')->putFileAs(
|
||||||
$this->directoryFromPath($normalizedPath),
|
$this->directoryFromPath($normalizedPath),
|
||||||
$file,
|
$file,
|
||||||
$filename,
|
$key,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (! is_string($storedPath) || $storedPath === '') {
|
if (! is_string($storedPath) || $storedPath === '') {
|
||||||
@@ -45,6 +47,7 @@ class AttachmentService
|
|||||||
try {
|
try {
|
||||||
/** @var Attachment $attachment */
|
/** @var Attachment $attachment */
|
||||||
$attachment = $attachable->attachments()->create([
|
$attachment = $attachable->attachments()->create([
|
||||||
|
'key' => $key,
|
||||||
'path' => $storedPath,
|
'path' => $storedPath,
|
||||||
'filename' => $filename,
|
'filename' => $filename,
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use App\Domains\Tenant\Models\Tenant;
|
|||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
@@ -37,12 +38,16 @@ class AttachmentTest extends TestCase
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->assertSame(AttachmentType::Image, $attachment->type);
|
$this->assertSame(AttachmentType::Image, $attachment->type);
|
||||||
Storage::disk('s3')->assertExists('attachments/acme/logo.png');
|
$this->assertTrue(Str::isUuid($attachment->key));
|
||||||
|
$this->assertSame('logo.png', $attachment->filename);
|
||||||
|
$this->assertSame('attachments/acme/'.$attachment->key, $attachment->path);
|
||||||
|
Storage::disk('s3')->assertExists('attachments/acme/'.$attachment->key);
|
||||||
$this->assertDatabaseHas('attachments', [
|
$this->assertDatabaseHas('attachments', [
|
||||||
'id' => $attachment->id,
|
'id' => $attachment->id,
|
||||||
'attachable_type' => $tenant->getMorphClass(),
|
'attachable_type' => $tenant->getMorphClass(),
|
||||||
'attachable_id' => $tenant->getKey(),
|
'attachable_id' => $tenant->getKey(),
|
||||||
'path' => 'attachments/acme/logo.png',
|
'key' => $attachment->key,
|
||||||
|
'path' => 'attachments/acme/'.$attachment->key,
|
||||||
'filename' => 'logo.png',
|
'filename' => 'logo.png',
|
||||||
'type' => AttachmentType::Image->value,
|
'type' => AttachmentType::Image->value,
|
||||||
]);
|
]);
|
||||||
@@ -68,7 +73,7 @@ class AttachmentTest extends TestCase
|
|||||||
->andReturn($disk);
|
->andReturn($disk);
|
||||||
$disk->shouldReceive('putFileAs')
|
$disk->shouldReceive('putFileAs')
|
||||||
->once()
|
->once()
|
||||||
->with('attachments/globex', Mockery::type(UploadedFile::class), 'manual.pdf')
|
->with('attachments/globex', Mockery::type(UploadedFile::class), Mockery::on(static fn (string $value): bool => Str::isUuid($value)))
|
||||||
->andReturn(false);
|
->andReturn(false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user