mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-13 06:51:29 +00:00
Added additional tests
This commit is contained in:
parent
cb669ad4ec
commit
65a6f46369
12 changed files with 2392 additions and 1441 deletions
79
tests/Services/LogSystem/LogDiffFormatterTest.php
Normal file
79
tests/Services/LogSystem/LogDiffFormatterTest.php
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Services\LogSystem;
|
||||
|
||||
use App\Services\LogSystem\LogDiffFormatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LogDiffFormatterTest extends TestCase
|
||||
{
|
||||
private LogDiffFormatter $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->service = new LogDiffFormatter();
|
||||
}
|
||||
|
||||
public function testPositiveNumericDiff(): void
|
||||
{
|
||||
$result = $this->service->formatDiff(1, 6);
|
||||
$this->assertStringContainsString('text-success', $result);
|
||||
$this->assertStringContainsString('+5', $result);
|
||||
}
|
||||
|
||||
public function testNegativeNumericDiff(): void
|
||||
{
|
||||
$result = $this->service->formatDiff(10, 3);
|
||||
$this->assertStringContainsString('text-danger', $result);
|
||||
$this->assertStringContainsString('-7', $result);
|
||||
}
|
||||
|
||||
public function testZeroNumericDiff(): void
|
||||
{
|
||||
$result = $this->service->formatDiff(5, 5);
|
||||
$this->assertStringContainsString('text-muted', $result);
|
||||
$this->assertStringContainsString('0', $result);
|
||||
}
|
||||
|
||||
public function testStringDiffReturnsNonEmptyHtml(): void
|
||||
{
|
||||
$result = $this->service->formatDiff('hello world', 'hello PHP');
|
||||
$this->assertNotEmpty($result);
|
||||
// DiffHelper returns HTML
|
||||
$this->assertStringContainsString('<', $result);
|
||||
}
|
||||
|
||||
public function testUnsupportedTypesReturnEmptyString(): void
|
||||
{
|
||||
// booleans are neither string nor numeric → empty
|
||||
$result = $this->service->formatDiff(true, false);
|
||||
$this->assertSame('', $result);
|
||||
}
|
||||
|
||||
public function testFloatDiff(): void
|
||||
{
|
||||
$result = $this->service->formatDiff(1.5, 3.0);
|
||||
$this->assertStringContainsString('text-success', $result);
|
||||
}
|
||||
}
|
||||
92
tests/Services/LogSystem/LogEntryExtraFormatterTest.php
Normal file
92
tests/Services/LogSystem/LogEntryExtraFormatterTest.php
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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('<i class="fas fa-long-arrow-alt-right"></i>', $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('<script>', $result);
|
||||
}
|
||||
}
|
||||
85
tests/Services/LogSystem/LogLevelHelperTest.php
Normal file
85
tests/Services/LogSystem/LogLevelHelperTest.php
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace App\Tests\Services\LogSystem;
|
||||
|
||||
use App\Services\LogSystem\LogLevelHelper;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
final class LogLevelHelperTest extends TestCase
|
||||
{
|
||||
private LogLevelHelper $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->service = new LogLevelHelper();
|
||||
}
|
||||
|
||||
public static function iconClassProvider(): \Generator
|
||||
{
|
||||
yield [LogLevel::DEBUG, 'fa-bug'];
|
||||
yield [LogLevel::INFO, 'fa-info'];
|
||||
yield [LogLevel::NOTICE, 'fa-flag'];
|
||||
yield [LogLevel::WARNING, 'fa-exclamation-circle'];
|
||||
yield [LogLevel::ERROR, 'fa-exclamation-triangle'];
|
||||
yield [LogLevel::CRITICAL, 'fa-bolt'];
|
||||
yield [LogLevel::ALERT, 'fa-radiation'];
|
||||
yield [LogLevel::EMERGENCY, 'fa-skull-crossbones'];
|
||||
}
|
||||
|
||||
#[DataProvider('iconClassProvider')]
|
||||
public function testLogLevelToIconClass(string $logLevel, string $expectedIcon): void
|
||||
{
|
||||
$this->assertSame($expectedIcon, $this->service->logLevelToIconClass($logLevel));
|
||||
}
|
||||
|
||||
public function testUnknownLogLevelReturnsDefaultIcon(): void
|
||||
{
|
||||
$this->assertSame('fa-question-circle', $this->service->logLevelToIconClass('unknown_level'));
|
||||
}
|
||||
|
||||
public static function tableColorProvider(): \Generator
|
||||
{
|
||||
yield [LogLevel::EMERGENCY, 'table-danger'];
|
||||
yield [LogLevel::ALERT, 'table-danger'];
|
||||
yield [LogLevel::CRITICAL, 'table-danger'];
|
||||
yield [LogLevel::ERROR, 'table-danger'];
|
||||
yield [LogLevel::WARNING, 'table-warning'];
|
||||
yield [LogLevel::NOTICE, 'table-info'];
|
||||
yield [LogLevel::INFO, ''];
|
||||
yield [LogLevel::DEBUG, ''];
|
||||
}
|
||||
|
||||
#[DataProvider('tableColorProvider')]
|
||||
public function testLogLevelToTableColorClass(string $logLevel, string $expectedClass): void
|
||||
{
|
||||
$this->assertSame($expectedClass, $this->service->logLevelToTableColorClass($logLevel));
|
||||
}
|
||||
|
||||
public function testUnknownLogLevelReturnsEmptyColor(): void
|
||||
{
|
||||
$this->assertSame('', $this->service->logLevelToTableColorClass('unknown_level'));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue