From 6a50ccfa2501f4b94af529c0f29e80bf87a8b4fe Mon Sep 17 00:00:00 2001 From: Eugene Ritter Date: Sat, 10 Feb 2018 15:51:32 -0600 Subject: [PATCH] Defect 11686 ObjectRegistry::normalizedArray normalizes the array the first time. When passing an array already normalized it continues trying to normalize producing a nested effect. This should resolve the issue. --- src/Core/ObjectRegistry.php | 6 +- tests/TestCase/View/HelperRegistryTest.php | 77 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/Core/ObjectRegistry.php b/src/Core/ObjectRegistry.php index 82568c76cc0..4aea1f919bc 100644 --- a/src/Core/ObjectRegistry.php +++ b/src/Core/ObjectRegistry.php @@ -278,7 +278,11 @@ public function normalizeArray($objects) $objectName = $i; } list(, $name) = pluginSplit($objectName); - $normal[$name] = ['class' => $objectName, 'config' => $config]; + if (isset($config['class'])) { + $normal[$name] = $config; + } else { + $normal[$name] = ['class' => $objectName, 'config' => $config]; + } } return $normal; diff --git a/tests/TestCase/View/HelperRegistryTest.php b/tests/TestCase/View/HelperRegistryTest.php index fe01f5a466e..196542c3fd7 100644 --- a/tests/TestCase/View/HelperRegistryTest.php +++ b/tests/TestCase/View/HelperRegistryTest.php @@ -351,4 +351,81 @@ public function testLoadMultipleTimesDifferentConfigValues() $this->Helpers->load('Html', ['key' => 'value']); $this->Helpers->load('Html', ['key' => 'new value']); } + + /** + * Test ObjectRegistry normalizeArray + * + * @return void + */ + public function testArrayIsNormalized() + { + $config = [ + 'SomeHelper' => [ + 'value' => 1, + 'value2' => 2 + ], + 'Plugin.SomeOtherHelper' => [ + 'value' => 1, + 'value2' => 2 + ] + ]; + $result = $this->Helpers->normalizeArray($config); + $expected = [ + 'SomeHelper' => [ + 'class' => 'SomeHelper', + 'config' => [ + 'value' => 1, + 'value2' => 2 + ] + ], + 'SomeOtherHelper' => [ + 'class' => 'Plugin.SomeOtherHelper', + 'config' => [ + 'value' => 1, + 'value2' => 2 + ] + ], + ]; + $this->assertEquals($expected, $result); + } + + /** + * Test that calling normalizeArray multiple times does + * not nest the configuration. + * + * @return void + */ + public function testArrayIsNormalizedAfterMultipleCalls() + { + $config = [ + 'SomeHelper' => [ + 'value' => 1, + 'value2' => 2 + ], + 'Plugin.SomeOtherHelper' => [ + 'value' => 1, + 'value2' => 2 + ] + ]; + + $result1 = $this->Helpers->normalizeArray($config); + $result2 = $this->Helpers->normalizeArray($result1); + $expected = [ + 'SomeHelper' => [ + 'class' => 'SomeHelper', + 'config' => [ + 'value' => 1, + 'value2' => 2 + ] + ], + 'SomeOtherHelper' => [ + 'class' => 'Plugin.SomeOtherHelper', + 'config' => [ + 'value' => 1, + 'value2' => 2 + ] + ], + ]; + $this->assertEquals($expected, $result2); + } }