Skip to content

Commit

Permalink
Allow AbstractAdmin::getListMode() to return the default value if t…
Browse files Browse the repository at this point in the history
…he request hasn't a session
  • Loading branch information
phansys authored and jordisala1991 committed Apr 17, 2021
1 parent 3ec8262 commit 3a5b5d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Admin/AbstractAdmin.php
Expand Up @@ -2694,7 +2694,7 @@ public function setListMode($mode)

public function getListMode()
{
if (!$this->hasRequest()) {
if (!$this->hasRequest() || !$this->getRequest()->hasSession()) {
return 'list';
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Admin/AdminTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3a5b5d3

Please sign in to comment.