From 78900642281a0b2c83d6d5b8f9bd12f9f1586b69 Mon Sep 17 00:00:00 2001 From: Nucleos Bot Date: Mon, 20 May 2024 19:29:13 +0000 Subject: [PATCH 1/8] Bumps changelog version to 3.4.2 Updates the CHANGELOG.md file to add a changelog entry for a new 3.4.2 version. --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93b0c234..7477f2e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,28 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. +## 3.4.2 - TBD + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 3.4.1 - 2024-05-20 From ca8d198b9fa2ed7ae12c866a7e501435f2c6d456 Mon Sep 17 00:00:00 2001 From: core23 Date: Sun, 7 Jul 2024 15:29:56 +0200 Subject: [PATCH 2/8] Fix unsetting locale --- src/EventListener/LocaleEventListener.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/EventListener/LocaleEventListener.php b/src/EventListener/LocaleEventListener.php index 66eb523e..0ee9c003 100644 --- a/src/EventListener/LocaleEventListener.php +++ b/src/EventListener/LocaleEventListener.php @@ -26,6 +26,10 @@ final class LocaleEventListener implements EventSubscriberInterface { + private const ATTR_LOCALE = '_locale'; + + private const ATTR_TIMEZONE = '_timezone'; + private readonly LocaleAwareTranslator $translator; public function __construct(LocaleAwareTranslator $translator) @@ -78,9 +82,8 @@ public function onKernelRequest(RequestEvent $event): void $session = $request->getSession(); - if (null !== $locale = $session->get('_locale')) { - $this->translator->setLocale($locale); - $request->setLocale($locale); + if (null !== $locale = $session->get(self::ATTR_LOCALE)) { + $this->setLocale($request, $locale); } } @@ -113,12 +116,14 @@ private function setLocale(Request $request, LocaleAwareUser $user): void $locale = $user->getLocale(); if ('' === $locale || null === $locale) { + $session->remove(self::ATTR_LOCALE); + return; } $this->translator->setLocale($locale); $request->setLocale($locale); - $session->set('_locale', $locale); + $session->set(self::ATTR_LOCALE, $locale); } private function setTimezone(Request $request, LocaleAwareUser $user): void @@ -127,13 +132,16 @@ private function setTimezone(Request $request, LocaleAwareUser $user): void return; } + $session = $request->getSession(); + $timezone = $user->getTimezone(); if ('' === $timezone || null === $timezone) { + $session->remove(self::ATTR_TIMEZONE); + return; } - $session = $request->getSession(); - $session->set('_timezone', $timezone); + $session->set(self::ATTR_TIMEZONE, $timezone); } } From 872f39a0adf6c8c4ec728e22348f1972da729443 Mon Sep 17 00:00:00 2001 From: Nucleos Bot Date: Sun, 7 Jul 2024 13:37:18 +0000 Subject: [PATCH 3/8] 3.4.2 readiness Updates the CHANGELOG.md to set the release date. --- CHANGELOG.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7477f2e5..055c3d5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,27 +24,24 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. -## 3.4.2 - TBD +## 3.4.2 - 2024-07-07 -### Added - -- Nothing. - -### Changed -- Nothing. +----- -### Deprecated +### Release Notes for [3.4.2](https://github.com/nucleos/NucleosUserBundle/milestone/43) -- Nothing. +3.4.x bugfix release (patch) -### Removed +### 3.4.2 -- Nothing. +- Total issues resolved: **0** +- Total pull requests resolved: **1** +- Total contributors: **1** -### Fixed +#### Bug -- Nothing. + - [757: Fix unsetting locale](https://github.com/nucleos/NucleosUserBundle/pull/757) thanks to @core23 ## 3.4.1 - 2024-05-20 From 082aa5d8dcb3b3996be548a7ab712cd61bbde920 Mon Sep 17 00:00:00 2001 From: Nucleos Bot Date: Sun, 7 Jul 2024 13:37:24 +0000 Subject: [PATCH 4/8] Bumps changelog version to 3.4.3 Updates the CHANGELOG.md file to add a changelog entry for a new 3.4.3 version. --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 055c3d5a..f8ac38df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,28 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. +## 3.4.3 - TBD + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + ## 3.4.2 - 2024-07-07 From bb873a55af5d3e615f2e2ac90b262831b7061cc1 Mon Sep 17 00:00:00 2001 From: core23 Date: Sun, 7 Jul 2024 19:53:04 +0200 Subject: [PATCH 5/8] Fix setting locale --- src/EventListener/LocaleEventListener.php | 20 +- .../EventListener/LocaleEventListenerTest.php | 178 ++++++++++++++++++ 2 files changed, 186 insertions(+), 12 deletions(-) create mode 100644 tests/EventListener/LocaleEventListenerTest.php diff --git a/src/EventListener/LocaleEventListener.php b/src/EventListener/LocaleEventListener.php index 0ee9c003..cd7e4700 100644 --- a/src/EventListener/LocaleEventListener.php +++ b/src/EventListener/LocaleEventListener.php @@ -56,8 +56,8 @@ public function onImplicitLogin(UserEvent $event): void return; } - $this->setLocale($event->getRequest(), $user); - $this->setTimezone($event->getRequest(), $user); + $this->setLocale($event->getRequest(), $user->getLocale()); + $this->setTimezone($event->getRequest(), $user->getTimezone()); } public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void @@ -68,8 +68,8 @@ public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void return; } - $this->setLocale($event->getRequest(), $user); - $this->setTimezone($event->getRequest(), $user); + $this->setLocale($event->getRequest(), $user->getLocale()); + $this->setTimezone($event->getRequest(), $user->getTimezone()); } public function onKernelRequest(RequestEvent $event): void @@ -92,7 +92,7 @@ public function onTimezoneChanged(UserEvent $event): void $user = $event->getUser(); if ($user instanceof LocaleAwareUser && null !== $event->getRequest()) { - $this->setTimezone($event->getRequest(), $user); + $this->setTimezone($event->getRequest(), $user->getTimezone()); } } @@ -101,11 +101,11 @@ public function onLocaleChanged(UserEvent $event): void $user = $event->getUser(); if ($user instanceof LocaleAwareUser && null !== $event->getRequest()) { - $this->setLocale($event->getRequest(), $user); + $this->setLocale($event->getRequest(), $user->getLocale()); } } - private function setLocale(Request $request, LocaleAwareUser $user): void + private function setLocale(Request $request, ?string $locale): void { if (!$request->hasSession()) { return; @@ -113,8 +113,6 @@ private function setLocale(Request $request, LocaleAwareUser $user): void $session = $request->getSession(); - $locale = $user->getLocale(); - if ('' === $locale || null === $locale) { $session->remove(self::ATTR_LOCALE); @@ -126,7 +124,7 @@ private function setLocale(Request $request, LocaleAwareUser $user): void $session->set(self::ATTR_LOCALE, $locale); } - private function setTimezone(Request $request, LocaleAwareUser $user): void + private function setTimezone(Request $request, ?string $timezone): void { if (!$request->hasSession()) { return; @@ -134,8 +132,6 @@ private function setTimezone(Request $request, LocaleAwareUser $user): void $session = $request->getSession(); - $timezone = $user->getTimezone(); - if ('' === $timezone || null === $timezone) { $session->remove(self::ATTR_TIMEZONE); diff --git a/tests/EventListener/LocaleEventListenerTest.php b/tests/EventListener/LocaleEventListenerTest.php new file mode 100644 index 00000000..16a40d57 --- /dev/null +++ b/tests/EventListener/LocaleEventListenerTest.php @@ -0,0 +1,178 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nucleos\UserBundle\Tests\EventListener; + +use Nucleos\UserBundle\Event\UserEvent; +use Nucleos\UserBundle\EventListener\LocaleEventListener; +use Nucleos\UserBundle\NucleosUserEvents; +use Nucleos\UserBundle\Tests\App\Entity\TestUser; +use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; +use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\KernelInterface; +use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; +use Symfony\Component\Security\Http\SecurityEvents; +use Symfony\Contracts\Translation\LocaleAwareInterface as LocaleAwareTranslator; + +final class LocaleEventListenerTest extends TestCase +{ + /** + * @var LocaleAwareTranslator&MockObject + */ + private LocaleAwareTranslator $translator; + + private LocaleEventListener $listener; + + protected function setUp(): void + { + $this->translator = $this->createMock(LocaleAwareTranslator::class); + $this->listener = new LocaleEventListener($this->translator); + } + + #[Test] + public function getSubscribedEvents(): void + { + self::assertSame([ + NucleosUserEvents::SECURITY_IMPLICIT_LOGIN => 'onImplicitLogin', + SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin', + KernelEvents::REQUEST => [['onKernelRequest', 20]], + NucleosUserEvents::USER_LOCALE_CHANGED => 'onLocaleChanged', + NucleosUserEvents::USER_TIMEZONE_CHANGED => 'onTimezoneChanged', + ], LocaleEventListener::getSubscribedEvents()); + } + + #[Test] + public function onImplicitLogin(): void + { + $session = new Session(new MockArraySessionStorage()); + + $request = new Request(); + $request->setSession($session); + + $user = new TestUser(); + $user->setLocale('fr'); + $user->setTimezone('Europe/Paris'); + + $event = new UserEvent($user, $request); + + $this->translator->expects(self::once()) + ->method('setLocale') + ->with('fr') + ; + + $this->listener->onImplicitLogin($event); + + self::assertSame('fr', $request->getLocale()); + self::assertSame('fr', $session->get('_locale')); + self::assertSame('Europe/Paris', $session->get('_timezone')); + } + + #[Test] + public function onSecurityInteractiveLogin(): void + { + $session = new Session(new MockArraySessionStorage()); + + $request = new Request(); + $request->setSession($session); + + $user = new TestUser(); + $user->setLocale('fr'); + $user->setTimezone('Europe/Paris'); + + $event = new InteractiveLoginEvent($request, new TestBrowserToken(user: $user)); + + $this->translator->expects(self::once()) + ->method('setLocale') + ->with('fr') + ; + + $this->listener->onSecurityInteractiveLogin($event); + + self::assertSame('fr', $request->getLocale()); + self::assertSame('fr', $session->get('_locale')); + self::assertSame('Europe/Paris', $session->get('_timezone')); + } + + #[Test] + public function onKernelRequest(): void + { + $session = new Session(new MockArraySessionStorage()); + $session->set('_locale', 'fr'); + $session->set('_timezone', 'Europe/Paris'); + + $request = new Request(); + $request->setSession($session); + $request->cookies->set($session->getName(), $session->getId()); + + $event = new RequestEvent($this->createMock(KernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST); + + $this->translator->expects(self::once()) + ->method('setLocale') + ->with('fr') + ; + + $this->listener->onKernelRequest($event); + + self::assertSame('fr', $request->getLocale()); + self::assertSame('fr', $session->get('_locale')); + self::assertSame('Europe/Paris', $session->get('_timezone')); + } + + #[Test] + public function onTimezoneChanged(): void + { + $session = new Session(new MockArraySessionStorage()); + + $request = new Request(); + $request->setSession($session); + + $user = new TestUser(); + $user->setTimezone('Europe/Paris'); + + $event = new UserEvent($user, $request); + + $this->listener->onTimezoneChanged($event); + + self::assertSame('Europe/Paris', $session->get('_timezone')); + } + + #[Test] + public function onLocaleChanged(): void + { + $session = new Session(new MockArraySessionStorage()); + + $request = new Request(); + $request->setSession($session); + + $user = new TestUser(); + $user->setLocale('de'); + + $event = new UserEvent($user, $request); + + $this->translator->expects(self::once()) + ->method('setLocale') + ->with('de') + ; + + $this->listener->onLocaleChanged($event); + + self::assertSame('de', $request->getLocale()); + self::assertSame('de', $session->get('_locale')); + } +} From 65877d846869429867712ef3c020184233c74a89 Mon Sep 17 00:00:00 2001 From: Nucleos Bot Date: Sun, 7 Jul 2024 17:56:15 +0000 Subject: [PATCH 6/8] 3.4.3 readiness Updates the CHANGELOG.md to set the release date. --- CHANGELOG.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8ac38df..b1ce2afc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,27 +24,24 @@ All notable changes to this project will be documented in this file, in reverse - Nothing. -## 3.4.3 - TBD +## 3.4.3 - 2024-07-07 -### Added - -- Nothing. - -### Changed -- Nothing. +----- -### Deprecated +### Release Notes for [3.4.3](https://github.com/nucleos/NucleosUserBundle/milestone/44) -- Nothing. +3.4.x bugfix release (patch) -### Removed +### 3.4.3 -- Nothing. +- Total issues resolved: **0** +- Total pull requests resolved: **1** +- Total contributors: **1** -### Fixed +#### Bug -- Nothing. + - [759: Fix setting locale](https://github.com/nucleos/NucleosUserBundle/pull/759) thanks to @core23 ## 3.4.2 - 2024-07-07 From 727fe8afba18ba78b9fab34efaed02b4a7f9357b Mon Sep 17 00:00:00 2001 From: core23 Date: Wed, 14 Aug 2024 17:43:14 +0200 Subject: [PATCH 7/8] Fix symfony deprecation --- src/DependencyInjection/NucleosUserExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DependencyInjection/NucleosUserExtension.php b/src/DependencyInjection/NucleosUserExtension.php index de878395..9c244315 100644 --- a/src/DependencyInjection/NucleosUserExtension.php +++ b/src/DependencyInjection/NucleosUserExtension.php @@ -23,10 +23,10 @@ use Symfony\Component\Config\Loader\FileLoader; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; final class NucleosUserExtension extends Extension implements PrependExtensionInterface { From f3b0c155473f4b8b00748b77e2b223b02e6bce74 Mon Sep 17 00:00:00 2001 From: Nucleos Bot Date: Wed, 14 Aug 2024 16:26:52 +0000 Subject: [PATCH 8/8] 3.5.0 readiness Updates the CHANGELOG.md to set the release date. --- CHANGELOG.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1ce2afc..83b49d1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,27 +2,28 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 3.5.0 - TBD +## 3.5.0 - 2024-08-14 -### Added -- Nothing. +----- -### Changed +### Release Notes for [3.5.0](https://github.com/nucleos/NucleosUserBundle/milestone/42) -- Nothing. +Feature release (minor) -### Deprecated +### 3.5.0 -- Nothing. +- Total issues resolved: **0** +- Total pull requests resolved: **2** +- Total contributors: **2** -### Removed +#### Enhancement -- Nothing. + - [764: Fix symfony deprecation](https://github.com/nucleos/NucleosUserBundle/pull/764) thanks to @core23 -### Fixed +#### dependency -- Nothing. + - [753: Update dependency Pygments to v2.18.0](https://github.com/nucleos/NucleosUserBundle/pull/753) thanks to @renovate[bot] ## 3.4.3 - 2024-07-07