| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
-
- namespace Tests\Feature\Stores;
-
- use App\Ai\AiManager;
- use App\Ai\AiResponse;
- use App\Mail\OrderConfirmation;
- use App\Models\Order;
- use App\Models\Store;
- use App\Models\User;
- use App\Storefront\Logos\LogoSvg;
- 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;
-
- /**
- * The store logo: uploaded, a ready design, or drawn by the AI (with a lifetime limit).
- */
- class StoreLogoTest extends TestCase
- {
- use RefreshDatabase;
-
- private User $user;
-
- private Store $store;
-
- protected function setUp(): void
- {
- parent::setUp();
-
- Storage::fake('public');
- config(['ai.tasks.logo.provider' => 'fake', 'ai.logo.lifetime_tries' => 3]);
-
- $this->user = User::factory()->create();
- $this->store = Store::factory()->for($this->user)->create(['name' => 'Kedai Ali']);
- $this->store->plan = SamplePlan::for($this->store);
- $this->store->save();
- $this->actingAs($this->user);
- }
-
- private function png(int $width = 600, int $height = 200): UploadedFile
- {
- $image = imagecreatetruecolor($width, $height);
- imagesavealpha($image, true);
- imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));
- imagefilledrectangle($image, 10, 10, 60, 60, imagecolorallocate($image, 200, 30, 30));
- $path = tempnam(sys_get_temp_dir(), 'logo').'.png';
- imagepng($image, $path);
-
- return new UploadedFile($path, 'logo.png', 'image/png', null, true);
- }
-
- public function test_an_uploaded_logo_is_made_smaller_saved_as_webp_and_shown_in_the_store()
- {
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)])
- ->assertSessionHasNoErrors();
-
- $logo = $this->store->fresh()->logo;
- $this->assertSame('upload', $logo['type']);
- $this->assertStringEndsWith('.webp', $logo['path']);
- $this->assertSame([800, 200], [$logo['width'], $logo['height']]);
- Storage::disk('public')->assertExists($logo['path']);
-
- $this->get(route('stores.preview', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('shop.logo.type', 'upload')
- ->where('shop.logo.width', 800));
-
- // A new logo replaces the old file.
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png()]);
- Storage::disk('public')->assertMissing($logo['path']);
- }
-
- public function test_a_squarish_uploaded_logo_becomes_the_browser_tab_icon()
- {
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)]);
-
- // The see-through edges are cut off, so the small red square fills the icon.
- $logo = $this->store->fresh()->logo;
- Storage::disk('public')->assertExists($logo['icon_path']);
- $this->assertSame([180, 180], array_slice(getimagesizefromstring(Storage::disk('public')->get($logo['icon_path'])), 0, 2));
-
- $url = Storage::disk('public')->url($logo['icon_path']);
- $this->get(route('stores.preview', $this->store->slug))
- ->assertSee('<link rel="icon" href="'.$url.'" type="image/png">', false)
- ->assertSee('<link rel="apple-touch-icon" href="'.$url.'">', false)
- ->assertDontSee('/favicon.svg', false);
-
- // Removing the logo removes its icon too.
- $this->delete(route('stores.logo.destroy', $this->store->slug));
- Storage::disk('public')->assertMissing($logo['icon_path']);
- }
-
- public function test_a_store_without_a_usable_picture_shows_its_initials_in_the_tab()
- {
- // No logo: the initials on the store's main colour, never our own icon.
- $primary = $this->store->plan['theme']['colors']['primary'];
- $this->get(route('stores.preview', $this->store->slug))
- ->assertSee('data:image/svg+xml,', false)
- ->assertSee(rawurlencode('>KA</text>'), false)
- ->assertSee(rawurlencode('fill="'.$primary.'"'), false)
- ->assertDontSee('/favicon.svg', false)
- ->assertDontSee('apple-touch-icon', false);
-
- // The round monogram design gets a round icon.
- $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'monogram-circle']);
- $this->get(route('stores.preview', $this->store->slug))->assertSee(rawurlencode('rx="32"'), false);
-
- // A wide, solid logo is too thin to read as an icon.
- $image = imagecreatetruecolor(900, 200);
- imagefill($image, 0, 0, imagecolorallocate($image, 20, 120, 60));
- $path = tempnam(sys_get_temp_dir(), 'logo').'.png';
- imagepng($image, $path);
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => new UploadedFile($path, 'wide.png', 'image/png', null, true)])
- ->assertSessionHasNoErrors();
-
- $this->assertArrayNotHasKey('icon_path', $this->store->fresh()->logo);
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page->where('logo.icon.type', 'image/svg+xml'))
- // The merchant's own dashboard keeps our icon.
- ->assertSee('/favicon.svg', false);
- }
-
- public function test_uploads_are_checked()
- {
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(40, 40)])
- ->assertSessionHasErrors('logo');
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => UploadedFile::fake()->create('logo.svg', 4, 'image/svg+xml')])
- ->assertSessionHasErrors('logo');
-
- $this->assertNull($this->store->fresh()->logo);
- }
-
- public function test_a_ready_design_can_be_picked_and_removed()
- {
- $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'monogram-circle'])->assertSessionHasNoErrors();
- $this->assertSame(['type' => 'design', 'key' => 'monogram-circle'], $this->store->fresh()->logo);
-
- $this->put(route('stores.logo.choose', $this->store->slug), ['design' => 'made-up'])->assertSessionHasErrors('design');
-
- $this->delete(route('stores.logo.destroy', $this->store->slug));
- $this->assertNull($this->store->fresh()->logo);
- }
-
- public function test_the_ai_draws_logos_that_can_be_picked_and_each_store_has_a_lifetime_limit()
- {
- $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasNoErrors();
-
- $store = $this->store->fresh();
- $this->assertSame(1, $store->ai_logo_tries);
- $this->assertCount(3, $store->logo_drafts);
- $this->assertStringContainsString('var(--sf-font-heading', $store->logo_drafts[0]);
-
- $this->put(route('stores.logo.choose', $this->store->slug), ['drawn' => 1])->assertSessionHasNoErrors();
- $this->assertSame('drawn', $this->store->fresh()->logo['type']);
- $this->put(route('stores.logo.choose', $this->store->slug), ['drawn' => 99])->assertSessionHasErrors('logo');
-
- $this->post(route('stores.logo.draw', $this->store->slug));
- $this->post(route('stores.logo.draw', $this->store->slug));
- $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasErrors('logo');
-
- $store = $this->store->fresh();
- $this->assertSame(3, $store->ai_logo_tries);
- $this->assertCount(9, $store->logo_drafts);
-
- $this->get(route('stores.show', $this->store->slug))
- ->assertInertia(fn (Assert $page) => $page
- ->where('logo.tries_left', 0)
- ->where('logo.tries_total', 3)
- ->has('logo.drafts', 9)
- ->has('logo.designs', 8));
- }
-
- public function test_a_failed_drawing_does_not_use_a_try()
- {
- $this->mock(AiManager::class)->shouldReceive('ask')->andReturn(new AiResponse('{"logos": [{"svg": "<script>alert(1)</script>"}]}', 'fake', 'fake'));
-
- $this->post(route('stores.logo.draw', $this->store->slug))->assertSessionHasErrors('logo');
-
- $this->assertSame(0, $this->store->fresh()->ai_logo_tries);
- $this->assertNull($this->store->fresh()->logo_drafts);
- }
-
- public function test_drawings_are_cleaned_of_anything_that_could_run_code_or_load_from_outside()
- {
- $clean = LogoSvg::clean(<<<'SVG'
- <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)">
- <script>alert(1)</script>
- <style>text { fill: red }</style>
- <defs><linearGradient id="g"><stop offset="0" stop-color="#112233"/><stop offset="1" stop-color="javascript:x"/></linearGradient></defs>
- <circle cx="50" cy="50" r="30" fill="url(#g)" onclick="alert(1)" style="fill: url(https://evil.test/x)"/>
- <image href="https://evil.test/x.png" width="10" height="10"/>
- <a href="javascript:alert(1)"><rect width="10" height="10"/></a>
- <foreignObject><div>hi</div></foreignObject>
- <path d="M0 0 L10 10 Z" fill="https://evil.test"/>
- <text x="100" y="60" font-size="40" font-family="Comic Sans">Kedai Ali</text>
- </svg>
- SVG);
-
- $this->assertNotNull($clean);
- $this->assertStringNotContainsString('script', $clean);
- $this->assertStringNotContainsString('onload', $clean);
- $this->assertStringNotContainsString('onclick', $clean);
- $this->assertStringNotContainsString('evil.test', $clean);
- $this->assertStringNotContainsString('javascript', $clean);
- $this->assertStringNotContainsString('<image', $clean);
- $this->assertStringNotContainsString('<a', $clean);
- $this->assertStringNotContainsString('foreignObject', $clean);
- $this->assertStringNotContainsString('Comic Sans', $clean);
- $this->assertStringNotContainsString('width="999"', $clean);
- $this->assertStringContainsString('Kedai Ali', $clean);
- $this->assertStringContainsString('<path d="M0 0 L10 10 Z"/>', $clean);
- // The gradient keeps working under its new, unique id.
- $this->assertMatchesRegularExpression('/id="(l[0-9a-f]{6}-g)".*fill="url\(#\1\)"/s', $clean);
-
- $this->assertNull(LogoSvg::clean('<div>not a drawing</div>'));
- $this->assertNull(LogoSvg::clean('<svg viewBox="0 0 5000 10"><rect width="1" height="1"/></svg>'));
- $this->assertNull(LogoSvg::clean('<!DOCTYPE svg [<!ENTITY x SYSTEM "file:///etc/passwd">]><svg viewBox="0 0 400 100"><text>&x;</text></svg>'));
- }
-
- public function test_an_uploaded_logo_tops_the_order_emails()
- {
- $this->post(route('stores.logo.upload', $this->store->slug), ['logo' => $this->png(1600, 400)]);
- $order = Order::factory()->for($this->store)->paid()->create();
-
- (new OrderConfirmation($order->fresh()))
- ->assertSeeInHtml('alt="Kedai Ali"', false)
- ->assertSeeInHtml('width="192" height="48"', false);
- }
-
- public function test_another_merchants_store_logo_cannot_be_changed()
- {
- $other = Store::factory()->create();
-
- $this->post(route('stores.logo.upload', $other->slug), ['logo' => $this->png()])->assertNotFound();
- $this->put(route('stores.logo.choose', $other->slug), ['design' => 'wordmark'])->assertNotFound();
- $this->post(route('stores.logo.draw', $other->slug))->assertNotFound();
- $this->delete(route('stores.logo.destroy', $other->slug))->assertNotFound();
- }
- }
|