mirror of
https://github.com/wallabag/wallabag.git
synced 2025-01-11 01:15:26 +00:00
Add tests on listeners
This commit is contained in:
parent
c89d35e851
commit
2aac2f278f
4 changed files with 147 additions and 0 deletions
|
@ -6,6 +6,9 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html
|
||||||
|
*/
|
||||||
class LocaleListener implements EventSubscriberInterface
|
class LocaleListener implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
private $defaultLocale;
|
private $defaultLocale;
|
||||||
|
|
|
@ -8,6 +8,8 @@ use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
||||||
/**
|
/**
|
||||||
* Stores the locale of the user in the session after the
|
* Stores the locale of the user in the session after the
|
||||||
* login. This can be used by the LocaleListener afterwards.
|
* login. This can be used by the LocaleListener afterwards.
|
||||||
|
*
|
||||||
|
* @see http://symfony.com/doc/master/cookbook/session/locale_sticky_session.html
|
||||||
*/
|
*/
|
||||||
class UserLocaleListener
|
class UserLocaleListener
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\CoreBundle\Tests\EventListener;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
|
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
|
use Wallabag\CoreBundle\EventListener\LocaleListener;
|
||||||
|
|
||||||
|
class LocaleListenerTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
private function getEvent(Request $request)
|
||||||
|
{
|
||||||
|
return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWithoutSession()
|
||||||
|
{
|
||||||
|
$request = Request::create('/');
|
||||||
|
|
||||||
|
$listener = new LocaleListener('fr');
|
||||||
|
$event = $this->getEvent($request);
|
||||||
|
|
||||||
|
$listener->onKernelRequest($event);
|
||||||
|
$this->assertEquals('en', $request->getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWithPreviousSession()
|
||||||
|
{
|
||||||
|
$request = Request::create('/');
|
||||||
|
// generate a previous session
|
||||||
|
$request->cookies->set('MOCKSESSID', 'foo');
|
||||||
|
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||||
|
|
||||||
|
$listener = new LocaleListener('fr');
|
||||||
|
$event = $this->getEvent($request);
|
||||||
|
|
||||||
|
$listener->onKernelRequest($event);
|
||||||
|
$this->assertEquals('fr', $request->getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLocaleFromRequestAttribute()
|
||||||
|
{
|
||||||
|
$request = Request::create('/');
|
||||||
|
// generate a previous session
|
||||||
|
$request->cookies->set('MOCKSESSID', 'foo');
|
||||||
|
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||||
|
$request->attributes->set('_locale', 'es');
|
||||||
|
|
||||||
|
$listener = new LocaleListener('fr');
|
||||||
|
$event = $this->getEvent($request);
|
||||||
|
|
||||||
|
$listener->onKernelRequest($event);
|
||||||
|
$this->assertEquals('en', $request->getLocale());
|
||||||
|
$this->assertEquals('es', $request->getSession()->get('_locale'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSubscribedEvents()
|
||||||
|
{
|
||||||
|
$request = Request::create('/');
|
||||||
|
// generate a previous session
|
||||||
|
$request->cookies->set('MOCKSESSID', 'foo');
|
||||||
|
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||||
|
|
||||||
|
$listener = new LocaleListener('fr');
|
||||||
|
$event = $this->getEvent($request);
|
||||||
|
|
||||||
|
$dispatcher = new EventDispatcher();
|
||||||
|
$dispatcher->addSubscriber($listener);
|
||||||
|
|
||||||
|
$dispatcher->dispatch(
|
||||||
|
KernelEvents::REQUEST,
|
||||||
|
$event
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals('fr', $request->getLocale());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\CoreBundle\Tests\EventListener;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
||||||
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||||
|
use Wallabag\CoreBundle\EventListener\UserLocaleListener;
|
||||||
|
use Wallabag\CoreBundle\Entity\Config;
|
||||||
|
use Wallabag\UserBundle\Entity\User;
|
||||||
|
|
||||||
|
class UserLocaleListenerTest extends KernelTestCase
|
||||||
|
{
|
||||||
|
public function testWithLanguage()
|
||||||
|
{
|
||||||
|
$session = new Session(new MockArraySessionStorage());
|
||||||
|
$listener = new UserLocaleListener($session);
|
||||||
|
|
||||||
|
$user = new User();
|
||||||
|
$user->setEnabled(true);
|
||||||
|
|
||||||
|
$config = new Config($user);
|
||||||
|
$config->setLanguage('fr');
|
||||||
|
|
||||||
|
$user->setConfig($config);
|
||||||
|
|
||||||
|
$userToken = new UsernamePasswordToken($user, '', 'test');
|
||||||
|
$request = Request::create('/');
|
||||||
|
$event = new InteractiveLoginEvent($request, $userToken);
|
||||||
|
|
||||||
|
$listener->onInteractiveLogin($event);
|
||||||
|
|
||||||
|
$this->assertEquals('fr', $session->get('_locale'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWithoutLanguage()
|
||||||
|
{
|
||||||
|
$session = new Session(new MockArraySessionStorage());
|
||||||
|
$listener = new UserLocaleListener($session);
|
||||||
|
|
||||||
|
$user = new User();
|
||||||
|
$user->setEnabled(true);
|
||||||
|
|
||||||
|
$config = new Config($user);
|
||||||
|
|
||||||
|
$user->setConfig($config);
|
||||||
|
|
||||||
|
$userToken = new UsernamePasswordToken($user, '', 'test');
|
||||||
|
$request = Request::create('/');
|
||||||
|
$event = new InteractiveLoginEvent($request, $userToken);
|
||||||
|
|
||||||
|
$listener->onInteractiveLogin($event);
|
||||||
|
|
||||||
|
$this->assertEquals('', $session->get('_locale'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue