小程序用户改用门店登录
This commit is contained in:
@@ -6,16 +6,16 @@ use App\Models\CustomerLevelModel;
|
||||
use App\Models\ProductModel;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\StoreOrderModel;
|
||||
use App\Models\UserModel;
|
||||
use Modules\SystemTool\Models\SysFileModel;
|
||||
|
||||
/**
|
||||
* 小程序下单:等级上浮价快照、服务端重算总价、取消限制、门店数据隔离
|
||||
* (门店即用户:小程序端登录主体为 StoreModel)
|
||||
*/
|
||||
class StoreOrderTest extends ProcurementTestCase
|
||||
{
|
||||
/**
|
||||
* @return array{0: StoreModel, 1: ProductModel, 2: UserModel}
|
||||
* @return array{0: StoreModel, 1: ProductModel}
|
||||
*/
|
||||
private function makeStoreWithProduct(string $price = '5.00'): array
|
||||
{
|
||||
@@ -26,14 +26,14 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
'cost_price' => $price,
|
||||
]);
|
||||
|
||||
return [$store, $product, UserModel::factory()->forStore($store->id)->create()];
|
||||
return [$store, $product];
|
||||
}
|
||||
|
||||
/** 下单快照等级价,服务端重算单品金额与订单总价 */
|
||||
public function test_place_order_snapshots_level_price_and_recalculates(): void
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct('5.50');
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store, $product] = $this->makeStoreWithProduct('5.50');
|
||||
$this->actingAsMiniStore($store);
|
||||
|
||||
$this->postJson('/mini/order', [
|
||||
'items' => [['product_id' => $product->id, 'quantity' => 3]],
|
||||
@@ -54,8 +54,8 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 前端传入的金额字段一律被忽略 */
|
||||
public function test_client_supplied_amount_is_ignored(): void
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct('5.00');
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store, $product] = $this->makeStoreWithProduct('5.00');
|
||||
$this->actingAsMiniStore($store);
|
||||
|
||||
$this->postJson('/mini/order', [
|
||||
'items' => [[
|
||||
@@ -73,9 +73,9 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 门店绑定的客户等级被删除时拒绝下单 */
|
||||
public function test_store_level_missing_rejected(): void
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct('5.00');
|
||||
[$store, $product] = $this->makeStoreWithProduct('5.00');
|
||||
CustomerLevelModel::whereKey($store->level_id)->delete();
|
||||
$this->actingAsMiniUser($user);
|
||||
$this->actingAsMiniStore($store);
|
||||
|
||||
$this->postJson('/mini/order', [
|
||||
'items' => [['product_id' => $product->id, 'quantity' => 1]],
|
||||
@@ -87,13 +87,13 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 取消:仅待汇总订单可取消 */
|
||||
public function test_cancel_only_pending_orders(): void
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store, $product] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniStore($store);
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 1]]]);
|
||||
$order = StoreOrderModel::where('store_id', $store->id)->first();
|
||||
|
||||
$this->putJson("/mini/order/{$order->id}/cancel")->assertJsonPath('success', true);
|
||||
$this->assertSame(StoreOrderModel::STATUS_CANCELLED, $order->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_CANCELLED, $order->refresh()->status);
|
||||
|
||||
// 已取消不可重复取消
|
||||
$this->putJson("/mini/order/{$order->id}/cancel")->assertJsonPath('success', false);
|
||||
@@ -102,27 +102,27 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 已汇总订单不可取消 */
|
||||
public function test_summarized_order_cannot_be_cancelled(): void
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store, $product] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniStore($store);
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 1]]]);
|
||||
$order = StoreOrderModel::where('store_id', $store->id)->first();
|
||||
$order->update(['status' => StoreOrderModel::STATUS_SUMMARIZED]);
|
||||
|
||||
$this->putJson("/mini/order/{$order->id}/cancel")->assertJsonPath('success', false);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $order->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $order->refresh()->status);
|
||||
}
|
||||
|
||||
/** 门店数据隔离:只能查看本店订单 */
|
||||
public function test_order_data_isolated_between_stores(): void
|
||||
{
|
||||
[$storeA, $product, $userA] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniUser($userA);
|
||||
[$storeA, $product] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniStore($storeA);
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 1]]]);
|
||||
$orderOfA = StoreOrderModel::where('store_id', $storeA->id)->first();
|
||||
|
||||
// 门店 B 用户访问门店 A 的订单
|
||||
// 门店 B 访问门店 A 的订单
|
||||
$storeB = StoreModel::factory()->create(['level_id' => $storeA->level_id]);
|
||||
$this->actingAsMiniUser(UserModel::factory()->forStore($storeB->id)->create());
|
||||
$this->actingAsMiniStore($storeB);
|
||||
|
||||
$this->getJson("/mini/order/{$orderOfA->id}")->assertJsonPath('success', false);
|
||||
$this->getJson('/mini/order')->assertJsonPath('data.total', 0);
|
||||
@@ -133,7 +133,7 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
{
|
||||
$level = CustomerLevelModel::factory()->create();
|
||||
$store = StoreModel::factory()->create(['level_id' => $level->id]);
|
||||
$this->actingAsMiniUser(UserModel::factory()->forStore($store->id)->create());
|
||||
$this->actingAsMiniStore($store);
|
||||
|
||||
$items = [];
|
||||
foreach (['白菜', '土豆', '番茄', '黄瓜'] as $name) {
|
||||
@@ -161,8 +161,8 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 订货日期区间筛选(配合 /order/summary 周期下钻) */
|
||||
public function test_order_list_filters_by_order_date_range(): void
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store, $product] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniStore($store);
|
||||
|
||||
foreach ([1, 1, 1] as $qty) {
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => $qty]]])
|
||||
@@ -181,8 +181,8 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 非法筛选参数被拦截(状态枚举 / 分页上限 / 日期区间倒置) */
|
||||
public function test_order_list_rejects_invalid_filters(): void
|
||||
{
|
||||
[, , $user] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store] = $this->makeStoreWithProduct();
|
||||
$this->actingAsMiniStore($store);
|
||||
|
||||
$this->getJson('/mini/order?status=7')->assertJsonPath('success', false);
|
||||
$this->getJson('/mini/order?pageSize=100')->assertJsonPath('success', false);
|
||||
@@ -192,8 +192,8 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
/** 造一笔指定状态的订单(商品 5.00 × 2 = 10.00) */
|
||||
private function makeOrderWithStatus(int $status): StoreOrderModel
|
||||
{
|
||||
[$store, $product, $user] = $this->makeStoreWithProduct('5.00');
|
||||
$this->actingAsMiniUser($user);
|
||||
[$store, $product] = $this->makeStoreWithProduct('5.00');
|
||||
$this->actingAsMiniStore($store);
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 2]]])
|
||||
->assertJsonPath('success', true);
|
||||
$order = StoreOrderModel::where('store_id', $store->id)->first();
|
||||
@@ -213,7 +213,7 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
$this->actingAsSysUser();
|
||||
$this->deleteJson("/order/store/{$order->id}")->assertOk()->assertJsonPath('success', true);
|
||||
|
||||
$this->assertNotNull($order->fresh()->deleted_at, '软删除应写入 deleted_at');
|
||||
$this->assertNotNull($order->refresh()->deleted_at, '软删除应写入 deleted_at');
|
||||
$this->assertNull(StoreOrderModel::find($order->id), '默认查询不可见软删除订单');
|
||||
$this->assertNotNull(StoreOrderModel::withTrashed()->find($order->id), '数据仍保留在库中');
|
||||
|
||||
@@ -222,7 +222,7 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
$this->getJson("/order/store/{$order->id}")->assertJsonPath('success', false);
|
||||
|
||||
// 小程序端历史订单同样不可见
|
||||
$this->actingAsMiniUser(UserModel::where('store_id', $order->store_id)->first());
|
||||
$this->actingAsMiniStore(StoreModel::find($order->store_id));
|
||||
$this->getJson('/mini/order')->assertOk()->assertJsonPath('data.total', 0);
|
||||
$this->getJson("/mini/order/{$order->id}")->assertJsonPath('success', false);
|
||||
}
|
||||
@@ -241,7 +241,7 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
$this->actingAsSysUser();
|
||||
$this->deleteJson("/order/store/{$order->id}")
|
||||
->assertOk()->assertJsonPath('success', false);
|
||||
$this->assertNull($order->fresh()->deleted_at, '非已取消订单不得删除');
|
||||
$this->assertNull($order->refresh()->deleted_at, '非已取消订单不得删除');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,13 +263,13 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
|
||||
$this->putJson("/order/store/{$order->id}/status", ['status' => StoreOrderModel::STATUS_SUMMARIZED])
|
||||
->assertJsonPath('success', true);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $order->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $order->refresh()->status);
|
||||
|
||||
$order2 = $this->makeOrderWithStatus(StoreOrderModel::STATUS_PENDING);
|
||||
$this->actingAsSysUser();
|
||||
$this->putJson("/order/store/{$order2->id}/status", ['status' => StoreOrderModel::STATUS_CANCELLED])
|
||||
->assertJsonPath('success', true);
|
||||
$this->assertSame(StoreOrderModel::STATUS_CANCELLED, $order2->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_CANCELLED, $order2->refresh()->status);
|
||||
}
|
||||
|
||||
/** 手动流转:配送中/已完成等目标状态被拒绝(由采购/账单链路自动推进) */
|
||||
@@ -280,7 +280,7 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
$this->actingAsSysUser();
|
||||
$this->putJson("/order/store/{$order->id}/status", ['status' => $target])
|
||||
->assertJsonPath('success', false);
|
||||
$this->assertSame(StoreOrderModel::STATUS_DELIVERING, $order->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_DELIVERING, $order->refresh()->status);
|
||||
}
|
||||
|
||||
// 已接单不能手动转采购中(须经生成采购单)
|
||||
@@ -302,8 +302,8 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
'ids' => [$pending1->id, $pending2->id],
|
||||
'status' => StoreOrderModel::STATUS_SUMMARIZED,
|
||||
])->assertJsonPath('success', true);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending1->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending2->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending1->refresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending2->refresh()->status);
|
||||
|
||||
// 批量完成被拒绝(目标状态校验拦截)
|
||||
$this->putJson('/order/store/batchStatus', [
|
||||
@@ -316,7 +316,7 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
'ids' => [$pending1->id],
|
||||
'status' => StoreOrderModel::STATUS_CANCELLED,
|
||||
])->assertJsonPath('success', false);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending1->fresh()->status);
|
||||
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending1->refresh()->status);
|
||||
}
|
||||
|
||||
/** 订单列表商品预览:附带首图/规格/单位供小程序端展示 */
|
||||
@@ -343,9 +343,8 @@ class StoreOrderTest extends ProcurementTestCase
|
||||
'image_ids' => (string) $file->id,
|
||||
'cost_price' => '5.00',
|
||||
]);
|
||||
$user = UserModel::factory()->forStore($store->id)->create();
|
||||
|
||||
$this->actingAsMiniUser($user);
|
||||
$this->actingAsMiniStore($store);
|
||||
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 2]]])
|
||||
->assertJsonPath('success', true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user