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

StoreLogoTest.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_uploads_are_checked()
  63. {
  64. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(40, 40)])
  65. ->assertSessionHasErrors('logo');
  66. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => UploadedFile::fake()->create('logo.svg', 4, 'image/svg+xml')])
  67. ->assertSessionHasErrors('logo');
  68. $this->assertNull($this->store->fresh()->logo);
  69. }
  70. public function test_a_ready_design_can_be_picked_and_removed()
  71. {
  72. $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'monogram-circle'])->assertSessionHasNoErrors();
  73. $this->assertSame(['type' => 'design', 'key' => 'monogram-circle'], $this->store->fresh()->logo);
  74. $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'made-up'])->assertSessionHasErrors('design');
  75. $this->delete(route('stores.logo.destroy', $this->store->slug));
  76. $this->assertNull($this->store->fresh()->logo);
  77. }
  78. public function test_the_ai_draws_logos_that_can_be_picked_and_each_store_has_a_lifetime_limit()
  79. {
  80. $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasNoErrors();
  81. $store = $this->store->fresh();
  82. $this->assertSame(1, $store->ai_logo_tries);
  83. $this->assertCount(3, $store->logo_drafts);
  84. $this->assertStringContainsString('var(--sf-font-heading', $store->logo_drafts[0]);
  85. $this->put(route('stores.logo.choose', $this->store->slug), ['drawn' => 1])->assertSessionHasNoErrors();
  86. $this->assertSame('drawn', $this->store->fresh()->logo['type']);
  87. $this->put(route('stores.logo.choose', $this->store->slug), ['drawn' => 99])->assertSessionHasErrors('logo');
  88. $this->post(route('stores.logo.draw', $this->store->slug));
  89. $this->post(route('stores.logo.draw', $this->store->slug));
  90. $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasErrors('logo');
  91. $store = $this->store->fresh();
  92. $this->assertSame(3, $store->ai_logo_tries);
  93. $this->assertCount(9, $store->logo_drafts);
  94. $this->get(route('stores.show', $this->store->slug))
  95. ->assertInertia(fn (Assert $page) => $page
  96. ->where('logo.tries_left', 0)
  97. ->where('logo.tries_total', 3)
  98. ->has('logo.drafts', 9)
  99. ->has('logo.designs', 8));
  100. }
  101. public function test_a_failed_drawing_does_not_use_a_try()
  102. {
  103. $this->mock(AiManager::class)->shouldReceive('ask')->andReturn(new AiResponse('{"logos": [{"svg": "<script>alert(1)</script>"}]}', 'fake', 'fake'));
  104. $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasErrors('logo');
  105. $this->assertSame(0, $this->store->fresh()->ai_logo_tries);
  106. $this->assertNull($this->store->fresh()->logo_drafts);
  107. }
  108. public function test_drawings_are_cleaned_of_anything_that_could_run_code_or_load_from_outside()
  109. {
  110. $clean = LogoSvg::clean(<<<'SVG'
  111. <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)">
  112. <script>alert(1)</script>
  113. <style>text { fill: red }</style>
  114. <defs><linearGradient id="g"><stop offset="0" stop-color="#112233"/><stop offset="1" stop-color="javascript:x"/></linearGradient></defs>
  115. <circle cx="50" cy="50" r="30" fill="url(#g)" onclick="alert(1)" style="fill: url(https://evil.test/x)"/>
  116. <image href="https://evil.test/x.png" width="10" height="10"/>
  117. <a href="javascript:alert(1)"><rect width="10" height="10"/></a>
  118. <foreignObject><div>hi</div></foreignObject>
  119. <path d="M0 0 L10 10 Z" fill="https://evil.test"/>
  120. <text x="100" y="60" font-size="40" font-family="Comic Sans">Kedai Ali</text>
  121. </svg>
  122. SVG);
  123. $this->assertNotNull($clean);
  124. $this->assertStringNotContainsString('script', $clean);
  125. $this->assertStringNotContainsString('onload', $clean);
  126. $this->assertStringNotContainsString('onclick', $clean);
  127. $this->assertStringNotContainsString('evil.test', $clean);
  128. $this->assertStringNotContainsString('javascript', $clean);
  129. $this->assertStringNotContainsString('<image', $clean);
  130. $this->assertStringNotContainsString('<a', $clean);
  131. $this->assertStringNotContainsString('foreignObject', $clean);
  132. $this->assertStringNotContainsString('Comic Sans', $clean);
  133. $this->assertStringNotContainsString('width="999"', $clean);
  134. $this->assertStringContainsString('Kedai Ali', $clean);
  135. $this->assertStringContainsString('<path d="M0 0 L10 10 Z"/>', $clean);
  136. // The gradient keeps working under its new, unique id.
  137. $this->assertMatchesRegularExpression('/id="(l[0-9a-f]{6}-g)".*fill="url\(#\1\)"/s', $clean);
  138. $this->assertNull(LogoSvg::clean('<div>not a drawing</div>'));
  139. $this->assertNull(LogoSvg::clean('<svg viewBox="0 0 5000 10"><rect width="1" height="1"/></svg>'));
  140. $this->assertNull(LogoSvg::clean('<!DOCTYPE svg [<!ENTITY x SYSTEM "file:///etc/passwd">]><svg viewBox="0 0 400 100"><text>&x;</text></svg>'));
  141. }
  142. public function test_an_uploaded_logo_tops_the_order_emails()
  143. {
  144. $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)]);
  145. $order = Order::factory()->for($this->store)->paid()->create();
  146. (new OrderConfirmation($order->fresh()))
  147. ->assertSeeInHtml('alt="Kedai Ali"', false)
  148. ->assertSeeInHtml('width="192" height="48"', false);
  149. }
  150. public function test_another_merchants_store_logo_cannot_be_changed()
  151. {
  152. $other = Store::factory()->create();
  153. $this->post(route('stores.logo.upload', $other->slug), ['logo' => $this->png()])->assertNotFound();
  154. $this->put(route('stores.logo.choose', $other->slug), ['design' => 'wordmark'])->assertNotFound();
  155. $this->post(route('stores.logo.draw', $other->slug))->assertNotFound();
  156. $this->delete(route('stores.logo.destroy', $other->slug))->assertNotFound();
  157. }
  158. }