Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #1537

Merged
merged 2 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a BlockAdmin can't be created unless is child of page or is a sharedBlock (another admin)

}
}

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');
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double added enabled (was added by the configureBlockFields above), it was also added in another block, and sonata complains about double property 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()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a weird hack to avoid creating a block outside child or shared admin, was better solved with the other solution

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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix missing variable previously changed on my other PR.

]);
}
}
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 admins for same class, I made blockadmin the default one.

'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')) }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completely broken too.

</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();
}
}