AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PublishingTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Commerce\Payments\PaymentGateway;
  4. use App\Enums\StoreStatus;
  5. use App\Mail\OrderConfirmation;
  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 PublishingTest extends TestCase
  17. {
  18. use RefreshDatabase;
  19. private User $user;
  20. private Store $store;
  21. protected function setUp(): void
  22. {
  23. parent::setUp();
  24. $this->user = User::factory()->create();
  25. $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
  26. $this->store->plan = SamplePlan::for($this->store);
  27. $this->store->save();
  28. }
  29. /** The address shoppers use for a page of the live store. */
  30. private function live(string $path = '/', ?Store $store = null): string
  31. {
  32. return 'http://'.($store ?? $this->store)->slug.'.localhost'.$path;
  33. }
  34. /** A dashboard address. Always on the app's own host, whichever host the last request went to. */
  35. private function app(string $name, mixed $parameters = []): string
  36. {
  37. return 'http://localhost'.route($name, $parameters, false);
  38. }
  39. private function publish(): void
  40. {
  41. $this->actingAs($this->user)->post($this->app('stores.publish', $this->store->slug))
  42. ->assertRedirect($this->app('stores.show', $this->store->slug));
  43. auth()->logout();
  44. }
  45. public function test_a_store_is_private_until_it_is_published()
  46. {
  47. $this->get($this->live())->assertNotFound();
  48. $this->get($this->live('/shop'))->assertNotFound();
  49. $this->get('http://no-such-store.localhost/')->assertNotFound();
  50. $this->actingAs($this->user)
  51. ->get($this->app('stores.show', $this->store->slug))
  52. ->assertInertia(fn (Assert $page) => $page
  53. ->where('store.is_published', false)
  54. ->where('store.status', 'draft')
  55. ->where('store.live_url', 'http://kedai-ali.localhost'));
  56. }
  57. public function test_the_owner_can_publish_and_anyone_can_then_open_the_store()
  58. {
  59. $this->publish();
  60. $this->store->refresh();
  61. $this->assertSame(StoreStatus::Published, $this->store->status);
  62. $this->assertEquals($this->store->plan, $this->store->published_plan);
  63. $this->assertNotNull($this->store->published_at);
  64. // No login.
  65. $this->get($this->live())
  66. ->assertOk()
  67. ->assertInertia(fn (Assert $page) => $page
  68. ->component('stores/preview')
  69. ->where('storeName', 'Kedai Ali')
  70. ->where('preview', false)
  71. // Links are built from the root of the store's own address.
  72. ->where('baseUrl', '')
  73. ->where('view.pageId', 'home')
  74. ->where('auth.user', null));
  75. $this->actingAs($this->user)
  76. ->get($this->app('stores.show', $this->store->slug))
  77. ->assertInertia(fn (Assert $page) => $page
  78. ->where('store.is_published', true)
  79. ->where('store.has_unpublished_changes', false)
  80. ->where('store.status', 'published'));
  81. }
  82. public function test_design_changes_stay_private_until_published_again()
  83. {
  84. $this->publish();
  85. $plan = $this->store->fresh()->plan;
  86. $plan['header']['content']['logoText'] = 'New Name';
  87. $this->store->plan = $plan;
  88. $this->store->save();
  89. $this->get($this->live())
  90. ->assertInertia(fn (Assert $page) => $page->where('plan.header.content.logoText', 'Kedai Ali'));
  91. // The owner's preview shows the working design.
  92. $this->actingAs($this->user)
  93. ->get($this->app('stores.preview', $this->store->slug))
  94. ->assertInertia(fn (Assert $page) => $page
  95. ->where('plan.header.content.logoText', 'New Name')
  96. ->where('preview', true));
  97. $this->get($this->app('stores.show', $this->store->slug))
  98. ->assertInertia(fn (Assert $page) => $page->where('store.has_unpublished_changes', true));
  99. $this->publish();
  100. $this->get($this->live())
  101. ->assertInertia(fn (Assert $page) => $page->where('plan.header.content.logoText', 'New Name'));
  102. }
  103. public function test_taking_a_store_offline_hides_it_again()
  104. {
  105. $this->publish();
  106. $this->actingAs($this->user)->post($this->app('stores.unpublish', $this->store->slug))
  107. ->assertRedirect($this->app('stores.show', $this->store->slug));
  108. auth()->logout();
  109. $this->store->refresh();
  110. $this->assertSame(StoreStatus::Draft, $this->store->status);
  111. $this->assertNull($this->store->published_plan);
  112. $this->assertNotNull($this->store->plan);
  113. $this->get($this->live())->assertNotFound();
  114. }
  115. public function test_a_store_needs_a_design_and_its_owner_to_be_published()
  116. {
  117. $empty = Store::factory()->for($this->user)->create();
  118. $stranger = User::factory()->create();
  119. $this->actingAs($this->user)->post($this->app('stores.publish', $empty->slug))->assertNotFound();
  120. $this->actingAs($stranger)->post($this->app('stores.publish', $this->store->slug))->assertNotFound();
  121. $this->actingAs($stranger)->post($this->app('stores.unpublish', $this->store->slug))->assertNotFound();
  122. auth()->logout();
  123. $this->post($this->app('stores.publish', $this->store->slug))->assertRedirect($this->app('login'));
  124. $this->assertFalse($this->store->fresh()->isPublished());
  125. }
  126. public function test_a_broken_design_cannot_go_live()
  127. {
  128. $plan = $this->store->plan;
  129. $plan['theme']['colors']['primary'] = 'not-a-colour';
  130. $this->store->plan = $plan;
  131. $this->store->save();
  132. $this->actingAs($this->user)->post($this->app('stores.publish', $this->store->slug));
  133. $this->assertFalse($this->store->fresh()->isPublished());
  134. }
  135. public function test_the_live_store_has_shop_product_and_extra_pages()
  136. {
  137. $plan = $this->store->plan;
  138. $plan['pages'][] = [
  139. 'id' => 'about',
  140. 'title' => 'About us',
  141. 'sections' => [[
  142. 'id' => 'about-main',
  143. 'type' => 'about',
  144. 'variant' => 'simple',
  145. 'content' => ['heading' => 'Our story', 'body' => 'We started small.', 'stats' => []],
  146. ]],
  147. ];
  148. $this->store->plan = $plan;
  149. $this->store->save();
  150. $visible = Product::factory()->for($this->store)->create(['name' => 'Sambal']);
  151. $hidden = Product::factory()->for($this->store)->draft()->create();
  152. $this->publish();
  153. $this->get($this->live('/shop'))->assertOk()->assertInertia(fn (Assert $page) => $page
  154. ->where('view.type', 'shop')
  155. ->has('products', 1)
  156. ->where('products.0.name', 'Sambal'));
  157. $this->get($this->live('/products/'.$visible->slug))->assertOk()
  158. ->assertInertia(fn (Assert $page) => $page->where('view.product.name', 'Sambal'));
  159. $this->get($this->live('/products/'.$hidden->slug))->assertNotFound();
  160. $this->get($this->live('/about'))->assertOk()
  161. ->assertInertia(fn (Assert $page) => $page->where('view.pageId', 'about'));
  162. $this->get($this->live('/home'))->assertRedirect('http://kedai-ali.localhost');
  163. $this->get($this->live('/no-such-page'))->assertNotFound();
  164. }
  165. public function test_the_dashboard_and_login_cannot_be_opened_on_a_stores_address()
  166. {
  167. $this->publish();
  168. foreach (['/dashboard', '/login', '/register', '/stores/create', '/settings/profile', '/stores/kedai-ali/preview', '/stripe/webhook'] as $path) {
  169. $this->get($this->live($path))->assertNotFound();
  170. }
  171. $this->post($this->live('/login'), ['email' => 'x@example.com', 'password' => 'x'])->assertNotFound();
  172. $this->actingAs($this->user)->get($this->live('/dashboard'))->assertNotFound();
  173. }
  174. public function test_a_shopper_can_buy_from_a_live_store_without_an_account()
  175. {
  176. Mail::fake();
  177. $gateway = new FakeGateway;
  178. $this->app->instance(PaymentGateway::class, $gateway);
  179. $product = Product::factory()->for($this->store)->create(['name' => 'Sambal', 'price' => 1290, 'stock' => 5]);
  180. $this->publish();
  181. $this->post($this->live('/cart/items'), ['product_id' => $product->id, 'quantity' => 2])->assertRedirect();
  182. $this->get($this->live('/cart'))->assertOk()->assertInertia(fn (Assert $page) => $page
  183. ->where('view.type', 'cart')
  184. ->where('cart.count', 2)
  185. ->where('cart.subtotal', 25.8));
  186. $this->get($this->live('/checkout'))->assertOk()
  187. ->assertInertia(fn (Assert $page) => $page->where('view.type', 'checkout'));
  188. $this->post($this->live('/checkout'), [
  189. 'name' => 'Aminah binti Ali',
  190. 'email' => 'aminah@example.com',
  191. 'phone' => '0123456789',
  192. 'line1' => '12 Jalan Mawar',
  193. 'city' => 'Shah Alam',
  194. 'postcode' => '40000',
  195. 'state' => 'Selangor',
  196. ])->assertRedirect('https://pay.test/cs_fake_1');
  197. $order = Order::firstOrFail();
  198. // The payment page sends the shopper back to the store's own address, not to the dashboard.
  199. $this->assertSame($this->live('/orders/'.$order->public_id), $gateway->successUrl);
  200. $this->assertSame($this->live('/cart'), $gateway->cancelUrl);
  201. $gateway->pay($order);
  202. $this->get($this->live('/orders/'.$order->public_id))->assertOk()->assertInertia(fn (Assert $page) => $page
  203. ->where('view.order.status', 'paid')
  204. ->where('cart.count', 0));
  205. $this->assertSame(3, $product->fresh()->stock);
  206. Mail::assertSent(OrderConfirmation::class, 1);
  207. // An order page only opens on the address of the store it belongs to.
  208. $other = Store::factory()->for($this->user)->create(['name' => 'Other']);
  209. $other->plan = SamplePlan::for($other);
  210. $other->save();
  211. $this->actingAs($this->user)->post($this->app('stores.publish', $other->slug));
  212. auth()->logout();
  213. $this->get($this->live('/orders/'.$order->public_id, $other))->assertNotFound();
  214. $this->get($this->live('/orders/01ARZ3NDEKTSV4RRFFQ69G5FAV'))->assertNotFound();
  215. }
  216. public function test_an_empty_live_cart_sends_checkout_back_to_the_cart()
  217. {
  218. $this->publish();
  219. $this->get($this->live('/checkout'))->assertRedirect($this->live('/cart'));
  220. }
  221. public function test_store_names_that_clash_with_app_addresses_get_a_different_address()
  222. {
  223. $login = Store::factory()->for($this->user)->create(['name' => 'Login']);
  224. $www = Store::factory()->for($this->user)->create(['name' => 'WWW']);
  225. $this->assertSame('login-2', $login->slug);
  226. $this->assertSame('www-2', $www->slug);
  227. }
  228. }