From 3a5b5d30d423cf0a2ab0acdb37dd3bf733b3fb53 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Sat, 17 Apr 2021 08:40:26 -0300 Subject: [PATCH] Allow `AbstractAdmin::getListMode()` to return the default value if the request hasn't a session --- src/Admin/AbstractAdmin.php | 2 +- tests/Admin/AdminTest.php | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) 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