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

Fix explicitly set use_intl_templates #5979

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ public function prepend(ContainerBuilder $container)
$configs = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration(new Configuration(), $configs);

$useIntlTemplates = $config['use_intl_templates'] || isset($bundles['SonataIntlBundle']);
$defaultUseIntlTemplates = $config['use_intl_templates'] || isset($bundles['SonataIntlBundle']);
Copy link
Member

Choose a reason for hiding this comment

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

This is correct? I must use the intl templates if i installed the SonataIntlBundle?

Copy link
Member Author

Choose a reason for hiding this comment

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

Before #5835, when you had installed SonataIntlBundle, the intl templates were automatically loaded from SonataIntlBundle. This simulated that behaviour.

The problem was that if you set use_intl_templates to false (explicitly) with the SonataIntlBundle installed, it did't matter, it was like it was set to true. Now this is fixed by the next line. So to sum up:

  • SonataIntlBundle installed and nothing configured: it uses intl templates
  • SonataIntlBundle not installed but use_intl_templates set to true: it loades intl templates
  • No matter if SonataIntlBundle is installed if you set use_intl_templates to false: it won't load intl templates

$useIntlTemplates = $this->getExplicitlyConfigured('use_intl_templates', $configs, $defaultUseIntlTemplates);

$container->setParameter('sonata.admin.configuration.use_intl_templates', $useIntlTemplates);

if (!isset($bundles['JMSDiExtraBundle'])) {
Expand Down Expand Up @@ -322,4 +324,18 @@ private function replacePropertyAccessor(ContainerBuilder $container): void
$modelChoice = $container->getDefinition('sonata.admin.form.type.model_choice');
$modelChoice->replaceArgument(0, new Reference('form.property_accessor'));
}

/**
* @return mixed
*/
private function getExplicitlyConfigured(string $property, array $configs, $defaultValue)
{
foreach ($configs as $config) {
if (isset($config[$property])) {
return $config[$property];
}
}

return $defaultValue;
}
}
106 changes: 75 additions & 31 deletions tests/DependencyInjection/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Bridge\Exporter\AdminExporter;
use Sonata\AdminBundle\DependencyInjection\Configuration;
use Sonata\AdminBundle\DependencyInjection\SonataAdminExtension;
use Sonata\AdminBundle\Event\AdminEventExtension;
use Sonata\AdminBundle\Filter\FilterFactory;
Expand All @@ -38,35 +39,27 @@
use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
use Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy;
use Sonata\AdminBundle\Twig\GlobalVariables;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Processor;

class SonataAdminExtensionTest extends AbstractExtensionTestCase
{
/**
* @var string[]
* @var array
*/
private $defaultStylesheets = [];

/**
* @var string[]
*/
private $defaultJavascripts = [];
private $defaultConfiguration = [];

protected function setUp(): void
{
parent::setUp();
$this->container->setParameter('kernel.bundles', []);
$this->load();
$this->defaultStylesheets = $this->container
->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets']
;
$this->defaultJavascripts = $this->container
->getDefinition('sonata.admin.pool')->getArgument(3)['javascripts']
;

$this->defaultConfiguration = (new Processor())->processConfiguration(new Configuration(), []);
}

public function testHasCoreServicesAlias(): void
{
$this->load();

$this->assertContainerBuilderHasService(Pool::class);
$this->assertContainerBuilderHasService(AdminPoolLoader::class);
$this->assertContainerBuilderHasService(AdminHelper::class);
Expand Down Expand Up @@ -117,6 +110,8 @@ public function testContainerCompileWithJMSDiExtraBundle(): void
'JMSDiExtraBundle' => true,
]);

$this->load();

$this->container->compile();
}

Expand Down Expand Up @@ -166,15 +161,20 @@ public function testHasDefaultServiceParameters(): void
public function testExtraStylesheetsGetAdded(): void
{
$this->container->setParameter('kernel.bundles', []);

$extraStylesheets = ['foo/bar.css', 'bar/quux.css'];
$this->load([
'assets' => [
'extra_stylesheets' => $extraStylesheets,
],
]);

$stylesheets = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets'];

$this->assertSame(array_merge($this->defaultStylesheets, $extraStylesheets), $stylesheets);
$this->assertSame(
array_merge($this->defaultConfiguration['assets']['stylesheets'], $extraStylesheets),
$stylesheets
);
}

