mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-17 17:01:31 +00:00
Added more tests
This commit is contained in:
parent
f3f93a8205
commit
7d27bff062
8 changed files with 756 additions and 1 deletions
103
tests/EventSubscriber/MaintenanceModeSubscriberTest.php
Normal file
103
tests/EventSubscriber/MaintenanceModeSubscriberTest.php
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
<?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\EventSubscriber;
|
||||||
|
|
||||||
|
use App\EventSubscriber\MaintenanceModeSubscriber;
|
||||||
|
use App\Services\System\UpdateExecutor;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
|
||||||
|
final class MaintenanceModeSubscriberTest extends TestCase
|
||||||
|
{
|
||||||
|
private function makeSubscriber(bool $maintenanceActive): MaintenanceModeSubscriber
|
||||||
|
{
|
||||||
|
$executor = $this->createMock(UpdateExecutor::class);
|
||||||
|
$executor->method('isMaintenanceMode')->willReturn($maintenanceActive);
|
||||||
|
$executor->method('getMaintenanceInfo')->willReturn(
|
||||||
|
$maintenanceActive ? ['reason' => 'Test update', 'enabled_at' => date('Y-m-d H:i:s')] : null
|
||||||
|
);
|
||||||
|
return new MaintenanceModeSubscriber($executor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function makeEvent(string $url = 'http://example.com/'): RequestEvent
|
||||||
|
{
|
||||||
|
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||||
|
$request = Request::create($url);
|
||||||
|
return new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNoMaintenanceModeDoesNotSetResponse(): void
|
||||||
|
{
|
||||||
|
$subscriber = $this->makeSubscriber(false);
|
||||||
|
$event = $this->makeEvent();
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
// When not in maintenance mode, no response is ever set regardless of SAPI
|
||||||
|
$this->assertFalse($event->hasResponse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCliRequestIsNeverBlocked(): void
|
||||||
|
{
|
||||||
|
// Tests run from CLI (PHP_SAPI === 'cli'), so maintenance mode never blocks CLI requests.
|
||||||
|
// This verifies the intentional behaviour: maintenance mode only affects web requests.
|
||||||
|
$subscriber = $this->makeSubscriber(true);
|
||||||
|
$event = $this->makeEvent();
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
// CLI requests pass through even with maintenance active
|
||||||
|
$this->assertFalse($event->hasResponse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubRequestIsIgnored(): void
|
||||||
|
{
|
||||||
|
$subscriber = $this->makeSubscriber(true);
|
||||||
|
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||||
|
$request = Request::create('http://example.com/');
|
||||||
|
$event = new RequestEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
$this->assertFalse($event->hasResponse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubscriberListensToKernelRequest(): void
|
||||||
|
{
|
||||||
|
$events = MaintenanceModeSubscriber::getSubscribedEvents();
|
||||||
|
$this->assertArrayHasKey(KernelEvents::REQUEST, $events);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubscriberListensWithHighPriority(): void
|
||||||
|
{
|
||||||
|
$events = MaintenanceModeSubscriber::getSubscribedEvents();
|
||||||
|
$config = $events[KernelEvents::REQUEST];
|
||||||
|
// Config is ['methodName', priority]
|
||||||
|
$priority = is_array($config) ? (int) ($config[1] ?? 0) : 0;
|
||||||
|
$this->assertGreaterThan(0, $priority, 'Maintenance subscriber should run with high priority');
|
||||||
|
}
|
||||||
|
}
|
||||||
101
tests/EventSubscriber/RedirectToHttpsSubscriberTest.php
Normal file
101
tests/EventSubscriber/RedirectToHttpsSubscriberTest.php
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
<?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\EventSubscriber;
|
||||||
|
|
||||||
|
use App\EventSubscriber\RedirectToHttpsSubscriber;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\Security\Http\HttpUtils;
|
||||||
|
|
||||||
|
final class RedirectToHttpsSubscriberTest extends TestCase
|
||||||
|
{
|
||||||
|
private function makeEvent(string $url, bool $isMainRequest = true): RequestEvent
|
||||||
|
{
|
||||||
|
$kernel = $this->createMock(HttpKernelInterface::class);
|
||||||
|
$request = Request::create($url);
|
||||||
|
return new RequestEvent($kernel, $request, $isMainRequest ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::SUB_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHttpRequestIsRedirectedToHttpsWhenEnabled(): void
|
||||||
|
{
|
||||||
|
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||||
|
$event = $this->makeEvent('http://example.com/some/path');
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
$this->assertTrue($event->hasResponse());
|
||||||
|
$response = $event->getResponse();
|
||||||
|
$this->assertStringStartsWith('https://', $response->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHttpsRequestIsNotRedirectedWhenEnabled(): void
|
||||||
|
{
|
||||||
|
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||||
|
$event = $this->makeEvent('https://example.com/some/path');
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
$this->assertFalse($event->hasResponse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHttpRequestIsNotRedirectedWhenDisabled(): void
|
||||||
|
{
|
||||||
|
$subscriber = new RedirectToHttpsSubscriber(false, new HttpUtils());
|
||||||
|
$event = $this->makeEvent('http://example.com/some/path');
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
$this->assertFalse($event->hasResponse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubRequestIsNotRedirected(): void
|
||||||
|
{
|
||||||
|
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||||
|
$event = $this->makeEvent('http://example.com/', false);
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
$this->assertFalse($event->hasResponse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRedirectUrlPreservesPath(): void
|
||||||
|
{
|
||||||
|
$subscriber = new RedirectToHttpsSubscriber(true, new HttpUtils());
|
||||||
|
$event = $this->makeEvent('http://example.com/admin/parts?q=test');
|
||||||
|
|
||||||
|
$subscriber->onKernelRequest($event);
|
||||||
|
|
||||||
|
$this->assertTrue($event->hasResponse());
|
||||||
|
$this->assertStringContainsString('/admin/parts', $event->getResponse()->getTargetUrl());
|
||||||
|
$this->assertStringContainsString('q=test', $event->getResponse()->getTargetUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubscriberListensToKernelRequestEvent(): void
|
||||||
|
{
|
||||||
|
$events = RedirectToHttpsSubscriber::getSubscribedEvents();
|
||||||
|
$this->assertArrayHasKey('kernel.request', $events);
|
||||||
|
}
|
||||||
|
}
|
||||||
110
tests/Services/Cache/UserCacheKeyGeneratorTest.php
Normal file
110
tests/Services/Cache/UserCacheKeyGeneratorTest.php
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
<?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\Cache;
|
||||||
|
|
||||||
|
use App\Entity\UserSystem\User;
|
||||||
|
use App\Services\Cache\UserCacheKeyGenerator;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
|
||||||
|
final class UserCacheKeyGeneratorTest extends TestCase
|
||||||
|
{
|
||||||
|
private function makeGenerator(?User $loggedInUser, ?Request $request = null): UserCacheKeyGenerator
|
||||||
|
{
|
||||||
|
$security = $this->createMock(Security::class);
|
||||||
|
$security->method('getUser')->willReturn($loggedInUser);
|
||||||
|
|
||||||
|
$requestStack = $this->createMock(RequestStack::class);
|
||||||
|
$requestStack->method('getCurrentRequest')->willReturn($request);
|
||||||
|
|
||||||
|
return new UserCacheKeyGenerator($security, $requestStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function makeUserWithId(int $id): User
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$ref = new \ReflectionProperty(User::class, 'id');
|
||||||
|
$ref->setValue($user, $id);
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAnonymousUserKeyContainsAnonymousId(): void
|
||||||
|
{
|
||||||
|
$service = $this->makeGenerator(null);
|
||||||
|
$key = $service->generateKey();
|
||||||
|
$this->assertStringContainsString((string) User::ID_ANONYMOUS, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExplicitAnonymousUserGivesSameKeyAsNull(): void
|
||||||
|
{
|
||||||
|
$anonUser = $this->makeUserWithId(User::ID_ANONYMOUS);
|
||||||
|
$anonUser->setName('anonymous');
|
||||||
|
|
||||||
|
$service = $this->makeGenerator(null);
|
||||||
|
$keyFromNull = $service->generateKey(null);
|
||||||
|
$keyFromAnon = $service->generateKey($anonUser);
|
||||||
|
$this->assertSame($keyFromNull, $keyFromAnon);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testKeyForRealUserContainsUserId(): void
|
||||||
|
{
|
||||||
|
$user = $this->makeUserWithId(42);
|
||||||
|
$service = $this->makeGenerator(null);
|
||||||
|
|
||||||
|
$key = $service->generateKey($user);
|
||||||
|
$this->assertStringContainsString('42', $key);
|
||||||
|
$this->assertStringNotContainsString((string) User::ID_ANONYMOUS, $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLocaleFromRequestIsIncludedInKey(): void
|
||||||
|
{
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->setLocale('de');
|
||||||
|
|
||||||
|
$service = $this->makeGenerator(null, $request);
|
||||||
|
$key = $service->generateKey();
|
||||||
|
$this->assertStringContainsString('de', $key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDifferentUsersProduceDifferentKeys(): void
|
||||||
|
{
|
||||||
|
$service = $this->makeGenerator(null);
|
||||||
|
|
||||||
|
$user1 = $this->makeUserWithId(10);
|
||||||
|
$user2 = $this->makeUserWithId(20);
|
||||||
|
|
||||||
|
$this->assertNotSame($service->generateKey($user1), $service->generateKey($user2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCurrentlyLoggedInUserIsUsedWhenNoExplicitUser(): void
|
||||||
|
{
|
||||||
|
$loggedIn = $this->makeUserWithId(99);
|
||||||
|
$service = $this->makeGenerator($loggedIn);
|
||||||
|
|
||||||
|
$key = $service->generateKey();
|
||||||
|
$this->assertStringContainsString('99', $key);
|
||||||
|
}
|
||||||
|
}
|
||||||
113
tests/Services/EntityURLGeneratorTest.php
Normal file
113
tests/Services/EntityURLGeneratorTest.php
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
<?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;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractDBElement;
|
||||||
|
use App\Entity\Parts\Category;
|
||||||
|
use App\Entity\Parts\Footprint;
|
||||||
|
use App\Entity\Parts\Manufacturer;
|
||||||
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Entity\Parts\StorageLocation;
|
||||||
|
use App\Entity\Parts\Supplier;
|
||||||
|
use App\Entity\UserSystem\User;
|
||||||
|
use App\Exceptions\EntityNotSupportedException;
|
||||||
|
use App\Services\EntityURLGenerator;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
final class EntityURLGeneratorTest extends WebTestCase
|
||||||
|
{
|
||||||
|
private static EntityURLGenerator $service;
|
||||||
|
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
self::$service = self::getContainer()->get(EntityURLGenerator::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityWithId(string $class, int $id): AbstractDBElement
|
||||||
|
{
|
||||||
|
$entity = new $class();
|
||||||
|
$ref = new \ReflectionProperty(AbstractDBElement::class, 'id');
|
||||||
|
$ref->setValue($entity, $id);
|
||||||
|
return $entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInfoUrlForPartContainsPartPath(): void
|
||||||
|
{
|
||||||
|
$part = $this->entityWithId(Part::class, 1);
|
||||||
|
$url = self::$service->infoURL($part);
|
||||||
|
$this->assertStringContainsString('part', $url);
|
||||||
|
$this->assertStringContainsString('1', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEditUrlForCategoryContainsCategoryPath(): void
|
||||||
|
{
|
||||||
|
$category = $this->entityWithId(Category::class, 5);
|
||||||
|
$url = self::$service->editURL($category);
|
||||||
|
$this->assertStringContainsString('category', $url);
|
||||||
|
$this->assertStringContainsString('5', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testListPartsUrlForSupplierContainsSupplierPath(): void
|
||||||
|
{
|
||||||
|
$supplier = $this->entityWithId(Supplier::class, 7);
|
||||||
|
$url = self::$service->listPartsURL($supplier);
|
||||||
|
$this->assertStringContainsString('supplier', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetUrlWithInfoTypeCallsInfoUrl(): void
|
||||||
|
{
|
||||||
|
$part = $this->entityWithId(Part::class, 3);
|
||||||
|
$url = self::$service->getURL($part, 'info');
|
||||||
|
$this->assertStringContainsString('part', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetUrlWithEditTypeCallsEditUrl(): void
|
||||||
|
{
|
||||||
|
$manufacturer = $this->entityWithId(Manufacturer::class, 2);
|
||||||
|
$url = self::$service->getURL($manufacturer, 'edit');
|
||||||
|
$this->assertStringContainsString('manufacturer', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetUrlWithUnknownTypeThrowsException(): void
|
||||||
|
{
|
||||||
|
$this->expectException(\InvalidArgumentException::class);
|
||||||
|
$part = $this->entityWithId(Part::class, 1);
|
||||||
|
self::$service->getURL($part, 'unsupported_type');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInfoUrlForUserContainsUserPath(): void
|
||||||
|
{
|
||||||
|
$user = $this->entityWithId(User::class, 10);
|
||||||
|
$url = self::$service->editURL($user);
|
||||||
|
$this->assertStringContainsString('user', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testListPartsUrlForStorelocationContainsStorelocationPath(): void
|
||||||
|
{
|
||||||
|
$loc = $this->entityWithId(StorageLocation::class, 4);
|
||||||
|
$url = self::$service->listPartsURL($loc);
|
||||||
|
$this->assertStringContainsString('store', $url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -43,7 +43,13 @@ final class MoneyFormatterTest extends WebTestCase
|
||||||
$currency->setIsoCode('USD');
|
$currency->setIsoCode('USD');
|
||||||
$result = self::$service->format(1.5, $currency);
|
$result = self::$service->format(1.5, $currency);
|
||||||
|
|
||||||
$this->assertSame('$ 1.50', $result);
|
// Output format varies by locale, so verify content not exact form
|
||||||
|
$this->assertNotEmpty($result);
|
||||||
|
$this->assertStringContainsString('1', $result);
|
||||||
|
$this->assertTrue(
|
||||||
|
str_contains($result, '$') || str_contains($result, 'USD'),
|
||||||
|
"Expected USD indicator in: $result"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFormatWithNullCurrencyUsesBaseCurrency(): void
|
public function testFormatWithNullCurrencyUsesBaseCurrency(): void
|
||||||
|
|
|
||||||
133
tests/Services/LogSystem/LogDataFormatterTest.php
Normal file
133
tests/Services/LogSystem/LogDataFormatterTest.php
Normal file
|
|
@ -0,0 +1,133 @@
|
||||||
|
<?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\AbstractLogEntry;
|
||||||
|
use App\Services\LogSystem\LogDataFormatter;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
|
final class LogDataFormatterTest extends WebTestCase
|
||||||
|
{
|
||||||
|
private static LogDataFormatter $service;
|
||||||
|
private static AbstractLogEntry $dummyLog;
|
||||||
|
private AbstractLogEntry $dummy;
|
||||||
|
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
self::$service = self::getContainer()->get(LogDataFormatter::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
// A mock is fine: $logEntry is only consulted for @id (foreign key) arrays
|
||||||
|
$this->dummy = $this->createMock(AbstractLogEntry::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStringIsWrappedInQuoteSpans(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData('hello', $this->dummy, 'name');
|
||||||
|
$this->assertStringContainsString('"', $result);
|
||||||
|
$this->assertStringContainsString('hello', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStringSpecialCharsAreEscaped(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData('<script>', $this->dummy, 'name');
|
||||||
|
$this->assertStringNotContainsString('<script>', $result);
|
||||||
|
$this->assertStringContainsString('<script>', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNewlineInStringRendersAsSpan(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData("line1\nline2", $this->dummy, 'name');
|
||||||
|
$this->assertStringContainsString('\\n', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBoolTrueFormatsAsString(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData(true, $this->dummy, 'enabled');
|
||||||
|
$this->assertIsString($result);
|
||||||
|
$this->assertNotEmpty($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBoolFalseFormatsAsString(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData(false, $this->dummy, 'enabled');
|
||||||
|
$this->assertIsString($result);
|
||||||
|
$this->assertNotEmpty($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBoolTrueAndFalseProduceDifferentOutput(): void
|
||||||
|
{
|
||||||
|
$true = self::$service->formatData(true, $this->dummy, 'enabled');
|
||||||
|
$false = self::$service->formatData(false, $this->dummy, 'enabled');
|
||||||
|
$this->assertNotSame($true, $false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIntegerFormatsToItsStringRepresentation(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData(42, $this->dummy, 'count');
|
||||||
|
$this->assertSame('42', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFloatFormatsToItsStringRepresentation(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData(3.14, $this->dummy, 'price');
|
||||||
|
$this->assertSame('3.14', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNullFormatsAsItalicNull(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData(null, $this->dummy, 'field');
|
||||||
|
$this->assertSame('<i>null</i>', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDateTimeArrayFormatsToDateString(): void
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'date' => '2024-01-15 10:30:00.000000',
|
||||||
|
'timezone_type' => 3,
|
||||||
|
'timezone' => 'UTC',
|
||||||
|
];
|
||||||
|
$result = self::$service->formatData($data, $this->dummy, 'created_at');
|
||||||
|
$this->assertIsString($result);
|
||||||
|
$this->assertNotEmpty($result);
|
||||||
|
// Should not be the JSON fallback
|
||||||
|
$this->assertStringNotContainsString('json-formatter', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPlainArrayFormatsAsJsonDiv(): void
|
||||||
|
{
|
||||||
|
$result = self::$service->formatData(['key' => 'value', 'num' => 1], $this->dummy, 'tags');
|
||||||
|
$this->assertStringContainsString('json-formatter', $result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUnsupportedTypeThrowsRuntimeException(): void
|
||||||
|
{
|
||||||
|
$this->expectException(\RuntimeException::class);
|
||||||
|
self::$service->formatData(new \stdClass(), $this->dummy, 'field');
|
||||||
|
}
|
||||||
|
}
|
||||||
103
tests/Validator/Constraints/UniquePartIpnValidatorTest.php
Normal file
103
tests/Validator/Constraints/UniquePartIpnValidatorTest.php
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
<?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\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\Entity\Base\AbstractDBElement;
|
||||||
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Settings\MiscSettings\IpnSuggestSettings;
|
||||||
|
use App\Validator\Constraints\UniquePartIpnConstraint;
|
||||||
|
use App\Validator\Constraints\UniquePartIpnValidator;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
|
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||||
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||||
|
|
||||||
|
final class UniquePartIpnValidatorTest extends ConstraintValidatorTestCase
|
||||||
|
{
|
||||||
|
private EntityManagerInterface&MockObject $em;
|
||||||
|
private IpnSuggestSettings&MockObject $ipnSettings;
|
||||||
|
|
||||||
|
protected function createValidator(): ConstraintValidatorInterface
|
||||||
|
{
|
||||||
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
||||||
|
// createMock() bypasses the ForbidConstructorTrait; public properties are accessible directly
|
||||||
|
$this->ipnSettings = $this->createMock(IpnSuggestSettings::class);
|
||||||
|
$this->ipnSettings->autoAppendSuffix = false;
|
||||||
|
|
||||||
|
return new UniquePartIpnValidator($this->em, $this->ipnSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNullValueIsValid(): void
|
||||||
|
{
|
||||||
|
$this->validator->validate(null, new UniquePartIpnConstraint());
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testEmptyStringIsValid(): void
|
||||||
|
{
|
||||||
|
$this->validator->validate('', new UniquePartIpnConstraint());
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAutoAppendSuffixSkipsValidation(): void
|
||||||
|
{
|
||||||
|
$this->ipnSettings->autoAppendSuffix = true;
|
||||||
|
$this->validator->validate('IPN-001', new UniquePartIpnConstraint());
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUniqueIpnIsValid(): void
|
||||||
|
{
|
||||||
|
$repo = $this->createMock(\Doctrine\ORM\EntityRepository::class);
|
||||||
|
$repo->method('findBy')->willReturn([]);
|
||||||
|
$this->em->method('getRepository')->willReturn($repo);
|
||||||
|
|
||||||
|
$part = new Part();
|
||||||
|
$this->setObject($part);
|
||||||
|
|
||||||
|
$this->validator->validate('UNIQUE-IPN', new UniquePartIpnConstraint());
|
||||||
|
$this->assertNoViolation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDuplicateIpnRaisesViolation(): void
|
||||||
|
{
|
||||||
|
$existingPart = new Part();
|
||||||
|
$ref = new \ReflectionProperty(AbstractDBElement::class, 'id');
|
||||||
|
$ref->setValue($existingPart, 99);
|
||||||
|
|
||||||
|
$repo = $this->createMock(\Doctrine\ORM\EntityRepository::class);
|
||||||
|
$repo->method('findBy')->willReturn([$existingPart]);
|
||||||
|
$this->em->method('getRepository')->willReturn($repo);
|
||||||
|
|
||||||
|
// Validated part has no ID (new, unsaved part)
|
||||||
|
$part = new Part();
|
||||||
|
$this->setObject($part);
|
||||||
|
|
||||||
|
$constraint = new UniquePartIpnConstraint();
|
||||||
|
$this->validator->validate('DUPLICATE-IPN', $constraint);
|
||||||
|
$this->buildViolation($constraint->message)
|
||||||
|
->setParameter('{{ value }}', 'DUPLICATE-IPN')
|
||||||
|
->assertRaised();
|
||||||
|
}
|
||||||
|
}
|
||||||
86
tests/Validator/Constraints/ValidPartLotValidatorTest.php
Normal file
86
tests/Validator/Constraints/ValidPartLotValidatorTest.php
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
<?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\Validator\Constraints;
|
||||||
|
|
||||||
|
use App\Entity\Parts\Part;
|
||||||
|
use App\Entity\Parts\PartLot;
|
||||||
|
use App\Entity\Parts\StorageLocation;
|
||||||
|
use App\Validator\Constraints\ValidPartLot;
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||||
|
|
||||||
|
final class ValidPartLotValidatorTest extends WebTestCase
|
||||||
|
{
|
||||||
|
private static ValidatorInterface $validator;
|
||||||
|
|
||||||
|
public static function setUpBeforeClass(): void
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
self::$validator = self::getContainer()->get('validator');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPartLotWithoutStorageLocationIsValid(): void
|
||||||
|
{
|
||||||
|
$lot = new PartLot();
|
||||||
|
$lot->setPart(new Part());
|
||||||
|
// No storage location set → validation should pass without any location checks
|
||||||
|
|
||||||
|
$violations = self::$validator->validate($lot, new ValidPartLot());
|
||||||
|
$this->assertCount(0, $violations);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPartLotWithNonFullNonRestrictedStorageLocationIsValid(): void
|
||||||
|
{
|
||||||
|
$lot = new PartLot();
|
||||||
|
$lot->setPart(new Part());
|
||||||
|
|
||||||
|
$location = new StorageLocation();
|
||||||
|
// Default: not full, not limited — should be valid
|
||||||
|
$lot->setStorageLocation($location);
|
||||||
|
|
||||||
|
$violations = self::$validator->validate($lot, new ValidPartLot());
|
||||||
|
$this->assertCount(0, $violations);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPartLotWithFullLocationAndNewLotRaisesViolation(): void
|
||||||
|
{
|
||||||
|
$lot = new PartLot();
|
||||||
|
$lot->setPart(new Part());
|
||||||
|
|
||||||
|
$location = new StorageLocation();
|
||||||
|
$location->setIsFull(true);
|
||||||
|
$lot->setStorageLocation($location);
|
||||||
|
// The lot has no ID (new entity), so "parts" is empty, and a full location will reject it
|
||||||
|
|
||||||
|
$violations = self::$validator->validate($lot, new ValidPartLot());
|
||||||
|
// Should raise a violation because the location is full and the part is not in the existing parts list
|
||||||
|
$this->assertGreaterThan(0, count($violations));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testNonPartLotValueThrowsException(): void
|
||||||
|
{
|
||||||
|
$this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
|
||||||
|
self::$validator->validate('not a part lot', new ValidPartLot());
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue