Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/3.x' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Dec 15, 2020
2 parents c7917db + 244cf56 commit b8603a6
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 118 deletions.
35 changes: 31 additions & 4 deletions tests/Action/SearchActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sonata\AdminBundle\Tests\Action;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Action\SearchAction;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
Expand All @@ -26,13 +27,36 @@
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

class SearchActionTest extends TestCase
final class SearchActionTest extends TestCase
{
/**
* @var Container
*/
private $container;

/**
* @var Pool
*/
private $pool;

/**
* @var SearchHandler
*/
private $searchHandler;

/**
* @var SearchAction
*/
private $action;

/**
* @var Stub&Environment
*/
private $twig;

/**
* @var Stub&BreadcrumbsBuilderInterface
*/
private $breadcrumbsBuilder;

protected function setUp(): void
Expand All @@ -45,8 +69,8 @@ protected function setUp(): void
'layout' => 'layout.html.twig',
]);

$this->breadcrumbsBuilder = $this->createMock(BreadcrumbsBuilderInterface::class);
$this->searchHandler = $this->createMock(SearchHandler::class);
$this->breadcrumbsBuilder = $this->createStub(BreadcrumbsBuilderInterface::class);
$this->searchHandler = new SearchHandler(true);
$this->twig = $this->createStub(Environment::class);

