A free open source IT asset/license management system
Added clone tests
snipe25b56e1a520c
diff --git a/tests/Feature/Accessories/Ui/CloneAccessoryTest.php b/tests/Feature/Accessories/Ui/CloneAccessoryTest.phpnew file mode 100644index 0000000000..f83e41f34a--- /dev/null+++ b/tests/Feature/Accessories/Ui/CloneAccessoryTest.php@@ -0,0 +1,81 @@+<?php++namespace Tests\Feature\Accessories\Ui;++use App\Models\Accessory;+use App\Models\Supplier;+use App\Models\User;+use Tests\TestCase;++/**+ * The Orders refactor moved supplier / purchase_date / purchase_cost /+ * order_number off the Accessory parent row and onto Order+OrderItem.+ * Plain `clone $accessory` no longer carries those fields to the create+ * form, so operators using the clone-to-restock workflow lost prefill+ * on four fields they were relying on. getClone() reads back the+ * source item's most recent order (via HasOrders::lastOrderPrefill)+ * and sets those values onto the cloned in-memory model so the create+ * form pre-populates them again.+ */+class CloneAccessoryTest extends TestCase+{+ public function test_permission_required_to_clone_accessory(): void+ {+ $accessory = Accessory::factory()->create();++ $this->actingAs(User::factory()->create())+ ->get(route('clone/accessories', $accessory))+ ->assertForbidden();+ }++ public function test_clone_page_renders(): void+ {+ $accessory = Accessory::factory()->create();++ $this->actingAs(User::factory()->createAccessories()->create())+ ->get(route('clone/accessories', $accessory))+ ->assertOk();+ }++ public function test_clone_prefills_supplier_and_order_context_from_last_order(): void+ {+ $supplier = Supplier::factory()->create();+ $source = Accessory::factory()+ ->withInitialAcquisition($supplier, 42.50, '2026-04-15')+ ->create(['qty' => 10]);++ $source->orderItems()->latest('id')->first()->order()->update([+ 'order_number' => 'PO-CLONE-PREFILL-42',+ ]);++ $response = $this->actingAs(User::factory()->createAccessories()->create())+ ->get(route('clone/accessories', $source))+ ->assertOk();++ $response->assertSee('name="supplier_id"', false);+ $response->assertSee('value="'.$supplier->id.'"', false);+ $response->assertSee('name="order_number"', false);+ $response->assertSee('value="PO-CLONE-PREFILL-42"', false);+ $response->assertSee('name="purchase_date"', false);+ $response->assertSee('value="2026-04-15"', false);+ $response->assertSee('name="purchase_cost"', false);+ $response->assertSee('value="42.50"', false);+ }++ public function test_clone_leaves_prefill_fields_blank_when_source_has_no_order_history(): void+ {+ $source = Accessory::factory()->create(['qty' => 0]);++ $this->assertNull(+ $source->orderItems()->latest('id')->first(),+ 'Expected qty=0 accessory to have no observer-written order (baseline for the no-history case).',+ );++ $response = $this->actingAs(User::factory()->createAccessories()->create())+ ->get(route('clone/accessories', $source))+ ->assertOk();++ $response->assertSee('name="order_number"', false);+ $response->assertDontSee('value="PO-', false);+ }+}diff --git a/tests/Feature/Components/Ui/CloneComponentTest.php b/tests/Feature/Components/Ui/CloneComponentTest.phpindex dca01f7ad3..198b967a3f 100644--- a/tests/Feature/Components/Ui/CloneComponentTest.php+++ b/tests/Feature/Components/Ui/CloneComponentTest.php@@ -3,6 +3,7 @@namespace Tests\Feature\Components\Ui;use App\Models\Component;+use App\Models\Supplier;use App\Models\User;use Tests\TestCase;@@ -34,4 +35,33 @@ class CloneComponentTest extends TestCase'Component to clone',], false);}++ /**+ * See tests/Feature/Accessories/Ui/CloneAccessoryTest for the+ * Orders-refactor rationale behind the clone-prefill fix.+ */+ public function test_clone_prefills_supplier_and_order_context_from_last_order(): void+ {+ $supplier = Supplier::factory()->create();+ $source = Component::factory()+ ->withInitialAcquisition($supplier, 12.34, '2026-04-01')+ ->create(['qty' => 5]);++ $source->orderItems()->latest('id')->first()->order()->update([+ 'order_number' => 'PO-COMP-CLONE-9',+ ]);++ $response = $this->actingAs(User::factory()->createComponents()->create())+ ->get(route('components.clone.create', $source))+ ->assertOk();++ $response->assertSee('name="supplier_id"', false);+ $response->assertSee('value="'.$supplier->id.'"', false);+ $response->assertSee('name="order_number"', false);+ $response->assertSee('value="PO-COMP-CLONE-9"', false);+ $response->assertSee('name="purchase_date"', false);+ $response->assertSee('value="2026-04-01"', false);+ $response->assertSee('name="purchase_cost"', false);+ $response->assertSee('value="12.34"', false);+ }}diff --git a/tests/Feature/Consumables/Ui/CloneConsumableTest.php b/tests/Feature/Consumables/Ui/CloneConsumableTest.phpnew file mode 100644index 0000000000..e9343765d8--- /dev/null+++ b/tests/Feature/Consumables/Ui/CloneConsumableTest.php@@ -0,0 +1,58 @@+<?php++namespace Tests\Feature\Consumables\Ui;++use App\Models\Consumable;+use App\Models\Supplier;+use App\Models\User;+use Tests\TestCase;++/**+ * Consumable mirror of CloneAccessoryTest. See that file for the+ * Orders-refactor rationale behind the clone-prefill fix.+ */+class CloneConsumableTest extends TestCase+{+ public function test_permission_required_to_clone_consumable(): void+ {+ $consumable = Consumable::factory()->create();++ $this->actingAs(User::factory()->create())+ ->get(route('consumables.clone.create', $consumable))+ ->assertForbidden();+ }++ public function test_clone_page_renders(): void+ {+ $consumable = Consumable::factory()->create();++ $this->actingAs(User::factory()->createConsumables()->create())+ ->get(route('consumables.clone.create', $consumable))+ ->assertOk();+ }++ public function test_clone_prefills_supplier_and_order_context_from_last_order(): void+ {+ $supplier = Supplier::factory()->create();+ $source = Consumable::factory()+ ->withInitialAcquisition($supplier, 5.75, '2026-05-10')+ ->create(['qty' => 20]);++ $source->orderItems()->latest('id')->first()->order()->update([+ 'order_number' => 'PO-CONS-CLONE-1',+ ]);++ $response = $this->actingAs(User::factory()->createConsumables()->create())+ ->get(route('consumables.clone.create', $source))+ ->assertOk();++ $response->assertSee('name="supplier_id"', false);+ $response->assertSee('value="'.$supplier->id.'"', false);+ $response->assertSee('name="order_number"', false);+ $response->assertSee('value="PO-CONS-CLONE-1"', false);+ $response->assertSee('name="purchase_date"', false);+ $response->assertSee('value="2026-05-10"', false);+ $response->assertSee('name="purchase_cost"', false);+ $response->assertSee('value="5.75"', false);+ }+}