AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DiscountCodeTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Commerce\Payments\PaymentGateway;
  4. use App\Mail\OrderConfirmation;
  5. use App\Models\DiscountCode;
  6. use App\Models\Order;
  7. use App\Models\Product;
  8. use App\Models\Store;
  9. use App\Models\User;
  10. use App\StorePlan\SamplePlan;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Support\Facades\Mail;
  13. use Inertia\Testing\AssertableInertia as Assert;
  14. use Tests\Support\FakeGateway;
  15. use Tests\TestCase;
  16. class DiscountCodeTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. private User $user;
  20. private Store $store;
  21. private FakeGateway $gateway;
  22. protected function setUp(): void
  23. {
  24. parent::setUp();
  25. Mail::fake();
  26. $this->gateway = new FakeGateway;
  27. $this->app->instance(PaymentGateway::class, $this->gateway);
  28. $this->user = User::factory()->create();
  29. $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
  30. $this->store->plan = SamplePlan::for($this->store);
  31. $this->store->save();
  32. $this->actingAs($this->user);
  33. }
  34. private function addToCart(int $price, int $quantity = 1): Product
  35. {
  36. $product = Product::factory()->for($this->store)->create(['price' => $price, 'stock' => 50]);
  37. $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity]);
  38. return $product;
  39. }
  40. private function apply(string $code)
  41. {
  42. return $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => $code]);
  43. }
  44. private function cart(): Assert|callable
  45. {
  46. return fn (callable $assert) => $this->get(route('stores.preview.cart', $this->store->slug))
  47. ->assertInertia(fn (Assert $page) => $assert($page));
  48. }
  49. private function placeOrder(string $state = 'Selangor'): Order
  50. {
  51. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  52. 'name' => 'Aminah binti Ali',
  53. 'email' => 'aminah@example.com',
  54. 'phone' => '0123456789',
  55. 'line1' => '12 Jalan Mawar',
  56. 'city' => 'Shah Alam',
  57. 'postcode' => '40000',
  58. 'state' => $state,
  59. ])->assertRedirect();
  60. return Order::latest('id')->firstOrFail();
  61. }
  62. /** @return array<string, mixed> */
  63. private function form(array $overrides = []): array
  64. {
  65. return [
  66. 'code' => 'raya10',
  67. 'type' => 'percent',
  68. 'value' => '10',
  69. 'minimum_subtotal' => '',
  70. 'starts_on' => '',
  71. 'ends_on' => '',
  72. 'usage_limit' => '',
  73. 'active' => '1',
  74. ...$overrides,
  75. ];
  76. }
  77. public function test_the_merchant_can_create_edit_and_delete_codes()
  78. {
  79. $this->post(route('stores.discounts.store', $this->store->slug), $this->form([
  80. 'minimum_subtotal' => '50',
  81. 'starts_on' => '2027-04-01',
  82. 'ends_on' => '2027-04-30',
  83. 'usage_limit' => '100',
  84. ]))->assertRedirect(route('stores.discounts.index', $this->store->slug));
  85. $code = $this->store->discountCodes()->firstOrFail();
  86. $this->assertSame('RAYA10', $code->code);
  87. $this->assertSame(10, $code->value);
  88. $this->assertSame(5000, $code->minimum_subtotal);
  89. $this->assertSame(100, $code->usage_limit);
  90. // Days are Malaysian days: 1 April 00:00 in Kuala Lumpur is 31 March 16:00 UTC.
  91. $this->assertSame('2027-03-31 16:00:00', $code->starts_at->utc()->toDateTimeString());
  92. $this->assertSame('2027-04-30 15:59:59', $code->ends_at->utc()->toDateTimeString());
  93. $this->get(route('stores.discounts.index', $this->store->slug))
  94. ->assertInertia(fn (Assert $page) => $page
  95. ->component('stores/discounts/index')
  96. ->has('codes', 1)
  97. ->where('codes.0.label', '10% off')
  98. ->where('codes.0.starts_on', '2027-04-01')
  99. ->where('codes.0.ends_on', '2027-04-30'));
  100. $this->put(route('stores.discounts.update', [$this->store->slug, $code->id]), $this->form([
  101. 'code' => 'RAYA10', 'type' => 'fixed', 'value' => '7.50', 'active' => '0',
  102. ]))->assertRedirect(route('stores.discounts.index', $this->store->slug));
  103. $code->refresh();
  104. $this->assertSame('fixed', $code->type);
  105. $this->assertSame(750, $code->value);
  106. $this->assertFalse($code->active);
  107. $this->assertSame('off', $code->status());
  108. $this->delete(route('stores.discounts.destroy', [$this->store->slug, $code->id]));
  109. $this->assertSame(0, $this->store->discountCodes()->count());
  110. }
  111. public function test_code_details_are_checked()
  112. {
  113. DiscountCode::factory()->for($this->store)->create(['code' => 'TAKEN']);
  114. $this->post(route('stores.discounts.store', $this->store->slug), $this->form([
  115. 'code' => 'no spaces!', 'value' => '150', 'ends_on' => '2027-01-01', 'starts_on' => '2027-02-01',
  116. ]))->assertSessionHasErrors(['code', 'value', 'ends_on']);
  117. $this->post(route('stores.discounts.store', $this->store->slug), $this->form(['code' => 'taken']))
  118. ->assertSessionHasErrors('code');
  119. $this->post(route('stores.discounts.store', $this->store->slug), $this->form(['type' => 'fixed', 'value' => '0']))
  120. ->assertSessionHasErrors('value');
  121. // The same code may exist in another store.
  122. DiscountCode::factory()->create(['code' => 'RAYA10']);
  123. $this->post(route('stores.discounts.store', $this->store->slug), $this->form())->assertSessionHasNoErrors();
  124. }
  125. public function test_a_percentage_code_takes_money_off_the_products_in_the_cart()
  126. {
  127. DiscountCode::factory()->for($this->store)->percent(10)->create(['code' => 'RAYA10']);
  128. $this->addToCart(1999, 3);
  129. // Shoppers may type it in small letters with spaces.
  130. $this->apply(' raya10 ')->assertSessionHasNoErrors();
  131. ($this->cart())(fn (Assert $page) => $page
  132. ->where('cart.subtotal', 59.97)
  133. // 10% of 59.97 is 5.997, rounded down to 5.99.
  134. ->where('cart.discount.code', 'RAYA10')
  135. ->where('cart.discount.label', '10% off')
  136. ->where('cart.discount.amount', 5.99)
  137. ->where('cart.discount.message', null)
  138. ->where('cart.total', 53.98));
  139. }
  140. public function test_a_fixed_code_never_takes_off_more_than_the_products_cost()
  141. {
  142. DiscountCode::factory()->for($this->store)->fixed(5000)->create(['code' => 'BIG']);
  143. $this->addToCart(3000);
  144. $this->apply('BIG');
  145. ($this->cart())(fn (Assert $page) => $page->where('cart.discount.amount', 30)->where('cart.total', 0));
  146. }
  147. public function test_unusable_codes_are_refused_with_a_reason()
  148. {
  149. $this->addToCart(2000);
  150. DiscountCode::factory()->for($this->store)->create(['code' => 'OFF', 'active' => false]);
  151. DiscountCode::factory()->for($this->store)->create(['code' => 'LATER', 'starts_at' => now()->addDay()]);
  152. DiscountCode::factory()->for($this->store)->create(['code' => 'OLD', 'ends_at' => now()->subMinute()]);
  153. DiscountCode::factory()->for($this->store)->create(['code' => 'GONE', 'usage_limit' => 5, 'times_used' => 5]);
  154. DiscountCode::factory()->create(['code' => 'OTHERSTORE']);
  155. $this->apply('NOPE')->assertSessionHasErrors(['code' => 'This code is not valid.']);
  156. $this->apply('OFF')->assertSessionHasErrors(['code' => 'This code is not valid.']);
  157. $this->apply('LATER')->assertSessionHasErrors(['code' => 'This code cannot be used yet.']);
  158. $this->apply('OLD')->assertSessionHasErrors(['code' => 'This code has expired.']);
  159. $this->apply('GONE')->assertSessionHasErrors(['code' => 'This code has been used up.']);
  160. $this->apply('OTHERSTORE')->assertSessionHasErrors(['code' => 'This code is not valid.']);
  161. ($this->cart())(fn (Assert $page) => $page->where('cart.discount', null));
  162. }
  163. public function test_a_code_with_a_minimum_spend_waits_until_the_cart_is_big_enough()
  164. {
  165. DiscountCode::factory()->for($this->store)->fixed(500)->create(['code' => 'MIN50', 'minimum_subtotal' => 5000]);
  166. $this->addToCart(3000);
  167. $this->apply('MIN50')->assertSessionHasNoErrors();
  168. ($this->cart())(fn (Assert $page) => $page
  169. ->where('cart.discount.code', 'MIN50')
  170. ->where('cart.discount.amount', 0)
  171. ->where('cart.discount.message', 'Spend at least RM 50.00 on products to use this code.')
  172. ->where('cart.total', 30));
  173. $this->addToCart(2000);
  174. ($this->cart())(fn (Assert $page) => $page
  175. ->where('cart.discount.amount', 5)
  176. ->where('cart.discount.message', null)
  177. ->where('cart.total', 45));
  178. }
  179. public function test_the_code_can_be_removed()
  180. {
  181. DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10']);
  182. $this->addToCart(2000);
  183. $this->apply('RAYA10');
  184. $this->delete(route('stores.preview.cart.discount.remove', $this->store->slug))->assertRedirect();
  185. ($this->cart())(fn (Assert $page) => $page->where('cart.discount', null)->where('cart.total', 20));
  186. }
  187. public function test_a_code_switched_off_after_it_was_applied_is_taken_out_and_the_shopper_is_told()
  188. {
  189. $code = DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10']);
  190. $this->addToCart(2000);
  191. $this->apply('RAYA10');
  192. $code->forceFill(['active' => false])->save();
  193. ($this->cart())(fn (Assert $page) => $page->where('cart.discount', null)->where('cart.adjusted', true));
  194. }
  195. public function test_the_order_records_the_discount_and_shipping_is_charged_in_full()
  196. {
  197. DiscountCode::factory()->for($this->store)->percent(20)->create(['code' => 'SAVE20']);
  198. $this->addToCart(5000);
  199. $this->apply('SAVE20');
  200. $order = $this->placeOrder('Sabah');
  201. $this->assertSame(5000, $order->subtotal);
  202. $this->assertSame('SAVE20', $order->discount_code);
  203. $this->assertSame(1000, $order->discount_total);
  204. $this->assertSame(1500, $order->shipping_total);
  205. $this->assertSame(5500, $order->total);
  206. }
  207. public function test_free_shipping_is_judged_on_the_price_after_the_discount()
  208. {
  209. $this->store->forceFill(['free_shipping_over' => 10000])->save();
  210. DiscountCode::factory()->for($this->store)->percent(10)->create(['code' => 'SAVE10']);
  211. $this->addToCart(10000);
  212. $this->apply('SAVE10');
  213. // RM 100 of products minus 10% is RM 90, which is under the RM 100 free-shipping amount.
  214. $this->get(route('stores.preview.checkout', $this->store->slug))
  215. ->assertInertia(fn (Assert $page) => $page->where('view.shippingByState.Selangor', 8));
  216. $this->assertSame(800, $this->placeOrder()->shipping_total);
  217. }
  218. public function test_a_code_that_stopped_working_stops_checkout_so_the_total_never_surprises()
  219. {
  220. $code = DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10']);
  221. $this->addToCart(2000);
  222. $this->apply('RAYA10');
  223. $code->forceFill(['ends_at' => now()->subMinute()])->save();
  224. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  225. 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
  226. 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
  227. ])->assertSessionHasErrors(['cart' => 'Your discount code can no longer be used, so it was removed. Please check your total.']);
  228. $this->assertSame(0, Order::count());
  229. }
  230. public function test_an_order_must_still_be_at_least_two_ringgit_after_the_discount()
  231. {
  232. $this->store->forceFill(['free_shipping_over' => 0])->save();
  233. DiscountCode::factory()->for($this->store)->percent(100)->create(['code' => 'FREE']);
  234. $this->addToCart(2000);
  235. $this->apply('FREE');
  236. $this->post(route('stores.preview.checkout.store', $this->store->slug), [
  237. 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
  238. 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
  239. ])->assertSessionHasErrors(['cart' => 'After the discount, the order total must be at least RM 2.00.']);
  240. }
  241. public function test_a_use_is_counted_only_when_the_order_is_paid_and_only_once()
  242. {
  243. $code = DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10', 'usage_limit' => 2]);
  244. $this->addToCart(2000);
  245. $this->apply('RAYA10');
  246. $order = $this->placeOrder();
  247. $this->assertSame(0, $code->fresh()->times_used);
  248. $this->gateway->pay($order);
  249. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
  250. ->assertInertia(fn (Assert $page) => $page
  251. ->where('view.order.discount_code', 'RAYA10')
  252. ->where('view.order.discount_total', 2));
  253. $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
  254. $this->assertSame(1, $code->fresh()->times_used);
  255. $mail = new OrderConfirmation($order->fresh());
  256. $mail->assertSeeInHtml('RAYA10');
  257. $mail->assertSeeInHtml('-RM 2.00');
  258. }
  259. public function test_messages_follow_the_store_language()
  260. {
  261. $this->store->update(['language' => 'ms']);
  262. DiscountCode::factory()->for($this->store)->percent(15)->create(['code' => 'MERDEKA']);
  263. $this->addToCart(2000);
  264. $this->apply('TIADA')->assertSessionHasErrors(['code' => 'Kod ini tidak sah.']);
  265. $this->apply('MERDEKA');
  266. ($this->cart())(fn (Assert $page) => $page->where('cart.discount.label', 'Diskaun 15%'));
  267. }
  268. public function test_another_merchant_cannot_see_or_change_codes()
  269. {
  270. $code = DiscountCode::factory()->for($this->store)->create();
  271. $this->actingAs(User::factory()->create());
  272. $this->get(route('stores.discounts.index', $this->store->slug))->assertNotFound();
  273. $this->post(route('stores.discounts.store', $this->store->slug), $this->form())->assertNotFound();
  274. $this->get(route('stores.discounts.edit', [$this->store->slug, $code->id]))->assertNotFound();
  275. $this->put(route('stores.discounts.update', [$this->store->slug, $code->id]), $this->form())->assertNotFound();
  276. $this->delete(route('stores.discounts.destroy', [$this->store->slug, $code->id]))->assertNotFound();
  277. $this->assertTrue($code->fresh()->exists);
  278. }
  279. public function test_codes_work_on_the_live_store_too()
  280. {
  281. DiscountCode::factory()->for($this->store)->percent(10)->create(['code' => 'RAYA10']);
  282. $product = Product::factory()->for($this->store)->create(['price' => 2000, 'stock' => 5]);
  283. $this->post(route('stores.publish', $this->store->slug));
  284. auth()->logout();
  285. $live = 'http://'.$this->store->slug.'.localhost';
  286. $this->post($live.'/cart/items', ['product_id' => $product->id, 'quantity' => 1]);
  287. $this->post($live.'/cart/discount', ['code' => 'raya10'])->assertSessionHasNoErrors();
  288. $this->get($live.'/cart')->assertInertia(fn (Assert $page) => $page
  289. ->where('cart.discount.amount', 2)
  290. ->where('cart.total', 18));
  291. $this->delete($live.'/cart/discount');
  292. $this->get($live.'/cart')->assertInertia(fn (Assert $page) => $page->where('cart.discount', null));
  293. }
  294. }