Skip to content

Commit

Permalink
NGSTACK-410: fix extended config not overriding default values
Browse files Browse the repository at this point in the history
  • Loading branch information
pspanja committed Mar 31, 2020
1 parent 1d2e281 commit 2fa8a33
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 1 deletion.
48 changes: 47 additions & 1 deletion bundle/DependencyInjection/Configuration/Parser/ContentView.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class ContentView extends AbstractParser
public const QUERY_KEY = 'queries';
public const NODE_KEY = 'ngcontent_view';
private const INFO = 'Template selection settings when displaying a content with Netgen Site API';
private const DEFAULT_MATCH_VALUE = ['NG_DEFAULT_MATCH_VALUE'];
private const DEFAULT_QUERIES_VALUE = ['NG_DEFAULT_QUERIES_VALUE'];
private const DEFAULT_PARAMS_VALUE = ['NG_DEFAULT_PARAMS_VALUE'];

/**
* Adds semantic configuration definition.
Expand Down Expand Up @@ -110,6 +113,7 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder): void
->end()
->arrayNode('match')
->info('Condition matchers configuration')
->defaultValue(self::DEFAULT_MATCH_VALUE)
->useAttributeAsKey('key')
->variablePrototype()->end()
->end()
Expand All @@ -127,6 +131,7 @@ public function addSemanticConfig(NodeBuilder $nodeBuilder): void
'osTypes' => ['osx', 'linux', 'windows'],
]
)
->defaultValue(self::DEFAULT_PARAMS_VALUE)
->useAttributeAsKey('key')
->variablePrototype()->end()
->end()
Expand Down Expand Up @@ -205,9 +210,13 @@ public function postMap(array $config, ContextualizerInterface $contextualizer):
private function extendViewConfig(&$config, string $viewPath, array $viewConfigs): void
{
if (!\array_key_exists('extends', $config)) {
$this->restoreDefaultValues($config);

return;
}

$this->unsetDefaultValues($config);

[$extendedViewType, $extendedName] = \explode('/', $config['extends'] . '/');

if (!isset($viewConfigs[$extendedViewType][$extendedName])) {
Expand All @@ -233,7 +242,43 @@ private function extendViewConfig(&$config, string $viewPath, array $viewConfigs
);
}

$config = \array_replace($baseConfig, $config);
$replacedConfig = \array_replace($baseConfig, $config);

if ($replacedConfig === null) {
throw new InvalidConfigurationException('Could not replace extended config');
}

$config = $replacedConfig;
}

private function restoreDefaultValues(array &$config): void
{
if ($config['match'] === self::DEFAULT_MATCH_VALUE) {
$config['match'] = [];
}

if ($config['queries'] === self::DEFAULT_QUERIES_VALUE) {
$config['queries'] = [];
}

if ($config['params'] === self::DEFAULT_PARAMS_VALUE) {
$config['params'] = [];
}
}

private function unsetDefaultValues(array &$config): void
{
if ($config['match'] === self::DEFAULT_MATCH_VALUE) {
unset($config['match']);
}

if ($config['queries'] === self::DEFAULT_QUERIES_VALUE) {
unset($config['queries']);
}

if ($config['params'] === self::DEFAULT_PARAMS_VALUE) {
unset($config['params']);
}
}

/**
Expand All @@ -246,6 +291,7 @@ private function getQueryNode(string $name): ArrayNodeDefinition
$queries = new ArrayNodeDefinition($name);
$queries
->info('Query configuration')
->defaultValue(self::DEFAULT_QUERIES_VALUE)
->useAttributeAsKey('key')
->arrayPrototype()
->beforeNormalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,183 @@ public function testValid(array $configurationValues): void
$this->assertTrue(true);
}

public function providerForTextExtends(): array
{
return [
[
[
'extends' => 'base/one',
],
[
'match' => [],
'queries' => [],
'params' => ['params'],
'extends' => 'base/one',
],
],
[
[
'extends' => 'base/two',
],
[
'match' => [],
'queries' => [
'query' => [
'query_type' => 'test',
'parameters' => [],
'use_filter' => true,
'max_per_page' => 25,
'page' => 1,
],
],
'params' => [],
'extends' => 'base/two',
],
],
[
[
'extends' => 'base/three',
],
[
'template' => 'template',
'match' => ['match'],
'queries' => [],
'params' => [],
'extends' => 'base/three',
],
],
[
[
'match' => ['selen'],
'params' => ['petrusimen'],
'extends' => 'base/one',
],
[
'match' => ['selen'],
'queries' => [],
'params' => ['petrusimen'],
'extends' => 'base/one',
],
],
[
[
'queries' => [
'query' => [
'query_type' => 'blušć',
],
],
'extends' => 'base/two',
],
[
'match' => [],
'queries' => [
'query' => [
'query_type' => 'blušć',
'parameters' => [],
'use_filter' => true,
'max_per_page' => 25,
'page' => 1,
],
],
'params' => [],
'extends' => 'base/two',
],
],
[
[
'template' => 'pazdej',
'extends' => 'base/three',
],
[
'template' => 'pazdej',
'match' => ['match'],
'queries' => [],
'params' => [],
'extends' => 'base/three',
],
],
];
}

/**
* @group xxx
* @dataProvider providerForTextExtends
*
* @param array $configurationValues
* @param array $expectedValues
*/
public function testExtends(array $configurationValues, array $expectedValues): void
{
$baseConfig = [
'system' => [
'siteaccess_group' => [
'ngcontent_view' => [
'base' => [
'one' => [
'match' => [],
'params' => ['params'],
],
'two' => [
'match' => null,
'queries' => [
'query' => [
'query_type' => 'test',
],
],
],
'three' => [
'template' => 'template',
'match' => ['match'],
],
],
'tested_view' => [
'tested_name' => $configurationValues,
],
],
],
],
];

$this->load($baseConfig);

$expectedValues = [
'base' => [
'one' => [
'match' => [],
'queries' => [],
'params' => ['params'],
],
'two' => [
'match' => [],
'queries' => [
'query' => [
'query_type' => 'test',
'parameters' => [],
'use_filter' => true,
'max_per_page' => 25,
'page' => 1,
],
],
'params' => [],
],
'three' => [
'template' => 'template',
'match' => ['match'],
'queries' => [],
'params' => [],
],
],
'tested_view' => [
'tested_name' => $expectedValues,
],
];

$this->assertContainerBuilderHasParameter('ezsettings.cro.ngcontent_view', $expectedValues);

// Avoid detecting risky tests
$this->assertTrue(true);
}

public function providerForTestInvalid(): array
{
return [
Expand Down

0 comments on commit 2fa8a33

Please sign in to comment.