AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OrderEmailTest.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Enums\StoreStatus;
  4. use App\Mail\OrderConfirmation;
  5. use App\Mail\OrderShipped;
  6. use App\Models\Order;
  7. use App\Models\Store;
  8. use App\Models\User;
  9. use App\StorePlan\SamplePlan;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Tests\TestCase;
  12. /**
  13. * The emails shoppers get are in the store's own look, with links back to their order.
  14. */
  15. class OrderEmailTest extends TestCase
  16. {
  17. use RefreshDatabase;
  18. private Store $store;
  19. private Order $order;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. config(['app.url' => 'https://aicommerce.test', 'storefront.domain' => 'shops.test']);
  24. $this->store = Store::factory()->for(User::factory())->create(['name' => 'Kedai Ali', 'slug' => 'kedai-ali', 'whatsapp_number' => '60123456789']);
  25. $plan = SamplePlan::for($this->store);
  26. $plan['theme']['colors']['primary'] = '#123456';
  27. $plan['theme']['colors']['primaryForeground'] = '#FEDCBA';
  28. $plan['theme']['fonts']['heading'] = 'fraunces';
  29. $plan['theme']['radius'] = 'large';
  30. $this->store->plan = $plan;
  31. $this->store->save();
  32. $this->order = Order::factory()->for($this->store)->paid()->create(['number' => 1042, 'subtotal' => 2580, 'shipping_total' => 800, 'total' => 3380]);
  33. $this->order->items()->create(['store_id' => $this->store->id, 'name' => 'Sambal Hijau', 'unit_price' => 1290, 'quantity' => 2, 'line_total' => 2580]);
  34. }
  35. private function publish(): void
  36. {
  37. $this->store->forceFill(['status' => StoreStatus::Published, 'published_plan' => $this->store->plan])->save();
  38. }
  39. public function test_the_paid_email_uses_the_stores_colours_fonts_and_corners()
  40. {
  41. $mail = new OrderConfirmation($this->order->fresh());
  42. $mail->assertSeeInHtml('background-color: #123456; color: #FEDCBA', false);
  43. $mail->assertSeeInHtml('&#039;Fraunces&#039;, Georgia', false);
  44. $mail->assertSeeInHtml('border-radius: 16px', false);
  45. $mail->assertSeeInHtml('Sambal Hijau');
  46. $mail->assertSeeInHtml('2 × RM 12.90');
  47. $mail->assertSeeInHtml('RM 33.80');
  48. // Our app's name is not shown to shoppers: they bought from the store.
  49. $mail->assertDontSeeInHtml(config('app.name'));
  50. }
  51. public function test_the_paid_email_links_to_the_order_page_and_whatsapp()
  52. {
  53. $this->publish();
  54. $mail = new OrderConfirmation($this->order->fresh());
  55. $mail->assertSeeInHtml('https://kedai-ali.shops.test/orders/'.$this->order->public_id, false);
  56. $mail->assertSeeInHtml('https://wa.me/60123456789?text='.rawurlencode('Hi Kedai Ali, I have a question about my order #1042.'), false);
  57. $mail->assertSeeInText('View your order: https://kedai-ali.shops.test/orders/'.$this->order->public_id);
  58. $mail->assertSeeInText('Total paid: RM 33.80');
  59. }
  60. public function test_a_store_that_is_not_live_links_to_its_preview_and_hides_whatsapp_when_there_is_no_number()
  61. {
  62. $this->store->forceFill(['whatsapp_number' => null])->save();
  63. $mail = new OrderConfirmation($this->order->fresh());
  64. $mail->assertSeeInHtml(route('stores.preview.order', ['kedai-ali', $this->order->public_id]), false);
  65. $mail->assertDontSeeInHtml('wa.me', false);
  66. }
  67. public function test_the_store_name_comes_from_its_header()
  68. {
  69. $plan = $this->store->plan;
  70. $plan['header']['content']['logoText'] = 'KEDAI ALI & CO';
  71. $this->store->forceFill(['plan' => $plan])->save();
  72. (new OrderConfirmation($this->order->fresh()))->assertSeeInHtml('KEDAI ALI &amp; CO', false);
  73. }
  74. public function test_the_paid_email_is_in_malay_for_a_malay_store()
  75. {
  76. $this->store->update(['language' => 'ms']);
  77. $mail = new OrderConfirmation($this->order->fresh());
  78. $mail->assertSeeInHtml('Terima kasih atas pesanan anda');
  79. $mail->assertSeeInHtml('Lihat pesanan anda');
  80. $mail->assertSeeInHtml('Jumlah dibayar');
  81. $mail->assertSeeInHtml(rawurlencode('Hai Kedai Ali, saya ada soalan tentang pesanan saya #1042.'), false);
  82. }
  83. public function test_the_shipped_email_has_a_tracking_button_when_the_courier_gives_a_link()
  84. {
  85. $this->order->forceFill(['shipped_at' => now(), 'shipping_courier' => 'J&T Express', 'tracking_number' => 'JT630012345678', 'tracking_url' => 'https://track.example.com/JT630012345678'])->save();
  86. $mail = new OrderShipped($this->order->fresh());
  87. $mail->assertSeeInHtml('Track your parcel');
  88. $mail->assertSeeInHtml('https://track.example.com/JT630012345678', false);
  89. $mail->assertSeeInHtml('JT630012345678');
  90. $mail->assertSeeInHtml('background-color: #123456; color: #FEDCBA', false);
  91. $mail->assertSeeInText('Tracking number: JT630012345678');
  92. $mail->assertSeeInText('J&T Express');
  93. }
  94. public function test_without_a_tracking_link_the_shipped_email_explains_how_to_track()
  95. {
  96. $this->order->forceFill(['shipped_at' => now(), 'shipping_courier' => 'Pos Laju', 'tracking_number' => 'EP123MY'])->save();
  97. $mail = new OrderShipped($this->order->fresh());
  98. $mail->assertDontSeeInHtml('Track your parcel');
  99. $mail->assertSeeInHtml('View your order');
  100. $mail->assertSeeInHtml("Use the tracking number on the courier's website");
  101. }
  102. public function test_a_store_without_a_design_still_gets_a_tidy_email()
  103. {
  104. $this->store->forceFill(['plan' => null])->save();
  105. (new OrderConfirmation($this->order->fresh()))
  106. ->assertSeeInHtml('Kedai Ali')
  107. ->assertSeeInHtml('background-color: #1F1F1F', false);
  108. }
  109. public function test_emails_come_under_the_stores_name_and_replies_go_to_its_contact_email()
  110. {
  111. config(['mail.from.address' => 'orders@aicommerce.test']);
  112. $this->store->forceFill(['contact_email' => 'hello@kedaiali.my'])->save();
  113. foreach ([new OrderConfirmation($this->order->fresh()), new OrderShipped($this->order->fresh())] as $mail) {
  114. $mail->assertFrom('orders@aicommerce.test', 'Kedai Ali');
  115. $mail->assertHasReplyTo('hello@kedaiali.my', 'Kedai Ali');
  116. $mail->assertSeeInHtml('mailto:hello@kedaiali.my', false);
  117. $mail->assertSeeInText('Questions? Reply to this email or write to hello@kedaiali.my');
  118. }
  119. }
  120. public function test_without_a_contact_email_there_is_no_reply_address()
  121. {
  122. $mail = new OrderConfirmation($this->order->fresh());
  123. $this->assertSame([], $mail->envelope()->replyTo);
  124. $mail->assertDontSeeInHtml('mailto:', false);
  125. }
  126. public function test_the_merchant_sets_the_contact_email_in_settings()
  127. {
  128. $this->actingAs($this->store->user);
  129. $save = fn (array $data) => $this->put(route('stores.settings.update', $this->store->slug), [
  130. 'shipping_west' => '8.00',
  131. 'shipping_east' => '15.00',
  132. ...$data,
  133. ]);
  134. $this->get(route('stores.settings', $this->store->slug))
  135. ->assertInertia(fn ($page) => $page
  136. ->where('settings.contact_email', null)
  137. ->where('accountEmail', $this->store->user->email));
  138. $save(['contact_email' => 'not-an-email'])->assertSessionHasErrors('contact_email');
  139. $save(['contact_email' => ' Hello@KedaiAli.my '])->assertSessionHasNoErrors();
  140. $this->assertSame('hello@kedaiali.my', $this->store->fresh()->contact_email);
  141. // Saving a form without the field keeps it; clearing it removes it.
  142. $save([])->assertSessionHasNoErrors();
  143. $this->assertSame('hello@kedaiali.my', $this->store->fresh()->contact_email);
  144. $save(['contact_email' => ''])->assertSessionHasNoErrors();
  145. $this->assertNull($this->store->fresh()->contact_email);
  146. }
  147. }