From 4d4953ea1391c987caeeefce8c7e966551114083 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Mon, 16 Mar 2020 21:11:30 +0100 Subject: [PATCH] Trying to fix inexistent parameter --- .../AbstractSonataAdminExtension.php | 6 +++- .../SonataAdminExtension.php | 12 ++++--- .../AddDependencyCallsCompilerPassTest.php | 13 ++++++++ .../Compiler/ExtensionCompilerPassTest.php | 13 ++++++-- .../SonataAdminExtensionTest.php | 29 +++++++++++++--- .../DummySonataAdminExtension.php | 33 +++++++++++++++++++ 6 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 tests/Fixtures/DependencyInjection/DummySonataAdminExtension.php diff --git a/src/DependencyInjection/AbstractSonataAdminExtension.php b/src/DependencyInjection/AbstractSonataAdminExtension.php index 198b4da47bd..a793b9b206c 100644 --- a/src/DependencyInjection/AbstractSonataAdminExtension.php +++ b/src/DependencyInjection/AbstractSonataAdminExtension.php @@ -78,7 +78,11 @@ protected function fixTemplatesConfiguration( ], ]; - $useIntlTemplates = $container->getParameter('sonata.admin.configuration.use_intl_templates'); + $sonataAdminConfigs = $container->getExtensionConfig('sonata_admin'); + $sonataAdminConfig = $this->processConfiguration(new Configuration(), $sonataAdminConfigs); + + $useIntlTemplates = $sonataAdminConfig['use_intl_templates'] || isset($bundles['SonataIntlBundle']); + if ($useIntlTemplates) { $defaultConfig['templates']['types']['list'] = array_merge($defaultConfig['templates']['types']['list'], [ 'date' => '@SonataAdmin/CRUD/Intl/list_date.html.twig', diff --git a/src/DependencyInjection/SonataAdminExtension.php b/src/DependencyInjection/SonataAdminExtension.php index 262bd6c4894..e87862d1dd3 100644 --- a/src/DependencyInjection/SonataAdminExtension.php +++ b/src/DependencyInjection/SonataAdminExtension.php @@ -88,9 +88,7 @@ public function load(array $configs, ContainerBuilder $container) $container->removeDefinition('sonata.admin.lock.extension'); } - $useIntlTemplates = $config['use_intl_templates'] || isset($bundles['SonataIntlBundle']); - - $container->setParameter('sonata.admin.configuration.use_intl_templates', $useIntlTemplates); + $useIntlTemplates = $container->getParameter('sonata.admin.configuration.use_intl_templates'); if ($useIntlTemplates) { if ('@SonataAdmin/CRUD/history_revision_timestamp.html.twig' === $config['templates']['history_revision_timestamp']) { @@ -211,12 +209,16 @@ public function prepend(ContainerBuilder $container) { $bundles = $container->getParameter('kernel.bundles'); + $configs = $container->getExtensionConfig($this->getAlias()); + $config = $this->processConfiguration(new Configuration(), $configs); + + $useIntlTemplates = $config['use_intl_templates'] || isset($bundles['SonataIntlBundle']); + $container->setParameter('sonata.admin.configuration.use_intl_templates', $useIntlTemplates); + if (!isset($bundles['JMSDiExtraBundle'])) { return; } - $configs = $container->getExtensionConfig($this->getAlias()); - $config = $this->processConfiguration(new Configuration(), $configs); if (!$config['options']['enable_jms_di_extra_autoregistration']) { return; } diff --git a/tests/DependencyInjection/Compiler/AddDependencyCallsCompilerPassTest.php b/tests/DependencyInjection/Compiler/AddDependencyCallsCompilerPassTest.php index 238419fc6a5..b429f23e3cc 100644 --- a/tests/DependencyInjection/Compiler/AddDependencyCallsCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/AddDependencyCallsCompilerPassTest.php @@ -75,6 +75,7 @@ public function testTranslatorDisabled(): void $container = $this->getContainer(); $container->removeAlias('translator'); $container->removeDefinition('translator'); + $this->extension->prepend($container); $this->extension->load([$this->config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -88,6 +89,7 @@ public function testTranslatorDisabled(): void public function testProcessParsingFullValidConfig(): void { $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$this->config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -152,6 +154,7 @@ public function testProcessParsingFullValidConfig(): void public function testProcessResultingConfig(): void { $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$this->config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -246,6 +249,7 @@ public function testProcessSortAdmins(): void $config['options']['sort_admins'] = true; unset($config['dashboard']['groups']); + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -272,6 +276,7 @@ public function testProcessGroupNameAsParameter(): void $container = $this->getContainer(); $container->setParameter('sonata.admin.parameter.groupname', 'resolved_group_name'); + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -288,6 +293,7 @@ public function testApplyTemplatesConfiguration(): void { $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$this->getConfig()], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -347,6 +353,7 @@ public function testApplyShowMosaicButtonConfiguration(): void { $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$this->getConfig()], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -386,6 +393,7 @@ public function testProcessMultipleOnTopOptions(): void 'route_params' => ['articleId' => 3], ]; + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -423,6 +431,7 @@ public function testProcessMultipleOnTopOptionsAdditionalGroup(): void 'roles' => ['ROLE_ONE'], ]; + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -440,6 +449,7 @@ public function testProcessMultipleOnTopOptionsInServiceDefinition(): void $config = $this->config; $config['dashboard']['groups'] = []; + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -462,6 +472,7 @@ public function testProcessMultipleOnTopOptionsInServiceDefinition1(): void $config = $this->config; $config['dashboard']['groups'] = []; + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -487,6 +498,7 @@ public function testProcessMultipleOnTopOptionsInServiceDefinition2(): void $config = $this->config; $config['dashboard']['groups'] = []; + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); @@ -515,6 +527,7 @@ public function testProcessAbstractAdminServiceInServiceDefinition(): void $config = $this->config; $config['dashboard']['groups'] = []; + $this->extension->prepend($container); $this->extension->load([$config], $container); $compilerPass = new AddDependencyCallsCompilerPass(); diff --git a/tests/DependencyInjection/Compiler/ExtensionCompilerPassTest.php b/tests/DependencyInjection/Compiler/ExtensionCompilerPassTest.php index 0fe9ea32fce..aa765a70750 100644 --- a/tests/DependencyInjection/Compiler/ExtensionCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/ExtensionCompilerPassTest.php @@ -66,7 +66,8 @@ public function setUp(): void */ public function testAdminExtensionLoad(): void { - $this->extension->load([], $container = $this->getContainer()); + $this->extension->prepend($container = $this->getContainer()); + $this->extension->load([], $container); $this->assertTrue($container->hasParameter($this->root.'.extension.map')); $this->assertIsArray($extensionMap = $container->getParameter($this->root.'.extension.map')); @@ -84,7 +85,8 @@ public function testAdminExtensionLoad(): void */ public function testFlattenEmptyExtensionConfiguration(): void { - $this->extension->load([], $container = $this->getContainer()); + $this->extension->prepend($container = $this->getContainer()); + $this->extension->load([], $container); $extensionMap = $container->getParameter($this->root.'.extension.map'); $method = new \ReflectionMethod( @@ -116,7 +118,8 @@ public function testFlattenEmptyExtensionConfiguration(): void public function testFlattenExtensionConfiguration(): void { $config = $this->getConfig(); - $this->extension->load([$config], $container = $this->getContainer()); + $this->extension->prepend($container = $this->getContainer()); + $this->extension->load([$config], $container); $extensionMap = $container->getParameter($this->root.'.extension.map'); $method = new \ReflectionMethod( @@ -197,6 +200,7 @@ public function testProcessWithInvalidExtensionId(): void ]; $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$config], $container); $extensionsPass = new ExtensionCompilerPass(); @@ -220,6 +224,7 @@ public function testProcessWithInvalidAdminId(): void ]; $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$config], $container); $extensionsPass = new ExtensionCompilerPass(); @@ -235,6 +240,7 @@ public function testProcessWithInvalidAdminId(): void public function testProcess(): void { $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$this->config], $container); $extensionsPass = new ExtensionCompilerPass(); @@ -297,6 +303,7 @@ public function testProcessThrowsExceptionIfTraitsAreNotAvailable(): void ]; $container = $this->getContainer(); + $this->extension->prepend($container); $this->extension->load([$config], $container); $extensionsPass = new ExtensionCompilerPass(); diff --git a/tests/DependencyInjection/SonataAdminExtensionTest.php b/tests/DependencyInjection/SonataAdminExtensionTest.php index 5b201e868e1..464e0d1a5f7 100644 --- a/tests/DependencyInjection/SonataAdminExtensionTest.php +++ b/tests/DependencyInjection/SonataAdminExtensionTest.php @@ -31,6 +31,7 @@ use Sonata\AdminBundle\Search\SearchHandler; use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; use Sonata\AdminBundle\Templating\TemplateRegistry; +use Sonata\AdminBundle\Tests\Fixtures\DependencyInjection\DummySonataAdminExtension; use Sonata\AdminBundle\Translator\BCLabelTranslatorStrategy; use Sonata\AdminBundle\Translator\FormLabelTranslatorStrategy; use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface; @@ -38,6 +39,7 @@ use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy; use Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy; use Sonata\AdminBundle\Twig\GlobalVariables; +use Symfony\Component\DependencyInjection\ContainerBuilder; class SonataAdminExtensionTest extends AbstractExtensionTestCase { @@ -309,13 +311,32 @@ public function testDefaultTemplates(): void public function testLoadIntlTemplate(): void { - $this->load([ - 'use_intl_templates' => true, - ]); - $templates = $this->container->getParameter('sonata.admin.configuration.templates'); + $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']); } + public function testLoadIntlTemplatesInOtherSonataAdminExtensions(): void + { + $container = new ContainerBuilder(); + $container->setParameter('kernel.bundles', []); + $container->prependExtensionConfig('sonata_admin', ['use_intl_templates' => true]); + $extension = new SonataAdminExtension(); + $dummyExtension = new DummySonataAdminExtension(); + $container->registerExtension($dummyExtension); + $container->registerExtension($extension); + $dummyExtension->load([], $container); + + $this->assertSame('@SonataAdmin/CRUD/Intl/list_date.html.twig', $dummyExtension->configs[0]['templates']['types']['list']['date']); + } + protected function getContainerExtensions(): array { return [new SonataAdminExtension()]; diff --git a/tests/Fixtures/DependencyInjection/DummySonataAdminExtension.php b/tests/Fixtures/DependencyInjection/DummySonataAdminExtension.php new file mode 100644 index 00000000000..ba4c94d5252 --- /dev/null +++ b/tests/Fixtures/DependencyInjection/DummySonataAdminExtension.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Fixtures\DependencyInjection; + +use Sonata\AdminBundle\DependencyInjection\AbstractSonataAdminExtension; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +class DummySonataAdminExtension extends AbstractSonataAdminExtension +{ + /** + * @var array + */ + public $configs; + + /** + * @inheritDoc + */ + public function load(array $configs, ContainerBuilder $container) + { + $this->configs = $this->fixTemplatesConfiguration($configs, $container); + } +}