From 4924c75511f7d10dbf407095bd3913159b4876e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tr=C3=A4nklein?= Date: Tue, 8 Nov 2016 21:06:41 +0100 Subject: [PATCH] feat(ResolveConfig): make resolve field group fail on invalid location and field --- lib/ACFComposer/ResolveConfig.php | 10 +++++- tests/test-resolveConfigForFieldGroup.php | 44 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/ACFComposer/ResolveConfig.php b/lib/ACFComposer/ResolveConfig.php index 5040ac6..7c6236e 100644 --- a/lib/ACFComposer/ResolveConfig.php +++ b/lib/ACFComposer/ResolveConfig.php @@ -10,13 +10,21 @@ public static function forFieldGroup($config) { $keySuffix = $output['name']; $output['key'] = "group_{$keySuffix}"; - $output = self::forNestedEntities($output, $keySuffix); $output['fields'] = array_map(function($field) use ($keySuffix){ return self::forField($field, $keySuffix); }, $output['fields']); + $output['location'] = array_map(function($locationArray) { + return array_map(function($location) { + return self::forLocation($location); + }, $locationArray); + }, $output['location']); return $output; } + public static function forLocation($config) { + return self::validateConfig($config, ['param', 'operator', 'value']); + } + public static function forField($config, $keySuffix = '') { return self::forEntity($config, ['name', 'label', 'type'], $keySuffix); } diff --git a/tests/test-resolveConfigForFieldGroup.php b/tests/test-resolveConfigForFieldGroup.php index ac7377d..bcd6428 100644 --- a/tests/test-resolveConfigForFieldGroup.php +++ b/tests/test-resolveConfigForFieldGroup.php @@ -32,4 +32,48 @@ function testForFieldGroupWithValidConfig() { $config['fields'] = [$fieldConfig]; $this->assertEquals($config, $output); } + + function testForFieldGroupFailsWithInvalidField() { + $fieldConfig = [ + 'name' => 'someField', + 'label' => 'Some Field' + ]; + $locationConfig = [ + 'param' => 'someParam', + 'operator' => 'someOperator', + 'value' => 'someValue' + ]; + $config = [ + 'name' => 'someGroup', + 'title' => 'Some Group', + 'fields' => [$fieldConfig], + 'location' => [ + [$locationConfig] + ] + ]; + $this->expectException(Exception::class); + ResolveConfig::forFieldGroup($config); + } + + function testForFieldGroupFailsWithInvalidLocation() { + $fieldConfig = [ + 'name' => 'someField', + 'label' => 'Some Field', + 'type' => 'someType' + ]; + $locationConfig = [ + 'operator' => 'someOperator', + 'value' => 'someValue' + ]; + $config = [ + 'name' => 'someGroup', + 'title' => 'Some Group', + 'fields' => [$fieldConfig], + 'location' => [ + [$locationConfig] + ] + ]; + $this->expectException(Exception::class); + ResolveConfig::forFieldGroup($config); + } }