diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index ad62c0e8d6..1e3dec5116 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -2694,7 +2694,7 @@ public function setListMode($mode) public function getListMode() { - if (!$this->hasRequest()) { + if (!$this->hasRequest() || !$this->getRequest()->hasSession()) { return 'list'; } diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index 210781c971..fa998db354 100644 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -84,6 +84,7 @@ use Symfony\Component\Form\ResolvedFormTypeFactory; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\Translation\TranslatorInterface; @@ -2308,6 +2309,44 @@ public function testShowMosaicButtonHideMosaic(): void $this->assertSame($expected, $admin->getListModes()); } + /** + * @dataProvider getListModeProvider + */ + public function testGetListMode(string $expected, ?Request $request = null): void + { + $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'Sonata\NewsBundle\Controller\PostAdminController'); + if (null !== $request) { + $admin->setRequest($request); + } + + $this->assertSame($expected, $admin->getListMode()); + } + + public function getListModeProvider(): iterable + { + yield ['list', null]; + + yield ['list', new Request()]; + + $request = new Request(); + $session = $this->createMock(SessionInterface::class); + $session + ->method('get') + ->with('sonata.post.admin.post.list_mode', 'list') + ->willReturn('list'); + $request->setSession($session); + yield ['list', $request]; + + $session = $this->createMock(SessionInterface::class); + $session + ->method('get') + ->with('sonata.post.admin.post.list_mode', 'list') + ->willReturn('some_list_mode'); + $request = new Request(); + $request->setSession($session); + yield ['some_list_mode', $request]; + } + /** * @covers \Sonata\AdminBundle\Admin\AbstractAdmin::getDashboardActions * @dataProvider provideGetBaseRouteName