AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProductTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Enums\ProductStatus;
  4. use App\Models\Product;
  5. use App\Models\Store;
  6. use App\Models\User;
  7. use App\StorePlan\SamplePlan;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Http\UploadedFile;
  10. use Illuminate\Support\Facades\Storage;
  11. use Inertia\Testing\AssertableInertia as Assert;
  12. use Tests\TestCase;
  13. class ProductTest extends TestCase
  14. {
  15. use RefreshDatabase;
  16. /**
  17. * @return array<string, mixed>
  18. */
  19. private function form(array $overrides = []): array
  20. {
  21. return [
  22. 'name' => 'Sambal Hijau 250g',
  23. 'description' => 'Green chilli sambal.',
  24. 'price' => '12.90',
  25. 'compare_at_price' => '',
  26. 'stock' => 20,
  27. 'status' => 'active',
  28. ...$overrides,
  29. ];
  30. }
  31. public function test_a_user_can_add_a_product_with_photos()
  32. {
  33. Storage::fake('public');
  34. $user = User::factory()->create();
  35. $store = Store::factory()->for($user)->create();
  36. $this->actingAs($user)
  37. ->post(route('stores.products.store', $store->slug), $this->form([
  38. 'compare_at_price' => '15.90',
  39. 'images' => [UploadedFile::fake()->image('a.jpg'), UploadedFile::fake()->image('b.png')],
  40. ]))
  41. ->assertRedirect(route('stores.products.index', $store->slug));
  42. $product = $store->products()->firstOrFail();
  43. $this->assertSame('sambal-hijau-250g', $product->slug);
  44. $this->assertSame(1290, $product->price);
  45. $this->assertSame(1590, $product->compare_at_price);
  46. $this->assertSame(20, $product->stock);
  47. $this->assertSame(ProductStatus::Active, $product->status);
  48. $this->assertCount(2, $product->images);
  49. Storage::disk('public')->assertExists($product->images[0]->path);
  50. $this->assertSame($store->id, $product->images[0]->store_id);
  51. $this->actingAs($user)
  52. ->get(route('stores.products.index', $store->slug))
  53. ->assertInertia(fn (Assert $page) => $page
  54. ->component('stores/products/index')
  55. ->has('products', 1)
  56. ->where('products.0.price', '12.90')
  57. ->where('products.0.compare_at_price', '15.90')
  58. ->has('products.0.images', 2));
  59. }
  60. public function test_prices_are_stored_exactly_in_sen()
  61. {
  62. $user = User::factory()->create();
  63. $store = Store::factory()->for($user)->create();
  64. foreach (['0.07' => 7, '19.99' => 1999, '1234' => 123400, '0' => 0] as $typed => $sen) {
  65. $this->actingAs($user)->post(route('stores.products.store', $store->slug), $this->form(['name' => "Item $typed", 'price' => $typed]));
  66. $this->assertSame($sen, $store->products()->where('name', "Item $typed")->firstOrFail()->price);
  67. }
  68. }
  69. public function test_product_details_are_checked()
  70. {
  71. $user = User::factory()->create();
  72. $store = Store::factory()->for($user)->create();
  73. $this->actingAs($user)
  74. ->post(route('stores.products.store', $store->slug), $this->form([
  75. 'name' => '',
  76. 'price' => '12.999',
  77. 'compare_at_price' => '5',
  78. 'stock' => -1,
  79. 'status' => 'secret',
  80. 'images' => [UploadedFile::fake()->create('virus.pdf', 10, 'application/pdf')],
  81. ]))
  82. ->assertSessionHasErrors(['name', 'price', 'compare_at_price', 'stock', 'status', 'images.0']);
  83. $this->assertSame(0, $store->products()->count());
  84. }
  85. public function test_two_products_with_the_same_name_get_different_addresses()
  86. {
  87. $user = User::factory()->create();
  88. $store = Store::factory()->for($user)->create();
  89. $other = Store::factory()->for($user)->create();
  90. $first = Product::factory()->for($store)->create(['name' => 'Sambal']);
  91. $second = Product::factory()->for($store)->create(['name' => 'Sambal']);
  92. $elsewhere = Product::factory()->for($other)->create(['name' => 'Sambal']);
  93. $this->assertSame('sambal', $first->slug);
  94. $this->assertSame('sambal-2', $second->slug);
  95. $this->assertSame('sambal', $elsewhere->slug);
  96. }
  97. public function test_a_user_can_edit_a_product_and_manage_its_photos()
  98. {
  99. Storage::fake('public');
  100. $user = User::factory()->create();
  101. $store = Store::factory()->for($user)->create();
  102. $product = Product::factory()->for($store)->create(['name' => 'Old name']);
  103. $image = $product->images()->create([
  104. 'store_id' => $store->id,
  105. 'path' => UploadedFile::fake()->image('old.jpg')->store("products/{$store->id}", 'public'),
  106. 'position' => 1,
  107. ]);
  108. $this->actingAs($user)
  109. ->get(route('stores.products.edit', [$store->slug, $product->id]))
  110. ->assertInertia(fn (Assert $page) => $page
  111. ->component('stores/products/edit')
  112. ->where('product.name', 'Old name')
  113. ->has('product.images', 1));
  114. $this->actingAs($user)
  115. ->post(route('stores.products.update', [$store->slug, $product->id]), $this->form([
  116. 'name' => 'New name',
  117. 'price' => '8.50',
  118. 'status' => 'draft',
  119. 'images' => [UploadedFile::fake()->image('new.jpg')],
  120. ]))
  121. ->assertRedirect(route('stores.products.index', $store->slug));
  122. $product->refresh();
  123. $this->assertSame('New name', $product->name);
  124. // The address stays the same when the name changes, so old links keep working.
  125. $this->assertSame('old-name', $product->slug);
  126. $this->assertSame(850, $product->price);
  127. $this->assertNull($product->compare_at_price);
  128. $this->assertSame(ProductStatus::Draft, $product->status);
  129. $this->assertCount(2, $product->images);
  130. $this->actingAs($user)
  131. ->delete(route('stores.products.images.destroy', [$store->slug, $product->id, $image->id]))
  132. ->assertRedirect();
  133. $this->assertCount(1, $product->fresh()->images);
  134. Storage::disk('public')->assertMissing($image->path);
  135. }
  136. public function test_a_product_cannot_have_more_than_five_photos()
  137. {
  138. Storage::fake('public');
  139. $user = User::factory()->create();
  140. $store = Store::factory()->for($user)->create();
  141. $product = Product::factory()->for($store)->create();
  142. foreach (range(1, 4) as $position) {
  143. $product->images()->create(['store_id' => $store->id, 'path' => "products/x$position.jpg", 'position' => $position]);
  144. }
  145. $this->actingAs($user)
  146. ->post(route('stores.products.update', [$store->slug, $product->id]), $this->form([
  147. 'images' => [UploadedFile::fake()->image('5.jpg'), UploadedFile::fake()->image('6.jpg')],
  148. ]))
  149. ->assertSessionHasErrors('images');
  150. $this->assertCount(4, $product->fresh()->images);
  151. }
  152. public function test_deleting_a_product_removes_its_photos()
  153. {
  154. Storage::fake('public');
  155. $user = User::factory()->create();
  156. $store = Store::factory()->for($user)->create();
  157. $product = Product::factory()->for($store)->create();
  158. $path = UploadedFile::fake()->image('a.jpg')->store("products/{$store->id}", 'public');
  159. $product->images()->create(['store_id' => $store->id, 'path' => $path, 'position' => 1]);
  160. $this->actingAs($user)
  161. ->delete(route('stores.products.destroy', [$store->slug, $product->id]))
  162. ->assertRedirect(route('stores.products.index', $store->slug));
  163. $this->assertSame(0, $store->products()->count());
  164. $this->assertDatabaseCount('product_images', 0);
  165. Storage::disk('public')->assertMissing($path);
  166. }
  167. public function test_a_user_cannot_see_or_change_someone_elses_products()
  168. {
  169. $user = User::factory()->create();
  170. $mine = Store::factory()->for($user)->create();
  171. $theirs = Store::factory()->create();
  172. $product = Product::factory()->for($theirs)->create();
  173. $this->get(route('stores.products.index', $theirs->slug))->assertRedirect(route('login'));
  174. $this->actingAs($user)->get(route('stores.products.index', $theirs->slug))->assertNotFound();
  175. $this->actingAs($user)->post(route('stores.products.store', $theirs->slug), $this->form())->assertNotFound();
  176. $this->actingAs($user)->get(route('stores.products.edit', [$theirs->slug, $product->id]))->assertNotFound();
  177. // Their product id inside my own store's address must not work either.
  178. $this->actingAs($user)->get(route('stores.products.edit', [$mine->slug, $product->id]))->assertNotFound();
  179. $this->actingAs($user)->post(route('stores.products.update', [$mine->slug, $product->id]), $this->form())->assertNotFound();
  180. $this->actingAs($user)->delete(route('stores.products.destroy', [$mine->slug, $product->id]))->assertNotFound();
  181. $this->assertSame(1, $theirs->products()->count());
  182. $this->assertSame($product->name, $product->fresh()->name);
  183. }
  184. public function test_the_storefront_shows_only_active_products()
  185. {
  186. $user = User::factory()->create();
  187. $store = Store::factory()->for($user)->create();
  188. $store->plan = SamplePlan::for($store);
  189. $store->save();
  190. $active = Product::factory()->for($store)->create(['name' => 'Visible', 'price' => 1290, 'compare_at_price' => 1590]);
  191. $soldOut = Product::factory()->for($store)->soldOut()->create(['name' => 'Gone']);
  192. $draft = Product::factory()->for($store)->draft()->create(['name' => 'Hidden']);
  193. Product::factory()->create(['name' => 'Another store']);
  194. $this->actingAs($user)
  195. ->get(route('stores.preview.shop', $store->slug))
  196. ->assertOk()
  197. ->assertInertia(fn (Assert $page) => $page
  198. ->component('stores/preview')
  199. ->where('view.type', 'shop')
  200. ->has('products', 2)
  201. ->where('products.0.name', 'Gone')
  202. ->where('products.0.in_stock', false)
  203. ->where('products.1.name', 'Visible')
  204. ->where('products.1.price', 12.9)
  205. ->where('products.1.compare_at_price', 15.9)
  206. ->where('products.1.in_stock', true));
  207. // The home page gets the same products, so its product section can show them.
  208. $this->actingAs($user)
  209. ->get(route('stores.preview', $store->slug))
  210. ->assertInertia(fn (Assert $page) => $page->has('products', 2));
  211. $this->actingAs($user)
  212. ->get(route('stores.preview.product', [$store->slug, $active->slug]))
  213. ->assertOk()
  214. ->assertInertia(fn (Assert $page) => $page
  215. ->where('view.type', 'product')
  216. ->where('view.product.name', 'Visible'));
  217. $this->actingAs($user)->get(route('stores.preview.product', [$store->slug, $soldOut->slug]))->assertOk();
  218. $this->actingAs($user)->get(route('stores.preview.product', [$store->slug, $draft->slug]))->assertNotFound();
  219. $this->actingAs($user)->get(route('stores.preview.product', [$store->slug, 'no-such-product']))->assertNotFound();
  220. }
  221. public function test_storefront_pages_need_a_design_and_the_owner()
  222. {
  223. $user = User::factory()->create();
  224. $store = Store::factory()->for($user)->create();
  225. $product = Product::factory()->for($store)->create();
  226. $this->actingAs($user)->get(route('stores.preview.shop', $store->slug))->assertNotFound();
  227. $store->plan = SamplePlan::for($store);
  228. $store->save();
  229. $stranger = User::factory()->create();
  230. $this->actingAs($stranger)->get(route('stores.preview.shop', $store->slug))->assertNotFound();
  231. $this->actingAs($stranger)->get(route('stores.preview.product', [$store->slug, $product->slug]))->assertNotFound();
  232. }
  233. }