Skip to content

A free open source IT asset/license management system

Handle pending acceptances when consumable deleted

Marcus Moore5373b81298e0
diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php
index 6dca3d60fd..8ddc8b51c5 100644
--- a/app/Observers/ConsumableObserver.php
+++ b/app/Observers/ConsumableObserver.php
@@ -3,6 +3,7 @@
namespace App\Observers;
use App\Models\Actionlog;
+use App\Models\CheckoutAcceptance;
use App\Models\Consumable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -58,6 +59,12 @@ class ConsumableObserver
{
$consumable->users()->detach();
+
+ CheckoutAcceptance::pending()
+ ->where('checkoutable_type', Consumable::class)
+ ->where('checkoutable_id', $consumable->id)
+ ->delete();
+
$uploads = $consumable->uploads;
foreach ($uploads as $file) {
diff --git a/tests/Unit/Models/Consumable/ConsumableDeletionTest.php b/tests/Unit/Models/Consumable/ConsumableDeletionTest.php
new file mode 100644
index 0000000000..9cd94738e0
--- /dev/null
+++ b/tests/Unit/Models/Consumable/ConsumableDeletionTest.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Tests\Unit\Models\Consumable;
+
+use App\Models\CheckoutAcceptance;
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+class ConsumableDeletionTest extends TestCase
+{
+ public function test_deleting_a_consumable_clears_its_pending_acceptances()
+ {
+ $user = User::factory()->create();
+ $consumable = Consumable::factory()->requiringAcceptance()->checkedOutToUser($user)->create();
+
+ $acceptance = $this->pendingAcceptanceFor($consumable, $user);
+
+ $consumable->delete();
+
+ $this->assertSoftDeleted($consumable);
+ $this->assertAcceptanceWasSoftDeleted($acceptance);
+ }
+
+ public function test_deleting_a_consumable_leaves_other_consumables_acceptances_alone()
+ {
+ $user = User::factory()->create();
+
+ $deleted = Consumable::factory()->requiringAcceptance()->checkedOutToUser($user)->create();
+ $untouched = Consumable::factory()->requiringAcceptance()->checkedOutToUser($user)->create();
+
+ $deletedAcceptance = $this->pendingAcceptanceFor($deleted, $user);
+ $untouchedAcceptance = $this->pendingAcceptanceFor($untouched, $user);
+
+ $deleted->delete();
+
+ $this->assertAcceptanceWasSoftDeleted($deletedAcceptance);
+ $this->assertAcceptanceSurvived($untouchedAcceptance);
+ }
+
+ public function test_deleting_a_consumable_leaves_answered_acceptances_alone()
+ {
+ $user = User::factory()->create();
+ $consumable = Consumable::factory()->requiringAcceptance()->checkedOutToUser($user)->create();
+
+ $accepted = CheckoutAcceptance::factory()
+ ->withoutActionLog()
+ ->accepted()
+ ->for($user, 'assignedTo')
+ ->for($consumable, 'checkoutable')
+ ->create();
+
+ $declined = CheckoutAcceptance::factory()
+ ->withoutActionLog()
+ ->declined()
+ ->for($user, 'assignedTo')
+ ->for($consumable, 'checkoutable')
+ ->create();
+
+ $consumable->delete();
+
+ $this->assertAcceptanceSurvived($accepted);
+ $this->assertAcceptanceSurvived($declined);
+ }
+
+ private function pendingAcceptanceFor(Consumable $consumable, User $user): CheckoutAcceptance
+ {
+ return CheckoutAcceptance::factory()
+ ->withoutActionLog()
+ ->pending()
+ ->for($user, 'assignedTo')
+ ->for($consumable, 'checkoutable')
+ ->create();
+ }
+
+ private function assertAcceptanceWasSoftDeleted(CheckoutAcceptance $acceptance): void
+ {
+ $this->assertNotNull(
+ CheckoutAcceptance::withTrashed()->find($acceptance->id)?->deleted_at,
+ 'Expected the pending acceptance to be soft-deleted when the consumable was deleted.'
+ );
+ }
+
+ private function assertAcceptanceSurvived(CheckoutAcceptance $acceptance): void
+ {
+ $this->assertNull(
+ CheckoutAcceptance::withTrashed()->find($acceptance->id)?->deleted_at,
+ 'Expected this acceptance to be left alone when the consumable was deleted.'
+ );
+ }
+}