From 6b3ab5e5525360b8805cc6d8532a0e5eb48bd2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fa=20G=C3=BCrdal?= Date: Wed, 16 Nov 2016 14:45:54 +0100 Subject: [PATCH 1/4] fix(resolveConfig): arrays of fields work now without showing a notice in ACF call still missing tests and refactoring --- lib/ACFComposer/ResolveConfig.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 24cd26e..fde8df9 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -10,9 +10,17 @@ public static function forFieldGroup($config) { $keySuffix = $output['name']; $output['key'] = "group_{$keySuffix}"; - $output['fields'] = array_map(function ($field) use ($keySuffix) { - return self::forField($field, [$keySuffix]); - }, $output['fields']); + $output['fields'] = array_reduce($config['fields'], function ($carry, $fieldConfig) use ($keySuffix) { + $fields = self::forField($fieldConfig, [$keySuffix]); + if (!self::isAssoc($fields)) { + foreach ($fields as $field) { + array_push($carry, $field); + } + } else { + array_push($carry, $fields); + } + return $carry; + }, []); $output['location'] = array_map('self::mapLocation', $output['location']); return $output; } @@ -31,6 +39,7 @@ public static function forLayout($config, $parentKeys = []) { protected static function forEntity($config, $requiredAttributes, $parentKeys = []) { if (is_string($config)) { + // TODO catch unapplied filters and show warning, then get out of this function to prevent exceptions $config = apply_filters($config, null); } if (!self::isAssoc($config)) { From e3b86ea694b14a0cdac2096829271ca394ec0118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fa=20G=C3=BCrdal?= Date: Mon, 5 Dec 2016 11:57:58 +0100 Subject: [PATCH 2/4] feat(resolveConfig): added check for unapplied filters now shows warning and prevents php errors to break the whole website --- lib/ACFComposer/ResolveConfig.php | 9 +++++++-- tests/test-resolveConfigForField.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index fde8df9..c476026 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -39,8 +39,13 @@ public static function forLayout($config, $parentKeys = []) { protected static function forEntity($config, $requiredAttributes, $parentKeys = []) { if (is_string($config)) { - // TODO catch unapplied filters and show warning, then get out of this function to prevent exceptions - $config = apply_filters($config, null); + $filterName = $config; + $config = apply_filters($filterName, null); + + if (is_null($config)) { + trigger_error("ACFComposer: Filter {$filterName} does not exist!", E_USER_WARNING); + return []; + } } if (!self::isAssoc($config)) { return array_map(function ($singleConfig) use ($requiredAttributes, $parentKeys) { diff --git a/tests/test-resolveConfigForField.php b/tests/test-resolveConfigForField.php index e17a4be..27c0b1c 100644 --- a/tests/test-resolveConfigForField.php +++ b/tests/test-resolveConfigForField.php @@ -71,6 +71,24 @@ function testForFieldGetConfigFromFilter() { $this->assertEquals($someField, $output); } + function testForFieldTriggerErrorWithoutFilter() { + $config = 'ACFComposer/Fields/someField'; + Filters::expectApplied($config) + ->once() + ->andReturn(null); + $this->expectException('PHPUnit_Framework_Error_Warning'); + $output = ResolveConfig::forField($config); + } + + function testForFieldReturnEmptyArrayWithoutFilter() { + $config = 'ACFComposer/Fields/someField'; + Filters::expectApplied($config) + ->once() + ->andReturn(null); + $output = @ResolveConfig::forField($config); + $this->assertEquals($output, []); + } + function testForFieldWithValidSubField() { $subFieldConfig = [ 'name' => 'subField', From 3ca2ed721f6b1a5984bed0306ab53d72af470a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fa=20G=C3=BCrdal?= Date: Mon, 5 Dec 2016 14:45:25 +0100 Subject: [PATCH 3/4] test(resolveConfig): added missing test for simple fields array fix --- tests/test-resolveConfigForFieldGroup.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test-resolveConfigForFieldGroup.php b/tests/test-resolveConfigForFieldGroup.php index bcd6428..14e2c0d 100644 --- a/tests/test-resolveConfigForFieldGroup.php +++ b/tests/test-resolveConfigForFieldGroup.php @@ -8,6 +8,7 @@ class ResolveConfigForFieldGroupTest extends TestCase { function testForFieldGroupWithValidConfig() { + $filterName = 'ACFComposer/Fields/someField'; $fieldConfig = [ 'name' => 'someField', 'label' => 'Some Field', @@ -21,15 +22,23 @@ function testForFieldGroupWithValidConfig() { $config = [ 'name' => 'someGroup', 'title' => 'Some Group', - 'fields' => [$fieldConfig], + 'fields' => [ + $filterName, + $fieldConfig + ], 'location' => [ [$locationConfig] ] ]; + + Filters::expectApplied($filterName) + ->once() + ->andReturn($fieldConfig); + $output = ResolveConfig::forFieldGroup($config); $fieldConfig['key'] = 'field_someGroup_someField'; $config['key'] = 'group_someGroup'; - $config['fields'] = [$fieldConfig]; + $config['fields'] = [$fieldConfig, $fieldConfig]; $this->assertEquals($config, $output); } From 3235129b018b4d1ac16b62c8a7e5ace2b1ac5d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Tue, 13 Dec 2016 19:26:20 +0100 Subject: [PATCH 4/4] test(forFieldGroup): add multi dimensional array to forFieldGroup test --- tests/test-resolveConfigForFieldGroup.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/test-resolveConfigForFieldGroup.php b/tests/test-resolveConfigForFieldGroup.php index 14e2c0d..42c4443 100644 --- a/tests/test-resolveConfigForFieldGroup.php +++ b/tests/test-resolveConfigForFieldGroup.php @@ -14,6 +14,18 @@ function testForFieldGroupWithValidConfig() { 'label' => 'Some Field', 'type' => 'someType' ]; + $fieldConfigMulti = [ + [ + 'name' => 'someField1', + 'label' => 'Some Field1', + 'type' => 'someType' + ], + [ + 'name' => 'someField2', + 'label' => 'Some Field2', + 'type' => 'someType' + ] + ]; $locationConfig = [ 'param' => 'someParam', 'operator' => 'someOperator', @@ -24,7 +36,8 @@ function testForFieldGroupWithValidConfig() { 'title' => 'Some Group', 'fields' => [ $filterName, - $fieldConfig + $fieldConfig, + $fieldConfigMulti ], 'location' => [ [$locationConfig] @@ -37,8 +50,10 @@ function testForFieldGroupWithValidConfig() { $output = ResolveConfig::forFieldGroup($config); $fieldConfig['key'] = 'field_someGroup_someField'; + $fieldConfigMulti[0]['key'] = 'field_someGroup_someField1'; + $fieldConfigMulti[1]['key'] = 'field_someGroup_someField2'; $config['key'] = 'group_someGroup'; - $config['fields'] = [$fieldConfig, $fieldConfig]; + $config['fields'] = [$fieldConfig, $fieldConfig, $fieldConfigMulti[0], $fieldConfigMulti[1]]; $this->assertEquals($config, $output); }