From 66d7596d688a8c11bb62f170390838446f79599d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Tue, 8 Nov 2016 21:30:35 +0100 Subject: [PATCH] feat(ResolveConfig): apply resolveEntity filters --- lib/ACFComposer/ResolveConfig.php | 5 +++ tests/test-resolveConfigForField.php | 54 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 7c6236e..eeb5de2 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -41,6 +41,11 @@ protected static function forEntity($config, $requiredAttributes, $parentKeySuff $keySuffix = empty($parentKeySuffix) ? $output['name'] : "{$parentKeySuffix}_{$output['name']}"; $output['key'] = "field_{$keySuffix}"; + + $output = apply_filters('ACFComposer/resolveEntity', $output); + $output = apply_filters("ACFComposer/resolveEntity?name={$output['name']}", $output); + $output = apply_filters("ACFComposer/resolveEntity?key={$output['key']}", $output); + $output = self::forNestedEntities($output, $keySuffix); return $output; } diff --git a/tests/test-resolveConfigForField.php b/tests/test-resolveConfigForField.php index 143cdf7..ab20e6a 100644 --- a/tests/test-resolveConfigForField.php +++ b/tests/test-resolveConfigForField.php @@ -118,4 +118,58 @@ function testForFieldWithValidLayout() { $layoutConfig['key'] = 'field_someField_someLayout'; $this->assertEquals($layoutConfig, $output['layouts'][0]); } + + function testAppliesResolveFilterGeneric() { + $config = [ + 'name' => 'someField', + 'label' => 'Some Field', + 'type' => 'someType' + ]; + $resolvedConfig = $config; + $resolvedConfig['key'] = 'field_someField'; + Filters::expectApplied('ACFComposer/resolveEntity') + ->once() + ->with($resolvedConfig) + ->andReturn(array_merge($resolvedConfig, ['foo' => 'bar'])); + + $output = ResolveConfig::forField($config); + $resolvedConfig['foo'] = 'bar'; + $this->assertEquals($resolvedConfig, $output); + } + + function testAppliesResolveFilterByName() { + $config = [ + 'name' => 'someField', + 'label' => 'Some Field', + 'type' => 'someType' + ]; + $resolvedConfig = $config; + $resolvedConfig['key'] = 'field_someField'; + Filters::expectApplied('ACFComposer/resolveEntity?name=someField') + ->once() + ->with($resolvedConfig) + ->andReturn(array_merge($resolvedConfig, ['foo' => 'bar'])); + + $output = ResolveConfig::forField($config); + $resolvedConfig['foo'] = 'bar'; + $this->assertEquals($resolvedConfig, $output); + } + + function testAppliesResolveFilterByKey() { + $config = [ + 'name' => 'someField', + 'label' => 'Some Field', + 'type' => 'someType' + ]; + $resolvedConfig = $config; + $resolvedConfig['key'] = 'field_someField'; + Filters::expectApplied('ACFComposer/resolveEntity?key=field_someField') + ->once() + ->with($resolvedConfig) + ->andReturn(array_merge($resolvedConfig, ['foo' => 'bar'])); + + $output = ResolveConfig::forField($config); + $resolvedConfig['foo'] = 'bar'; + $this->assertEquals($resolvedConfig, $output); + } }