| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
-
- namespace Tests\Feature\Malaysia;
-
- use App\Commerce\Payments\PaymentGateway;
- use App\Commerce\Payments\StripeGateway;
- use App\Mail\OrderConfirmation;
- use App\Mail\OrderShipped;
- use App\Models\Order;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use App\Support\MalaysianPhone;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Mail;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\Support\FakeGateway;
- use Tests\TestCase;
-
- class ShippingTest 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 checkout(string $state, int $price = 1000, int $quantity = 1): Order
- {
- $product = Product::factory()->for($this->store)->create(['price' => $price, 'stock' => 50]);
-
- // Start every order with an empty cart, since an unpaid order keeps its cart.
- session()->forget('cart.'.$this->store->id);
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => $quantity]);
- $this->post(route('stores.preview.checkout.store', $this->store->slug), [
- 'name' => 'Aminah binti Ali',
- 'email' => 'aminah@example.com',
- 'phone' => '012-345 6789',
- 'line1' => '12 Jalan Mawar',
- 'city' => 'Kota Kinabalu',
- 'postcode' => '88000',
- 'state' => $state,
- ])->assertRedirect();
-
- return Order::latest('id')->firstOrFail();
- }
-
- private function settings(array $overrides = [])
- {
- return $this->put(route('stores.settings.update', $this->store->slug), [
- 'whatsapp_number' => '',
- 'shipping_west' => '8.00',
- 'shipping_east' => '15.00',
- 'free_shipping_over' => '',
- ...$overrides,
- ]);
- }
-
- public function test_a_new_store_has_sensible_starting_shipping_fees()
- {
- $this->get(route('stores.settings', $this->store->slug))
- ->assertOk()
- ->assertInertia(fn (Assert $page) => $page
- ->component('stores/settings')
- ->where('settings.shipping_west', '8.00')
- ->where('settings.shipping_east', '15.00')
- ->where('settings.free_shipping_over', null)
- ->where('settings.whatsapp_number', null));
- }
-
- public function test_the_merchant_can_change_shipping_fees_and_whatsapp_number()
- {
- $this->settings([
- 'whatsapp_number' => '+60 12-345 6789',
- 'shipping_west' => '6.5',
- 'shipping_east' => '18',
- 'free_shipping_over' => '150',
- ])->assertRedirect(route('stores.settings', $this->store->slug));
-
- $this->store->refresh();
-
- $this->assertSame('60123456789', $this->store->whatsapp_number);
- $this->assertSame(650, $this->store->shipping_west);
- $this->assertSame(1800, $this->store->shipping_east);
- $this->assertSame(15000, $this->store->free_shipping_over);
-
- // Clearing the fields removes WhatsApp and free shipping.
- $this->settings();
- $this->store->refresh();
-
- $this->assertNull($this->store->whatsapp_number);
- $this->assertNull($this->store->free_shipping_over);
- }
-
- public function test_settings_are_checked()
- {
- $this->settings(['whatsapp_number' => '12345', 'shipping_west' => '-1', 'shipping_east' => 'abc', 'free_shipping_over' => '1.234'])
- ->assertSessionHasErrors(['shipping_west', 'shipping_east', 'free_shipping_over']);
-
- $this->settings(['whatsapp_number' => '12345'])->assertSessionHasErrors('whatsapp_number');
-
- $this->assertNull($this->store->fresh()->whatsapp_number);
- }
-
- public function test_east_malaysia_pays_the_east_fee_and_the_peninsula_the_west_fee()
- {
- foreach (['Sabah' => 1500, 'Sarawak' => 1500, 'Labuan' => 1500, 'Selangor' => 800, 'Johor' => 800, 'Kuala Lumpur' => 800] as $state => $fee) {
- $order = $this->checkout($state);
-
- $this->assertSame($fee, $order->shipping_total, "Wrong fee for [$state].");
- $this->assertSame(1000 + $fee, $order->total);
- }
- }
-
- public function test_orders_over_the_free_shipping_amount_ship_free()
- {
- $this->settings(['free_shipping_over' => '50']);
-
- $this->assertSame(800, $this->checkout('Selangor', price: 4999)->shipping_total);
- $this->assertSame(0, $this->checkout('Sabah', price: 5000)->shipping_total);
- $this->assertSame(0, $this->checkout('Selangor', price: 2500, quantity: 2)->shipping_total);
-
- $this->settings(['free_shipping_over' => '0']);
-
- $this->assertSame(0, $this->checkout('Sarawak')->shipping_total);
- }
-
- public function test_the_checkout_page_shows_the_fee_for_each_state()
- {
- $this->settings(['free_shipping_over' => '100']);
- $product = Product::factory()->for($this->store)->create(['price' => 2000, 'stock' => 5]);
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]);
-
- $this->get(route('stores.preview.checkout', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.shippingByState.Selangor', 8)
- ->where('view.shippingByState.Sabah', 15)
- ->has('view.shippingByState', 16)
- ->where('shop.free_shipping_over', 100)
- ->where('shop.whatsapp', null));
- }
-
- public function test_a_paid_order_includes_shipping_in_what_must_be_paid_and_in_the_email()
- {
- $order = $this->checkout('Sabah', price: 2580);
-
- // A payment that covers only the products is not enough.
- $this->gateway->pay($order, amount: 2580);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
- $this->assertFalse($order->fresh()->isPaid());
-
- $this->gateway->pay($order, amount: 4080);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.order.status', 'paid')
- ->where('view.order.shipping_total', 15)
- ->where('view.order.total', 40.8));
-
- $mail = new OrderConfirmation($order->fresh());
- $mail->assertSeeInHtml('RM 15.00');
- $mail->assertSeeInHtml('RM 40.80');
- }
-
- public function test_the_merchant_can_mark_a_paid_order_as_shipped_and_the_shopper_is_emailed_once()
- {
- $order = $this->checkout('Selangor');
- $ship = fn (array $data) => $this->post(route('stores.orders.ship', [$this->store->slug, $order->id]), $data);
-
- // Not paid yet.
- $ship(['shipping_courier' => 'J&T Express', 'tracking_number' => 'JT123'])->assertNotFound();
-
- $this->gateway->pay($order);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
-
- $ship(['shipping_courier' => 'Teleport', 'tracking_number' => 'x'])->assertSessionHasErrors('shipping_courier');
- $ship(['shipping_courier' => 'Pos Laju', 'tracking_number' => '<script>'])->assertSessionHasErrors('tracking_number');
-
- $ship(['shipping_courier' => 'J&T Express', 'tracking_number' => ' 630012345678 '])
- ->assertRedirect(route('stores.orders.show', [$this->store->slug, $order->id]));
-
- $order->refresh();
- $this->assertNotNull($order->shipped_at);
- $this->assertSame('J&T Express', $order->shipping_courier);
- $this->assertSame('630012345678', $order->tracking_number);
- Mail::assertSent(OrderShipped::class, fn (OrderShipped $mail) => $mail->hasTo('aminah@example.com'));
-
- // Correcting a typo saves it without emailing again.
- $shippedAt = $order->shipped_at;
- $ship(['shipping_courier' => 'Ninja Van', 'tracking_number' => 'NV999']);
-
- $order->refresh();
- $this->assertSame('Ninja Van', $order->shipping_courier);
- $this->assertEquals($shippedAt, $order->shipped_at);
- Mail::assertSent(OrderShipped::class, 1);
-
- // The shopper sees it on their order page.
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]))
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.order.shipping_courier', 'Ninja Van')
- ->where('view.order.tracking_number', 'NV999'));
- }
-
- public function test_the_shipped_email_is_in_the_store_language()
- {
- $order = Order::factory()->for($this->store)->paid()->create([
- 'number' => 1009, 'shipped_at' => now(), 'shipping_courier' => 'Pos Laju', 'tracking_number' => 'EP123MY',
- ]);
- $order->items()->create(['store_id' => $this->store->id, 'name' => 'Sambal Hijau', 'unit_price' => 1290, 'quantity' => 2, 'line_total' => 2580]);
-
- $mail = new OrderShipped($order);
- $mail->assertHasSubject('Order #1009 is on its way - Kedai Ali');
- $mail->assertSeeInHtml('EP123MY');
- $mail->assertSeeInHtml('Pos Laju');
-
- $this->store->update(['language' => 'ms']);
- $mail = new OrderShipped($order->fresh());
- $mail->assertHasSubject('Pesanan #1009 telah dihantar - Kedai Ali');
- $mail->assertSeeInHtml('Nombor penjejakan');
- }
-
- public function test_another_merchant_cannot_ship_or_change_settings()
- {
- $order = $this->checkout('Selangor');
- $this->gateway->pay($order);
- $this->get(route('stores.preview.order', [$this->store->slug, $order->public_id]));
-
- $this->actingAs(User::factory()->create());
-
- $this->post(route('stores.orders.ship', [$this->store->slug, $order->id]), ['shipping_courier' => 'Pos Laju'])->assertNotFound();
- $this->get(route('stores.settings', $this->store->slug))->assertNotFound();
- $this->settings(['shipping_west' => '0'])->assertNotFound();
-
- $this->assertNull($order->fresh()->shipped_at);
- $this->assertSame(800, $this->store->fresh()->shipping_west);
- }
-
- public function test_the_whatsapp_number_reaches_the_store_and_the_merchant_gets_a_chat_link_to_the_shopper()
- {
- $this->settings(['whatsapp_number' => '0123456789']);
- $order = $this->checkout('Selangor');
-
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('shop.whatsapp', '60123456789'));
-
- $this->get(route('stores.orders.show', [$this->store->slug, $order->id]))
- ->assertInertia(fn (Assert $page) => $page
- ->has('couriers', 10)
- ->where('customer_whatsapp_url', fn (string $url) => str_starts_with($url, 'https://wa.me/60123456789?text=')
- && str_contains(rawurldecode($url), 'order #'.$order->number)));
- }
-
- public function test_malaysian_phone_numbers_are_normalised_for_whatsapp()
- {
- $this->assertSame('60123456789', MalaysianPhone::normalize('012-345 6789'));
- $this->assertSame('60123456789', MalaysianPhone::normalize('+60 12 345 6789'));
- $this->assertSame('601123456789', MalaysianPhone::normalize('011-2345 6789'));
- $this->assertSame('60123456789', MalaysianPhone::normalize('123456789'));
- $this->assertNull(MalaysianPhone::normalize('12345'));
- $this->assertNull(MalaysianPhone::normalize('+65 9123 4567'));
- $this->assertNull(MalaysianPhone::normalize(''));
- $this->assertSame('https://wa.me/60123456789?text=Hi%20there%20%231', MalaysianPhone::whatsappLink('60123456789', 'Hi there #1'));
- }
-
- public function test_checkout_messages_follow_the_store_language()
- {
- $this->store->update(['language' => 'ms']);
- $product = Product::factory()->for($this->store)->create(['price' => 1000, 'stock' => 5]);
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $product->id, 'quantity' => 1]);
-
- $response = $this->post(route('stores.preview.checkout.store', $this->store->slug), [
- 'name' => '', 'email' => 'nope', 'phone' => 'x', 'line1' => 'ab', 'city' => 'KL', 'postcode' => '123', 'state' => 'Atlantis',
- ]);
-
- $response->assertSessionHasErrors([
- 'name' => 'Nama penuh diperlukan.',
- 'email' => 'E-mel mestilah alamat e-mel yang sah.',
- 'phone' => 'Sila masukkan nombor telefon yang sah.',
- 'line1' => 'Alamat mestilah sekurang-kurangnya 3 aksara.',
- 'postcode' => 'Poskod mestilah 5 digit.',
- 'state' => 'Sila pilih negeri.',
- ]);
-
- $soldOut = Product::factory()->for($this->store)->soldOut()->create();
- $this->post(route('stores.preview.cart.add', $this->store->slug), ['product_id' => $soldOut->id, 'quantity' => 1])
- ->assertSessionHasErrors(['quantity' => 'Produk ini telah habis dijual.']);
- }
-
- public function test_stripe_is_given_the_shipping_fee_as_its_own_line()
- {
- $order = Order::factory()->for($this->store)->create(['shipping_total' => 1500]);
- $method = new \ReflectionMethod(StripeGateway::class, 'shippingLabel');
-
- $this->assertSame('Shipping', $method->invoke(new StripeGateway, $order));
- $this->assertSame('Free shipping', $method->invoke(new StripeGateway, $order->replicate()->fill([])->forceFill(['shipping_total' => 0])));
-
- $this->store->update(['language' => 'ms']);
- $this->assertSame('Penghantaran', $method->invoke(new StripeGateway, $order->fresh()));
- }
- }
|