Files
amare/tests/Feature/PublicSite/PublicPagesTest.php
Manoel Freitas 2fe262ab59 Hardening public blades: overflow, fallbacks, branded errors, JS robustness (#14)
* fix: prevent long strings from overflowing public layout

* fix: fall back to default hero copy and tighten hero line-height

* fix: skip blank testimonials and guard empty portfolio metadata

* feat: add branded 419, 429 and 503 error pages

* fix: reset submit state on bfcache restore, swap label while sending, trap mobile menu focus

* chore: track hardening task status

* fix: restore contact submit state after bfcache

* fix: allow contact links to wrap long unbroken strings
2026-08-06 00:22:53 -03:00

222 lines
7.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\PublicSite;
use App\Models\PortfolioCase;
use App\Models\PortfolioImage;
use App\Models\Service;
use App\Models\SiteSetting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class PublicPagesTest extends TestCase
{
use RefreshDatabase;
public function test_services_index_lists_published_services_in_sort_order(): void
{
SiteSetting::instance();
Service::factory()->create(['title' => 'Rascunho', 'published_at' => null]);
Service::factory()->published()->create(['title' => 'Serviço B', 'sort_order' => 20]);
Service::factory()->published()->create(['title' => 'Serviço A', 'sort_order' => 10]);
$this->get(route('services.index'))
->assertOk()
->assertSeeInOrder(['Serviço A', 'Serviço B'])
->assertDontSee('Rascunho');
}
public function test_portfolio_index_paginates_and_excludes_drafts(): void
{
SiteSetting::instance();
PortfolioCase::factory()->create(['title' => 'Rascunho', 'published_at' => null]);
foreach (range(1, 10) as $index) {
PortfolioCase::factory()->published()->create([
'title' => 'Caso '.$index,
'slug' => 'caso-pagina-'.$index,
'sort_order' => $index,
]);
}
$this->assertSame(10, PortfolioCase::query()->published()->count());
$this->get(route('portfolio.index'))
->assertOk()
->assertSee('>Caso 1</', false)
->assertSee('>Caso 9</', false)
->assertDontSee('>Caso 10</', false)
->assertDontSee('Rascunho');
$this->get(route('portfolio.index', ['page' => 2]))
->assertOk()
->assertSee('>Caso 10</', false)
->assertDontSee('>Caso 9</', false);
}
public function test_portfolio_show_renders_published_case_gallery_order_and_404_for_draft(): void
{
SiteSetting::instance();
$published = PortfolioCase::factory()->published()->create([
'slug' => 'casamento-jardim',
'title' => 'Casamento Jardim',
'challenge' => 'Desafio do caso',
'solution' => 'Solução do caso',
'result' => 'Resultado do caso',
]);
PortfolioImage::query()->create([
'portfolio_case_id' => $published->id,
'path' => 'content/gallery-b.jpg',
'alt_text' => 'Imagem B',
'caption' => null,
'sort_order' => 2,
]);
PortfolioImage::query()->create([
'portfolio_case_id' => $published->id,
'path' => 'content/gallery-a.jpg',
'alt_text' => 'Imagem A',
'caption' => null,
'sort_order' => 1,
]);
PortfolioCase::factory()->create([
'slug' => 'rascunho',
'published_at' => null,
]);
$this->get(route('portfolio.show', 'casamento-jardim'))
->assertOk()
->assertSee('Casamento Jardim')
->assertSee('Desafio do caso')
->assertSee('Solução do caso')
->assertSee('Resultado do caso')
->assertSeeInOrder(['Imagem A', 'Imagem B']);
$this->get(route('portfolio.show', 'rascunho'))->assertNotFound();
}
public function test_about_privacy_and_contact_use_site_settings_without_creating_leads(): void
{
$settings = SiteSetting::instance();
$settings->update([
'about_summary' => 'Sobre a Amare boutique',
'email' => 'amareassessoriaeventos@gmail.com',
'phone' => '(11) 90000-0000',
'city' => 'São Paulo - SP',
'social_links' => ['instagram' => 'https://instagram.com/amare'],
]);
$this->get(route('about'))
->assertOk()
->assertSee('Sobre a Amare boutique')
->assertSee('São Paulo - SP')
->assertDontSee('Fortaleza')
->assertSee('min-h-[calc(100dvh-14rem)]', false);
$this->get(route('privacy'))
->assertOk()
->assertSee('Política de privacidade')
->assertSee('amareassessoriaeventos@gmail.com');
$this->get(route('contact'))
->assertOk()
->assertSee('amareassessoriaeventos@gmail.com')
->assertSee('(11) 90000-0000')
->assertSee('São Paulo - SP')
->assertDontSee('Fortaleza')
->assertSee('https://instagram.com/amare', false)
->assertSee('min-h-[calc(100dvh-14rem)]', false)
->assertSee('<form', false)
->assertSee('name="nome"', false)
->assertSee('name="privacidade"', false)
->assertSee(route('privacy'), false);
}
public function test_site_setting_defaults_use_sao_paulo_and_official_email(): void
{
$settings = SiteSetting::instance();
$this->assertSame('São Paulo - SP', $settings->city);
$this->assertSame('amareassessoriaeventos@gmail.com', $settings->email);
$this->assertStringNotContainsStringIgnoringCase('Fortaleza', (string) $settings->city);
}
public function test_branded_404_and_500_without_stack_trace_when_debug_disabled(): void
{
SiteSetting::instance();
$this->get('/rota-inexistente-amare')
->assertNotFound()
->assertSee('Página não encontrada')
->assertSee('Voltar para a home');
Route::get('/__test-server-error', function (): never {
throw new \RuntimeException('segredo-interno-amare');
});
config(['app.debug' => false]);
$response = $this->get('/__test-server-error');
$response
->assertStatus(500)
->assertSee('Algo deu errado')
->assertDontSee('segredo-interno-amare')
->assertDontSee('RuntimeException');
}
public function test_portfolio_show_avoids_n_plus_one_on_gallery(): void
{
SiteSetting::instance();
$case = PortfolioCase::factory()->published()->create(['slug' => 'caso-n1']);
foreach (range(1, 8) as $order) {
PortfolioImage::query()->create([
'portfolio_case_id' => $case->id,
'path' => "content/gallery-{$order}.jpg",
'alt_text' => "Imagem {$order}",
'caption' => null,
'sort_order' => $order,
]);
}
DB::enableQueryLog();
$this->get(route('portfolio.show', 'caso-n1'))->assertOk();
$queryCount = count(DB::getQueryLog());
DB::disableQueryLog();
$this->assertLessThan(15, $queryCount);
}
public function test_case_without_event_type_does_not_render_empty_eyebrow(): void
{
SiteSetting::instance();
$case = PortfolioCase::factory()->published()->create([
'slug' => 'caso-sem-tipo',
'title' => 'Caso Sem Tipo',
'event_type' => '',
]);
$response = $this->get(route('portfolio.show', $case->slug));
$response->assertOk()->assertSee('Caso Sem Tipo');
$emptyAccentParagraphs = preg_match_all(
'/class="[^"]*text-amare-accent[^"]*"><\/p>/',
(string) $response->getContent(),
);
expect($emptyAccentParagraphs)->toBe(0);
}
}