'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_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": ""}]}', '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);
$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('assertStringNotContainsString('assertStringNotContainsString('foreignObject', $clean);
$this->assertStringNotContainsString('Comic Sans', $clean);
$this->assertStringNotContainsString('width="999"', $clean);
$this->assertStringContainsString('Kedai Ali', $clean);
$this->assertStringContainsString('', $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('not a drawing
'));
$this->assertNull(LogoSvg::clean(''));
$this->assertNull(LogoSvg::clean(']>'));
}
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();
}
}