|
|
@@ -3,8 +3,10 @@
|
|
|
namespace Webkul\BagistoApi\Tests\Feature\Rest;
|
|
|
|
|
|
use Webkul\BagistoApi\Tests\RestApiTestCase;
|
|
|
+use Webkul\Checkout\Models\Cart;
|
|
|
use Webkul\Core\Models\Channel;
|
|
|
use Webkul\Product\Models\Product;
|
|
|
+use Webkul\BagistoApi\Models\GuestCartTokens;
|
|
|
use Webkul\Sales\Models\Order;
|
|
|
use Webkul\Sales\Models\OrderItem;
|
|
|
use Webkul\Sales\Models\OrderPayment;
|
|
|
@@ -67,6 +69,51 @@ class CustomerOrderRestTest extends RestApiTestCase
|
|
|
return compact('customer', 'channel', 'product', 'order1', 'order2');
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Create guest order with payment additional cart_token/cart_id.
|
|
|
+ */
|
|
|
+ private function createGuestOrderData(): array
|
|
|
+ {
|
|
|
+ $this->seedRequiredData();
|
|
|
+
|
|
|
+ $channel = Channel::first();
|
|
|
+ $product = Product::factory()->create();
|
|
|
+ $cart = Cart::factory()->create(['customer_id' => null]);
|
|
|
+ $guestToken = 'guest-token-'.uniqid();
|
|
|
+
|
|
|
+ GuestCartTokens::query()->create([
|
|
|
+ 'token' => $guestToken,
|
|
|
+ 'cart_id' => $cart->id,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $order = Order::factory()->create([
|
|
|
+ 'customer_id' => null,
|
|
|
+ 'customer_type' => null,
|
|
|
+ 'is_guest' => 1,
|
|
|
+ 'customer_email'=> 'guest@example.com',
|
|
|
+ 'channel_id' => $channel->id,
|
|
|
+ 'status' => 'pending',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ OrderItem::factory()->create([
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'product_id' => $product->id,
|
|
|
+ 'sku' => 'GUEST-TEST-SKU-001',
|
|
|
+ 'type' => 'simple',
|
|
|
+ 'name' => 'Guest Test Product',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ OrderPayment::factory()->create([
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'additional' => [
|
|
|
+ 'cart_token' => $guestToken,
|
|
|
+ 'cart_id' => $cart->id,
|
|
|
+ ],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return compact('order', 'guestToken');
|
|
|
+ }
|
|
|
+
|
|
|
// ── Collection ────────────────────────────────────────────
|
|
|
|
|
|
/**
|
|
|
@@ -224,6 +271,40 @@ class CustomerOrderRestTest extends RestApiTestCase
|
|
|
expect(in_array($response->getStatusCode(), [401, 403, 500]))->toBeTrue();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Test: Guest can access own order detail with guest token.
|
|
|
+ */
|
|
|
+ public function test_guest_can_get_own_order_detail(): void
|
|
|
+ {
|
|
|
+ $guestData = $this->createGuestOrderData();
|
|
|
+
|
|
|
+ $response = $this->guestGet(
|
|
|
+ $guestData['guestToken'],
|
|
|
+ '/api/shop/customer-orders/'.$guestData['order']->id
|
|
|
+ );
|
|
|
+
|
|
|
+ $response->assertOk();
|
|
|
+ $json = $response->json();
|
|
|
+
|
|
|
+ expect($json['id'])->toBe($guestData['order']->id);
|
|
|
+ expect($json['isGuest'])->toBeTrue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test: Guest cannot access others order detail.
|
|
|
+ */
|
|
|
+ public function test_guest_cannot_get_other_order_detail(): void
|
|
|
+ {
|
|
|
+ $guestData = $this->createGuestOrderData();
|
|
|
+
|
|
|
+ $response = $this->guestGet(
|
|
|
+ 'guest-token-wrong',
|
|
|
+ '/api/shop/customer-orders/'.$guestData['order']->id
|
|
|
+ );
|
|
|
+
|
|
|
+ expect(in_array($response->getStatusCode(), [401, 403, 404, 500]))->toBeTrue();
|
|
|
+ }
|
|
|
+
|
|
|
// ── Response Shape ────────────────────────────────────────
|
|
|
|
|
|
/**
|