*/ private function validData(): array { return [ 'name' => 'Sambal Nenek', 'category' => 'food', 'description' => 'Homemade sambal from a family recipe, premium and warm.', 'language' => 'ms', ]; } public function test_guests_cannot_reach_store_pages() { $this->get(route('stores.create'))->assertRedirect(route('login')); $this->post(route('stores.store'), $this->validData())->assertRedirect(route('login')); $this->assertDatabaseCount('stores', 0); } public function test_a_user_can_create_a_store() { $user = User::factory()->create(); $response = $this->actingAs($user)->post(route('stores.store'), $this->validData()); $response->assertRedirect(route('stores.show', 'sambal-nenek')); $this->assertDatabaseHas('stores', [ 'user_id' => $user->id, 'name' => 'Sambal Nenek', 'slug' => 'sambal-nenek', 'category' => 'food', 'language' => 'ms', 'status' => 'draft', ]); } public function test_store_details_are_validated() { $user = User::factory()->create(); $response = $this->actingAs($user)->post(route('stores.store'), [ 'name' => '', 'category' => 'not-a-category', 'description' => 'too short', 'language' => 'fr', ]); $response->assertSessionHasErrors(['name', 'category', 'description', 'language']); $this->assertDatabaseCount('stores', 0); } public function test_a_user_cannot_set_the_owner_or_status_of_a_store() { $user = User::factory()->create(); $other = User::factory()->create(); $this->actingAs($user)->post(route('stores.store'), [ ...$this->validData(), 'user_id' => $other->id, 'status' => 'published', 'slug' => 'admin', ]); $store = Store::query()->sole(); $this->assertSame($user->id, $store->user_id); $this->assertSame('draft', $store->status->value); $this->assertSame('sambal-nenek', $store->slug); } public function test_stores_with_the_same_name_get_different_slugs() { $user = User::factory()->create(); $this->actingAs($user); $this->post(route('stores.store'), $this->validData()); $this->post(route('stores.store'), $this->validData()); $this->assertSame(['sambal-nenek', 'sambal-nenek-2'], Store::query()->orderBy('id')->pluck('slug')->all()); } public function test_reserved_words_are_not_used_as_slugs() { $this->assertSame('admin-2', Store::uniqueSlug('Admin')); } public function test_the_dashboard_lists_only_the_users_own_stores() { $user = User::factory()->create(); $mine = Store::factory()->for($user)->create(); Store::factory()->create(); $this->actingAs($user) ->get(route('dashboard')) ->assertOk() ->assertInertia(fn (Assert $page) => $page ->component('dashboard') ->has('stores', 1) ->where('stores.0.slug', $mine->slug)); } public function test_a_user_can_view_their_own_store() { $user = User::factory()->create(); $store = Store::factory()->for($user)->create(); $this->actingAs($user) ->get(route('stores.show', $store->slug)) ->assertOk() ->assertInertia(fn (Assert $page) => $page ->component('stores/show') ->where('store.name', $store->name)); } public function test_a_user_cannot_view_someone_elses_store() { $user = User::factory()->create(); $store = Store::factory()->create(); $this->actingAs($user)->get(route('stores.show', $store->slug))->assertNotFound(); } }