test: add comprehensive tests for retail stock transfer functionality, including permission checks, quantity validation, and stock updates
This commit is contained in:
parent
0b3bf891cc
commit
2cb283018c
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user