bug #25789 Enableable ArrayNodeDefinition is disabled for empty configuration (kejwmen)

This PR was squashed before being merged into the 2.7 branch (closes #25789).

Discussion
----------

 Enableable ArrayNodeDefinition is disabled for empty configuration

| Q             | A
| ------------- | ---
| Branch?       | 2.7+
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25760
| License       | MIT

Fixes #25760.

Currently, documented behavior is not true:

70c8c2d47b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php (L207-L208)

Commits
-------

a6a330dcd9  Enableable ArrayNodeDefinition is disabled for empty configuration
This commit is contained in:
Fabien Potencier 2018-01-17 21:35:17 +01:00
commit 132cec44f2
3 changed files with 37 additions and 1 deletions

View File

@ -226,7 +226,9 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition
->beforeNormalization()
->ifArray()
->then(function ($v) {
$v['enabled'] = isset($v['enabled']) ? $v['enabled'] : true;
if (!isset($v['enabled'])) {
$v['enabled'] = !empty($v);
}
return $v;
})

View File

@ -207,6 +207,20 @@ class ArrayNodeDefinitionTest extends TestCase
$this->assertTrue($this->getField($enabledNode, 'defaultValue'));
}
public function testEnableableNodeIsDisabledForEmptyConfigurationWhenNormalized()
{
$config = array();
$node = new ArrayNodeDefinition('root');
$node->canBeEnabled();
$this->assertEquals(
array('enabled' => false),
$node->getNode()->normalize($config),
'An enableable node is disabled by default'
);
}
public function testIgnoreExtraKeys()
{
$node = new ArrayNodeDefinition('root');
@ -240,6 +254,7 @@ class ArrayNodeDefinitionTest extends TestCase
array(array('enabled' => true, 'foo' => 'baz'), array(array('foo' => 'baz')), 'any configuration enables an enableable node'),
array(array('enabled' => false, 'foo' => 'baz'), array(array('foo' => 'baz', 'enabled' => false)), 'An enableable node can be disabled'),
array(array('enabled' => false, 'foo' => 'bar'), array(false), 'false disables an enableable node'),
array(array('enabled' => false, 'foo' => 'bar'), array(), 'enableable node is disabled by default'),
);
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Config\Tests\Definition\Builder;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder as CustomNodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@ -131,4 +132,22 @@ class TreeBuilderTest extends TestCase
$this->assertInternalType('array', $tree->getExample());
$this->assertEquals('example', $children['child']->getExample());
}
public function testRootNodeThatCanBeEnabledIsDisabledByDefault()
{
$builder = new TreeBuilder();
$builder->root('test')
->canBeEnabled();
$tree = $builder->buildTree();
$children = $tree->getChildren();
$this->assertFalse($children['enabled']->getDefaultValue());
$processor = new Processor();
$result = $processor->process($tree, array());
$this->assertEquals(array('enabled' => false), $result);
}
}