From db19414e27faba4c35605e7ddaf0f61e5b47d1dc Mon Sep 17 00:00:00 2001 From: Wahiba Date: Tue, 25 Apr 2017 16:17:15 +0200 Subject: [PATCH] feat(ACFComposer/ResolveConfig): Handle filter arguments in conditional 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 --- lib/ACFComposer/ResolveConfig.php | 9 +++-- tests/test-resolveConfigForField.php | 56 +++++++++++++++++++++++++++ tests/test-resolveConfigForLayout.php | 54 ++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 4 deletions(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 07cb52a..a92bbff 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -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; @@ -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']); diff --git a/tests/test-resolveConfigForField.php b/tests/test-resolveConfigForField.php index ab01d1d..23f40d9 100644 --- a/tests/test-resolveConfigForField.php +++ b/tests/test-resolveConfigForField.php @@ -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'; diff --git a/tests/test-resolveConfigForLayout.php b/tests/test-resolveConfigForLayout.php index ff7dda8..65a4f71 100644 --- a/tests/test-resolveConfigForLayout.php +++ b/tests/test-resolveConfigForLayout.php @@ -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); + } }