$this->action = new SearchAction(
Expand Down Expand Up @@ -74,7 +98,10 @@ public function testGlobalPage(): void

public function testAjaxCall(): void
{
$admin = new CleanAdmin('code', 'class', 'controller');
$adminCode = 'code';

$this->searchHandler->configureAdminSearch([$adminCode => false]);
$admin = new CleanAdmin($adminCode, 'class', 'controller');
$this->container->set('foo', $admin);
$this->pool->setAdminServiceIds(['foo']);
$request = new Request(['admin' => 'foo', 'q' => 'fooTerm', 'page' => 5, 'offset' => 10]);
Expand Down
11 changes: 8 additions & 3 deletions tests/Block/AdminSearchBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author Sullivan Senechal <[email protected]>
*/
class AdminSearchBlockServiceTest extends BlockServiceTestCase
final class AdminSearchBlockServiceTest extends BlockServiceTestCase
{
/**
* @var Pool
Expand All @@ -47,7 +47,7 @@ protected function setUp(): void
parent::setUp();

$this->pool = $this->createMock(Pool::class);
$this->searchHandler = $this->createMock(SearchHandler::class);
$this->searchHandler = new SearchHandler(true);
$this->templateRegistry = $this->createMock(TemplateRegistryInterface::class);
$this->templateRegistry->method('getTemplate')->willReturn('@SonataAdmin/Block/block_search_result.html.twig');
}
Expand Down Expand Up @@ -98,7 +98,12 @@ public function testGlobalSearchReturnsResponse(): void

public function testGlobalSearchReturnsEmptyWhenFiltersAreDisabled(): void
{
$adminCode = 'code';

$admin = $this->createMock(AbstractAdmin::class);
$admin
->method('getCode')
->willReturn($adminCode);

$blockService = new AdminSearchBlockService(
$this->twig,
Expand All @@ -109,7 +114,7 @@ public function testGlobalSearchReturnsEmptyWhenFiltersAreDisabled(): void
);
$blockContext = $this->getBlockContext($blockService);

$this->searchHandler->expects(self::once())->method('search')->willReturn(null);
$this->searchHandler->configureAdminSearch([$adminCode => false]);
$this->pool->expects(self::once())->method('getAdminByAdminCode')->willReturn($admin);
$admin->expects(self::once())->method('checkAccess')->with('list');

Expand Down
116 changes: 66 additions & 50 deletions tests/Controller/CRUDControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\AdminBundle\Tests\Controller;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
Expand All @@ -35,7 +36,7 @@
use Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin;
use Sonata\AdminBundle\Tests\Fixtures\Controller\BatchAdminController;
use Sonata\AdminBundle\Tests\Fixtures\Controller\PreCRUDController;
use Sonata\AdminBundle\Util\AdminObjectAclData;
use Sonata\AdminBundle\Tests\Fixtures\Util\DummyDomainObject;
use Sonata\AdminBundle\Util\AdminObjectAclManipulator;
use Sonata\Exporter\Exporter;
use Sonata\Exporter\Source\SourceIteratorInterface;
Expand All @@ -45,7 +46,9 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand All @@ -58,6 +61,7 @@
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
Expand Down Expand Up @@ -153,6 +157,11 @@ class CRUDControllerTest extends TestCase
*/
private $httpMethodParameterOverride = false;

/**
* @var Stub&FormFactoryInterface
*/
private $formFactory;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -202,9 +211,9 @@ protected function setUp(): void

$this->auditManager = $this->createMock(AuditManagerInterface::class);

$this->adminObjectAclManipulator = $this->getMockBuilder(AdminObjectAclManipulator::class)
->disableOriginalConstructor()
->getMock();
$this->formFactory = $this->createStub(FormFactoryInterface::class);

$this->adminObjectAclManipulator = new AdminObjectAclManipulator($this->formFactory, AdminPermissionMap::class);

$this->csrfProvider = $this->getMockBuilder(CsrfTokenManagerInterface::class)
->getMock();
Expand Down Expand Up @@ -2605,7 +2614,7 @@ public function testAclAction(): void
->method('isAclEnabled')
->willReturn(true);

$object = new \stdClass();
$object = new DummyDomainObject();

$this->admin->expects($this->once())
->method('getObject')
Expand All @@ -2619,10 +2628,6 @@ public function testAclAction(): void
->method('getSecurityInformation')
->willReturn([]);

$this->adminObjectAclManipulator->expects($this->once())
->method('getMaskBuilderClass')
->willReturn(AdminPermissionMap::class);

$aclUsersForm = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -2639,22 +2644,27 @@ public function testAclAction(): void
->method('createView')
->willReturn($this->createMock(FormView::class));

$this->adminObjectAclManipulator->expects($this->once())
->method('createAclUsersForm')
->with($this->isInstanceOf(AdminObjectAclData::class))
->willReturn($aclUsersForm);

$this->adminObjectAclManipulator->expects($this->once())
->method('createAclRolesForm')
->with($this->isInstanceOf(AdminObjectAclData::class))
->willReturn($aclRolesForm);
$formBuilder = $this->createStub(FormBuilderInterface::class);
$formBuilder
->method('getForm')
->willReturnOnConsecutiveCalls(
$aclUsersForm,
$aclRolesForm
);

$aclSecurityHandler = $this->createMock(AclSecurityHandlerInterface::class);
$this->formFactory
->method('createNamedBuilder')
->willReturn($formBuilder);

$aclSecurityHandler = $this->createStub(AclSecurityHandlerInterface::class);
$aclSecurityHandler
->method('getObjectPermissions')
->willReturn([]);

$aclSecurityHandler
->method('createAcl')
->willReturn($this->createStub(MutableAclInterface::class));

$this->admin
->method('getSecurityHandler')
->willReturn($aclSecurityHandler);
Expand Down Expand Up @@ -2686,7 +2696,7 @@ public function testAclActionInvalidUpdate(): void
->method('isAclEnabled')
->willReturn(true);

$object = new \stdClass();
$object = new DummyDomainObject();

$this->admin->expects($this->once())
->method('getObject')
Expand All @@ -2700,10 +2710,6 @@ public function testAclActionInvalidUpdate(): void
->method('getSecurityInformation')
->willReturn([]);

$this->adminObjectAclManipulator->expects($this->once())
->method('getMaskBuilderClass')
->willReturn(AdminPermissionMap::class);

$aclUsersForm = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -2724,22 +2730,27 @@ public function testAclActionInvalidUpdate(): void
->method('createView')
->willReturn($this->createMock(FormView::class));

$this->adminObjectAclManipulator->expects($this->once())
->method('createAclUsersForm')
->with($this->isInstanceOf(AdminObjectAclData::class))
->willReturn($aclUsersForm);

$this->adminObjectAclManipulator->expects($this->once())
->method('createAclRolesForm')
->with($this->isInstanceOf(AdminObjectAclData::class))
->willReturn($aclRolesForm);
$formBuilder = $this->createStub(FormBuilderInterface::class);
$formBuilder
->method('getForm')
->willReturnOnConsecutiveCalls(
$aclUsersForm,
$aclRolesForm
);

$aclSecurityHandler = $this->createMock(AclSecurityHandlerInterface::class);
$this->formFactory
->method('createNamedBuilder')
->willReturn($formBuilder);

$aclSecurityHandler = $this->createStub(AclSecurityHandlerInterface::class);
$aclSecurityHandler
->method('getObjectPermissions')
->willReturn([]);

$aclSecurityHandler
->method('createAcl')
->willReturn($this->createStub(MutableAclInterface::class));

$this->admin
->method('getSecurityHandler')
->willReturn($aclSecurityHandler);
Expand Down Expand Up @@ -2773,7 +2784,7 @@ public function testAclActionSuccessfulUpdate(): void
->method('isAclEnabled')
->willReturn(true);

$object = new \stdClass();
$object = new DummyDomainObject();

$this->admin->expects($this->once())
->method('getObject')
Expand All @@ -2787,10 +2798,6 @@ public function testAclActionSuccessfulUpdate(): void
->method('getSecurityInformation')
->willReturn([]);

$this->adminObjectAclManipulator->expects($this->once())
->method('getMaskBuilderClass')
->willReturn(AdminPermissionMap::class);

$aclUsersForm = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
Expand All @@ -2803,6 +2810,10 @@ public function testAclActionSuccessfulUpdate(): void
->disableOriginalConstructor()
->getMock();

$aclRolesForm
->method('getData')
->willReturn([]);

$aclRolesForm
->method('createView')
->willReturn($this->createMock(FormView::class));
Expand All @@ -2811,22 +2822,27 @@ public function testAclActionSuccessfulUpdate(): void
->method('isValid')
->willReturn(true);

$this->adminObjectAclManipulator->expects($this->once())
->method('createAclUsersForm')
->with($this->isInstanceOf(AdminObjectAclData::class))
->willReturn($aclUsersForm);

$this->adminObjectAclManipulator->expects($this->once())
->method('createAclRolesForm')
->with($this->isInstanceOf(AdminObjectAclData::class))
->willReturn($aclRolesForm);
$formBuilder = $this->createStub(FormBuilderInterface::class);
$formBuilder
->method('getForm')
->willReturnOnConsecutiveCalls(
$aclUsersForm,
$aclRolesForm
);

$aclSecurityHandler = $this->createMock(AclSecurityHandlerInterface::class);
$this->formFactory
->method('createNamedBuilder')
->willReturn($formBuilder);

$aclSecurityHandler = $this->createStub(AclSecurityHandlerInterface::class);
$aclSecurityHandler
->method('getObjectPermissions')
->willReturn([]);

$aclSecurityHandler
->method('createAcl')
->willReturn($this->createStub(MutableAclInterface::class));

$this->admin
->method('getSecurityHandler')
->willReturn($aclSecurityHandler);
Expand All @@ -2840,7 +2856,7 @@ public function testAclActionSuccessfulUpdate(): void
$this->assertInstanceOf(RedirectResponse::class, $response);

$this->assertSame(['flash_acl_edit_success'], $this->session->getFlashBag()->get('sonata_flash_success'));
$this->assertSame('stdClass_acl', $response->getTargetUrl());
$this->assertSame(sprintf('%s_acl', DummyDomainObject::class), $response->getTargetUrl());
}

public function testHistoryViewRevisionActionAccessDenied(): void
Expand Down
Loading

0 comments on commit b8603a6

Please sign in to comment.