Skip to content

Commit

Permalink
feat(ACFComposer/ResolveConfig): Handle filter arguments in condition…
Browse files Browse the repository at this point in the history
…al logic

* refactor(lib/ACFComposer/ResolveConfig.php): Added prefix handling to parent keys for conditional lo

* feat(tests/test-resolveConfigForField): Added a test with prefix and conditional logic

Testing forField with filter and prefix, with  conditional logic

* feat(tests/test-resolveConfigForLayout): Added a test for forLayout  with prefix and conditional log

Testing forLayout with filter and prefix, with conditional logic
  • Loading branch information
vandebled authored and domtra committed Apr 25, 2017
1 parent f811252 commit db19414
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/ACFComposer/ResolveConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function forLayout($config, $parentKeys = [])
return self::forEntity($config, ['name', 'label'], $parentKeys);
}

protected static function forEntity($config, $requiredAttributes, $parentKeys = [])
protected static function forEntity($config, $requiredAttributes, $parentKeys = [], $prefix = null)
{
if (is_string($config)) {
$filterName = $config;
Expand All @@ -68,14 +68,15 @@ protected static function forEntity($config, $requiredAttributes, $parentKeys =
}
}
if (!self::isAssoc($config)) {
return array_map(function ($singleConfig) use ($requiredAttributes, $parentKeys) {
return self::forEntity($singleConfig, $requiredAttributes, $parentKeys);
return array_map(function ($singleConfig) use ($requiredAttributes, $parentKeys, $prefix) {
return self::forEntity($singleConfig, $requiredAttributes, $parentKeys, $prefix);
}, $config);
}

$output = self::validateConfig($config, $requiredAttributes);

$output = self::forConditionalLogic($output, $parentKeys);
$parentKeysIncludingPrefix = isset($prefix) ? array_merge($parentKeys, [$prefix]) : $parentKeys;
$output = self::forConditionalLogic($output, $parentKeysIncludingPrefix);

array_push($parentKeys, $output['name']);

Expand Down
56 changes: 56 additions & 0 deletions tests/test-resolveConfigForField.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,62 @@ public function testMultipleForFieldGetConfigFromFilterWithArgument()
$this->assertEquals($someField, $output);
}

public function testForFieldGetConfigFromFilterWithArgumentAndConditionalLogic()
{
$config = [
'ACFComposer/Fields/someField#prefix',
'ACFComposer/Fields/someFieldWithConditional#otherprefix'
];

$filter = 'ACFComposer/Fields/someField';
$someField = [
'name' => 'someField',
'label' => 'Some Field',
'type' => 'someType'
];
Filters::expectApplied($filter)
->with(null, 'prefix')
->once()
->andReturn($someField);

$filterConditional = 'ACFComposer/Fields/someFieldWithConditional';
$someFieldWithConditional = [
'name' => 'someOtherField',
'label' => 'Some Other Field',
'type' => 'someType',
'conditional_logic' => [
[
[
'fieldPath' => 'someField',
'operator' => '==',
'value' => 'someValue'
]
]
]
];
Filters::expectApplied($filterConditional)
->with(null, 'otherprefix')
->once()
->andReturn($someFieldWithConditional);

$output = array_map(function ($singleConfig) {
return ResolveConfig::forField($singleConfig);
}, $config);

$someField['key'] = 'field_prefix_someField';
$someField['name'] = 'prefix_someField';

$someFieldWithConditional['key'] = 'field_otherprefix_someOtherField';
$someFieldWithConditional['name'] = 'otherprefix_someOtherField';
$someFieldWithConditional['conditional_logic'][0][0]['field'] = 'field_otherprefix_someField';
unset($someFieldWithConditional['conditional_logic'][0][0]['fieldPath']);
$config = [
$someField,
$someFieldWithConditional
];
$this->assertEquals($config, $output);
}

public function testForFieldTriggerErrorWithoutFilter()
{
$config = 'ACFComposer/Fields/someField';
Expand Down
54 changes: 54 additions & 0 deletions tests/test-resolveConfigForLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,58 @@ public function testForLayoutFailWithInvalidSubField()
$this->expectException(Exception::class);
ResolveConfig::forLayout($config);
}

public function testforLayoutGetConfigFromFilterWithArgumentAndNestedConditionalLogic()
{
$config = 'ACFComposer/Fields/someField#prefix';
$filter = 'ACFComposer/Fields/someField';
$layout = [
'name' => 'layout',
'label' => 'Layout',
'sub_fields' => [
[
'name' => 'someBoolean',
'label' => 'Some Boolean',
'type' => 'boolean'
],
[
'name' => 'someRepeater',
'label' => 'Some Repeater',
'type' => 'repeater',
'sub_fields' => [
[
'name' => 'someNestedImage',
'label' => 'Some Nested Image',
'type' => 'image',
'conditional_logic' => [
[
[
'fieldPath' => '../someBoolean',
'operator' => '==',
'value' => '1'
]
]
]
]
]
]
]
];

Filters::expectApplied($filter)
->with(null, 'prefix')
->once()
->andReturn($layout);

$output = ResolveConfig::forLayout($config);

$layout['key'] = 'field_prefix_layout';
$layout['name'] = 'prefix_layout';
$layout['sub_fields'][0]['key'] = 'field_prefix_layout_someBoolean';
$layout['sub_fields'][1]['key'] = 'field_prefix_layout_someRepeater';
$layout['sub_fields'][1]['sub_fields'][0]['key'] = 'field_prefix_layout_someRepeater_someNestedImage';
$layout['sub_fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['field'] = 'field_prefix_layout_someBoolean';
unset($layout['sub_fields'][1]['sub_fields'][0]['conditional_logic'][0][0]['fieldPath']);
$this->assertEquals($layout, $output);
}
}

0 comments on commit db19414

Please sign in to comment.