Skip to content

Commit

Permalink
Merge pull request #235 from symfony-cmf/routing-bundle-optional
Browse files Browse the repository at this point in the history
do not load publish workflow request listener if routing is not availble
  • Loading branch information
dbu authored Oct 11, 2017
2 parents 4b5fcee + 26b668c commit cfc5f7b
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 15 deletions.
42 changes: 29 additions & 13 deletions src/DependencyInjection/CmfCoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,7 @@ public function load(array $configs, ContainerBuilder $container)
'%cmf_core.persistence.phpcr.manager_name%',
]);
}
if ($config['publish_workflow']['enabled']) {
$this->loadPublishWorkflow($config['publish_workflow'], $loader, $container);
} else {
$loader->load('no-publish-workflow.xml');
}
$this->loadPublishWorkflow($config['publish_workflow'], $loader, $container);

if (isset($config['multilang'])) {
$container->setParameter($this->getAlias().'.multilang.locales', $config['multilang']['locales']);
Expand Down Expand Up @@ -315,19 +311,39 @@ public function setupFormTypes(ContainerBuilder $container, LoaderInterface $loa
*/
private function loadPublishWorkflow($config, XmlFileLoader $loader, ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');

if (false === $config['enabled']
|| ('auto' === $config['enabled'] && !array_key_exists('SecurityBundle', $bundles))
) {
$loader->load('no-publish-workflow.xml');

return;
}

if (!array_key_exists('SecurityBundle', $bundles)) {
throw new InvalidConfigurationException(
'The "publish_workflow" may not be enabled unless "symfony/security-bundle" is available.'
);
}

$container->setParameter($this->getAlias().'.publish_workflow.view_non_published_role', $config['view_non_published_role']);
$loader->load('publish-workflow.xml');

if (!$config['request_listener']) {
$container->setAlias('cmf_core.publish_workflow.checker', $config['checker_service']);

if (false === $config['request_listener']
|| ('auto' === $config['request_listener'] && !class_exists(DynamicRouter::class))
) {
$container->removeDefinition($this->getAlias().'.publish_workflow.request_listener');
} elseif (!class_exists(DynamicRouter::class)) {
throw new InvalidConfigurationException(sprintf(
'The "publish_workflow.request_listener" may not be enabled unless "%s" is available.',
DynamicRouter::class
));
}

$container->setAlias('cmf_core.publish_workflow.checker', $config['checker_service']);
return;
}
if (!class_exists(DynamicRouter::class)) {
throw new InvalidConfigurationException(
'The "publish_workflow.request_listener" may not be enabled unless "symfony-cmf/routing-bundle" is available.'
);
}
}

/**
Expand Down
21 changes: 19 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,28 @@ public function getConfigTreeBuilder()
->end()
->arrayNode('publish_workflow')
->addDefaultsIfNotSet()
->canBeDisabled()
->treatFalseLike(['enabled' => false])
->treatTrueLike(['enabled' => true])
->treatNullLike(['enabled' => 'auto'])
->beforeNormalization()
->ifArray()
->then(function ($v) {
$v['enabled'] = isset($v['enabled']) ? $v['enabled'] : true;

return $v;
})
->end()
->children()
->enumNode('enabled')
->values([true, false, 'auto'])
->defaultValue('auto')
->end()
->scalarNode('checker_service')->defaultValue('cmf_core.publish_workflow.checker.default')->end()
->scalarNode('view_non_published_role')->defaultValue('ROLE_CAN_VIEW_NON_PUBLISHED')->end()
->booleanNode('request_listener')->defaultTrue()->end()
->enumNode('request_listener')
->values([true, false, 'auto'])
->defaultValue('auto')
->end()
->end()
->end()
->end()
Expand Down
3 changes: 3 additions & 0 deletions tests/Functional/DependencyInjection/CmfCoreExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

class CmfCoreExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ContainerBuilder
*/
private $container;

protected function setUp()
Expand Down
136 changes: 136 additions & 0 deletions tests/Unit/DependencyInjection/CmfCoreExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Bundle\CoreBundle\Tests\Unit\DependencyInjection;

use Symfony\Bundle\SecurityBundle\SecurityBundle;
use Symfony\Cmf\Bundle\CoreBundle\DependencyInjection\CmfCoreExtension;
use Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Routing\Router;

class CmfCoreExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CmfCoreExtension
*/
protected $extension;

protected function setUp()
{
$this->extension = new CmfCoreExtension();
}

public function testPublishWorkflowAutoSupported()
{
$container = $this->createContainer(['kernel.bundles' => ['SecurityBundle' => SecurityBundle::class]]);

$this->extension->load([['publish_workflow' => ['request_listener' => false]]], $container);

$this->assertTrue($container->hasAlias('cmf_core.publish_workflow.checker'));
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
$this->assertTrue($container->hasDefinition('cmf_core.security.publishable_voter'));
$this->assertTrue($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
}

public function testPublishWorkflowListenerEnabled()
{
$container = $this->createContainer(['kernel.bundles' => [
'SecurityBundle' => SecurityBundle::class,
'CmfRoutingBundle' => CmfRoutingBundle::class,
]]);

$this->extension->load([], $container);

$this->assertTrue($container->hasAlias('cmf_core.publish_workflow.checker'));
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
$this->assertTrue($container->hasDefinition('cmf_core.security.publishable_voter'));
$this->assertTrue($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
}

public function testPublishWorkflowAutoNotSupported()
{
$container = $this->createContainer(['kernel.bundles' => []]);

$this->extension->load([], $container);

$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker'));
$this->assertFalse($container->hasAlias('cmf_core.publish_workflow.checker'));
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
$this->assertFalse($container->hasDefinition('cmf_core.security.publishable_voter'));
$this->assertFalse($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
}

public function testPublishWorkflowFalse()
{
$container = $this->createContainer(['kernel.bundles' => [
'SecurityBundle' => SecurityBundle::class,
'CmfRoutingBundle' => CmfRoutingBundle::class,
]]);

$this->extension->load([['publish_workflow' => false]], $container);

$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker'));
$this->assertFalse($container->hasAlias('cmf_core.publish_workflow.checker'));
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
$this->assertFalse($container->hasDefinition('cmf_core.security.publishable_voter'));
$this->assertFalse($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
$this->assertFalse($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
}

public function testPublishWorkflowTrueSupported()
{
$container = $this->createContainer(['kernel.bundles' => [
'SecurityBundle' => SecurityBundle::class,
'CmfRoutingBundle' => CmfRoutingBundle::class,
]]);

$this->extension->load([['publish_workflow' => true]], $container);

$this->assertTrue($container->hasAlias('cmf_core.publish_workflow.checker'));
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.checker.default'));
$this->assertTrue($container->hasDefinition('cmf_core.security.publishable_voter'));
$this->assertTrue($container->hasDefinition('cmf_core.security.publish_time_period_voter'));
$this->assertTrue($container->hasDefinition('cmf_core.publish_workflow.request_listener'));
}

public function testPublishWorkflowTrueNotSupported()
{
$container = $this->createContainer(['kernel.bundles' => [
'CmfRoutingBundle' => CmfRoutingBundle::class,
]]);

$this->expectException(InvalidConfigurationException::class);
$this->extension->load([['publish_workflow' => true]], $container);
}

private function createContainer(array $parameters)
{
$parameters = array_merge(['kernel.debug' => false], $parameters);
$container = new ContainerBuilder(
new ParameterBag($parameters)
);

// The cache_manager service depends on the router service
$container->setDefinition(
'router',
new Definition(Router::class)
);

return $container;
}
}

0 comments on commit cfc5f7b

Please sign in to comment.