diff -u b/src/ConfigDiffer.php b/src/ConfigDiffer.php --- b/src/ConfigDiffer.php +++ b/src/ConfigDiffer.php @@ -62,9 +62,9 @@ /** * Normalizes config for comparison. * - * Removes elements in the ignore list from top level of configuration and - * sorts at each level by array key, so that config from different storage can - * be compared meaningfully. + * Removes elements in the ignore list from the top level of configuration, + * and at each level of the array, removes empty arrays and sorts by array + * key, so that config from different storage can be compared meaningfully. * * @param array|null $config * Configuration array to normalize. @@ -80,35 +80,39 @@ return []; } - // Remove "ignore" elements. + // Remove "ignore" elements, only at the top level. foreach ($this->ignore as $element) { unset($config[$element]); } - // Recursive sort by key and return. - return $this->recursiveKeySort($config); + // Recursively normalize and return. + return $this->normalizeArray($config); } /** - * Recursively sort an array by key. + * Recursively sorts an array by key, and removes empty arrays. * * @param array $array - * An array that should be sorted by keys. + * An array to normalize. * * @return array - * Returns a sorted array by keys. + * An array that is sorted by key, at each level of the array, with empty + * arrays removed. */ - protected function recursiveKeySort(array $array) { - // Recursively normalize remaining elements, if they are arrays. + protected function normalizeArray(array $array) { foreach ($array as $key => $value) { - if (is_array($value) && !empty($value)) { - $array[$key] = $this->recursiveKeySort($value); + if (is_array($value)) { + $new = $this->normalizearray($value); + if (count($new)) { + $array[$key] = $new; + } + else { + unset($array[$key]); + } } } - // Sort and return. ksort($array); - return $array; } diff -u b/tests/src/Unit/ConfigDifferTest.php b/tests/src/Unit/ConfigDifferTest.php --- b/tests/src/Unit/ConfigDifferTest.php +++ b/tests/src/Unit/ConfigDifferTest.php @@ -32,11 +32,10 @@ * {@inheritdoc} */ protected function setUp() { - // The translation is used only for formatting NULL values. $this->stringTranslation = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationInterface')->getMock(); $this->stringTranslation - ->method('translateString') - ->willReturn('(NULL)'); + ->method('t') + ->will($this->returnArgument(0)); $this->configDiffer = new ConfigDiffer($this->stringTranslation); } @@ -60,13 +59,14 @@ 'c' => [ 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], ]; - return [ [$base, $base, TRUE], - // Add _core, omit uuid. Should match. + // Add _core, omit uuid at top level. Should match, as both are removed + // in normalization process. [ $base, [ @@ -76,6 +76,7 @@ 'c' => [ 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], ], TRUE, @@ -90,13 +91,15 @@ 'a' => 'a', 'c' => [ 'e' => FALSE, + 'empty' => [], 'd' => TRUE, ], ], TRUE, ], - // Add _core in deeper level. Should not match. + // Add _core in deeper level. Should not match, as this is removed + // only at the top level during normalization. [ $base, [ @@ -107,12 +110,14 @@ '_core' => 'do-not-use-this-key', 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], ], FALSE, ], - // Add uuid in deeper level. Should not match. + // Add uuid in deeper level. Should not match, as this is removed + // only at the top level during normalization. [ $base, [ @@ -123,6 +128,7 @@ 'd' => TRUE, 'e' => FALSE, 'uuid' => 'important', + 'empty' => [], ], ], FALSE, @@ -137,6 +143,7 @@ 'c' => [ 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], ], FALSE, @@ -152,6 +159,7 @@ 'c' => [ 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], 'f' => 'f', ], @@ -169,6 +177,7 @@ 'c' => [ 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], ], FALSE, @@ -185,6 +194,7 @@ 'c' => [ 'd' => TRUE, 'e' => FALSE, + 'empty' => [], ], ], FALSE, @@ -201,6 +211,7 @@ 'c' => [ 'd' => TRUE, 'e' => 'e', + 'empty' => [], ], ], FALSE, @@ -217,12 +228,14 @@ 'c' => [ 'd' => 'd', 'e' => FALSE, + 'empty' => [], ], ], FALSE, ], - // Empty arrays should not be ignored. + // Add an empty array at top, and remove at lower level. Should still + // match. [ $base, [ @@ -234,9 +247,9 @@ 'd' => TRUE, 'e' => FALSE, ], - 'empty_array' => [], + 'empty_two' => [], ], - FALSE, + TRUE, ], ]; }