AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StoreLogoTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Ai\AiManager;
  4. use App\Ai\AiResponse;
  5. use App\Mail\OrderConfirmation;
  6. use App\Models\Order;
  7. use App\Models\Store;
  8. use App\Models\User;
  9. use App\Storefront\Logos\LogoSvg;
  10. use App\StorePlan\SamplePlan;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Illuminate\Http\UploadedFile;
  13. use Illuminate\Support\Facades\Storage;
  14. use Inertia\Testing\AssertableInertia as Assert;
  15. use Tests\TestCase;
  16. /**
  17. * The store logo: uploaded, a ready design, or drawn by the AI (with a lifetime limit).
  18. */
  19. class StoreLogoTest extends TestCase
  20. {
  21. use RefreshDatabase;
  22. private User $user;
  23. private Store $store;
  24. protected function setUp(): void
  25. {
  26. parent::setUp();
  27. Storage::fake('public');
  28. config(['ai.tasks.logo.provider' => 'fake', 'ai.logo.lifetime_tries' => 3]);
  29. $this->user = User::factory()->create();
  30. $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
  31. $this->store->plan = SamplePlan::for($this->store);
  32. $this->store->save();
  33. $this->actingAs($this->user);
  34. }
  35. private function png(int $width = 600, int $height = 200): UploadedFile
  36. {
  37. $image = imagecreatetruecolor($width, $height);
  38. imagesavealpha($image, true);
  39. imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));
  40. imagefilledrectangle($image, 10, 10, 60, 60, imagecolorallocate($image, 200, 30, 30));
  41. $path = tempnam(sys_get_temp_dir(), 'logo').'.png';
  42. imagepng($image, $path);
  43. return new UploadedFile($path, 'logo.png', 'image/png', null, true);
  44. }
  45. public function test_an_uploaded_logo_is_made_smaller_saved_as_webp_and_shown_in_the_store()
  46. {
  47. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)])
  48. ->assertSessionHasNoErrors();
  49. $logo = $this->store->fresh()->logo;
  50. $this->assertSame('upload', $logo['type']);
  51. $this->assertStringEndsWith('.webp', $logo['path']);
  52. $this->assertSame([800, 200], [$logo['width'], $logo['height']]);
  53. Storage::disk('public')->assertExists($logo['path']);
  54. $this->get(route('stores.preview', $this->store->slug))
  55. ->assertInertia(fn (Assert $page) => $page
  56. ->where('shop.logo.type', 'upload')
  57. ->where('shop.logo.width', 800));
  58. // A new logo replaces the old file.
  59. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png()]);
  60. Storage::disk('public')->assertMissing($logo['path']);
  61. }
  62. public function test_a_squarish_uploaded_logo_becomes_the_browser_tab_icon()
  63. {
  64. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)]);
  65. // The see-through edges are cut off, so the small red square fills the icon.
  66. $logo = $this->store->fresh()->logo;
  67. Storage::disk('public')->assertExists($logo['icon_path']);
  68. $this->assertSame([180, 180], array_slice(getimagesizefromstring(Storage::disk('public')->get($logo['icon_path'])), 0, 2));
  69. $url = Storage::disk('public')->url($logo['icon_path']);
  70. $this->get(route('stores.preview', $this->store->slug))
  71. ->assertSee('<link rel="icon" href="'.$url.'" type="image/png">', false)
  72. ->assertSee('<link rel="apple-touch-icon" href="'.$url.'">', false)
  73. ->assertDontSee('/favicon.svg', false);
  74. // Removing the logo removes its icon too.
  75. $this->delete(route('stores.logo.destroy', $this->store->slug));
  76. Storage::disk('public')->assertMissing($logo['icon_path']);
  77. }
  78. public function test_a_store_without_a_usable_picture_shows_its_initials_in_the_tab()
  79. {
  80. // No logo: the initials on the store's main colour, never our own icon.
  81. $primary = $this->store->plan['theme']['colors']['primary'];
  82. $this->get(route('stores.preview', $this->store->slug))
  83. ->assertSee('data:image/svg+xml,', false)
  84. ->assertSee(rawurlencode('>KA</text>'), false)
  85. ->assertSee(rawurlencode('fill="'.$primary.'"'), false)
  86. ->assertDontSee('/favicon.svg', false)
  87. ->assertDontSee('apple-touch-icon', false);
  88. // The round monogram design gets a round icon.
  89. $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'monogram-circle']);
  90. $this->get(route('stores.preview', $this->store->slug))->assertSee(rawurlencode('rx="32"'), false);
  91. // A wide, solid logo is too thin to read as an icon.
  92. $image = imagecreatetruecolor(900, 200);
  93. imagefill($image, 0, 0, imagecolorallocate($image, 20, 120, 60));
  94. $path = tempnam(sys_get_temp_dir(), 'logo').'.png';
  95. imagepng($image, $path);
  96. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => new UploadedFile($path, 'wide.png', 'image/png', null, true)])
  97. ->assertSessionHasNoErrors();
  98. $this->assertArrayNotHasKey('icon_path', $this->store->fresh()->logo);
  99. $this->get(route('stores.show', $this->store->slug))
  100. ->assertInertia(fn (Assert $page) => $page->where('logo.icon.type', 'image/svg+xml'))
  101. // The merchant's own dashboard keeps our icon.
  102. ->assertSee('/favicon.svg', false);
  103. }
  104. public function test_uploads_are_checked()
  105. {
  106. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(40, 40)])
  107. ->assertSessionHasErrors('logo');
  108. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => UploadedFile::fake()->create('logo.svg', 4, 'image/svg+xml')])
  109. ->assertSessionHasErrors('logo');
  110. $this->assertNull($this->store->fresh()->logo);
  111. }
  112. public function test_a_ready_design_can_be_picked_and_removed()
  113. {
  114. $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'monogram-circle'])->assertSessionHasNoErrors();
  115. $this->assertSame(['type' => 'design', 'key' => 'monogram-circle'], $this->store->fresh()->logo);
  116. $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'made-up'])->assertSessionHasErrors('design');
  117. $this->delete(route('stores.logo.destroy', $this->store->slug));
  118. $this->assertNull($this->store->fresh()->logo);
  119. }
  120. public function test_the_ai_draws_logos_that_can_be_picked_and_each_store_has_a_lifetime_limit()
  121. {
  122. $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasNoErrors();
  123. $store = $this->store->fresh();
  124. $this->assertSame(1, $store->ai_logo_tries);
  125. $this->assertCount(3, $store->logo_drafts);
  126. $this->assertStringContainsString('var(--sf-font-heading', $store->logo_drafts[0]);
  127. $this->put(route('stores.logo.choose', $this->store->slug), ['drawn' => 1])->assertSessionHasNoErrors();
  128. $this->assertSame('drawn', $this->store->fresh()->logo['type']);
  129. $this->put(route('stores.logo.choose', $this->store->slug), ['drawn' => 99])->assertSessionHasErrors('logo');
  130. $this->post(route('stores.logo.draw', $this->store->slug));
  131. $this->post(route('stores.logo.draw', $this->store->slug));
  132. $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasErrors('logo');
  133. $store = $this->store->fresh();
  134. $this->assertSame(3, $store->ai_logo_tries);
  135. $this->assertCount(9, $store->logo_drafts);
  136. $this->get(route('stores.show', $this->store->slug))
  137. ->assertInertia(fn (Assert $page) => $page
  138. ->where('logo.tries_left', 0)
  139. ->where('logo.tries_total', 3)
  140. ->has('logo.drafts', 9)
  141. ->has('logo.designs', 8));
  142. }
  143. public function test_a_failed_drawing_does_not_use_a_try()
  144. {
  145. $this->mock(AiManager::class)->shouldReceive('ask')->andReturn(new AiResponse('{"logos": [{"svg": "<script>alert(1)</script>"}]}', 'fake', 'fake'));
  146. $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasErrors('logo');
  147. $this->assertSame(0, $this->store->fresh()->ai_logo_tries);
  148. $this->assertNull($this->store->fresh()->logo_drafts);
  149. }
  150. public function test_drawings_are_cleaned_of_anything_that_could_run_code_or_load_from_outside()
  151. {
  152. $clean = LogoSvg::clean(<<<'SVG'
  153. <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 400 100" width="999" onload="alert(1)">
  154. <script>alert(1)</script>
  155. <style>text { fill: red }</style>
  156. <defs><linearGradient id="g"><stop offset="0" stop-color="#112233"/><stop offset="1" stop-color="javascript:x"/></linearGradient></defs>
  157. <circle cx="50" cy="50" r="30" fill="url(#g)" onclick="alert(1)" style="fill: url(https://evil.test/x)"/>
  158. <image href="https://evil.test/x.png" width="10" height="10"/>
  159. <a href="javascript:alert(1)"><rect width="10" height="10"/></a>
  160. <foreignObject><div>hi</div></foreignObject>
  161. <path d="M0 0 L10 10 Z" fill="https://evil.test"/>
  162. <text x="100" y="60" font-size="40" font-family="Comic Sans">Kedai Ali</text>
  163. </svg>
  164. SVG);
  165. $this->assertNotNull($clean);
  166. $this->assertStringNotContainsString('script', $clean);
  167. $this->assertStringNotContainsString('onload', $clean);
  168. $this->assertStringNotContainsString('onclick', $clean);
  169. $this->assertStringNotContainsString('evil.test', $clean);
  170. $this->assertStringNotContainsString('javascript', $clean);
  171. $this->assertStringNotContainsString('<image', $clean);
  172. $this->assertStringNotContainsString('<a', $clean);
  173. $this->assertStringNotContainsString('foreignObject', $clean);
  174. $this->assertStringNotContainsString('Comic Sans', $clean);
  175. $this->assertStringNotContainsString('width="999"', $clean);
  176. $this->assertStringContainsString('Kedai Ali', $clean);
  177. $this->assertStringContainsString('<path d="M0 0 L10 10 Z"/>', $clean);
  178. // The gradient keeps working under its new, unique id.
  179. $this->assertMatchesRegularExpression('/id="(l[0-9a-f]{6}-g)".*fill="url\(#\1\)"/s', $clean);
  180. $this->assertNull(LogoSvg::clean('<div>not a drawing</div>'));
  181. $this->assertNull(LogoSvg::clean('<svg viewBox="0 0 5000 10"><rect width="1" height="1"/></svg>'));
  182. $this->assertNull(LogoSvg::clean('<!DOCTYPE svg [<!ENTITY x SYSTEM "file:///etc/passwd">]><svg viewBox="0 0 400 100"><text>&x;</text></svg>'));
  183. }
  184. public function test_an_uploaded_logo_tops_the_order_emails()
  185. {
  186. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)]);
  187. $order = Order::factory()->for($this->store)->paid()->create();
  188. (new OrderConfirmation($order->fresh()))
  189. ->assertSeeInHtml('alt="Kedai Ali"', false)
  190. ->assertSeeInHtml('width="192" height="48"', false);
  191. }
  192. public function test_another_merchants_store_logo_cannot_be_changed()
  193. {
  194. $other = Store::factory()->create();
  195. $this->post(route('stores.logo.upload', $other->slug), ['logo' => $this->png()])->assertNotFound();
  196. $this->put(route('stores.logo.choose', $other->slug), ['design' => 'wordmark'])->assertNotFound();
  197. $this->post(route('stores.logo.draw', $other->slug))->assertNotFound();
  198. $this->delete(route('stores.logo.destroy', $other->slug))->assertNotFound();
  199. }
  200. }