Files
kimai/tests/EventSubscriber/UserPreferenceSubscriberTest.php
Kevin Papst 677f8d776c [3.0] Update Symfony to 7.4 and dependencies, removed deprecated code, removed API password (#5622)
* deactivate deprecated auto-mapping
* use native month-picker with fallback on old browser
* bump dependencies to new major versions
* replace deprecated ARRAY column type with JSON
* named arguments
* nullable arguments
* native lazy ghosts
* update routes to use php config
* disable PHP < 8.4
* use static RTL configuration
* new qr code package
* clear out env file
* recipe updates
* fix deprecations
* preset new env variable if not existing
* bump maria db version examples
* remove deprecated api-token support
* fix validator deprecations
* remove timesheet category column
* fix broken validator autoconfiguration
* stabilize tests
* fix migration syntax compatibility
* fix doctrine deprecation
* fix datetime format
* remove constructor dependency
* only include visible preferences in invoice templates
* fix test container dependency
* removed ProjectConstraint multi-constraint logic
* allow to disable plugins in certain environments
* refactor TimesheetConstraint to non-service
* activate new interface methods
* prevent update issues with .env
* improved customer fixture
* removed $user->isExportDecimal()
2026-01-04 15:04:25 +01:00

125 lines
3.9 KiB
PHP

<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\EventSubscriber;
use App\Entity\User;
use App\Entity\UserPreference;
use App\Event\PrepareUserEvent;
use App\EventSubscriber\UserPreferenceSubscriber;
use App\Tests\Mocks\SystemConfigurationFactory;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
#[CoversClass(UserPreferenceSubscriber::class)]
class UserPreferenceSubscriberTest extends TestCase
{
public const EXPECTED_PREFERENCES = [
'hourly_rate',
'internal_rate',
'timezone',
'language',
'locale',
'first_weekday',
'skin',
'update_browser_title',
'calendar_initial_view',
'login_initial_view',
'daily_stats',
'favorite_routes',
];
public function testGetSubscribedEvents(): void
{
$events = UserPreferenceSubscriber::getSubscribedEvents();
self::assertArrayHasKey(PrepareUserEvent::class, $events);
$methodName = $events[PrepareUserEvent::class][0];
self::assertIsString($methodName);
self::assertTrue(method_exists(UserPreferenceSubscriber::class, $methodName));
}
public function testWithHourlyRateAllowed(): void
{
$sut = $this->getSubscriber(true);
$user = new User();
$event = new PrepareUserEvent($user);
self::assertSame($user, $event->getUser());
$prefs = $sut->getDefaultPreferences($user);
foreach ($prefs as $pref) {
self::assertTrue(\in_array($pref->getName(), self::EXPECTED_PREFERENCES), 'Unknown user preference: ' . $pref->getName());
}
self::assertCount(\count(self::EXPECTED_PREFERENCES), $prefs);
foreach ($prefs as $pref) {
switch ($pref->getName()) {
case UserPreference::HOURLY_RATE:
case UserPreference::INTERNAL_RATE:
self::assertTrue($pref->isEnabled());
break;
case UserPreference::FIRST_WEEKDAY:
self::assertEquals('monday', $pref->getValue());
break;
default:
self::assertTrue($pref->isEnabled());
}
}
}
public function testWithHourlyRateNotAllowed(): void
{
$sut = $this->getSubscriber(false);
$user = new User();
$event = new PrepareUserEvent($user);
self::assertSame($user, $event->getUser());
// TODO test merging values
$sut->loadUserPreferences($event);
$prefs = $event->getUser()->getPreferences();
self::assertCount(\count(self::EXPECTED_PREFERENCES), $prefs);
foreach ($prefs as $pref) {
switch ($pref->getName()) {
case UserPreference::HOURLY_RATE:
case UserPreference::INTERNAL_RATE:
self::assertFalse($pref->isEnabled());
break;
default:
self::assertTrue($pref->isEnabled());
}
}
}
protected function getSubscriber(bool $seeHourlyRate): UserPreferenceSubscriber
{
$authMock = $this->createMock(AuthorizationCheckerInterface::class);
$authMock->method('isGranted')->willReturn($seeHourlyRate);
$eventMock = $this->createMock(EventDispatcherInterface::class);
$formConfigMock = SystemConfigurationFactory::createStub([
'defaults' => [
'user' => [
'language' => 'en',
'currency' => 'EUR',
]
]
]);
return new UserPreferenceSubscriber($eventMock, $authMock, $formConfigMock);
}
}