| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Models\Store;
- use App\Models\Subscription;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\TestCase;
-
- class StoreTest extends TestCase
- {
- use RefreshDatabase;
-
- /**
- * @return array<string, string>
- */
- 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();
- // The free trial allows one store; this plan allows three.
- Subscription::factory()->for($user)->plan('growth')->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();
- }
- }
|