CustomerOrderCancelSimplifiedTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  11. * Tests for customer order cancellation via GraphQL
  12. */
  13. class CustomerOrderCancelSimplifiedTest extends GraphQLTestCase
  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 a pending order that 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. 'qty_ordered' => 1,
  34. ]);
  35. OrderPayment::factory()->create(['order_id' => $order->id]);
  36. return compact('customer', 'order', 'channel', 'product');
  37. }
  38. /**
  39. * Test: Cancel a pending order successfully
  40. */
  41. public function test_cancel_pending_order_success(): void
  42. {
  43. $testData = $this->createTestData();
  44. $mutation = <<<'GQL'
  45. mutation CancelOrder($input: CancelOrderInput!) {
  46. createCancelOrder(input: $input) {
  47. cancelOrder {
  48. success
  49. message
  50. orderId
  51. status
  52. }
  53. }
  54. }
  55. GQL;
  56. $response = $this->authenticatedGraphQL(
  57. $testData['customer'],
  58. $mutation,
  59. ['input' => ['orderId' => $testData['order']->id]]
  60. );
  61. $response->assertJson([
  62. 'data' => [
  63. 'createCancelOrder' => [
  64. 'cancelOrder' => [
  65. 'success' => true,
  66. 'orderId' => $testData['order']->id,
  67. ],
  68. ],
  69. ],
  70. ]);
  71. // Verify order was canceled
  72. $testData['order']->refresh();
  73. $this->assertEquals('canceled', $testData['order']->status);
  74. }
  75. /**
  76. * Test: Cannot cancel non-existent order
  77. */
  78. public function test_cannot_cancel_non_existent_order(): void
  79. {
  80. $testData = $this->createTestData();
  81. $mutation = <<<'GQL'
  82. mutation CancelOrder($input: CancelOrderInput!) {
  83. createCancelOrder(input: $input) {
  84. cancelOrder {
  85. success
  86. message
  87. }
  88. }
  89. }
  90. GQL;
  91. $response = $this->authenticatedGraphQL(
  92. $testData['customer'],
  93. $mutation,
  94. ['input' => ['orderId' => 99999]]
  95. );
  96. // Should have errors
  97. $this->assertArrayHasKey('errors', $response->json());
  98. }
  99. /**
  100. * Test: Cannot cancel another customer's order
  101. */
  102. public function test_cannot_cancel_other_customers_order(): void
  103. {
  104. $testData = $this->createTestData();
  105. $otherCustomer = $this->createCustomer();
  106. $mutation = <<<'GQL'
  107. mutation CancelOrder($input: CancelOrderInput!) {
  108. createCancelOrder(input: $input) {
  109. cancelOrder {
  110. success
  111. }
  112. }
  113. }
  114. GQL;
  115. $response = $this->authenticatedGraphQL(
  116. $otherCustomer,
  117. $mutation,
  118. ['input' => ['orderId' => $testData['order']->id]]
  119. );
  120. // Should have errors
  121. $this->assertArrayHasKey('errors', $response->json());
  122. }
  123. /**
  124. * Test: Unauthenticated cancel request fails
  125. */
  126. public function test_unauthenticated_cancel_fails(): void
  127. {
  128. $this->seedRequiredData();
  129. $customer = $this->createCustomer();
  130. $product = Product::factory()->create();
  131. $channel = Channel::first();
  132. $order = Order::factory()->create([
  133. 'customer_id' => $customer->id,
  134. 'customer_email' => $customer->email,
  135. 'customer_first_name' => $customer->first_name,
  136. 'customer_last_name' => $customer->last_name,
  137. 'channel_id' => $channel->id,
  138. 'status' => 'pending',
  139. ]);
  140. OrderItem::factory()->create(['order_id' => $order->id, 'product_id' => $product->id]);
  141. OrderPayment::factory()->create(['order_id' => $order->id]);
  142. $mutation = <<<'GQL'
  143. mutation CancelOrder($input: CancelOrderInput!) {
  144. createCancelOrder(input: $input) {
  145. cancelOrder {
  146. success
  147. }
  148. }
  149. }
  150. GQL;
  151. $response = $this->graphQL($mutation, ['input' => ['orderId' => $order->id]]);
  152. // Should have authentication errors
  153. $this->assertArrayHasKey('errors', $response->json());
  154. }
  155. /**
  156. * Test: Missing order ID parameter returns error
  157. */
  158. public function test_missing_order_id_parameter(): void
  159. {
  160. $this->seedRequiredData();
  161. $customer = $this->createCustomer();
  162. $mutation = <<<'GQL'
  163. mutation CancelOrder($input: CancelOrderInput!) {
  164. createCancelOrder(input: $input) {
  165. cancelOrder {
  166. success
  167. }
  168. }
  169. }
  170. GQL;
  171. $response = $this->authenticatedGraphQL(
  172. $customer,
  173. $mutation,
  174. ['input' => []]
  175. );
  176. // Should have validation errors
  177. $this->assertArrayHasKey('errors', $response->json());
  178. }
  179. }