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/ProfileSubscriberTest.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

132 lines
5 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\EventSubscriber\LastLoginSubscriber;
use App\EventSubscriber\ProfileSubscriber;
use App\Utils\ProfileManager;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
/**
* @covers \App\EventSubscriber\ProfileSubscriber
*/
class ProfileSubscriberTest extends TestCase
{
public function testGetSubscribedEvents(): void
{
$events = ProfileSubscriber::getSubscribedEvents();
self::assertArrayHasKey(LoginSuccessEvent::class, $events);
$methodName = $events[LoginSuccessEvent::class];
self::assertIsString($methodName);
self::assertTrue(method_exists(LastLoginSubscriber::class, $methodName));
}
public function testOnLoginSuccessWithoutProfileSetsDesktop(): void
{
$manager = new ProfileManager();
$sut = new ProfileSubscriber($manager);
$request = new Request();
$session = new Session(new MockFileSessionStorage());
$request->setSession($session);
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
$user = new User();
$authenticator = $this->createMock(AuthenticatorInterface::class);
$passport = $this->createMock(Passport::class);
$passport->method('getUser')->willReturn($user);
$event = new LoginSuccessEvent($authenticator, $passport, new UsernamePasswordToken($user, 'sdf'), $request, null, 'xyz');
$sut->onFormLogin($event);
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
self::assertEquals(ProfileManager::PROFILE_DESKTOP, $manager->getProfileFromSession($session));
}
public function testOnLoginSuccessWithProfile(): void
{
$manager = new ProfileManager();
$sut = new ProfileSubscriber($manager);
$request = new Request();
$session = new Session(new MockFileSessionStorage());
$request->setSession($session);
$request->cookies->set(ProfileManager::COOKIE_PROFILE, 'mobile');
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
$user = new User();
self::assertNull($user->getLastLogin());
$authenticator = $this->createMock(AuthenticatorInterface::class);
$passport = $this->createMock(Passport::class);
$passport->method('getUser')->willReturn($user);
$event = new LoginSuccessEvent($authenticator, $passport, new UsernamePasswordToken($user, 'sdf'), $request, null, 'xyz');
$sut->onFormLogin($event);
self::assertNotNull($session->get(ProfileManager::SESSION_PROFILE));
self::assertEquals(ProfileManager::PROFILE_MOBILE, $session->get(ProfileManager::SESSION_PROFILE));
self::assertEquals(ProfileManager::PROFILE_MOBILE, $manager->getProfileFromSession($session));
}
public static function getInvalidCookies()
{
return [
['MOBILE'],
['mobilE'],
['mobile2'],
['mobile2'],
['foo'],
['mobile '],
[' desktop'],
['DESKTOP'],
// the next two ones are misleading: default value "desktop" will not be set and session value will be removed
['desktop'],
[ProfileManager::PROFILE_DESKTOP],
];
}
/**
* @dataProvider getInvalidCookies
*/
public function testOnLoginSuccessWithInvalidProfile(string $cookieValue): void
{
$manager = new ProfileManager();
$sut = new ProfileSubscriber($manager);
$request = new Request();
$session = new Session(new MockFileSessionStorage());
$request->setSession($session);
$request->cookies->set(ProfileManager::COOKIE_PROFILE, $cookieValue);
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
$user = new User();
self::assertNull($user->getLastLogin());
$authenticator = $this->createMock(AuthenticatorInterface::class);
$passport = $this->createMock(Passport::class);
$passport->method('getUser')->willReturn($user);
$event = new LoginSuccessEvent($authenticator, $passport, new UsernamePasswordToken($user, 'sdf'), $request, null, 'xyz');
$sut->onFormLogin($event);
self::assertNull($session->get(ProfileManager::SESSION_PROFILE));
self::assertEquals(ProfileManager::PROFILE_DESKTOP, $manager->getProfileFromSession($session));
}
}