CustomerOrderCancelTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Feature\GraphQL;
  3. use Webkul\BagistoApi\Tests\GraphQLTestCase;
  4. use Webkul\Core\Models\Channel;
  5. use Webkul\Customer\Models\Customer;
  6. use Webkul\Product\Models\Product;
  7. use Webkul\Sales\Models\Order;
  8. use Webkul\Sales\Models\OrderItem;
  9. use Webkul\Sales\Models\OrderPayment;
  10. class CustomerOrderCancelTest extends GraphQLTestCase
  11. {
  12. /**
  13. * Create test data — customer with cancellable order
  14. */
  15. private function createTestData(): array
  16. {
  17. $this->seedRequiredData();
  18. $customer = $this->createCustomer();
  19. $channel = Channel::first();
  20. $product = Product::factory()->create();
  21. // Create an order in 'pending' status which can be canceled
  22. $order = Order::factory()->create([
  23. 'customer_id' => $customer->id,
  24. 'customer_email' => $customer->email,
  25. 'customer_first_name' => $customer->first_name,
  26. 'customer_last_name' => $customer->last_name,
  27. 'channel_id' => $channel->id,
  28. 'status' => 'pending',
  29. ]);
  30. OrderItem::factory()->create([
  31. 'order_id' => $order->id,
  32. 'product_id' => $product->id,
  33. 'sku' => 'TEST-SKU-CANCEL-001',
  34. 'type' => 'simple',
  35. 'name' => 'Test Product for Cancel',
  36. 'qty_ordered' => 1,
  37. ]);
  38. OrderPayment::factory()->create([
  39. 'order_id' => $order->id,
  40. ]);
  41. // Create a completed order that cannot be canceled
  42. $completedOrder = Order::factory()->create([
  43. 'customer_id' => $customer->id,
  44. 'customer_email' => $customer->email,
  45. 'customer_first_name' => $customer->first_name,
  46. 'customer_last_name' => $customer->last_name,
  47. 'channel_id' => $channel->id,
  48. 'status' => 'completed',
  49. ]);
  50. OrderItem::factory()->create([
  51. 'order_id' => $completedOrder->id,
  52. 'product_id' => $product->id,
  53. 'sku' => 'TEST-SKU-COMPLETED',
  54. 'type' => 'simple',
  55. 'name' => 'Completed Order Product',
  56. ]);
  57. OrderPayment::factory()->create([
  58. 'order_id' => $completedOrder->id,
  59. ]);
  60. return compact('customer', 'order', 'completedOrder', 'channel', 'product');
  61. }
  62. // ── Cancel Order Mutation Tests ────────────────────────────────────
  63. /**
  64. * Test: Cancel a pending order successfully
  65. */
  66. public function test_cancel_pending_order_success(): void
  67. {
  68. $testData = $this->createTestData();
  69. $mutation = <<<'GQL'
  70. mutation cancelOrder($input: CancelOrderInput!) {
  71. cancelOrder(input: $input) {
  72. success
  73. message
  74. orderId
  75. status
  76. }
  77. }
  78. GQL;
  79. $variables = [
  80. 'input' => [
  81. 'orderId' => $testData['order']->id,
  82. ],
  83. ];
  84. $response = $this->authenticatedGraphQL(
  85. $testData['customer'],
  86. $mutation,
  87. $variables
  88. );
  89. $response->assertJson([
  90. 'data' => [
  91. 'cancelOrder' => [
  92. 'success' => true,
  93. 'orderId' => $testData['order']->id,
  94. ],
  95. ],
  96. ]);
  97. // Verify the order status changed to 'canceled'
  98. $testData['order']->refresh();
  99. $this->assertEquals('canceled', $testData['order']->status);
  100. }
  101. /**
  102. * Test: Cannot cancel a completed order
  103. */
  104. public function test_cannot_cancel_completed_order(): void
  105. {
  106. $testData = $this->createTestData();
  107. $mutation = <<<'GQL'
  108. mutation cancelOrder($input: CancelOrderInput!) {
  109. cancelOrder(input: $input) {
  110. success
  111. message
  112. orderId
  113. status
  114. }
  115. }
  116. GQL;
  117. $variables = [
  118. 'input' => [
  119. 'orderId' => $testData['completedOrder']->id,
  120. ],
  121. ];
  122. $response = $this->authenticatedGraphQL(
  123. $testData['customer'],
  124. $mutation,
  125. $variables
  126. );
  127. // Should fail to cancel
  128. $this->assertEquals(false, $response['data']['cancelOrder']['success']);
  129. }
  130. /**
  131. * Test: Cannot cancel non-existent order
  132. */
  133. public function test_cannot_cancel_non_existent_order(): void
  134. {
  135. $testData = $this->createTestData();
  136. $mutation = <<<'GQL'
  137. mutation cancelOrder($input: CancelOrderInput!) {
  138. cancelOrder(input: $input) {
  139. success
  140. message
  141. orderId
  142. }
  143. }
  144. GQL;
  145. $variables = [
  146. 'input' => [
  147. 'orderId' => 99999, // Non-existent ID
  148. ],
  149. ];
  150. $response = $this->authenticatedGraphQL(
  151. $testData['customer'],
  152. $mutation,
  153. $variables
  154. );
  155. // Should return an error
  156. $this->assertArrayHasKey('errors', $response);
  157. }
  158. /**
  159. * Test: Cannot cancel order of another customer
  160. */
  161. public function test_cannot_cancel_other_customers_order(): void
  162. {
  163. $testData = $this->createTestData();
  164. $otherCustomer = $this->createCustomer();
  165. $mutation = <<<'GQL'
  166. mutation cancelOrder($input: CancelOrderInput!) {
  167. cancelOrder(input: $input) {
  168. success
  169. message
  170. orderId
  171. }
  172. }
  173. GQL;
  174. $variables = [
  175. 'input' => [
  176. 'orderId' => $testData['order']->id,
  177. ],
  178. ];
  179. $response = $this->authenticatedGraphQL(
  180. $otherCustomer,
  181. $mutation,
  182. $variables
  183. );
  184. // Should return an error
  185. $this->assertArrayHasKey('errors', $response);
  186. }
  187. /**
  188. * Test: Missing order ID should return error
  189. */
  190. public function test_cancel_without_order_id(): void
  191. {
  192. $testData = $this->createTestData();
  193. $mutation = <<<'GQL'
  194. mutation cancelOrder($input: CancelOrderInput!) {
  195. cancelOrder(input: $input) {
  196. success
  197. message
  198. }
  199. }
  200. GQL;
  201. $variables = [
  202. 'input' => [],
  203. ];
  204. $response = $this->authenticatedGraphQL(
  205. $testData['customer'],
  206. $mutation,
  207. $variables
  208. );
  209. // Should return an error for missing orderId
  210. $this->assertArrayHasKey('errors', $response);
  211. }
  212. /**
  213. * Test: Unauthenticated cancel request fails
  214. */
  215. public function test_unauthenticated_cancel_fails(): void
  216. {
  217. $testData = $this->createTestData();
  218. $mutation = <<<'GQL'
  219. mutation cancelOrder($input: CancelOrderInput!) {
  220. cancelOrder(input: $input) {
  221. success
  222. message
  223. }
  224. }
  225. GQL;
  226. $variables = [
  227. 'input' => [
  228. 'orderId' => $testData['order']->id,
  229. ],
  230. ];
  231. // Call without authentication
  232. $response = $this->graphQL($mutation, $variables);
  233. // GraphQL returns response as array from postJson
  234. $data = $response->json();
  235. $this->assertArrayHasKey('errors', $data);
  236. $this->assertTrue(
  237. isset($data['errors'][0]['message']),
  238. 'Expected GraphQL error message to be present'
  239. );
  240. }
  241. /**
  242. * Test: Cancel order response includes all expected fields
  243. */
  244. public function test_cancel_response_includes_all_fields(): void
  245. {
  246. $testData = $this->createTestData();
  247. $mutation = <<<'GQL'
  248. mutation cancelOrder($input: CancelOrderInput!) {
  249. cancelOrder(input: $input) {
  250. success
  251. message
  252. orderId
  253. status
  254. }
  255. }
  256. GQL;
  257. $variables = [
  258. 'input' => [
  259. 'orderId' => $testData['order']->id,
  260. ],
  261. ];
  262. $response = $this->authenticatedGraphQL(
  263. $testData['customer'],
  264. $mutation,
  265. $variables
  266. );
  267. $data = $response->json();
  268. $this->assertArrayHasKey('data', $data);
  269. $this->assertArrayHasKey('cancelOrder', $data['data']);
  270. $this->assertArrayHasKey('success', $data['data']['cancelOrder']);
  271. $this->assertArrayHasKey('message', $data['data']['cancelOrder']);
  272. $this->assertArrayHasKey('orderId', $data['data']['cancelOrder']);
  273. $this->assertArrayHasKey('status', $data['data']['cancelOrder']);
  274. }
  275. }