Skip to content

A free open source IT asset/license management system

Acceptance: Backfill tests for existing behavior

Marcus Moored601f3a694de
diff --git a/database/factories/AccessoryFactory.php b/database/factories/AccessoryFactory.php
index 0e44fcedcf..b018e334a1 100644
--- a/database/factories/AccessoryFactory.php
+++ b/database/factories/AccessoryFactory.php
@@ -144,6 +144,13 @@ class AccessoryFactory extends Factory
});
}
+ public function notRequiringAcceptance()
+ {
+ return $this->afterCreating(function ($accessory) {
+ $accessory->category->update(['require_acceptance' => 0]);
+ });
+ }
+
public function checkedOutToUser(?User $user = null)
{
return $this->afterCreating(function (Accessory $accessory) use ($user) {
diff --git a/database/factories/ConsumableFactory.php b/database/factories/ConsumableFactory.php
index a8787e8c85..b5287eb9ad 100644
--- a/database/factories/ConsumableFactory.php
+++ b/database/factories/ConsumableFactory.php
@@ -118,6 +118,13 @@ class ConsumableFactory extends Factory
});
}
+ public function notRequiringAcceptance()
+ {
+ return $this->afterCreating(function (Consumable $consumable) {
+ $consumable->category->update(['require_acceptance' => 0]);
+ });
+ }
+
public function checkedOutToUser(?User $user = null)
{
return $this->afterCreating(function (Consumable $consumable) use ($user) {
diff --git a/tests/Feature/Checkouts/General/SettingAlertOnResponseTest.php b/tests/Feature/Checkouts/General/SettingAlertOnResponseTest.php
index bfa30f7c0d..50dcbbccb9 100644
--- a/tests/Feature/Checkouts/General/SettingAlertOnResponseTest.php
+++ b/tests/Feature/Checkouts/General/SettingAlertOnResponseTest.php
@@ -4,6 +4,7 @@ namespace Tests\Feature\Checkouts\General;
use App\Models\Accessory;
use App\Models\Asset;
+use App\Models\Consumable;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\Statuslabel;
@@ -39,6 +40,7 @@ class SettingAlertOnResponseTest extends TestCase
'checkoutable_id' => $accessory->id,
'assigned_to_id' => $this->assignedUser->id,
'alert_on_response_id' => $this->actor->id,
+ 'qty' => 1,
]);
}
@@ -75,6 +77,7 @@ class SettingAlertOnResponseTest extends TestCase
'checkoutable_id' => $asset->id,
'assigned_to_id' => $this->assignedUser->id,
'alert_on_response_id' => $this->actor->id,
+ 'qty' => 1,
]);
}
@@ -111,6 +114,7 @@ class SettingAlertOnResponseTest extends TestCase
'checkoutable_type' => LicenseSeat::class,
'assigned_to_id' => $this->assignedUser->id,
'alert_on_response_id' => $this->actor->id,
+ 'qty' => 1,
]);
}
@@ -132,6 +136,42 @@ class SettingAlertOnResponseTest extends TestCase
]);
}
+ public function test_sets_alert_on_response_if_enabled_by_category_for_consumable()
+ {
+ $consumable = Consumable::factory()->create(['qty' => 5]);
+ $consumable->category->update([
+ 'require_acceptance' => true,
+ 'alert_on_response' => true,
+ ]);
+
+ $this->postConsumableCheckout($consumable);
+
+ $this->assertDatabaseHas('checkout_acceptances', [
+ 'checkoutable_type' => Consumable::class,
+ 'checkoutable_id' => $consumable->id,
+ 'assigned_to_id' => $this->assignedUser->id,
+ 'alert_on_response_id' => $this->actor->id,
+ ]);
+ }
+
+ public function test_does_not_set_alert_on_response_if_disabled_by_category_for_consumable()
+ {
+ $consumable = Consumable::factory()->create(['qty' => 5]);
+ $consumable->category->update([
+ 'require_acceptance' => true,
+ 'alert_on_response' => false,
+ ]);
+
+ $this->postConsumableCheckout($consumable);
+
+ $this->assertDatabaseHas('checkout_acceptances', [
+ 'checkoutable_type' => Consumable::class,
+ 'checkoutable_id' => $consumable->id,
+ 'assigned_to_id' => $this->assignedUser->id,
+ 'alert_on_response_id' => null,
+ ]);
+ }
+
private function postAssetCheckout(Asset $asset): void
{
$this->actingAs($this->actor)
@@ -161,4 +201,14 @@ class SettingAlertOnResponseTest extends TestCase
'assigned_to' => $this->assignedUser->id,
]);
}
+
+ private function postConsumableCheckout(Consumable $consumable): void
+ {
+ $this->actingAs($this->actor)
+ ->post(route('consumables.checkout.store', $consumable), [
+ 'checkout_to_type' => 'user',
+ 'assigned_to' => $this->assignedUser->id,
+ 'checkout_qty' => 1,
+ ]);
+ }
}
diff --git a/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php b/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php
index e1515bad42..1515c12f8f 100644
--- a/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/AccessoryCheckoutTest.php
@@ -294,6 +294,38 @@ class AccessoryCheckoutTest extends TestCase
$this->assertNotNull($acceptance);
$this->assertEquals(2, $acceptance->qty);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
+
+ $response->assertStatus(302)
+ ->assertRedirect(route('account.accept.item', $acceptance));
+ }
+
+ public function test_accessory_sign_in_place_creates_acceptance_when_acceptance_not_required()
+ {
+ $targetUser = User::factory()->create();
+ $accessory = Accessory::factory()->notRequiringAcceptance()->create(['qty' => 5]);
+
+ $response = $this->actingAs(User::factory()->admin()->create())
+ ->from(route('accessories.checkout.show', $accessory))
+ ->post(route('accessories.checkout.store', $accessory), [
+ 'assigned_user' => $targetUser->id,
+ 'checkout_to_type' => 'user',
+ 'redirect_option' => 'index',
+ 'checkout_qty' => 2,
+ 'sign_in_place' => 1,
+ ]);
+
+ $acceptance = CheckoutAcceptance::query()
+ ->where('checkoutable_type', Accessory::class)
+ ->where('checkoutable_id', $accessory->id)
+ ->where('assigned_to_id', $targetUser->id)
+ ->pending()
+ ->latest()
+ ->first();
+
+ $this->assertNotNull($acceptance);
+ $this->assertEquals(2, $acceptance->qty);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
$response->assertStatus(302)
->assertRedirect(route('account.accept.item', $acceptance));
diff --git a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
index 976684d789..4e22ce0cd5 100644
--- a/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/AssetCheckoutTest.php
@@ -491,6 +491,8 @@ class AssetCheckoutTest extends TestCase
->first();
$this->assertNotNull($acceptance);
+ $this->assertNull($acceptance->qty);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
$response->assertStatus(302)
->assertRedirect(route('account.accept.item', $acceptance));
diff --git a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php
index e403adc12e..03cc76b95f 100644
--- a/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/ConsumableCheckoutTest.php
@@ -198,6 +198,37 @@ class ConsumableCheckoutTest extends TestCase
$this->assertNotNull($acceptance);
$this->assertEquals(2, $acceptance->qty);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
+
+ $response->assertStatus(302)
+ ->assertRedirect(route('account.accept.item', $acceptance));
+ }
+
+ public function test_consumable_sign_in_place_creates_acceptance_when_acceptance_not_required()
+ {
+ $targetUser = User::factory()->create();
+ $consumable = Consumable::factory()->notRequiringAcceptance()->create(['qty' => 5]);
+
+ $response = $this->actingAs(User::factory()->admin()->create())
+ ->from(route('consumables.checkout.show', $consumable))
+ ->post(route('consumables.checkout.store', $consumable), [
+ 'assigned_to' => $targetUser->id,
+ 'redirect_option' => 'index',
+ 'checkout_qty' => 2,
+ 'sign_in_place' => 1,
+ ]);
+
+ $acceptance = CheckoutAcceptance::query()
+ ->where('checkoutable_type', Consumable::class)
+ ->where('checkoutable_id', $consumable->id)
+ ->where('assigned_to_id', $targetUser->id)
+ ->pending()
+ ->latest()
+ ->first();
+
+ $this->assertNotNull($acceptance);
+ $this->assertEquals(2, $acceptance->qty);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
$response->assertStatus(302)
->assertRedirect(route('account.accept.item', $acceptance));
diff --git a/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php b/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php
index bec80d2a27..ba81e6e30e 100644
--- a/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php
+++ b/tests/Feature/Checkouts/Ui/LicenseCheckoutTest.php
@@ -147,6 +147,36 @@ class LicenseCheckoutTest extends TestCase
->first();
$this->assertNotNull($acceptance);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
+
+ $response->assertStatus(302)
+ ->assertRedirect(route('account.accept.item', $acceptance));
+ }
+
+ public function test_license_sign_in_place_creates_acceptance_when_acceptance_not_required()
+ {
+ $targetUser = User::factory()->create();
+ $seat = LicenseSeat::factory()->create();
+
+ $response = $this->actingAs(User::factory()->admin()->create())
+ ->from(route('licenses.checkout', $seat->license))
+ ->post(route('licenses.checkout', $seat->license), [
+ 'assigned_to' => $targetUser->id,
+ 'redirect_option' => 'index',
+ 'sign_in_place' => 1,
+ ]);
+
+ $acceptance = CheckoutAcceptance::query()
+ ->where('checkoutable_type', LicenseSeat::class)
+ ->where('assigned_to_id', $targetUser->id)
+ ->pending()
+ ->latest()
+ ->first();
+
+ $this->assertNotNull($acceptance);
+ // The fallback does not set qty, so it stays null (the column default).
+ $this->assertNull($acceptance->qty);
+ $this->assertDatabaseCount('checkout_acceptances', 1);
$response->assertStatus(302)
->assertRedirect(route('account.accept.item', $acceptance));