Skip to content

Commit

Permalink
Remove usage of 'admin_pool' in twig templates (#1533)
Browse files Browse the repository at this point in the history
* Add functional test for crud

* Fix tests
  • Loading branch information
jordisala1991 authored Aug 10, 2022
1 parent d660cf8 commit 21d2e7d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 10 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ It's auto-generated by sonata-project/dev-kit package.
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>

<extensions>
<extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>

<php>
<ini name="precision" value="8" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
Expand Down
18 changes: 13 additions & 5 deletions src/Controller/PageAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -219,6 +221,7 @@ public function composeAction(Request $request): Response
'page' => $page,
'containers' => $containers,
'orphanContainers' => $orphanContainers,
'blockAdmin' => $blockAdmin,
'csrfTokens' => [
'remove' => $this->getCsrfToken('sonata.delete'),
],
Expand All @@ -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'));
Expand All @@ -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,
]);
}
}
4 changes: 2 additions & 2 deletions src/Resources/views/BlockAdmin/compose_preview.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
data-block-type="{{ child.type }}"
>
<a class="page-composer__container__child__edit"
href="{{ admin_pool.getAdminByAdminCode('sonata.page.admin.block').generateUrl('edit', { 'id': child.id, 'composer': true }) }}"
href="{{ blockAdmin.generateUrl('edit', { 'id': child.id, 'composer': true }) }}"
>
{% set service = attribute(blockServices, child.type) %}
{% if service.metadata is defined %}
Expand All @@ -30,7 +30,7 @@

<div class="page-composer__container__child__right">
<div class="page-composer__container__child__remove">
<a class="badge" href="{{ admin_pool.getAdminByAdminCode('sonata.page.admin.block').generateUrl('delete', { 'id': child.id }) }}">{{ 'composer.remove'|trans({}, 'SonataPageBundle') }} <i class="fa fa-times"></i> </a>
<a class="badge" href="{{ blockAdmin.generateUrl('delete', { 'id': child.id }) }}">{{ 'composer.remove'|trans({}, 'SonataPageBundle') }} <i class="fa fa-times"></i> </a>
</div>

<div class="page-composer__container__child__switch-enabled"
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/views/PageAdmin/compose.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ file that was distributed with this source code.
composer_status_error: 'page.composer_status_error'|trans({}, 'SonataPageBundle')
},
routes: {
save_blocks_positions: admin_pool.getAdminByAdminCode('sonata.page.admin.block').generateUrl('savePosition', { 'id': page.id }),
block_switch_parent: admin_pool.getAdminByAdminCode('sonata.page.admin.block').generateUrl('switchParent'),
block_preview: admin_pool.getAdminByAdminCode('sonata.page.admin.block').generateUrl('composePreview', { 'block_id': 'BLOCK_ID' })
save_blocks_positions: blockAdmin.generateUrl('savePosition', { 'id': page.id }),
block_switch_parent: blockAdmin.generateUrl('switchParent'),
block_preview: blockAdmin.generateUrl('composePreview', { 'block_id': 'BLOCK_ID' })
}
}|json_encode()|raw }}'>
<div class="page-composer__page-preview__containers">
Expand Down
2 changes: 2 additions & 0 deletions tests/App/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +53,7 @@ public function __construct()
public function registerBundles(): iterable
{
$bundles = [
new DAMADoctrineTestBundle(),
new FrameworkBundle(),
new TwigBundle(),
new SecurityBundle(),
Expand Down
93 changes: 93 additions & 0 deletions tests/Functional/Admin/PageAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* 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<string, mixed> $parameters
*/
public function testCrudUrls(string $url, array $parameters = []): void
{
$client = self::createClient();

$this->prepareData();

$client->request('GET', $url, $parameters);

self::assertResponseIsSuccessful();
}

/**
* @return iterable<array<string|array<string, mixed>>>
*
* @phpstan-return iterable<array{0: string, 1?: array<string, mixed>}>
*/
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<KernelInterface>
*/
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();
}
}

0 comments on commit 21d2e7d

Please sign in to comment.