Move User event listeners to Core

This commit is contained in:
Yassine Guedidi
2023-12-30 23:54:25 +01:00
parent d8f2d3c7ee
commit 615adc81ff
7 changed files with 10 additions and 10 deletions

View File

@ -0,0 +1,69 @@
<?php
namespace Tests\Wallabag\CoreBundle\Event\Listener;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Wallabag\CoreBundle\Event\Listener\AuthenticationFailureListener;
class AuthenticationFailureListenerTest extends TestCase
{
private $requestStack;
private $logHandler;
private $listener;
private $dispatcher;
protected function setUp(): void
{
$request = Request::create('/');
$request->request->set('_username', 'admin');
$this->requestStack = new RequestStack();
$this->requestStack->push($request);
$this->logHandler = new TestHandler();
$logger = new Logger('test', [$this->logHandler]);
$this->listener = new AuthenticationFailureListener(
$this->requestStack,
$logger
);
$this->dispatcher = new EventDispatcher();
$this->dispatcher->addSubscriber($this->listener);
}
public function testOnAuthenticationFailure()
{
$token = $this->getMockBuilder(TokenInterface::class)
->disableOriginalConstructor()
->getMock();
$exception = $this->getMockBuilder(AuthenticationException::class)
->disableOriginalConstructor()
->getMock();
$event = new AuthenticationFailureEvent(
$token,
$exception
);
$this->dispatcher->dispatch(
$event,
AuthenticationEvents::AUTHENTICATION_FAILURE
);
$records = $this->logHandler->getRecords();
$this->assertCount(1, $records);
$this->assertSame('Authentication failure for user "admin", from IP "127.0.0.1", with UA: "Symfony".', $records[0]['message']);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace Tests\Wallabag\CoreBundle\Event\Listener;
use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Event\Listener\CreateConfigListener;
class CreateConfigListenerTest extends TestCase
{
private $em;
private $listener;
private $dispatcher;
private $request;
private $response;
protected function setUp(): void
{
$session = new Session(new MockArraySessionStorage());
$this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor()
->getMock();
$this->listener = new CreateConfigListener(
$this->em,
20,
50,
'fr',
1,
1,
1,
1,
$session
);
$this->dispatcher = new EventDispatcher();
$this->dispatcher->addSubscriber($this->listener);
$this->request = Request::create('/');
$this->response = Response::create();
}
public function testWithValidUser()
{
$user = new User();
$user->setEnabled(true);
$event = new FilterUserResponseEvent(
$user,
$this->request,
$this->response
);
$config = new Config($user);
$config->setItemsPerPage(20);
$config->setFeedLimit(50);
$config->setLanguage('fr');
$config->setReadingSpeed(200);
$this->em->expects($this->once())
->method('persist')
->willReturn($config);
$this->em->expects($this->once())
->method('flush');
$this->dispatcher->dispatch(
$event,
FOSUserEvents::REGISTRATION_COMPLETED
);
}
}