| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Enums\StoreStatus;
- use App\Mail\OrderConfirmation;
- use App\Mail\OrderShipped;
- use App\Models\Order;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
-
- /**
- * The emails shoppers get are in the store's own look, with links back to their order.
- */
- class OrderEmailTest extends TestCase
- {
- use RefreshDatabase;
-
- private Store $store;
-
- private Order $order;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- config(['app.url' => 'https://aicommerce.test', 'storefront.domain' => 'shops.test']);
-
- $this->store = Store::factory()->for(User::factory())->create(['name' => 'Kedai Ali', 'slug' => 'kedai-ali', 'whatsapp_number' => '60123456789']);
- $plan = SamplePlan::for($this->store);
- $plan['theme']['colors']['primary'] = '#123456';
- $plan['theme']['colors']['primaryForeground'] = '#FEDCBA';
- $plan['theme']['fonts']['heading'] = 'fraunces';
- $plan['theme']['radius'] = 'large';
- $this->store->plan = $plan;
- $this->store->save();
-
- $this->order = Order::factory()->for($this->store)->paid()->create(['number' => 1042, 'subtotal' => 2580, 'shipping_total' => 800, 'total' => 3380]);
- $this->order->items()->create(['store_id' => $this->store->id, 'name' => 'Sambal Hijau', 'unit_price' => 1290, 'quantity' => 2, 'line_total' => 2580]);
- }
-
- private function publish(): void
- {
- $this->store->forceFill(['status' => StoreStatus::Published, 'published_plan' => $this->store->plan])->save();
- }
-
- public function test_the_paid_email_uses_the_stores_colours_fonts_and_corners()
- {
- $mail = new OrderConfirmation($this->order->fresh());
-
- $mail->assertSeeInHtml('background-color: #123456; color: #FEDCBA', false);
- $mail->assertSeeInHtml(''Fraunces', Georgia', false);
- $mail->assertSeeInHtml('border-radius: 16px', false);
- $mail->assertSeeInHtml('Sambal Hijau');
- $mail->assertSeeInHtml('2 × RM 12.90');
- $mail->assertSeeInHtml('RM 33.80');
- // Our app's name is not shown to shoppers: they bought from the store.
- $mail->assertDontSeeInHtml(config('app.name'));
- }
-
- public function test_the_paid_email_links_to_the_order_page_and_whatsapp()
- {
- $this->publish();
- $mail = new OrderConfirmation($this->order->fresh());
-
- $mail->assertSeeInHtml('https://kedai-ali.shops.test/orders/'.$this->order->public_id, false);
- $mail->assertSeeInHtml('https://wa.me/60123456789?text='.rawurlencode('Hi Kedai Ali, I have a question about my order #1042.'), false);
- $mail->assertSeeInText('View your order: https://kedai-ali.shops.test/orders/'.$this->order->public_id);
- $mail->assertSeeInText('Total paid: RM 33.80');
- }
-
- public function test_a_store_that_is_not_live_links_to_its_preview_and_hides_whatsapp_when_there_is_no_number()
- {
- $this->store->forceFill(['whatsapp_number' => null])->save();
- $mail = new OrderConfirmation($this->order->fresh());
-
- $mail->assertSeeInHtml(route('stores.preview.order', ['kedai-ali', $this->order->public_id]), false);
- $mail->assertDontSeeInHtml('wa.me', false);
- }
-
- public function test_the_store_name_comes_from_its_header()
- {
- $plan = $this->store->plan;
- $plan['header']['content']['logoText'] = 'KEDAI ALI & CO';
- $this->store->forceFill(['plan' => $plan])->save();
-
- (new OrderConfirmation($this->order->fresh()))->assertSeeInHtml('KEDAI ALI & CO', false);
- }
-
- public function test_the_paid_email_is_in_malay_for_a_malay_store()
- {
- $this->store->update(['language' => 'ms']);
- $mail = new OrderConfirmation($this->order->fresh());
-
- $mail->assertSeeInHtml('Terima kasih atas pesanan anda');
- $mail->assertSeeInHtml('Lihat pesanan anda');
- $mail->assertSeeInHtml('Jumlah dibayar');
- $mail->assertSeeInHtml(rawurlencode('Hai Kedai Ali, saya ada soalan tentang pesanan saya #1042.'), false);
- }
-
- public function test_the_shipped_email_has_a_tracking_button_when_the_courier_gives_a_link()
- {
- $this->order->forceFill(['shipped_at' => now(), 'shipping_courier' => 'J&T Express', 'tracking_number' => 'JT630012345678', 'tracking_url' => 'https://track.example.com/JT630012345678'])->save();
- $mail = new OrderShipped($this->order->fresh());
-
- $mail->assertSeeInHtml('Track your parcel');
- $mail->assertSeeInHtml('https://track.example.com/JT630012345678', false);
- $mail->assertSeeInHtml('JT630012345678');
- $mail->assertSeeInHtml('background-color: #123456; color: #FEDCBA', false);
- $mail->assertSeeInText('Tracking number: JT630012345678');
- $mail->assertSeeInText('J&T Express');
- }
-
- public function test_without_a_tracking_link_the_shipped_email_explains_how_to_track()
- {
- $this->order->forceFill(['shipped_at' => now(), 'shipping_courier' => 'Pos Laju', 'tracking_number' => 'EP123MY'])->save();
- $mail = new OrderShipped($this->order->fresh());
-
- $mail->assertDontSeeInHtml('Track your parcel');
- $mail->assertSeeInHtml('View your order');
- $mail->assertSeeInHtml("Use the tracking number on the courier's website");
- }
-
- public function test_a_store_without_a_design_still_gets_a_tidy_email()
- {
- $this->store->forceFill(['plan' => null])->save();
-
- (new OrderConfirmation($this->order->fresh()))
- ->assertSeeInHtml('Kedai Ali')
- ->assertSeeInHtml('background-color: #1F1F1F', false);
- }
-
- public function test_emails_come_under_the_stores_name_and_replies_go_to_its_contact_email()
- {
- config(['mail.from.address' => 'orders@aicommerce.test']);
- $this->store->forceFill(['contact_email' => 'hello@kedaiali.my'])->save();
-
- foreach ([new OrderConfirmation($this->order->fresh()), new OrderShipped($this->order->fresh())] as $mail) {
- $mail->assertFrom('orders@aicommerce.test', 'Kedai Ali');
- $mail->assertHasReplyTo('hello@kedaiali.my', 'Kedai Ali');
- $mail->assertSeeInHtml('mailto:hello@kedaiali.my', false);
- $mail->assertSeeInText('Questions? Reply to this email or write to hello@kedaiali.my');
- }
- }
-
- public function test_without_a_contact_email_there_is_no_reply_address()
- {
- $mail = new OrderConfirmation($this->order->fresh());
-
- $this->assertSame([], $mail->envelope()->replyTo);
- $mail->assertDontSeeInHtml('mailto:', false);
- }
-
- public function test_the_merchant_sets_the_contact_email_in_settings()
- {
- $this->actingAs($this->store->user);
- $save = fn (array $data) => $this->put(route('stores.settings.update', $this->store->slug), [
- 'shipping_west' => '8.00',
- 'shipping_east' => '15.00',
- ...$data,
- ]);
-
- $this->get(route('stores.settings', $this->store->slug))
- ->assertInertia(fn ($page) => $page
- ->where('settings.contact_email', null)
- ->where('accountEmail', $this->store->user->email));
-
- $save(['contact_email' => 'not-an-email'])->assertSessionHasErrors('contact_email');
-
- $save(['contact_email' => ' Hello@KedaiAli.my '])->assertSessionHasNoErrors();
- $this->assertSame('hello@kedaiali.my', $this->store->fresh()->contact_email);
-
- // Saving a form without the field keeps it; clearing it removes it.
- $save([])->assertSessionHasNoErrors();
- $this->assertSame('hello@kedaiali.my', $this->store->fresh()->contact_email);
- $save(['contact_email' => ''])->assertSessionHasNoErrors();
- $this->assertNull($this->store->fresh()->contact_email);
- }
- }
|