|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\Badge\Badge; |
| 4 | +use App\Models\Event; |
| 5 | +use App\Models\EventUser; |
| 6 | +use App\Models\Fursuit\Fursuit; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | + |
| 11 | +use function Pest\Laravel\actingAs; |
| 12 | +use function Pest\Laravel\delete; |
| 13 | + |
| 14 | +uses(RefreshDatabase::class); |
| 15 | + |
| 16 | +function badgeDeletedAt(Badge $badge) |
| 17 | +{ |
| 18 | + return DB::table('badges')->where('id', $badge->id)->value('deleted_at'); |
| 19 | +} |
| 20 | + |
| 21 | +function fursuitDeletedAt(Fursuit $fursuit) |
| 22 | +{ |
| 23 | + return DB::table('fursuits')->where('id', $fursuit->id)->value('deleted_at'); |
| 24 | +} |
| 25 | + |
| 26 | +test('soft-deleting a fursuit cascades to its badges', function () { |
| 27 | + $fursuit = Fursuit::factory()->create(); |
| 28 | + $b1 = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 29 | + $b2 = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 30 | + |
| 31 | + $fursuit->delete(); |
| 32 | + |
| 33 | + expect(fursuitDeletedAt($fursuit))->not->toBeNull(); |
| 34 | + expect(badgeDeletedAt($b1))->not->toBeNull(); |
| 35 | + expect(badgeDeletedAt($b2))->not->toBeNull(); |
| 36 | +}); |
| 37 | + |
| 38 | +test('soft-deleting the last badge cascades to its fursuit', function () { |
| 39 | + $fursuit = Fursuit::factory()->create(); |
| 40 | + $badge = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 41 | + |
| 42 | + $badge->delete(); |
| 43 | + |
| 44 | + expect(badgeDeletedAt($badge))->not->toBeNull(); |
| 45 | + expect(fursuitDeletedAt($fursuit))->not->toBeNull(); |
| 46 | +}); |
| 47 | + |
| 48 | +test('deleting one badge keeps the fursuit while another active badge remains', function () { |
| 49 | + $fursuit = Fursuit::factory()->create(); |
| 50 | + $b1 = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 51 | + $b2 = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 52 | + |
| 53 | + $b1->delete(); |
| 54 | + |
| 55 | + expect(badgeDeletedAt($b1))->not->toBeNull(); |
| 56 | + expect(fursuitDeletedAt($fursuit))->toBeNull(); |
| 57 | + expect(badgeDeletedAt($b2))->toBeNull(); |
| 58 | +}); |
| 59 | + |
| 60 | +test('soft-deleting a main badge cascades to its spare copies and the empty fursuit', function () { |
| 61 | + $fursuit = Fursuit::factory()->create(); |
| 62 | + $main = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 63 | + $copy = Badge::factory()->for($fursuit)->create(['extra_copy_of' => $main->id, 'extra_copy' => true]); |
| 64 | + |
| 65 | + $main->delete(); |
| 66 | + |
| 67 | + expect(badgeDeletedAt($main))->not->toBeNull(); |
| 68 | + expect(badgeDeletedAt($copy))->not->toBeNull(); |
| 69 | + expect(fursuitDeletedAt($fursuit))->not->toBeNull(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('public badge deletion soft-deletes the badge and its now-empty fursuit', function () { |
| 73 | + $event = Event::factory()->create([ |
| 74 | + 'starts_at' => now()->subDays(5), |
| 75 | + 'ends_at' => now()->addDays(20), |
| 76 | + 'order_starts_at' => now()->subDays(5), |
| 77 | + 'order_ends_at' => now()->addDays(20), |
| 78 | + ]); |
| 79 | + $user = User::factory()->create(); |
| 80 | + EventUser::create([ |
| 81 | + 'user_id' => $user->id, |
| 82 | + 'event_id' => $event->id, |
| 83 | + 'attendee_id' => 'TEST-'.$user->id, |
| 84 | + 'valid_registration' => true, |
| 85 | + 'prepaid_badges' => 0, |
| 86 | + ]); |
| 87 | + |
| 88 | + $fursuit = Fursuit::factory()->recycle($user)->recycle($event)->create(['status' => 'approved']); |
| 89 | + $badge = Badge::factory()->for($fursuit)->create([ |
| 90 | + 'status_fulfillment' => 'pending', |
| 91 | + 'extra_copy_of' => null, |
| 92 | + ]); |
| 93 | + |
| 94 | + actingAs($user); |
| 95 | + delete(route('badges.destroy', $badge))->assertRedirect(); |
| 96 | + |
| 97 | + expect(badgeDeletedAt($badge))->not->toBeNull(); |
| 98 | + expect(fursuitDeletedAt($fursuit))->not->toBeNull(); |
| 99 | +}); |
| 100 | + |
| 101 | +test('force-deleting a badge removes the row and does not cascade to the fursuit', function () { |
| 102 | + $fursuit = Fursuit::factory()->create(); |
| 103 | + $badge = Badge::factory()->for($fursuit)->create(['extra_copy_of' => null]); |
| 104 | + |
| 105 | + $badge->forceDelete(); |
| 106 | + |
| 107 | + expect(Badge::withTrashed()->find($badge->id))->toBeNull(); |
| 108 | + // Force delete deliberately removes the row; the soft-delete cascade does not run. |
| 109 | + expect(fursuitDeletedAt($fursuit))->toBeNull(); |
| 110 | +}); |
| 111 | + |
| 112 | +test('backfill migration soft-deletes existing orphans and leaves healthy rows alone', function () { |
| 113 | + // Orphan A: active fursuit whose only badge was soft-deleted without cascade (legacy state). |
| 114 | + $fursuitA = Fursuit::factory()->create(); |
| 115 | + $badgeA = Badge::factory()->for($fursuitA)->create(['extra_copy_of' => null]); |
| 116 | + DB::table('badges')->where('id', $badgeA->id)->update(['deleted_at' => now()]); |
| 117 | + |
| 118 | + // Orphan B: trashed fursuit with a still-active badge (the other direction). |
| 119 | + $fursuitB = Fursuit::factory()->create(); |
| 120 | + $badgeB = Badge::factory()->for($fursuitB)->create(['extra_copy_of' => null]); |
| 121 | + DB::table('fursuits')->where('id', $fursuitB->id)->update(['deleted_at' => now()]); |
| 122 | + |
| 123 | + // Healthy: active fursuit with an active badge — must be left untouched. |
| 124 | + $healthy = Fursuit::factory()->create(); |
| 125 | + Badge::factory()->for($healthy)->create(['extra_copy_of' => null]); |
| 126 | + |
| 127 | + expect(fursuitDeletedAt($fursuitA))->toBeNull(); |
| 128 | + expect(badgeDeletedAt($badgeB))->toBeNull(); |
| 129 | + |
| 130 | + $migration = include database_path('migrations/2026_06_14_120000_soft_delete_orphaned_fursuits_and_badges.php'); |
| 131 | + $migration->up(); |
| 132 | + $migration->up(); // idempotent — running twice changes nothing further |
| 133 | + |
| 134 | + expect(fursuitDeletedAt($fursuitA))->not->toBeNull(); |
| 135 | + expect(badgeDeletedAt($badgeB))->not->toBeNull(); |
| 136 | + expect(fursuitDeletedAt($healthy))->toBeNull(); |
| 137 | +}); |
0 commit comments