0
0
Fork 0
mirror of https://github.com/kevinpapst/kimai2.git synced 2025-04-18 19:28:57 +00:00
kevinpapst_kimai2/tests/EventSubscriber/UserPreferenceSubscriberTest.php
Kevin Papst 2e6b700b43
Release 2.32 (#5411)
* bump packages
* dynamic invoice options
* make sure that invoice previews can be detected
* support for mpdf associated files
* do not include any future times in work contract calculation
* re-add username column in Excel spreadsheet
* deactivate internal rate editing
* show if plugin update exists
* shorten name to Kimai only, without Time-Tracking
* remove check for existing id in work contract
* fix metafield already defined in search
* helper methods to unlock months
* new translation
* send event on unlock month
2025-04-06 09:53:48 +02:00

126 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\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* @covers \App\EventSubscriber\UserPreferenceSubscriber
*/
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',
'export_decimal',
'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);
}
}