Skip to content

A free open source IT asset/license management system

More tests

snipe5afd4195c687
diff --git a/tests/Feature/Accessories/AccessoryObserverOrderTest.php b/tests/Feature/Accessories/AccessoryObserverOrderTest.php
new file mode 100644
index 0000000000..2c6bd12a00
--- /dev/null
+++ b/tests/Feature/Accessories/AccessoryObserverOrderTest.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace Tests\Feature\Accessories;
+
+use App\Enums\ActionType;
+use App\Models\Accessory;
+use App\Models\Actionlog;
+use App\Models\Location;
+use App\Models\Order;
+use App\Models\OrderItem;
+use App\Models\Setting;
+use App\Models\Supplier;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * AccessoryObserver::created writes an initial Order + OrderItem for
+ * every new accessory (mirrors AssetObserver's per-asset behavior).
+ * The paired `create` action_log entry links back to that OrderItem via
+ * order_item_id so history-tab renders can walk to the acquisition
+ * context in one hop.
+ */
+class AccessoryObserverOrderTest extends TestCase
+{
+ public function test_accessory_create_writes_matching_order_and_order_item(): void
+ {
+ $actor = User::factory()->createAccessories()->create();
+ $supplier = Supplier::factory()->create();
+
+ $accessory = Accessory::factory()
+ ->withInitialAcquisition($supplier, 12.34, '2026-04-01')
+ ->create([
+ 'created_by' => $actor->id,
+ 'company_id' => $actor->company_id,
+ 'qty' => 7,
+ ]);
+
+ $line = OrderItem::where('item_type', Accessory::class)
+ ->where('item_id', $accessory->id)
+ ->firstOrFail();
+
+ $this->assertSame(7, (int) $line->qty);
+ $this->assertEquals(12.34, (float) $line->price);
+ $this->assertSame($actor->id, (int) $line->created_by);
+
+ $order = Order::findOrFail($line->order_id);
+ $this->assertSame($supplier->id, (int) $order->supplier_id);
+ $this->assertSame($actor->company_id, $order->company_id);
+ $this->assertSame('2026-04-01', $order->purchase_date->toDateString());
+ $this->assertSame($actor->id, (int) $order->created_by);
+ }
+
+ public function test_create_action_log_links_to_order_item_id(): void
+ {
+ $accessory = Accessory::factory()->create(['qty' => 3]);
+
+ $line = OrderItem::where('item_type', Accessory::class)
+ ->where('item_id', $accessory->id)
+ ->firstOrFail();
+
+ $log = Actionlog::where('item_type', Accessory::class)
+ ->where('item_id', $accessory->id)
+ ->where('action_type', ActionType::Create->value)
+ ->firstOrFail();
+
+ $this->assertSame($line->id, (int) $log->order_item_id);
+ }
+
+ public function test_order_currency_prefers_location_currency_when_set(): void
+ {
+ $location = Location::factory()->create(['currency' => 'JPY']);
+ $accessory = Accessory::factory()->for($location)->create();
+
+ $line = $accessory->orderItems()->firstOrFail();
+
+ $this->assertSame('JPY', $line->order->currency);
+ }
+
+ public function test_every_new_accessory_gets_its_own_order_and_line(): void
+ {
+ $ordersBefore = Order::count();
+ $linesBefore = OrderItem::count();
+
+ Accessory::factory()->count(3)->create();
+
+ // Each create fires the observer, which writes 1 Order + 1
+ // OrderItem. No dedupe on accessory create (order_number is
+ // null so blank-order semantic applies).
+ $this->assertSame($ordersBefore + 3, Order::count());
+ $this->assertSame($linesBefore + 3, OrderItem::count());
+ }
+}
diff --git a/tests/Feature/Accessories/Api/IndexAccessoryTest.php b/tests/Feature/Accessories/Api/IndexAccessoryTest.php
index 9c9082fdc4..afdf6e0dda 100644
--- a/tests/Feature/Accessories/Api/IndexAccessoryTest.php
+++ b/tests/Feature/Accessories/Api/IndexAccessoryTest.php
@@ -121,7 +121,7 @@ class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS
'company_id' => $targetCompany->id,
'category_id' => $targetCategory->id,
'manufacturer_id' => $targetManufacturer->id,
- 'supplier_id' => $targetSupplier->id,
+ 'default_supplier_id' => $targetSupplier->id,
'location_id' => $targetLocation->id,
'notes' => 'NOTE-A',
]);
@@ -131,7 +131,7 @@ class IndexAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS
'company_id' => $otherCompany->id,
'category_id' => $otherCategory->id,
'manufacturer_id' => $otherManufacturer->id,
- 'supplier_id' => $otherSupplier->id,
+ 'default_supplier_id' => $otherSupplier->id,
'location_id' => $otherLocation->id,
'notes' => 'NOTE-B',
]);
diff --git a/tests/Feature/Accessories/Api/StoreAccessoryTest.php b/tests/Feature/Accessories/Api/StoreAccessoryTest.php
index da85ee79b7..13589f3dc5 100644
--- a/tests/Feature/Accessories/Api/StoreAccessoryTest.php
+++ b/tests/Feature/Accessories/Api/StoreAccessoryTest.php
@@ -67,17 +67,20 @@ class StoreAccessoryTest extends TestCase implements TestsFullMultipleCompaniesS
'supplier_id' => $supplier->id,
])->assertStatusMessageIs('success');
+ // supplier_id / purchase_date / purchase_cost from the request
+ // body no longer land on the accessories row — they flow to the
+ // observer-written Order + OrderItem (see AccessoryObserverOrderTest).
+ // The parent's default_supplier_id gets seeded from supplier_id
+ // so future orders pre-populate correctly.
$this->assertDatabaseHas('accessories', [
'name' => 'My Awesome Accessory',
'qty' => 2,
- 'purchase_cost' => 100.00,
- 'purchase_date' => '2024-09-18',
'model_number' => '98765',
'category_id' => $category->id,
'company_id' => $company->id,
'location_id' => $location->id,
'manufacturer_id' => $manufacturer->id,
- 'supplier_id' => $supplier->id,
+ 'default_supplier_id' => $supplier->id,
]);
}
}
diff --git a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php
index 5a7d98fa71..336b21b532 100644
--- a/tests/Feature/Accessories/Api/UpdateAccessoryTest.php
+++ b/tests/Feature/Accessories/Api/UpdateAccessoryTest.php
@@ -101,21 +101,20 @@ class UpdateAccessoryTest extends TestCase implements TestsFullMultipleCompanies
$accessory = Accessory::factory()->create([
'name' => 'A Name to Change',
'qty' => 5,
- 'purchase_cost' => 99.99,
'model_number' => 'ABC098',
'category_id' => $categoryA->id,
'company_id' => $companyA->id,
'location_id' => $locationA->id,
'manufacturer_id' => $manufacturerA->id,
- 'supplier_id' => $supplierA->id,
+ 'default_supplier_id' => $supplierA->id,
]);
- // Payload shape preserved: qty / order_number / supplier_id are
- // all accepted on update. supplier_id writes through to the model.
- // qty change routes through adjustQuantity (asserted in the
- // dedicated test below). order_number rides on the QuantityAdjust
- // log entry created by that qty change; the parent's own
- // legacy_order_number column is not written to on update.
+ // Payload shape preserved for API back-compat. Acquisition-only
+ // fields (order_number, purchase_date, purchase_cost, and the
+ // per-Order supplier_id) don't touch the parent — post-create
+ // changes flow through Orders + OrderItems via the
+ // adjust-quantity endpoint. default_supplier_id IS editable
+ // (parent template).
$this->actingAsForApi(User::factory()->editAccessories()->create())
->patchJson(route('api.accessories.update', $accessory), [
'name' => 'A New Name',
@@ -127,19 +126,18 @@ class UpdateAccessoryTest extends TestCase implements TestsFullMultipleCompanies
'company_id' => $companyB->id,
'location_id' => $locationB->id,
'manufacturer_id' => $manufacturerB->id,
- 'supplier_id' => $supplierB->id,
+ 'default_supplier_id' => $supplierB->id,
])
->assertOk();
$accessory = $accessory->fresh();
$this->assertEquals('A New Name', $accessory->name);
+ // qty stays supported on the PATCH endpoint for API back-compat:
+ // the update controller routes qty deltas through adjustQuantity
+ // so a payload with qty=10 (original 5) writes a +5 OrderItem
+ // and moves the parent qty to 10.
$this->assertEquals(10, $accessory->qty);
- $this->assertEquals($supplierB->id, $accessory->supplier_id);
- // purchase_cost is now create-only on the parent — the PATCH
- // payload's 199.99 rides onto the OrderItem created by the
- // qty-change adjustQuantity call (asserted in the dedicated
- // qty test below) rather than overwriting the parent column.
- $this->assertEquals(99.99, $accessory->purchase_cost);
+ $this->assertEquals($supplierB->id, $accessory->default_supplier_id);
$this->assertEquals('XYZ123', $accessory->model_number);
$this->assertEquals($categoryB->id, $accessory->category_id);
$this->assertEquals($companyB->id, $accessory->company_id);
diff --git a/tests/Feature/Accessories/Ui/CreateAccessoriesTest.php b/tests/Feature/Accessories/Ui/CreateAccessoriesTest.php
index 43a7ae898f..cd7ad126af 100644
--- a/tests/Feature/Accessories/Ui/CreateAccessoriesTest.php
+++ b/tests/Feature/Accessories/Ui/CreateAccessoriesTest.php
@@ -54,10 +54,10 @@ class CreateAccessoriesTest extends TestCase
$manufacturer = Manufacturer::factory()->create();
$supplier = Supplier::factory()->create();
- // order_number was dropped from the create payload when the parent
- // column was renamed to legacy_order_number and taken out of
- // fillable. Order-number tracking lives on QuantityAdjust action_log
- // rows now.
+ // order_number, supplier_id, purchase_date, purchase_cost are
+ // valid inputs on create — they flow to the observer-written
+ // Order + OrderItem. supplier_id ALSO seeds the parent's
+ // default_supplier_id template for future orders.
$data = [
'category_id' => $category->id,
'company_id' => $company->id,
@@ -79,6 +79,18 @@ class CreateAccessoriesTest extends TestCase
->post(route('accessories.store'), array_merge($data, ['redirect_option' => 'index']))
->assertRedirect(route('accessories.index'));
- $this->assertDatabaseHas('accessories', array_merge($data, ['created_by' => $user->id]));
+ $this->assertDatabaseHas('accessories', [
+ 'category_id' => $category->id,
+ 'company_id' => $company->id,
+ 'location_id' => $location->id,
+ 'manufacturer_id' => $manufacturer->id,
+ 'min_amt' => '1',
+ 'model_number' => '12345',
+ 'name' => 'My Accessory Name',
+ 'notes' => 'Some notes here',
+ 'qty' => '3',
+ 'default_supplier_id' => $supplier->id,
+ 'created_by' => $user->id,
+ ]);
}
}
diff --git a/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php b/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php
index a137887fcc..f11555444e 100644
--- a/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php
+++ b/tests/Feature/Accessories/Ui/UpdateAccessoryTest.php
@@ -56,12 +56,10 @@ class UpdateAccessoryTest extends TestCase
'company_id' => (string) $accessory->company_id,
'name' => $accessory->name,
'category_id' => (string) $accessory->category_id,
- 'supplier_id' => (string) $accessory->supplier_id,
+ 'default_supplier_id' => (string) $accessory->default_supplier_id,
'manufacturer_id' => (string) $accessory->manufacturer_id,
'location_id' => (string) $accessory->location_id,
'model_number' => $accessory->model_number,
- 'purchase_date' => $accessory->purchase_date,
- 'purchase_cost' => $accessory->purchase_cost,
'min_amt' => $accessory->min_amt,
'notes' => $accessory->notes,
// the important part...
@@ -81,28 +79,26 @@ class UpdateAccessoryTest extends TestCase
$accessory = Accessory::factory()
->for($companyA)
->for($categoryA)
- ->for($supplierA)
->for($manufacturerA)
->for($locationA)
->create([
'min_amt' => 1,
'qty' => 5,
- 'purchase_cost' => 42.00,
+ 'default_supplier_id' => $supplierA->id,
]);
- // qty, order_number, and purchase_cost are all create-only for
- // accessories now. qty flows through the adjust-quantity modal
- // instead; order_number and purchase_cost ride on OrderItems
- // rather than the parent column. The edit form drops all three
- // silently, so a POST body carrying them shouldn't overwrite
- // the corresponding parent columns.
+ // qty, order_number, purchase_date, purchase_cost are all
+ // create-only on the parent post-Orders — the edit form drops
+ // them and post-create changes live on Order + OrderItem rows.
+ // default_supplier_id IS editable (parent "typical supplier"
+ // template that seeds new orders).
$this->actingAs(User::factory()->editAccessories()->create())
->put(route('accessories.update', $accessory), [
'redirect_option' => 'index',
'company_id' => (string) $companyB->id,
'name' => 'Changed Name',
'category_id' => (string) $categoryB->id,
- 'supplier_id' => (string) $supplierB->id,
+ 'default_supplier_id' => (string) $supplierB->id,
'manufacturer_id' => (string) $manufacturerB->id,
'location_id' => (string) $locationB->id,
'model_number' => 'changed 1234',
@@ -118,13 +114,11 @@ class UpdateAccessoryTest extends TestCase
'company_id' => $companyB->id,
'name' => 'Changed Name',
'category_id' => $categoryB->id,
- 'supplier_id' => $supplierB->id,
+ 'default_supplier_id' => $supplierB->id,
'manufacturer_id' => $manufacturerB->id,
'location_id' => $locationB->id,
'model_number' => 'changed 1234',
- 'purchase_date' => '2024-10-11',
- 'purchase_cost' => '42.00', // unchanged from create; edit ignores purchase_cost
- 'qty' => '5', // unchanged from create; edit ignores qty
+ 'qty' => '5', // unchanged; create-only
'min_amt' => '10',
'notes' => 'A new note',
]);
diff --git a/tests/Feature/Components/Api/ComponentIndexTest.php b/tests/Feature/Components/Api/ComponentIndexTest.php
index 46e58c70dd..f428eddac5 100644
--- a/tests/Feature/Components/Api/ComponentIndexTest.php
+++ b/tests/Feature/Components/Api/ComponentIndexTest.php
@@ -78,7 +78,7 @@ class ComponentIndexTest extends TestCase
'name' => 'Target Component',
'company_id' => $targetCompany->id,
'category_id' => $targetCategory->id,
- 'supplier_id' => $targetSupplier->id,
+ 'default_supplier_id' => $targetSupplier->id,
'manufacturer_id' => $targetManufacturer->id,
'model_number' => 'COMP-MODEL-A',
'location_id' => $targetLocation->id,
@@ -89,7 +89,7 @@ class ComponentIndexTest extends TestCase
'name' => 'Other Component',
'company_id' => $otherCompany->id,
'category_id' => $otherCategory->id,
- 'supplier_id' => $otherSupplier->id,
+ 'default_supplier_id' => $otherSupplier->id,
'manufacturer_id' => $otherManufacturer->id,
'model_number' => 'COMP-MODEL-B',
'location_id' => $otherLocation->id,
diff --git a/tests/Feature/Components/Api/ComponentUpdateTest.php b/tests/Feature/Components/Api/ComponentUpdateTest.php
index 5885906e23..d0b5ca70f3 100644
--- a/tests/Feature/Components/Api/ComponentUpdateTest.php
+++ b/tests/Feature/Components/Api/ComponentUpdateTest.php
@@ -32,16 +32,21 @@ class ComponentUpdateTest extends TestCase
$this->assertNotEmpty($log->note);
}
- public function test_supplier_id_is_editable_via_api()
+ public function test_default_supplier_id_is_editable_on_update()
{
- $component = Component::factory()->create();
+ // default_supplier_id on the parent is the "typical supplier"
+ // template that seeds new Orders. It stays editable after create;
+ // per-acquisition supplier lives on Order.supplier_id and is
+ // unaffected by this write.
+ $original = Supplier::factory()->create();
+ $component = Component::factory()->create(['default_supplier_id' => $original->id]);
$newSupplier = Supplier::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
- ->patchJson(route('api.components.update', $component), ['supplier_id' => $newSupplier->id])
+ ->patchJson(route('api.components.update', $component), ['default_supplier_id' => $newSupplier->id])
->assertOk();
- $this->assertSame($newSupplier->id, (int) $component->fresh()->supplier_id);
+ $this->assertSame($newSupplier->id, (int) $component->fresh()->default_supplier_id);
}
public function test_qty_only_change_via_api_creates_no_update_log_entry()
diff --git a/tests/Feature/Components/ComponentObserverOrderTest.php b/tests/Feature/Components/ComponentObserverOrderTest.php
new file mode 100644
index 0000000000..6b108a1bae
--- /dev/null
+++ b/tests/Feature/Components/ComponentObserverOrderTest.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Tests\Feature\Components;
+
+use App\Enums\ActionType;
+use App\Models\Actionlog;
+use App\Models\Component;
+use App\Models\Location;
+use App\Models\Order;
+use App\Models\OrderItem;
+use App\Models\Setting;
+use App\Models\Supplier;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * ComponentObserver::created writes an initial Order + OrderItem for
+ * every new component. The paired `create` action_log entry links back
+ * to that OrderItem via order_item_id.
+ */
+class ComponentObserverOrderTest extends TestCase
+{
+ public function test_component_create_writes_matching_order_and_order_item(): void
+ {
+ $actor = User::factory()->superuser()->create();
+ $supplier = Supplier::factory()->create();
+
+ $component = Component::factory()
+ ->withInitialAcquisition($supplier, 89.00, '2026-06-01')
+ ->create([
+ 'created_by' => $actor->id,
+ 'company_id' => $actor->company_id,
+ 'qty' => 12,
+ ]);
+
+ $line = OrderItem::where('item_type', Component::class)
+ ->where('item_id', $component->id)
+ ->firstOrFail();
+
+ $this->assertSame(12, (int) $line->qty);
+ $this->assertEquals(89.00, (float) $line->price);
+ $this->assertSame($actor->id, (int) $line->created_by);
+
+ $order = Order::findOrFail($line->order_id);
+ $this->assertSame($supplier->id, (int) $order->supplier_id);
+ $this->assertSame($actor->company_id, $order->company_id);
+ $this->assertSame('2026-06-01', $order->purchase_date->toDateString());
+ $this->assertSame($actor->id, (int) $order->created_by);
+ }
+
+ public function test_create_action_log_links_to_order_item_id(): void
+ {
+ $component = Component::factory()->create(['qty' => 4]);
+
+ $line = OrderItem::where('item_type', Component::class)
+ ->where('item_id', $component->id)
+ ->firstOrFail();
+
+ $log = Actionlog::where('item_type', Component::class)
+ ->where('item_id', $component->id)
+ ->where('action_type', ActionType::Create->value)
+ ->firstOrFail();
+
+ $this->assertSame($line->id, (int) $log->order_item_id);
+ }
+
+}
diff --git a/tests/Feature/Consumables/Api/ConsumableIndexTest.php b/tests/Feature/Consumables/Api/ConsumableIndexTest.php
index f4e24802e9..4d85ada3ae 100644
--- a/tests/Feature/Consumables/Api/ConsumableIndexTest.php
+++ b/tests/Feature/Consumables/Api/ConsumableIndexTest.php
@@ -105,7 +105,7 @@ class ConsumableIndexTest extends TestCase
'category_id' => $targetCategory->id,
'model_number' => 'CONS-MODEL-A',
'manufacturer_id' => $targetManufacturer->id,
- 'supplier_id' => $targetSupplier->id,
+ 'default_supplier_id' => $targetSupplier->id,
'location_id' => $targetLocation->id,
'notes' => 'CONS-NOTES-A',
]);
@@ -116,7 +116,7 @@ class ConsumableIndexTest extends TestCase
'category_id' => $otherCategory->id,
'model_number' => 'CONS-MODEL-B',
'manufacturer_id' => $otherManufacturer->id,
- 'supplier_id' => $otherSupplier->id,
+ 'default_supplier_id' => $otherSupplier->id,
'location_id' => $otherLocation->id,
'notes' => 'CONS-NOTES-B',
]);
diff --git a/tests/Feature/Consumables/Api/ConsumableUpdateTest.php b/tests/Feature/Consumables/Api/ConsumableUpdateTest.php
index 5256d2ebac..118a0f31bb 100644
--- a/tests/Feature/Consumables/Api/ConsumableUpdateTest.php
+++ b/tests/Feature/Consumables/Api/ConsumableUpdateTest.php
@@ -70,15 +70,18 @@ class ConsumableUpdateTest extends TestCase
$this->assertNotEmpty($log->note);
}
- public function test_supplier_id_is_editable_via_api()
+ public function test_default_supplier_id_is_editable_on_update()
{
- $consumable = Consumable::factory()->create();
+ // default_supplier_id on the parent is the "typical supplier"
+ // template — see ComponentUpdateTest for the full rationale.
+ $original = Supplier::factory()->create();
+ $consumable = Consumable::factory()->create(['default_supplier_id' => $original->id]);
$newSupplier = Supplier::factory()->create();
$this->actingAsForApi(User::factory()->superuser()->create())
- ->patchJson(route('api.consumables.update', $consumable), ['supplier_id' => $newSupplier->id])
+ ->patchJson(route('api.consumables.update', $consumable), ['default_supplier_id' => $newSupplier->id])
->assertOk();
- $this->assertSame($newSupplier->id, (int) $consumable->fresh()->supplier_id);
+ $this->assertSame($newSupplier->id, (int) $consumable->fresh()->default_supplier_id);
}
}
diff --git a/tests/Feature/Consumables/ConsumableObserverOrderTest.php b/tests/Feature/Consumables/ConsumableObserverOrderTest.php
new file mode 100644
index 0000000000..47af607958
--- /dev/null
+++ b/tests/Feature/Consumables/ConsumableObserverOrderTest.php
@@ -0,0 +1,76 @@
+<?php
+
+namespace Tests\Feature\Consumables;
+
+use App\Enums\ActionType;
+use App\Models\Actionlog;
+use App\Models\Consumable;
+use App\Models\Location;
+use App\Models\Order;
+use App\Models\OrderItem;
+use App\Models\Setting;
+use App\Models\Supplier;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * ConsumableObserver::created writes an initial Order + OrderItem for
+ * every new consumable. The paired `create` action_log entry links
+ * back to that OrderItem via order_item_id.
+ */
+class ConsumableObserverOrderTest extends TestCase
+{
+ public function test_consumable_create_writes_matching_order_and_order_item(): void
+ {
+ $actor = User::factory()->createConsumables()->create();
+ $supplier = Supplier::factory()->create();
+
+ $consumable = Consumable::factory()
+ ->withInitialAcquisition($supplier, 5.50, '2026-05-01')
+ ->create([
+ 'created_by' => $actor->id,
+ 'company_id' => $actor->company_id,
+ 'qty' => 100,
+ ]);
+
+ $line = OrderItem::where('item_type', Consumable::class)
+ ->where('item_id', $consumable->id)
+ ->firstOrFail();
+
+ $this->assertSame(100, (int) $line->qty);
+ $this->assertEquals(5.50, (float) $line->price);
+ $this->assertSame($actor->id, (int) $line->created_by);
+
+ $order = Order::findOrFail($line->order_id);
+ $this->assertSame($supplier->id, (int) $order->supplier_id);
+ $this->assertSame($actor->company_id, $order->company_id);
+ $this->assertSame('2026-05-01', $order->purchase_date->toDateString());
+ $this->assertSame($actor->id, (int) $order->created_by);
+ }
+
+ public function test_create_action_log_links_to_order_item_id(): void
+ {
+ $consumable = Consumable::factory()->create(['qty' => 20]);
+
+ $line = OrderItem::where('item_type', Consumable::class)
+ ->where('item_id', $consumable->id)
+ ->firstOrFail();
+
+ $log = Actionlog::where('item_type', Consumable::class)
+ ->where('item_id', $consumable->id)
+ ->where('action_type', ActionType::Create->value)
+ ->firstOrFail();
+
+ $this->assertSame($line->id, (int) $log->order_item_id);
+ }
+
+ public function test_order_currency_prefers_location_currency_when_set(): void
+ {
+ $location = Location::factory()->create(['currency' => 'EUR']);
+ $consumable = Consumable::factory()->for($location)->create();
+
+ $line = $consumable->orderItems()->firstOrFail();
+
+ $this->assertSame('EUR', $line->order->currency);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/CreateConsumableTest.php b/tests/Feature/Consumables/Ui/CreateConsumableTest.php
index 3e3517b3aa..90a0243622 100644
--- a/tests/Feature/Consumables/Ui/CreateConsumableTest.php
+++ b/tests/Feature/Consumables/Ui/CreateConsumableTest.php
@@ -30,15 +30,17 @@ class CreateConsumableTest extends TestCase implements TestsPermissionsRequireme
public function test_can_create_consumable()
{
- // order_number was dropped from the create payload when the parent
- // column was renamed to legacy_order_number and taken out of
- // fillable. Order-number tracking lives on QuantityAdjust action_log
- // rows now.
+ // supplier_id / purchase_date / purchase_cost / order_number in
+ // the request body flow to the observer-written Order + OrderItem
+ // (see ConsumableObserverOrderTest). The parent's
+ // default_supplier_id gets seeded from supplier_id so future
+ // orders pre-populate correctly.
+ $supplier = Supplier::factory()->create();
$data = [
'company_id' => Company::factory()->create()->id,
'name' => 'My Consumable',
'category_id' => Category::factory()->consumableInkCategory()->create()->id,
- 'supplier_id' => Supplier::factory()->create()->id,
+ 'supplier_id' => $supplier->id,
'manufacturer_id' => Manufacturer::factory()->create()->id,
'location_id' => Location::factory()->create()->id,
'model_number' => '1234',
@@ -57,7 +59,19 @@ class CreateConsumableTest extends TestCase implements TestsPermissionsRequireme
])
->assertRedirect(route('consumables.index'));
- $this->assertDatabaseHas('consumables', $data);
+ $this->assertDatabaseHas('consumables', [
+ 'company_id' => $data['company_id'],
+ 'name' => 'My Consumable',
+ 'category_id' => $data['category_id'],
+ 'manufacturer_id' => $data['manufacturer_id'],
+ 'location_id' => $data['location_id'],
+ 'model_number' => '1234',
+ 'item_no' => '5678',
+ 'qty' => '10',
+ 'min_amt' => '1',
+ 'notes' => 'Some Notes',
+ 'default_supplier_id' => $supplier->id,
+ ]);
}
public function test_page_renders()
diff --git a/tests/Feature/Consumables/Ui/UpdateConsumableTest.php b/tests/Feature/Consumables/Ui/UpdateConsumableTest.php
index a5e02d4bdf..881c63093c 100644
--- a/tests/Feature/Consumables/Ui/UpdateConsumableTest.php
+++ b/tests/Feature/Consumables/Ui/UpdateConsumableTest.php
@@ -59,16 +59,18 @@ class UpdateConsumableTest extends TestCase
public function test_can_update_consumable()
{
- $consumable = Consumable::factory()->create(['purchase_cost' => 42.00]);
+ $originalSupplier = Supplier::factory()->create();
+ $consumable = Consumable::factory()->create([
+ 'default_supplier_id' => $originalSupplier->id,
+ ]);
$originalQty = (int) $consumable->qty;
- $originalPurchaseCost = $consumable->purchase_cost;
$newSupplier = Supplier::factory()->create();
- // qty, order_number, and purchase_cost are all create-only on
- // the parent now. qty routes through adjust-quantity;
- // order_number and purchase_cost ride on OrderItems. The edit
- // form drops all three silently, so a POST body carrying them
- // shouldn't overwrite the corresponding parent columns.
+ // qty, order_number, purchase_date, purchase_cost are create-only
+ // on the parent post-Orders. The edit form drops them and
+ // post-create changes live on Order + OrderItem rows via the
+ // adjust-quantity flow. default_supplier_id IS editable — it's
+ // the parent's "typical supplier" template.
$editable = [
'company_id' => Company::factory()->create()->id,
'name' => 'My Consumable',
@@ -77,9 +79,9 @@ class UpdateConsumableTest extends TestCase
'location_id' => Location::factory()->create()->id,
'model_number' => '8765',
'item_no' => '5678',
- 'purchase_date' => '2024-12-05',
'min_amt' => '7',
'notes' => 'Some Notes',
+ 'default_supplier_id' => $newSupplier->id,
];
$this->actingAs(User::factory()->createConsumables()->editConsumables()->create())
@@ -88,15 +90,13 @@ class UpdateConsumableTest extends TestCase
'category_type' => 'consumable',
'order_number' => 'ignored-908',
'qty' => '9999',
- 'purchase_cost' => '89.45', // dropped — see comment above
- 'supplier_id' => $newSupplier->id,
+ 'purchase_cost' => '89.45',
+ 'purchase_date' => '2024-12-05',
])
->assertRedirect(route('consumables.index'));
$this->assertDatabaseHas('consumables', $editable + [
'qty' => $originalQty,
- 'purchase_cost' => $originalPurchaseCost,
- 'supplier_id' => $newSupplier->id,
]);
}
}
diff --git a/tests/Unit/AssetTest.php b/tests/Unit/AssetTest.php
index 05c5c99df7..77ae6fda6f 100644
--- a/tests/Unit/AssetTest.php
+++ b/tests/Unit/AssetTest.php
@@ -354,8 +354,11 @@ class AssetTest extends TestCase
$asset = Asset::factory()->create();
$otherAsset = Asset::factory()->create();
- $primaryAccessory = Accessory::factory()->create(['purchase_cost' => 10]);
- $secondaryAccessory = Accessory::factory()->create(['purchase_cost' => 15]);
+ // See test_asset_components_calculated_total_uses_assigned_quantity
+ // for why per-unit cost must be seeded via withInitialAcquisition
+ // rather than a create-array override.
+ $primaryAccessory = Accessory::factory()->withInitialAcquisition(null, 10.0)->create();
+ $secondaryAccessory = Accessory::factory()->withInitialAcquisition(null, 15.0)->create();
AccessoryCheckout::factory()->create([
'accessory_id' => $primaryAccessory->id,
@@ -386,8 +389,12 @@ class AssetTest extends TestCase
{
$asset = Asset::factory()->create();
- $firstComponent = Component::factory()->create(['purchase_cost' => 10]);
- $secondComponent = Component::factory()->create(['purchase_cost' => 25]);
+ // Per-unit cost lives on the initial OrderItem the observer
+ // writes; withInitialAcquisition seeds that OrderItem's price so
+ // calculatedPurchaseCost (via lastOrderDefaults) reads the
+ // expected value back for this test.
+ $firstComponent = Component::factory()->withInitialAcquisition(null, 10.0)->create();
+ $secondComponent = Component::factory()->withInitialAcquisition(null, 25.0)->create();
$asset->components()->attach($firstComponent->id, [
'assigned_qty' => 3,
@@ -403,7 +410,6 @@ class AssetTest extends TestCase
$freshAsset = $asset->fresh();
- $this->assertEquals(35, $freshAsset->components->sum('purchase_cost'));
$this->assertEquals(80, $freshAsset->components->sum('calculated_purchase_cost'));
$this->assertSame(80.0, $freshAsset->getComponentCost());
}
@@ -412,7 +418,12 @@ class AssetTest extends TestCase
{
$asset = Asset::factory()->create();
- $componentWithoutCost = Component::factory()->create(['purchase_cost' => null]);
+ // Force the observer-written OrderItem's price to null AND null
+ // out the parent's default_purchase_cost so lastOrderDefaults
+ // returns unit_cost=null; calculatedPurchaseCost coerces to 0.0
+ // when assigned to an asset.
+ $componentWithoutCost = Component::factory()->create(['default_purchase_cost' => null]);
+ $componentWithoutCost->orderItems()->update(['price' => null]);
$asset->components()->attach($componentWithoutCost->id, [
'assigned_qty' => 4,