From 2cb283018c6d002e5cbd6457359a8a53177011a4 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sun, 5 Jul 2026 17:46:45 +0700 Subject: [PATCH] test: add comprehensive tests for retail stock transfer functionality, including permission checks, quantity validation, and stock updates --- tests/Feature/Admin/Manage/StockTest.php | 119 +++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tests/Feature/Admin/Manage/StockTest.php b/tests/Feature/Admin/Manage/StockTest.php index a58f474..07a42dc 100644 --- a/tests/Feature/Admin/Manage/StockTest.php +++ b/tests/Feature/Admin/Manage/StockTest.php @@ -195,3 +195,122 @@ function createCompletedCuttingSetup(): array ]); }); }); + +// ─── Retail Stock Transfer ────────────────────────────────── + +describe('Retail Stock Transfer', function () { + test('user with permission can transfer stock from good to retail', function () { + $user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW); + + $product = Product::factory()->create(); + $variant = ProductVariant::factory()->create([ + 'product_id' => $product->id, + 'stock' => 10, + 'retail_stock' => 5, + ]); + + $this->actingAs($user) + ->postJson(route('admin.manage.retail-stock.transfer'), [ + 'product_variant_id' => $variant->id, + 'quantity' => 3, + 'notes' => 'Transfer test', + ]) + ->assertOk(); + + $variant->refresh(); + + $this->assertEquals(7, $variant->stock); + $this->assertEquals(8, $variant->retail_stock); + + $this->assertDatabaseHas('retail_stock_histories', [ + 'product_variant_id' => $variant->id, + 'user_id' => $user->id, + 'quantity' => 3, + 'stock_before' => 10, + 'retail_stock_before' => 5, + 'stock_after' => 7, + 'retail_stock_after' => 8, + 'notes' => 'Transfer test', + ]); + }); + + test('retail stock transfer does not require owner verification', function () { + $user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW); + + $product = Product::factory()->create(); + $variant = ProductVariant::factory()->create([ + 'product_id' => $product->id, + 'stock' => 10, + 'retail_stock' => 5, + ]); + + $this->actingAs($user) + ->postJson(route('admin.manage.retail-stock.transfer'), [ + 'product_variant_id' => $variant->id, + 'quantity' => 3, + ]) + ->assertOk(); + + $this->assertDatabaseCount('owner_verification_requests', 0); + + $variant->refresh(); + $this->assertEquals(7, $variant->stock); + $this->assertEquals(8, $variant->retail_stock); + }); + + test('retail stock transfer fails when quantity exceeds stock', function () { + $user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW); + + $product = Product::factory()->create(); + $variant = ProductVariant::factory()->create([ + 'product_id' => $product->id, + 'stock' => 5, + 'retail_stock' => 0, + ]); + + $this->actingAs($user) + ->postJson(route('admin.manage.retail-stock.transfer'), [ + 'product_variant_id' => $variant->id, + 'quantity' => 10, + ]) + ->assertUnprocessable() + ->assertJsonValidationErrors('quantity'); + }); + + test('retail stock transfer fails when quantity is zero', function () { + $user = createStockUserWithPermission(PermissionEnum::STOCKS_VIEW); + + $product = Product::factory()->create(); + $variant = ProductVariant::factory()->create([ + 'product_id' => $product->id, + 'stock' => 10, + 'retail_stock' => 5, + ]); + + $this->actingAs($user) + ->postJson(route('admin.manage.retail-stock.transfer'), [ + 'product_variant_id' => $variant->id, + 'quantity' => 0, + ]) + ->assertUnprocessable() + ->assertJsonValidationErrors('quantity'); + }); + + test('user without permission cannot transfer stock', function () { + $user = User::factory()->create(); + + $product = Product::factory()->create(); + $variant = ProductVariant::factory()->create([ + 'product_id' => $product->id, + 'stock' => 10, + 'retail_stock' => 5, + ]); + + $this->actingAs($user) + ->postJson(route('admin.manage.retail-stock.transfer'), [ + 'product_variant_id' => $variant->id, + 'quantity' => 3, + ]) + ->assertForbidden(); + }); +});