.
*/
namespace App\Tests\Services\LogSystem;
use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
use App\Entity\LogSystem\UserLoginLogEntry;
use App\Entity\LogSystem\UserLogoutLogEntry;
use App\Services\LogSystem\LogEntryExtraFormatter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class LogEntryExtraFormatterTest extends WebTestCase
{
private static LogEntryExtraFormatter $service;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$service = self::getContainer()->get(LogEntryExtraFormatter::class);
}
public function testFormatUserLoginLogEntryContainsIp(): void
{
$entry = new UserLoginLogEntry('127.0.0.1', anonymize: false);
$result = self::$service->format($entry);
$this->assertNotEmpty($result);
$this->assertStringContainsString('127.0.0.1', $result);
}
public function testFormatDatabaseUpdatedLogEntryContainsVersions(): void
{
$entry = new DatabaseUpdatedLogEntry('1.0.0', '2.0.0');
$result = self::$service->format($entry);
$this->assertStringContainsString('1.0.0', $result);
$this->assertStringContainsString('2.0.0', $result);
}
public function testFormatUserLogoutContainsIp(): void
{
$entry = new UserLogoutLogEntry('10.0.0.1', anonymize: false);
$result = self::$service->format($entry);
$this->assertNotEmpty($result);
$this->assertStringContainsString('10.0.0.1', $result);
}
public function testFormatConsoleReplacesHtmlTags(): void
{
$entry = new DatabaseUpdatedLogEntry('1.0', '2.0');
$result = self::$service->formatConsole($entry);
// Console format replaces the arrow icon with →
$this->assertStringContainsString('→', $result);
// No raw HTML tags should remain from the arrow icon
$this->assertStringNotContainsString('', $result);
}
public function testFormatConsoleReturnsString(): void
{
$entry = new UserLoginLogEntry('192.168.1.1', anonymize: false);
$result = self::$service->formatConsole($entry);
$this->assertIsString($result);
$this->assertNotEmpty($result);
}
public function testIpAddressIsHtmlEscapedInFormat(): void
{
// Verify that the IP embedded in the result is safe (htmlspecialchars is applied)
$entry = new UserLoginLogEntry('192.168.0.1', anonymize: false);
$result = self::$service->format($entry);
// The result must not contain unescaped HTML even from a crafted IP
$this->assertStringNotContainsString('