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(); } /** The address shoppers use for a page of the live store. */ private function live(string $path = '/', ?Store $store = null): string { return 'http://'.($store ?? $this->store)->slug.'.localhost'.$path; } /** A dashboard address. Always on the app's own host, whichever host the last request went to. */ private function app(string $name, mixed $parameters = []): string { return 'http://localhost'.route($name, $parameters, false); } private function publish(): void { $this->actingAs($this->user)->post($this->app('stores.publish', $this->store->slug)) ->assertRedirect($this->app('stores.show', $this->store->slug)); auth()->logout(); } public function test_a_store_is_private_until_it_is_published() { $this->get($this->live())->assertNotFound(); $this->get($this->live('/shop'))->assertNotFound(); $this->get('http://no-such-store.localhost/')->assertNotFound(); $this->actingAs($this->user) ->get($this->app('stores.show', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('store.is_published', false) ->where('store.status', 'draft') ->where('store.live_url', 'http://kedai-ali.localhost')); } public function test_the_owner_can_publish_and_anyone_can_then_open_the_store() { $this->publish(); $this->store->refresh(); $this->assertSame(StoreStatus::Published, $this->store->status); $this->assertEquals($this->store->plan, $this->store->published_plan); $this->assertNotNull($this->store->published_at); // No login. $this->get($this->live()) ->assertOk() ->assertInertia(fn (Assert $page) => $page ->component('stores/preview') ->where('storeName', 'Kedai Ali') ->where('preview', false) // Links are built from the root of the store's own address. ->where('baseUrl', '') ->where('view.pageId', 'home') ->where('auth.user', null)); $this->actingAs($this->user) ->get($this->app('stores.show', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('store.is_published', true) ->where('store.has_unpublished_changes', false) ->where('store.status', 'published')); } public function test_design_changes_stay_private_until_published_again() { $this->publish(); $plan = $this->store->fresh()->plan; $plan['header']['content']['logoText'] = 'New Name'; $this->store->plan = $plan; $this->store->save(); $this->get($this->live()) ->assertInertia(fn (Assert $page) => $page->where('plan.header.content.logoText', 'Kedai Ali')); // The owner's preview shows the working design. $this->actingAs($this->user) ->get($this->app('stores.preview', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page ->where('plan.header.content.logoText', 'New Name') ->where('preview', true)); $this->get($this->app('stores.show', $this->store->slug)) ->assertInertia(fn (Assert $page) => $page->where('store.has_unpublished_changes', true)); $this->publish(); $this->get($this->live()) ->assertInertia(fn (Assert $page) => $page->where('plan.header.content.logoText', 'New Name')); } public function test_taking_a_store_offline_hides_it_again() { $this->publish(); $this->actingAs($this->user)->post($this->app('stores.unpublish', $this->store->slug)) ->assertRedirect($this->app('stores.show', $this->store->slug)); auth()->logout(); $this->store->refresh(); $this->assertSame(StoreStatus::Draft, $this->store->status); $this->assertNull($this->store->published_plan); $this->assertNotNull($this->store->plan); $this->get($this->live())->assertNotFound(); } public function test_a_store_needs_a_design_and_its_owner_to_be_published() { $empty = Store::factory()->for($this->user)->create(); $stranger = User::factory()->create(); $this->actingAs($this->user)->post($this->app('stores.publish', $empty->slug))->assertNotFound(); $this->actingAs($stranger)->post($this->app('stores.publish', $this->store->slug))->assertNotFound(); $this->actingAs($stranger)->post($this->app('stores.unpublish', $this->store->slug))->assertNotFound(); auth()->logout(); $this->post($this->app('stores.publish', $this->store->slug))->assertRedirect($this->app('login')); $this->assertFalse($this->store->fresh()->isPublished()); } public function test_a_broken_design_cannot_go_live() { $plan = $this->store->plan; $plan['theme']['colors']['primary'] = 'not-a-colour'; $this->store->plan = $plan; $this->store->save(); $this->actingAs($this->user)->post($this->app('stores.publish', $this->store->slug)); $this->assertFalse($this->store->fresh()->isPublished()); } public function test_the_live_store_has_shop_product_and_extra_pages() { $plan = $this->store->plan; $plan['pages'][] = [ 'id' => 'about', 'title' => 'About us', 'sections' => [[ 'id' => 'about-main', 'type' => 'about', 'variant' => 'simple', 'content' => ['heading' => 'Our story', 'body' => 'We started small.', 'stats' => []], ]], ]; $this->store->plan = $plan; $this->store->save(); $visible = Product::factory()->for($this->store)->create(['name' => 'Sambal']); $hidden = Product::factory()->for($this->store)->draft()->create(); $this->publish(); $this->get($this->live('/shop'))->assertOk()->assertInertia(fn (Assert $page) => $page ->where('view.type', 'shop') ->has('products', 1) ->where('products.0.name', 'Sambal')); $this->get($this->live('/products/'.$visible->slug))->assertOk() ->assertInertia(fn (Assert $page) => $page->where('view.product.name', 'Sambal')); $this->get($this->live('/products/'.$hidden->slug))->assertNotFound(); $this->get($this->live('/about'))->assertOk() ->assertInertia(fn (Assert $page) => $page->where('view.pageId', 'about')); $this->get($this->live('/home'))->assertRedirect('http://kedai-ali.localhost'); $this->get($this->live('/no-such-page'))->assertNotFound(); } public function test_the_dashboard_and_login_cannot_be_opened_on_a_stores_address() { $this->publish(); foreach (['/dashboard', '/login', '/register', '/stores/create', '/settings/profile', '/stores/kedai-ali/preview', '/stripe/webhook'] as $path) { $this->get($this->live($path))->assertNotFound(); } $this->post($this->live('/login'), ['email' => 'x@example.com', 'password' => 'x'])->assertNotFound(); $this->actingAs($this->user)->get($this->live('/dashboard'))->assertNotFound(); } public function test_a_shopper_can_buy_from_a_live_store_without_an_account() { Mail::fake(); $gateway = new FakeGateway; $this->app->instance(PaymentGateway::class, $gateway); $product = Product::factory()->for($this->store)->create(['name' => 'Sambal', 'price' => 1290, 'stock' => 5]); $this->publish(); $this->post($this->live('/cart/items'), ['product_id' => $product->id, 'quantity' => 2])->assertRedirect(); $this->get($this->live('/cart'))->assertOk()->assertInertia(fn (Assert $page) => $page ->where('view.type', 'cart') ->where('cart.count', 2) ->where('cart.subtotal', 25.8)); $this->get($this->live('/checkout'))->assertOk() ->assertInertia(fn (Assert $page) => $page->where('view.type', 'checkout')); $this->post($this->live('/checkout'), [ 'name' => 'Aminah binti Ali', 'email' => 'aminah@example.com', 'phone' => '0123456789', 'line1' => '12 Jalan Mawar', 'city' => 'Shah Alam', 'postcode' => '40000', 'state' => 'Selangor', ])->assertRedirect('https://pay.test/cs_fake_1'); $order = Order::firstOrFail(); // The payment page sends the shopper back to the store's own address, not to the dashboard. $this->assertSame($this->live('/orders/'.$order->public_id), $gateway->successUrl); $this->assertSame($this->live('/cart'), $gateway->cancelUrl); $gateway->pay($order); $this->get($this->live('/orders/'.$order->public_id))->assertOk()->assertInertia(fn (Assert $page) => $page ->where('view.order.status', 'paid') ->where('cart.count', 0)); $this->assertSame(3, $product->fresh()->stock); Mail::assertSent(OrderConfirmation::class, 1); // An order page only opens on the address of the store it belongs to. $other = Store::factory()->for($this->user)->create(['name' => 'Other']); $other->plan = SamplePlan::for($other); $other->save(); $this->actingAs($this->user)->post($this->app('stores.publish', $other->slug)); auth()->logout(); $this->get($this->live('/orders/'.$order->public_id, $other))->assertNotFound(); $this->get($this->live('/orders/01ARZ3NDEKTSV4RRFFQ69G5FAV'))->assertNotFound(); } public function test_an_empty_live_cart_sends_checkout_back_to_the_cart() { $this->publish(); $this->get($this->live('/checkout'))->assertRedirect($this->live('/cart')); } public function test_store_names_that_clash_with_app_addresses_get_a_different_address() { $login = Store::factory()->for($this->user)->create(['name' => 'Login']); $www = Store::factory()->for($this->user)->create(['name' => 'WWW']); $this->assertSame('login-2', $login->slug); $this->assertSame('www-2', $www->slug); } }