Fix CreateConfigListener to not use session in CLI context

This commit is contained in:
Yassine Guedidi 2024-01-21 13:21:00 +01:00
parent 1a5e63a1b8
commit c93c47cdec
2 changed files with 18 additions and 9 deletions

View file

@ -6,7 +6,7 @@ use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\UserEvent; use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\RequestStack;
use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Config;
/** /**
@ -22,10 +22,10 @@ class CreateConfigListener implements EventSubscriberInterface
private $readingSpeed; private $readingSpeed;
private $actionMarkAsRead; private $actionMarkAsRead;
private $listMode; private $listMode;
private $session; private $requestStack;
private $displayThumbnails; private $displayThumbnails;
public function __construct(EntityManagerInterface $em, $itemsOnPage, $feedLimit, $language, $readingSpeed, $actionMarkAsRead, $listMode, $displayThumbnails, SessionInterface $session) public function __construct(EntityManagerInterface $em, $itemsOnPage, $feedLimit, $language, $readingSpeed, $actionMarkAsRead, $listMode, $displayThumbnails, RequestStack $requestStack)
{ {
$this->em = $em; $this->em = $em;
$this->itemsOnPage = $itemsOnPage; $this->itemsOnPage = $itemsOnPage;
@ -34,7 +34,7 @@ class CreateConfigListener implements EventSubscriberInterface
$this->readingSpeed = $readingSpeed; $this->readingSpeed = $readingSpeed;
$this->actionMarkAsRead = $actionMarkAsRead; $this->actionMarkAsRead = $actionMarkAsRead;
$this->listMode = $listMode; $this->listMode = $listMode;
$this->session = $session; $this->requestStack = $requestStack;
$this->displayThumbnails = $displayThumbnails; $this->displayThumbnails = $displayThumbnails;
} }
@ -51,10 +51,17 @@ class CreateConfigListener implements EventSubscriberInterface
public function createConfig(UserEvent $event) public function createConfig(UserEvent $event)
{ {
$language = $this->language;
if ($this->requestStack->getMasterRequest()) {
$session = $this->requestStack->getMasterRequest()->getSession();
$language = $session->get('_locale', $this->language);
}
$config = new Config($event->getUser()); $config = new Config($event->getUser());
$config->setItemsPerPage($this->itemsOnPage); $config->setItemsPerPage($this->itemsOnPage);
$config->setFeedLimit($this->feedLimit); $config->setFeedLimit($this->feedLimit);
$config->setLanguage($this->session->get('_locale', $this->language)); $config->setLanguage($language);
$config->setReadingSpeed($this->readingSpeed); $config->setReadingSpeed($this->readingSpeed);
$config->setActionMarkAsRead($this->actionMarkAsRead); $config->setActionMarkAsRead($this->actionMarkAsRead);
$config->setListMode($this->listMode); $config->setListMode($this->listMode);

View file

@ -8,9 +8,8 @@ use FOS\UserBundle\FOSUserEvents;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; 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\Config;
use Wallabag\CoreBundle\Entity\User; use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Event\Listener\CreateConfigListener; use Wallabag\CoreBundle\Event\Listener\CreateConfigListener;
@ -22,13 +21,16 @@ class CreateConfigListenerTest extends TestCase
private $dispatcher; private $dispatcher;
private $request; private $request;
private $response; private $response;
private $requestStack;
protected function setUp(): void protected function setUp(): void
{ {
$session = new Session(new MockArraySessionStorage());
$this->em = $this->getMockBuilder(EntityManager::class) $this->em = $this->getMockBuilder(EntityManager::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->requestStack = $this->getMockBuilder(RequestStack::class)
->disableOriginalConstructor()
->getMock();
$this->listener = new CreateConfigListener( $this->listener = new CreateConfigListener(
$this->em, $this->em,
@ -39,7 +41,7 @@ class CreateConfigListenerTest extends TestCase
1, 1,
1, 1,
1, 1,
$session $this->requestStack
); );
$this->dispatcher = new EventDispatcher(); $this->dispatcher = new EventDispatcher();