From: colemanw Date: Thu, 10 Aug 2023 00:51:32 +0000 (-0400) Subject: Fix dev/core#4483 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=da9bfc7ec8fe4180551c97515de019b37d000e70;p=civicrm-core.git Fix dev/core#4483 --- diff --git a/Civi/Api4/Utils/FormattingUtil.php b/Civi/Api4/Utils/FormattingUtil.php index 9498079c3f..ca0a681260 100644 --- a/Civi/Api4/Utils/FormattingUtil.php +++ b/Civi/Api4/Utils/FormattingUtil.php @@ -446,7 +446,7 @@ class FormattingUtil { * @return array */ public static function filterByPath(array $values, string $fieldPath, string $fieldName): array { - $prefix = substr($fieldPath, 0, strpos($fieldPath, $fieldName)); + $prefix = substr($fieldPath, 0, strrpos($fieldPath, $fieldName)); return \CRM_Utils_Array::filterByPrefix($values, $prefix); } diff --git a/tests/phpunit/api/v4/Utils/FormattingUtilTest.php b/tests/phpunit/api/v4/Utils/FormattingUtilTest.php new file mode 100644 index 0000000000..1f19ef493d --- /dev/null +++ b/tests/phpunit/api/v4/Utils/FormattingUtilTest.php @@ -0,0 +1,42 @@ +assertEquals($expectedValues, FormattingUtil::filterByPath($originalValues, $fieldPath, $fieldName)); + } + + public function getFilterByPathExamples(): array { + $originalValueSets = [ + [ + 'id' => 'a', + 'name' => 'a_name', + 'id_foo.id' => 'b', + 'id_foo.name' => 'b_name', + 'id.id_foo.id' => 'c', + 'id.id_foo.name' => 'c_name', + ], + ]; + return [ + ['id', 'id', $originalValueSets[0], $originalValueSets[0]], + ['id_foo.id', 'id', $originalValueSets[0], ['id' => 'b', 'name' => 'b_name']], + ['id.id_foo.id', 'id', $originalValueSets[0], ['id' => 'c', 'name' => 'c_name']], + ]; + } + +}