| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Commerce\Payments\PaymentGateway;
- use App\Mail\OrderConfirmation;
- use App\Models\DiscountCode;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Mail;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\FakeGateway;
- use Tests\TestCase;
-
- class DiscountCodeTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- private FakeGateway $gateway;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- Mail::fake();
- $this->gateway = new FakeGateway;
- $this->app->instance(PaymentGateway::class, $this->gateway);
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->actingAs($this->user);
- }
-
- private function addToCart(int $price, int $quantity = 1): Product
- {
- $product = Product::factory()->for($this->store)->create(['price' => $price, 'stock' => 50]);
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity]);
-
- return $product;
- }
-
- private function apply(string $code)
- {
- return $this->post(route('stores.preview.cart.discount', $this->store->slug), ['code' => $code]);
- }
-
- private function cart(): Assert|callable
- {
- return fn (callable $assert) => $this->get(route('stores.preview.cart', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $assert($page));
- }
-
- private function placeOrder(string $state = 'Selangor'): Order
- {
- $this->post(route('stores.preview.checkout.store', $this->store->slug), [
- 'name' => 'Aminah binti Ali',
- 'email' => 'aminah@example.com',
- 'phone' => '0123456789',
- 'line1' => '12 Jalan Mawar',
- 'city' => 'Shah Alam',
- 'postcode' => '40000',
- 'state' => $state,
- ])->assertRedirect();
-
- return Order::latest('id')->firstOrFail();
- }
-
- /** @return array<string, mixed> */
- private function form(array $overrides = []): array
- {
- return [
- 'code' => 'raya10',
- 'type' => 'percent',
- 'value' => '10',
- 'minimum_subtotal' => '',
- 'starts_on' => '',
- 'ends_on' => '',
- 'usage_limit' => '',
- 'active' => '1',
- ...$overrides,
- ];
- }
-
- public function test_the_merchant_can_create_edit_and_delete_codes()
- {
- $this->post(route('stores.discounts.store', $this->store->slug), $this->form([
- 'minimum_subtotal' => '50',
- 'starts_on' => '2027-04-01',
- 'ends_on' => '2027-04-30',
- 'usage_limit' => '100',
- ]))->assertRedirect(route('stores.discounts.index', $this->store->slug));
-
- $code = $this->store->discountCodes()->firstOrFail();
-
- $this->assertSame('RAYA10', $code->code);
- $this->assertSame(10, $code->value);
- $this->assertSame(5000, $code->minimum_subtotal);
- $this->assertSame(100, $code->usage_limit);
- // Days are Malaysian days: 1 April 00:00 in Kuala Lumpur is 31 March 16:00 UTC.
- $this->assertSame('2027-03-31 16:00:00', $code->starts_at->utc()->toDateTimeString());
- $this->assertSame('2027-04-30 15:59:59', $code->ends_at->utc()->toDateTimeString());
-
- $this->get(route('stores.discounts.index', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->component('stores/discounts/index')
- ->has('codes', 1)
- ->where('codes.0.label', '10% off')
- ->where('codes.0.starts_on', '2027-04-01')
- ->where('codes.0.ends_on', '2027-04-30'));
-
- $this->put(route('stores.discounts.update', [$this->store->slug, $code->id]), $this->form([
- 'code' => 'RAYA10', 'type' => 'fixed', 'value' => '7.50', 'active' => '0',
- ]))->assertRedirect(route('stores.discounts.index', $this->store->slug));
-
- $code->refresh();
- $this->assertSame('fixed', $code->type);
- $this->assertSame(750, $code->value);
- $this->assertFalse($code->active);
- $this->assertSame('off', $code->status());
-
- $this->delete(route('stores.discounts.destroy', [$this->store->slug, $code->id]));
- $this->assertSame(0, $this->store->discountCodes()->count());
- }
-
- public function test_code_details_are_checked()
- {
- DiscountCode::factory()->for($this->store)->create(['code' => 'TAKEN']);
-
- $this->post(route('stores.discounts.store', $this->store->slug), $this->form([
- 'code' => 'no spaces!', 'value' => '150', 'ends_on' => '2027-01-01', 'starts_on' => '2027-02-01',
- ]))->assertSessionHasErrors(['code', 'value', 'ends_on']);
-
- $this->post(route('stores.discounts.store', $this->store->slug), $this->form(['code' => 'taken']))
- ->assertSessionHasErrors('code');
-
- $this->post(route('stores.discounts.store', $this->store->slug), $this->form(['type' => 'fixed', 'value' => '0']))
- ->assertSessionHasErrors('value');
-
- // The same code may exist in another store.
- DiscountCode::factory()->create(['code' => 'RAYA10']);
- $this->post(route('stores.discounts.store', $this->store->slug), $this->form())->assertSessionHasNoErrors();
- }
-
- public function test_a_percentage_code_takes_money_off_the_products_in_the_cart()
- {
- DiscountCode::factory()->for($this->store)->percent(10)->create(['code' => 'RAYA10']);
- $this->addToCart(1999, 3);
-
- // Shoppers may type it in small letters with spaces.
- $this->apply(' raya10 ')->assertSessionHasNoErrors();
-
- ($this->cart())(fn (Assert $page) => $page
- ->where('cart.subtotal', 59.97)
- // 10% of 59.97 is 5.997, rounded down to 5.99.
- ->where('cart.discount.code', 'RAYA10')
- ->where('cart.discount.label', '10% off')
- ->where('cart.discount.amount', 5.99)
- ->where('cart.discount.message', null)
- ->where('cart.total', 53.98));
- }
-
- public function test_a_fixed_code_never_takes_off_more_than_the_products_cost()
- {
- DiscountCode::factory()->for($this->store)->fixed(5000)->create(['code' => 'BIG']);
- $this->addToCart(3000);
-
- $this->apply('BIG');
-
- ($this->cart())(fn (Assert $page) => $page->where('cart.discount.amount', 30)->where('cart.total', 0));
- }
-
- public function test_unusable_codes_are_refused_with_a_reason()
- {
- $this->addToCart(2000);
-
- DiscountCode::factory()->for($this->store)->create(['code' => 'OFF', 'active' => false]);
- DiscountCode::factory()->for($this->store)->create(['code' => 'LATER', 'starts_at' => now()->addDay()]);
- DiscountCode::factory()->for($this->store)->create(['code' => 'OLD', 'ends_at' => now()->subMinute()]);
- DiscountCode::factory()->for($this->store)->create(['code' => 'GONE', 'usage_limit' => 5, 'times_used' => 5]);
- DiscountCode::factory()->create(['code' => 'OTHERSTORE']);
-
- $this->apply('NOPE')->assertSessionHasErrors(['code' => 'This code is not valid.']);
- $this->apply('OFF')->assertSessionHasErrors(['code' => 'This code is not valid.']);
- $this->apply('LATER')->assertSessionHasErrors(['code' => 'This code cannot be used yet.']);
- $this->apply('OLD')->assertSessionHasErrors(['code' => 'This code has expired.']);
- $this->apply('GONE')->assertSessionHasErrors(['code' => 'This code has been used up.']);
- $this->apply('OTHERSTORE')->assertSessionHasErrors(['code' => 'This code is not valid.']);
-
- ($this->cart())(fn (Assert $page) => $page->where('cart.discount', null));
- }
-
- public function test_a_code_with_a_minimum_spend_waits_until_the_cart_is_big_enough()
- {
- DiscountCode::factory()->for($this->store)->fixed(500)->create(['code' => 'MIN50', 'minimum_subtotal' => 5000]);
- $this->addToCart(3000);
-
- $this->apply('MIN50')->assertSessionHasNoErrors();
-
- ($this->cart())(fn (Assert $page) => $page
- ->where('cart.discount.code', 'MIN50')
- ->where('cart.discount.amount', 0)
- ->where('cart.discount.message', 'Spend at least RM 50.00 on products to use this code.')
- ->where('cart.total', 30));
-
- $this->addToCart(2000);
-
- ($this->cart())(fn (Assert $page) => $page
- ->where('cart.discount.amount', 5)
- ->where('cart.discount.message', null)
- ->where('cart.total', 45));
- }
-
- public function test_the_code_can_be_removed()
- {
- DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10']);
- $this->addToCart(2000);
- $this->apply('RAYA10');
-
- $this->delete(route('stores.preview.cart.discount.remove', $this->store->slug))->assertRedirect();
-
- ($this->cart())(fn (Assert $page) => $page->where('cart.discount', null)->where('cart.total', 20));
- }
-
- public function test_a_code_switched_off_after_it_was_applied_is_taken_out_and_the_shopper_is_told()
- {
- $code = DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10']);
- $this->addToCart(2000);
- $this->apply('RAYA10');
-
- $code->forceFill(['active' => false])->save();
-
- ($this->cart())(fn (Assert $page) => $page->where('cart.discount', null)->where('cart.adjusted', true));
- }
-
- public function test_the_order_records_the_discount_and_shipping_is_charged_in_full()
- {
- DiscountCode::factory()->for($this->store)->percent(20)->create(['code' => 'SAVE20']);
- $this->addToCart(5000);
- $this->apply('SAVE20');
-
- $order = $this->placeOrder('Sabah');
-
- $this->assertSame(5000, $order->subtotal);
- $this->assertSame('SAVE20', $order->discount_code);
- $this->assertSame(1000, $order->discount_total);
- $this->assertSame(1500, $order->shipping_total);
- $this->assertSame(5500, $order->total);
- }
-
- public function test_free_shipping_is_judged_on_the_price_after_the_discount()
- {
- $this->store->forceFill(['free_shipping_over' => 10000])->save();
- DiscountCode::factory()->for($this->store)->percent(10)->create(['code' => 'SAVE10']);
- $this->addToCart(10000);
- $this->apply('SAVE10');
-
- // RM 100 of products minus 10% is RM 90, which is under the RM 100 free-shipping amount.
- $this->get(route('stores.preview.checkout', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('view.shippingByState.Selangor', 8));
-
- $this->assertSame(800, $this->placeOrder()->shipping_total);
- }
-
- public function test_a_code_that_stopped_working_stops_checkout_so_the_total_never_surprises()
- {
- $code = DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10']);
- $this->addToCart(2000);
- $this->apply('RAYA10');
-
- $code->forceFill(['ends_at' => now()->subMinute()])->save();
-
- $this->post(route('stores.preview.checkout.store', $this->store->slug), [
- 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
- 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
- ])->assertSessionHasErrors(['cart' => 'Your discount code can no longer be used, so it was removed. Please check your total.']);
-
- $this->assertSame(0, Order::count());
- }
-
- public function test_an_order_must_still_be_at_least_two_ringgit_after_the_discount()
- {
- $this->store->forceFill(['free_shipping_over' => 0])->save();
- DiscountCode::factory()->for($this->store)->percent(100)->create(['code' => 'FREE']);
- $this->addToCart(2000);
- $this->apply('FREE');
-
- $this->post(route('stores.preview.checkout.store', $this->store->slug), [
- 'name' => 'Aminah', 'email' => 'a@example.com', 'phone' => '0123456789',
- 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor',
- ])->assertSessionHasErrors(['cart' => 'After the discount, the order total must be at least RM 2.00.']);
- }
-
- public function test_a_use_is_counted_only_when_the_order_is_paid_and_only_once()
- {
- $code = DiscountCode::factory()->for($this->store)->create(['code' => 'RAYA10', 'usage_limit' => 2]);
- $this->addToCart(2000);
- $this->apply('RAYA10');
- $order = $this->placeOrder();
-
- $this->assertSame(0, $code->fresh()->times_used);
-
- $this->gateway->pay($order);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.order.discount_code', 'RAYA10')
- ->where('view.order.discount_total', 2));
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
-
- $this->assertSame(1, $code->fresh()->times_used);
-
- $mail = new OrderConfirmation($order->fresh());
- $mail->assertSeeInHtml('RAYA10');
- $mail->assertSeeInHtml('-RM 2.00');
- }
-
- public function test_messages_follow_the_store_language()
- {
- $this->store->update(['language' => 'ms']);
- DiscountCode::factory()->for($this->store)->percent(15)->create(['code' => 'MERDEKA']);
- $this->addToCart(2000);
-
- $this->apply('TIADA')->assertSessionHasErrors(['code' => 'Kod ini tidak sah.']);
- $this->apply('MERDEKA');
-
- ($this->cart())(fn (Assert $page) => $page->where('cart.discount.label', 'Diskaun 15%'));
- }
-
- public function test_another_merchant_cannot_see_or_change_codes()
- {
- $code = DiscountCode::factory()->for($this->store)->create();
-
- $this->actingAs(User::factory()->create());
-
- $this->get(route('stores.discounts.index', $this->store->slug))->assertNotFound();
- $this->post(route('stores.discounts.store', $this->store->slug), $this->form())->assertNotFound();
- $this->get(route('stores.discounts.edit', [$this->store->slug, $code->id]))->assertNotFound();
- $this->put(route('stores.discounts.update', [$this->store->slug, $code->id]), $this->form())->assertNotFound();
- $this->delete(route('stores.discounts.destroy', [$this->store->slug, $code->id]))->assertNotFound();
-
- $this->assertTrue($code->fresh()->exists);
- }
-
- public function test_codes_work_on_the_live_store_too()
- {
- DiscountCode::factory()->for($this->store)->percent(10)->create(['code' => 'RAYA10']);
- $product = Product::factory()->for($this->store)->create(['price' => 2000, 'stock' => 5]);
- $this->post(route('stores.publish', $this->store->slug));
- auth()->logout();
-
- $live = 'http://'.$this->store->slug.'.localhost';
-
- $this->post($live.'/cart/items', ['product_id' => $product->id, 'quantity' => 1]);
- $this->post($live.'/cart/discount', ['code' => 'raya10'])->assertSessionHasNoErrors();
-
- $this->get($live.'/cart')->assertInertia(fn (Assert $page) => $page
- ->where('cart.discount.amount', 2)
- ->where('cart.total', 18));
-
- $this->delete($live.'/cart/discount');
-
- $this->get($live.'/cart')->assertInertia(fn (Assert $page) => $page->where('cart.discount', null));
- }
- }
|