From 8a0535371ec37c022a2773fa8c57c70c187c9cb8 Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Wed, 10 Aug 2022 08:53:27 +0200 Subject: [PATCH 1/2] Add functional test for crud --- composer.json | 1 + phpunit.xml.dist | 4 + tests/App/AppKernel.php | 2 + tests/Functional/Admin/PageAdminTest.php | 93 ++++++++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 tests/Functional/Admin/PageAdminTest.php diff --git a/composer.json b/composer.json index cd1b0b314..93f3f39e0 100644 --- a/composer.json +++ b/composer.json @@ -50,6 +50,7 @@ "twig/twig": "^2.12.1 || ^3.0" }, "require-dev": { + "dama/doctrine-test-bundle": "^6.7", "doctrine/annotations": "^1.13.3", "friendsofphp/php-cs-fixer": "^3.4", "matthiasnoback/symfony-dependency-injection-test": "^4.1.1", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dbe863873..d16fa273b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -30,6 +30,10 @@ It's auto-generated by sonata-project/dev-kit package. + + + + diff --git a/tests/App/AppKernel.php b/tests/App/AppKernel.php index ee458e986..733b37858 100644 --- a/tests/App/AppKernel.php +++ b/tests/App/AppKernel.php @@ -13,6 +13,7 @@ namespace Sonata\PageBundle\Tests\App; +use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Knp\Bundle\MenuBundle\KnpMenuBundle; use Sonata\AdminBundle\SonataAdminBundle; @@ -52,6 +53,7 @@ public function __construct() public function registerBundles(): iterable { $bundles = [ + new DAMADoctrineTestBundle(), new FrameworkBundle(), new TwigBundle(), new SecurityBundle(), diff --git a/tests/Functional/Admin/PageAdminTest.php b/tests/Functional/Admin/PageAdminTest.php new file mode 100644 index 000000000..a294d2c00 --- /dev/null +++ b/tests/Functional/Admin/PageAdminTest.php @@ -0,0 +1,93 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\PageBundle\Tests\Functional\Admin; + +use Doctrine\ORM\EntityManagerInterface; +use Sonata\PageBundle\Tests\App\AppKernel; +use Sonata\PageBundle\Tests\App\Entity\SonataPageBlock; +use Sonata\PageBundle\Tests\App\Entity\SonataPagePage; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\HttpKernel\KernelInterface; + +final class PageAdminTest extends WebTestCase +{ + /** + * @dataProvider provideCrudUrlsCases + * + * @param array $parameters + */ + public function testCrudUrls(string $url, array $parameters = []): void + { + $client = self::createClient(); + + $this->prepareData(); + + $client->request('GET', $url, $parameters); + + self::assertResponseIsSuccessful(); + } + + /** + * @return iterable>> + * + * @phpstan-return iterable}> + */ + public static function provideCrudUrlsCases(): iterable + { + yield 'Tree Page' => ['/admin/tests/app/sonatapagepage/tree']; + + yield 'List Page' => ['/admin/tests/app/sonatapagepage/list', ['filter' => [ + 'name' => ['value' => 'name'], + ]]]; + + yield 'Create Page' => ['/admin/tests/app/sonatapagepage/create']; + yield 'Edit Page' => ['/admin/tests/app/sonatapagepage/1/edit']; + yield 'Remove Page' => ['/admin/tests/app/sonatapagepage/1/delete']; + yield 'Compose Page' => ['/admin/tests/app/sonatapagepage/1/compose']; + yield 'Compose Show Page' => ['/admin/tests/app/sonatapagepage/compose/container/1']; + } + + /** + * @return class-string + */ + protected static function getKernelClass(): string + { + return AppKernel::class; + } + + /** + * @psalm-suppress UndefinedPropertyFetch + */ + private function prepareData(): void + { + // TODO: Simplify this when dropping support for Symfony 4. + // @phpstan-ignore-next-line + $container = method_exists($this, 'getContainer') ? self::getContainer() : self::$container; + $manager = $container->get('doctrine.orm.entity_manager'); + \assert($manager instanceof EntityManagerInterface); + + $page = new SonataPagePage(); + $page->setName('name'); + $page->setTemplateCode('default'); + + $block = new SonataPageBlock(); + $block->setType('sonata.page.block.container'); + $block->setPage($page); + + $manager->persist($page); + $manager->persist($block); + + $manager->flush(); + } +} From fb10e856b1e509b1fa69950f48f20b6c51072c5c Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Wed, 10 Aug 2022 08:58:09 +0200 Subject: [PATCH 2/2] Fix tests --- src/Controller/PageAdminController.php | 18 +++++++++++++----- .../views/BlockAdmin/compose_preview.html.twig | 4 ++-- .../views/PageAdmin/compose.html.twig | 6 +++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Controller/PageAdminController.php b/src/Controller/PageAdminController.php index 7214e07d4..8bc0d8171 100644 --- a/src/Controller/PageAdminController.php +++ b/src/Controller/PageAdminController.php @@ -157,7 +157,9 @@ public function composeAction(Request $request): Response { $this->admin->checkAccess('compose'); - if (false === $this->container->get('sonata.page.admin.block')->isGranted('LIST')) { + $blockAdmin = $this->container->get('sonata.page.admin.block'); + + if (false === $blockAdmin->isGranted('LIST')) { throw new AccessDeniedException(); } @@ -219,6 +221,7 @@ public function composeAction(Request $request): Response 'page' => $page, 'containers' => $containers, 'orphanContainers' => $orphanContainers, + 'blockAdmin' => $blockAdmin, 'csrfTokens' => [ 'remove' => $this->getCsrfToken('sonata.delete'), ], @@ -231,20 +234,24 @@ public function composeAction(Request $request): Response */ public function composeContainerShowAction(Request $request): Response { - if (false === $this->container->get('sonata.page.admin.block')->isGranted('LIST')) { + $blockAdmin = $this->container->get('sonata.page.admin.block'); + + if (false === $blockAdmin->isGranted('LIST')) { throw new AccessDeniedException(); } $id = $request->get($this->admin->getIdParameter()); - $block = $this->container->get('sonata.page.admin.block')->getObject($id); + $block = $blockAdmin->getObject($id); if (!$block) { throw new NotFoundHttpException(sprintf('unable to find the block with id : %s', $id)); } $blockServices = $this->container->get('sonata.block.manager')->getServicesByContext('sonata_page_bundle', false); + $page = $block->getPage(); + // filter service using the template configuration - if ($page = $block->getPage()) { + if (null !== $page) { $template = $this->container->get('sonata.page.template_manager')->get($page->getTemplateCode()); $container = $template->getContainer($block->getSetting('code')); @@ -262,8 +269,9 @@ public function composeContainerShowAction(Request $request): Response return $this->renderWithExtraParams($this->admin->getTemplateRegistry()->getTemplate('compose_container_show'), [ 'blockServices' => $blockServices, + 'blockAdmin' => $blockAdmin, 'container' => $block, - 'page' => $block->getPage(), + 'page' => $page, ]); } } diff --git a/src/Resources/views/BlockAdmin/compose_preview.html.twig b/src/Resources/views/BlockAdmin/compose_preview.html.twig index ad90df71b..7251685c0 100644 --- a/src/Resources/views/BlockAdmin/compose_preview.html.twig +++ b/src/Resources/views/BlockAdmin/compose_preview.html.twig @@ -5,7 +5,7 @@ data-block-type="{{ child.type }}" > {% set service = attribute(blockServices, child.type) %} {% if service.metadata is defined %} @@ -30,7 +30,7 @@