<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Tests\TestCase;

class AdminPlatformLeadIndexBackLinkNotEscapedTest extends TestCase
{
    use RefreshDatabase;

    protected function loginAsPlatformAdmin(): void
    {
        $this->seed();

        $this->post('/admin/login', [
            'email' => 'platform.admin@demo.local',
            'password' => 'Platform@123456',
        ])->assertRedirect('/admin');
    }

    public function test_index_should_render_back_link_with_ampersand_not_escaped(): void
    {
        $this->loginAsPlatformAdmin();

        $back = '/admin/platform-orders?' . Arr::query([
            'lead_id' => 12,
            'payment_status' => 'paid',
        ]);

        $url = '/admin/platform-leads?' . Arr::query([
            'back' => $back,
        ]);

        $res = $this->get($url);
        $res->assertOk();

        $res->assertSee('← 返回上一页（保留上下文）', false);
        $res->assertSee('href="' . $back . '"', false);

        // 页面其它位置（例如表单隐藏字段）可能会出现 &amp;，这里只要求“返回上一页”这个链接不被转义。
        $res->assertDontSee('href="' . str_replace('&', '&amp;', $back) . '"', false);
    }
}
