From 02e028a05a6743b18b8d4a2cd617713d25dc1f09 Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Wed, 24 Aug 2016 12:24:46 -0500 Subject: [PATCH] Fix CRM-19240 --- CRM/Contact/BAO/Relationship.php | 36 +++++- .../CRM/Contact/BAO/RelationshipTest.php | 116 ++++++++++++++++++ 2 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/BAO/RelationshipTest.php diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 26ca09eb47..0af25f39fe 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -595,9 +595,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { } if ($biDirectional) { - // lets clean up the data and eliminate all duplicate values - // (i.e. the relationship is bi-directional) - $relationshipType = array_unique($relationshipType); + $relationshipType = self::removeRelationshipTypeDuplicates($relationshipType, $contactSuffix); } // sort the relationshipType in ascending order CRM-7736 @@ -605,6 +603,38 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { return $relationshipType; } + /** + * Given a list of relationship types, return the list with duplicate types + * removed, being careful to retain only the duplicate which matches the given + * 'a_b' or 'b_a' suffix. + * + * @param array $relationshipTypeList A list of relationship types, in the format + * returned by self::getContactRelationshipType(). + * @param string $suffix Either 'a_b' or 'b_a'; defaults to 'a_b' + * + * @return array The modified value of $relationshipType + */ + public static function removeRelationshipTypeDuplicates($relationshipTypeList, $suffix = NULL) { + if (empty($suffix)) { + $suffix = 'a_b'; + } + + // Find those labels which are listed more than once. + $duplicateValues = array_diff_assoc($relationshipTypeList, array_unique($relationshipTypeList)); + + // For each duplicate label, find its keys, and remove from $relationshipType + // the key which does not match $suffix. + foreach ($duplicateValues as $value) { + $keys = array_keys($relationshipTypeList, $value); + foreach ($keys as $key) { + if (substr($key, -3) != $suffix) { + unset($relationshipTypeList[$key]); + } + } + } + return $relationshipTypeList; + } + /** * Delete current employer relationship. * diff --git a/tests/phpunit/CRM/Contact/BAO/RelationshipTest.php b/tests/phpunit/CRM/Contact/BAO/RelationshipTest.php new file mode 100644 index 0000000000..52288f3ae5 --- /dev/null +++ b/tests/phpunit/CRM/Contact/BAO/RelationshipTest.php @@ -0,0 +1,116 @@ +assertEquals($expected, $result, "Failure on set '$description'"); + } + + public function getRelationshipTypeDuplicates() { + $relationshipTypeList = array( + '1_a_b' => 'duplicate one', + '1_b_a' => 'duplicate one', + '2_a_b' => 'two a', + '2_b_a' => 'two b', + ); + $data = array( + array( + $relationshipTypeList, + 'a_b', + array( + '1_a_b' => 'duplicate one', + '2_a_b' => 'two a', + '2_b_a' => 'two b', + ), + 'With suffix a_b', + ), + array( + $relationshipTypeList, + 'b_a', + array( + '1_b_a' => 'duplicate one', + '2_a_b' => 'two a', + '2_b_a' => 'two b', + ), + 'With suffix b_a', + ), + array( + $relationshipTypeList, + NULL, + array( + '1_a_b' => 'duplicate one', + '2_a_b' => 'two a', + '2_b_a' => 'two b', + ), + 'With suffix NULL', + ), + array( + $relationshipTypeList, + NULL, + array( + '1_a_b' => 'duplicate one', + '2_a_b' => 'two a', + '2_b_a' => 'two b', + ), + 'With suffix "" (empty string)', + ), + ); + return $data; + } + +} -- 2.25.1