| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Enums\ProductStatus;
- use App\Models\Product;
- use App\Models\Store;
- use App\Models\User;
- use App\StorePlan\SamplePlan;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\Storage;
- use Inertia\Testing\AssertableInertia as Assert;
- use Tests\TestCase;
-
- class ProductTest extends TestCase
- {
- use RefreshDatabase;
-
- /**
- * @return array<string, mixed>
- */
- private function form(array $overrides = []): array
- {
- return [
- 'name' => 'Sambal Hijau 250g',
- 'description' => 'Green chilli sambal.',
- 'price' => '12.90',
- 'compare_at_price' => '',
- 'stock' => 20,
- 'status' => 'active',
- ...$overrides,
- ];
- }
-
- public function test_a_user_can_add_a_product_with_photos()
- {
- Storage::fake('public');
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- $this->actingAs($user)
- ->post(route('stores.products.store', $store->slug), $this->form([
- 'compare_at_price' => '15.90',
- 'images' => [UploadedFile::fake()->image('a.jpg'), UploadedFile::fake()->image('b.png')],
- ]))
- ->assertRedirect(route('stores.products.index', $store->slug));
-
- $product = $store->products()->firstOrFail();
-
- $this->assertSame('sambal-hijau-250g', $product->slug);
- $this->assertSame(1290, $product->price);
- $this->assertSame(1590, $product->compare_at_price);
- $this->assertSame(20, $product->stock);
- $this->assertSame(ProductStatus::Active, $product->status);
- $this->assertCount(2, $product->images);
- Storage::disk('public')->assertExists($product->images[0]->path);
- $this->assertSame($store->id, $product->images[0]->store_id);
-
- $this->actingAs($user)
- ->get(route('stores.products.index', $store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->component('stores/products/index')
- ->has('products', 1)
- ->where('products.0.price', '12.90')
- ->where('products.0.compare_at_price', '15.90')
- ->has('products.0.images', 2));
- }
-
- public function test_prices_are_stored_exactly_in_sen()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- foreach (['0.07' => 7, '19.99' => 1999, '1234' => 123400, '0' => 0] as $typed => $sen) {
- $this->actingAs($user)->post(route('stores.products.store', $store->slug), $this->form(['name' => "Item $typed", 'price' => $typed]));
-
- $this->assertSame($sen, $store->products()->where('name', "Item $typed")->firstOrFail()->price);
- }
- }
-
- public function test_product_details_are_checked()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
-
- $this->actingAs($user)
- ->post(route('stores.products.store', $store->slug), $this->form([
- 'name' => '',
- 'price' => '12.999',
- 'compare_at_price' => '5',
- 'stock' => -1,
- 'status' => 'secret',
- 'images' => [UploadedFile::fake()->create('virus.pdf', 10, 'application/pdf')],
- ]))
- ->assertSessionHasErrors(['name', 'price', 'compare_at_price', 'stock', 'status', 'images.0']);
-
- $this->assertSame(0, $store->products()->count());
- }
-
- public function test_two_products_with_the_same_name_get_different_addresses()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $other = Store::factory()->for($user)->create();
-
- $first = Product::factory()->for($store)->create(['name' => 'Sambal']);
- $second = Product::factory()->for($store)->create(['name' => 'Sambal']);
- $elsewhere = Product::factory()->for($other)->create(['name' => 'Sambal']);
-
- $this->assertSame('sambal', $first->slug);
- $this->assertSame('sambal-2', $second->slug);
- $this->assertSame('sambal', $elsewhere->slug);
- }
-
- public function test_a_user_can_edit_a_product_and_manage_its_photos()
- {
- Storage::fake('public');
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $product = Product::factory()->for($store)->create(['name' => 'Old name']);
- $image = $product->images()->create([
- 'store_id' => $store->id,
- 'path' => UploadedFile::fake()->image('old.jpg')->store("products/{$store->id}", 'public'),
- 'position' => 1,
- ]);
-
- $this->actingAs($user)
- ->get(route('stores.products.edit', [$store->slug, $product->id]))
- ->assertInertia(fn (Assert $page) => $page
- ->component('stores/products/edit')
- ->where('product.name', 'Old name')
- ->has('product.images', 1));
-
- $this->actingAs($user)
- ->post(route('stores.products.update', [$store->slug, $product->id]), $this->form([
- 'name' => 'New name',
- 'price' => '8.50',
- 'status' => 'draft',
- 'images' => [UploadedFile::fake()->image('new.jpg')],
- ]))
- ->assertRedirect(route('stores.products.index', $store->slug));
-
- $product->refresh();
-
- $this->assertSame('New name', $product->name);
- // The address stays the same when the name changes, so old links keep working.
- $this->assertSame('old-name', $product->slug);
- $this->assertSame(850, $product->price);
- $this->assertNull($product->compare_at_price);
- $this->assertSame(ProductStatus::Draft, $product->status);
- $this->assertCount(2, $product->images);
-
- $this->actingAs($user)
- ->delete(route('stores.products.images.destroy', [$store->slug, $product->id, $image->id]))
- ->assertRedirect();
-
- $this->assertCount(1, $product->fresh()->images);
- Storage::disk('public')->assertMissing($image->path);
- }
-
- public function test_a_product_cannot_have_more_than_five_photos()
- {
- Storage::fake('public');
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $product = Product::factory()->for($store)->create();
-
- foreach (range(1, 4) as $position) {
- $product->images()->create(['store_id' => $store->id, 'path' => "products/x$position.jpg", 'position' => $position]);
- }
-
- $this->actingAs($user)
- ->post(route('stores.products.update', [$store->slug, $product->id]), $this->form([
- 'images' => [UploadedFile::fake()->image('5.jpg'), UploadedFile::fake()->image('6.jpg')],
- ]))
- ->assertSessionHasErrors('images');
-
- $this->assertCount(4, $product->fresh()->images);
- }
-
- public function test_deleting_a_product_removes_its_photos()
- {
- Storage::fake('public');
-
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $product = Product::factory()->for($store)->create();
- $path = UploadedFile::fake()->image('a.jpg')->store("products/{$store->id}", 'public');
- $product->images()->create(['store_id' => $store->id, 'path' => $path, 'position' => 1]);
-
- $this->actingAs($user)
- ->delete(route('stores.products.destroy', [$store->slug, $product->id]))
- ->assertRedirect(route('stores.products.index', $store->slug));
-
- $this->assertSame(0, $store->products()->count());
- $this->assertDatabaseCount('product_images', 0);
- Storage::disk('public')->assertMissing($path);
- }
-
- public function test_a_user_cannot_see_or_change_someone_elses_products()
- {
- $user = User::factory()->create();
- $mine = Store::factory()->for($user)->create();
- $theirs = Store::factory()->create();
- $product = Product::factory()->for($theirs)->create();
-
- $this->get(route('stores.products.index', $theirs->slug))->assertRedirect(route('login'));
-
- $this->actingAs($user)->get(route('stores.products.index', $theirs->slug))->assertNotFound();
- $this->actingAs($user)->post(route('stores.products.store', $theirs->slug), $this->form())->assertNotFound();
- $this->actingAs($user)->get(route('stores.products.edit', [$theirs->slug, $product->id]))->assertNotFound();
- // Their product id inside my own store's address must not work either.
- $this->actingAs($user)->get(route('stores.products.edit', [$mine->slug, $product->id]))->assertNotFound();
- $this->actingAs($user)->post(route('stores.products.update', [$mine->slug, $product->id]), $this->form())->assertNotFound();
- $this->actingAs($user)->delete(route('stores.products.destroy', [$mine->slug, $product->id]))->assertNotFound();
-
- $this->assertSame(1, $theirs->products()->count());
- $this->assertSame($product->name, $product->fresh()->name);
- }
-
- public function test_the_storefront_shows_only_active_products()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $store->plan = SamplePlan::for($store);
- $store->save();
-
- $active = Product::factory()->for($store)->create(['name' => 'Visible', 'price' => 1290, 'compare_at_price' => 1590]);
- $soldOut = Product::factory()->for($store)->soldOut()->create(['name' => 'Gone']);
- $draft = Product::factory()->for($store)->draft()->create(['name' => 'Hidden']);
- Product::factory()->create(['name' => 'Another store']);
-
- $this->actingAs($user)
- ->get(route('stores.preview.shop', $store->slug))
- ->assertOk()
- ->assertInertia(fn (Assert $page) => $page
- ->component('stores/preview')
- ->where('view.type', 'shop')
- ->has('products', 2)
- ->where('products.0.name', 'Gone')
- ->where('products.0.in_stock', false)
- ->where('products.1.name', 'Visible')
- ->where('products.1.price', 12.9)
- ->where('products.1.compare_at_price', 15.9)
- ->where('products.1.in_stock', true));
-
- // The home page gets the same products, so its product section can show them.
- $this->actingAs($user)
- ->get(route('stores.preview', $store->slug))
- ->assertInertia(fn (Assert $page) => $page->has('products', 2));
-
- $this->actingAs($user)
- ->get(route('stores.preview.product', [$store->slug, $active->slug]))
- ->assertOk()
- ->assertInertia(fn (Assert $page) => $page
- ->where('view.type', 'product')
- ->where('view.product.name', 'Visible'));
-
- $this->actingAs($user)->get(route('stores.preview.product', [$store->slug, $soldOut->slug]))->assertOk();
- $this->actingAs($user)->get(route('stores.preview.product', [$store->slug, $draft->slug]))->assertNotFound();
- $this->actingAs($user)->get(route('stores.preview.product', [$store->slug, 'no-such-product']))->assertNotFound();
- }
-
- public function test_storefront_pages_need_a_design_and_the_owner()
- {
- $user = User::factory()->create();
- $store = Store::factory()->for($user)->create();
- $product = Product::factory()->for($store)->create();
-
- $this->actingAs($user)->get(route('stores.preview.shop', $store->slug))->assertNotFound();
-
- $store->plan = SamplePlan::for($store);
- $store->save();
-
- $stranger = User::factory()->create();
-
- $this->actingAs($stranger)->get(route('stores.preview.shop', $store->slug))->assertNotFound();
- $this->actingAs($stranger)->get(route('stores.preview.product', [$store->slug, $product->slug]))->assertNotFound();
- }
- }
|