diff --git a/tests/Feature/Accessories/Ui/CloneAccessoryTest.php b/tests/Feature/Accessories/Ui/CloneAccessoryTest.php new file mode 100644 index 0000000000..f83e41f34a --- /dev/null +++ b/tests/Feature/Accessories/Ui/CloneAccessoryTest.php @@ -0,0 +1,81 @@ +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.php index 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.php new file mode 100644 index 0000000000..e9343765d8 --- /dev/null +++ b/tests/Feature/Consumables/Ui/CloneConsumableTest.php @@ -0,0 +1,58 @@ +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); + } +}