public function testRemoveStylesheetsGetRemoved(): void
Expand All @@ -191,7 +191,12 @@ public function testRemoveStylesheetsGetRemoved(): void
]);
$stylesheets = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['stylesheets'];

$this->assertSame(array_values(array_diff($this->defaultStylesheets, $removeStylesheets)), $stylesheets);
$this->assertSame(
array_values(
array_diff($this->defaultConfiguration['assets']['stylesheets'], $removeStylesheets)
),
$stylesheets
);
}

public function testExtraJavascriptsGetAdded(): void
Expand All @@ -205,7 +210,10 @@ public function testExtraJavascriptsGetAdded(): void
]);
$javascripts = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['javascripts'];

$this->assertSame(array_merge($this->defaultJavascripts, $extraJavascripts), $javascripts);
$this->assertSame(
array_merge($this->defaultConfiguration['assets']['javascripts'], $extraJavascripts),
$javascripts
);
}

public function testRemoveJavascriptsGetRemoved(): void
Expand All @@ -222,7 +230,12 @@ public function testRemoveJavascriptsGetRemoved(): void
]);
$javascripts = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['javascripts'];

$this->assertSame(array_values(array_diff($this->defaultJavascripts, $removeJavascripts)), $javascripts);
$this->assertSame(
array_values(
array_diff($this->defaultConfiguration['assets']['javascripts'], $removeJavascripts)
),
$javascripts
);
}

public function testAssetsCanBeAddedAndRemoved(): void
Expand Down Expand Up @@ -251,20 +264,28 @@ public function testAssetsCanBeAddedAndRemoved(): void
;

$this->assertSame(
array_merge(array_diff($this->defaultStylesheets, $removeStylesheets), $extraStylesheets),
array_merge(
array_diff($this->defaultConfiguration['assets']['stylesheets'], $removeStylesheets),
$extraStylesheets
),
$stylesheets
);

$javascripts = $this->container->getDefinition('sonata.admin.pool')->getArgument(3)['javascripts'];

$this->assertSame(
array_merge(array_diff($this->defaultJavascripts, $removeJavascripts), $extraJavascripts),
array_merge(
array_diff($this->defaultConfiguration['assets']['javascripts'], $removeJavascripts),
$extraJavascripts
),
$javascripts
);
}

public function testDefaultTemplates(): void
{
$this->load();

$this->assertSame([
'user_block' => '@SonataAdmin/Core/user_block.html.twig',
'add_block' => '@SonataAdmin/Core/add_block.html.twig',
Expand Down Expand Up @@ -310,16 +331,39 @@ public function testDefaultTemplates(): void

public function testLoadIntlTemplate(): void
{
$container = new ContainerBuilder();
$container->setParameter('kernel.bundles', []);
$container->prependExtensionConfig('sonata_admin', ['use_intl_templates' => true]);
$extension = new SonataAdminExtension();
$extension->prepend($container);
$configs = $container->getExtensionConfig('sonata_admin');
$extension->load($configs, $container);

$templates = $container->getParameter('sonata.admin.configuration.templates');
$this->assertSame('@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig', $templates['history_revision_timestamp']);
$this->container->prependExtensionConfig('sonata_admin', ['use_intl_templates' => true]);
$this->load();

$templates = $this->container->getParameter('sonata.admin.configuration.templates');
$this->assertSame(
'@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig',
$templates['history_revision_timestamp']
);
}

public function testLoadIntlTemplateIfTheSonataIntlBundleIsEnabled(): void
{
$this->container->setParameter('kernel.bundles', ['SonataIntlBundle' => true]);
$this->load();

$templates = $this->container->getParameter('sonata.admin.configuration.templates');
$this->assertSame(
'@SonataAdmin/CRUD/Intl/history_revision_timestamp.html.twig',
$templates['history_revision_timestamp']
);
}

public function testDoNotLoadIntlTemplatesWithIntlBundleEnabledAndParameterSetToFalse(): void
{
$this->container->setParameter('kernel.bundles', ['SonataIntlBundle' => true]);
$this->container->prependExtensionConfig('sonata_admin', ['use_intl_templates' => false]);
$this->load();

$templates = $this->container->getParameter('sonata.admin.configuration.templates');
$this->assertSame(
'@SonataAdmin/CRUD/history_revision_timestamp.html.twig',
$templates['history_revision_timestamp']
);
}

protected function getContainerExtensions(): array
Expand Down