Skip to content

Commit

Permalink
Add more tests (#1537)
Browse files Browse the repository at this point in the history
* Add more tests

* Fix tests
  • Loading branch information
jordisala1991 committed Aug 10, 2022
1 parent db2f304 commit 1fe85dc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/Admin/BlockAdmin.php
Expand Up @@ -98,6 +98,10 @@ protected function configureRoutes(RouteCollectionInterface $collection): void
$collection->add('composePreview', '{block_id}/compose_preview', [
'block_id' => null,
]);

if (!$this->isChild()) {
$collection->remove('create');
}
}

protected function configureFormFields(FormMapper $form): void
Expand Down Expand Up @@ -171,12 +175,6 @@ protected function configureFormFields(FormMapper $form): void
]);
}

if (false !== $isComposer) {
$form->add('enabled', HiddenType::class, ['data' => true]);
} else {
$form->add('enabled');
}

if ($isStandardBlock) {
$form->add('position', IntegerType::class);
}
Expand All @@ -187,6 +185,12 @@ protected function configureFormFields(FormMapper $form): void

$this->configureBlockFields($form, $block);

if (false !== $isComposer) {
$form->add('enabled', HiddenType::class, ['data' => true]);
} else {
$form->add('enabled');
}

$form->end();
} else {
$form
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/BlockAdminController.php
Expand Up @@ -86,8 +86,7 @@ public function createAction(Request $request): Response
{
$this->admin->checkAccess('create');

$sharedBlockAdminClass = $this->getParameter('sonata.page.admin.shared_block.class');
if (!$this->admin->isChild() && \get_class($this->admin) !== $sharedBlockAdminClass) {
if (!$this->admin->isChild()) {
throw new PageNotFoundException('You cannot create a block without a page');
}

Expand Down Expand Up @@ -162,6 +161,7 @@ public function composePreviewAction(Request $request): Response
'container' => $container,
'child' => $block,
'blockServices' => $blockServices,
'blockAdmin' => $this->admin,
]);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/admin.php
Expand Up @@ -58,6 +58,7 @@
'controller' => 'sonata.page.controller.admin.block',
'manager_type' => 'orm',
'show_in_dashboard' => false,
'default' => true,
'group' => 'sonata_page',
'translation_domain' => 'SonataPageBundle',
'label' => 'block',
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/BlockAdmin/compose_preview.html.twig
Expand Up @@ -14,7 +14,7 @@
{% set metadata = service.blockMetadata %}
{% endif %}
<h4 class="page-composer__container__child__name">
{{ child.name|default(service.name)|trans({}, service.blockMetadata.domain|default('SonataPageBundle')) }}
{{ child.name|default(metadata.title)|trans({}, metadata.domain|default('SonataPageBundle')) }}
</h4>
{% if not metadata.image %}
<i class="{{ metadata.option('class') }}" ></i>
Expand Down
91 changes: 91 additions & 0 deletions tests/Functional/Admin/BlockAdminTest.php
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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 BlockAdminTest 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 'List Block' => ['/admin/tests/app/sonatapageblock/list'];
yield 'Edit Block' => ['/admin/tests/app/sonatapageblock/1/edit'];
yield 'Remove Block' => ['/admin/tests/app/sonatapageblock/1/delete'];
yield 'Compose preview Block' => ['/admin/tests/app/sonatapageblock/2/compose_preview'];
}

/**
* @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');

$parentBlock = new SonataPageBlock();
$parentBlock->setType('sonata.page.block.container');
$parentBlock->setPage($page);

$block = new SonataPageBlock();
$block->setType('sonata.block.service.text');
$block->setParent($parentBlock);

$manager->persist($page);
$manager->persist($parentBlock);
$manager->persist($block);

$manager->flush();
}
}

0 comments on commit 1fe85dc

Please sign in to comment.