|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\DB; |
| 6 | +use Relaticle\CustomFields\Models\CustomField; |
| 7 | +use Relaticle\CustomFields\Models\CustomFieldValue; |
| 8 | +use Relaticle\CustomFields\Tests\Fixtures\Models\Comment; |
| 9 | +use Relaticle\CustomFields\Tests\Fixtures\Models\Post; |
| 10 | +use Relaticle\CustomFields\Tests\Fixtures\Models\User; |
| 11 | + |
| 12 | +beforeEach(function (): void { |
| 13 | + $this->user = User::factory()->create(); |
| 14 | + $this->actingAs($this->user); |
| 15 | +}); |
| 16 | + |
| 17 | +function deleteEntityWithoutEvents(string $table, mixed $id): void |
| 18 | +{ |
| 19 | + DB::table($table)->where('id', $id)->delete(); |
| 20 | +} |
| 21 | + |
| 22 | +it('reports no orphaned values when none exist', function (): void { |
| 23 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--force' => true]) |
| 24 | + ->expectsOutput('No custom field values found.') |
| 25 | + ->assertSuccessful(); |
| 26 | +}); |
| 27 | + |
| 28 | +it('reports no orphaned values when all entities exist', function (): void { |
| 29 | + $post = Post::factory()->create(); |
| 30 | + $customField = CustomField::factory()->create(['entity_type' => Post::class]); |
| 31 | + |
| 32 | + CustomFieldValue::factory()->create([ |
| 33 | + 'entity_id' => $post->getKey(), |
| 34 | + 'entity_type' => Post::class, |
| 35 | + 'custom_field_id' => $customField->getKey(), |
| 36 | + ]); |
| 37 | + |
| 38 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--force' => true]) |
| 39 | + ->expectsOutput('No orphaned custom field values found.') |
| 40 | + ->assertSuccessful(); |
| 41 | +}); |
| 42 | + |
| 43 | +it('detects orphaned values in dry-run mode without deleting', function (): void { |
| 44 | + $post = Post::factory()->create(); |
| 45 | + $customField = CustomField::factory()->create(['entity_type' => Post::class]); |
| 46 | + |
| 47 | + CustomFieldValue::factory()->create([ |
| 48 | + 'entity_id' => $post->getKey(), |
| 49 | + 'entity_type' => Post::class, |
| 50 | + 'custom_field_id' => $customField->getKey(), |
| 51 | + ]); |
| 52 | + |
| 53 | + deleteEntityWithoutEvents('posts', $post->getKey()); |
| 54 | + |
| 55 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--dry-run' => true]) |
| 56 | + ->assertSuccessful(); |
| 57 | + |
| 58 | + expect(CustomFieldValue::withoutGlobalScopes()->count())->toBe(1); |
| 59 | +}); |
| 60 | + |
| 61 | +it('deletes orphaned values when entity is hard-deleted', function (): void { |
| 62 | + $post = Post::factory()->create(); |
| 63 | + $customField = CustomField::factory()->create(['entity_type' => Post::class]); |
| 64 | + |
| 65 | + CustomFieldValue::factory()->create([ |
| 66 | + 'entity_id' => $post->getKey(), |
| 67 | + 'entity_type' => Post::class, |
| 68 | + 'custom_field_id' => $customField->getKey(), |
| 69 | + ]); |
| 70 | + |
| 71 | + deleteEntityWithoutEvents('posts', $post->getKey()); |
| 72 | + |
| 73 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--force' => true]) |
| 74 | + ->assertSuccessful(); |
| 75 | + |
| 76 | + expect(CustomFieldValue::withoutGlobalScopes()->count())->toBe(0); |
| 77 | +}); |
| 78 | + |
| 79 | +it('preserves values for soft-deleted entities', function (): void { |
| 80 | + $post = Post::factory()->create(); |
| 81 | + $customField = CustomField::factory()->create(['entity_type' => Post::class]); |
| 82 | + |
| 83 | + CustomFieldValue::factory()->create([ |
| 84 | + 'entity_id' => $post->getKey(), |
| 85 | + 'entity_type' => Post::class, |
| 86 | + 'custom_field_id' => $customField->getKey(), |
| 87 | + ]); |
| 88 | + |
| 89 | + $post->delete(); |
| 90 | + |
| 91 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--force' => true]) |
| 92 | + ->expectsOutput('No orphaned custom field values found.') |
| 93 | + ->assertSuccessful(); |
| 94 | + |
| 95 | + expect(CustomFieldValue::withoutGlobalScopes()->count())->toBe(1); |
| 96 | +}); |
| 97 | + |
| 98 | +it('deletes orphaned values for entities without soft deletes', function (): void { |
| 99 | + $comment = Comment::factory()->create(); |
| 100 | + $customField = CustomField::factory()->create(['entity_type' => Comment::class]); |
| 101 | + |
| 102 | + CustomFieldValue::factory()->create([ |
| 103 | + 'entity_id' => $comment->getKey(), |
| 104 | + 'entity_type' => Comment::class, |
| 105 | + 'custom_field_id' => $customField->getKey(), |
| 106 | + ]); |
| 107 | + |
| 108 | + deleteEntityWithoutEvents('comments', $comment->getKey()); |
| 109 | + |
| 110 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--force' => true]) |
| 111 | + ->assertSuccessful(); |
| 112 | + |
| 113 | + expect(CustomFieldValue::withoutGlobalScopes()->count())->toBe(0); |
| 114 | +}); |
| 115 | + |
| 116 | +it('only deletes orphaned values and preserves valid ones', function (): void { |
| 117 | + $activePost = Post::factory()->create(); |
| 118 | + $deletedPost = Post::factory()->create(); |
| 119 | + $customField = CustomField::factory()->create(['entity_type' => Post::class]); |
| 120 | + |
| 121 | + CustomFieldValue::factory()->create([ |
| 122 | + 'entity_id' => $activePost->getKey(), |
| 123 | + 'entity_type' => Post::class, |
| 124 | + 'custom_field_id' => $customField->getKey(), |
| 125 | + ]); |
| 126 | + |
| 127 | + CustomFieldValue::factory()->create([ |
| 128 | + 'entity_id' => $deletedPost->getKey(), |
| 129 | + 'entity_type' => Post::class, |
| 130 | + 'custom_field_id' => $customField->getKey(), |
| 131 | + ]); |
| 132 | + |
| 133 | + deleteEntityWithoutEvents('posts', $deletedPost->getKey()); |
| 134 | + |
| 135 | + $this->artisan('custom-fields:cleanup-orphaned-values', ['--force' => true]) |
| 136 | + ->assertSuccessful(); |
| 137 | + |
| 138 | + expect(CustomFieldValue::withoutGlobalScopes()->count())->toBe(1); |
| 139 | + expect( |
| 140 | + CustomFieldValue::withoutGlobalScopes() |
| 141 | + ->where('entity_id', $activePost->getKey()) |
| 142 | + ->exists() |
| 143 | + )->toBeTrue(); |
| 144 | +}); |
0 commit comments