From 5ad36be5d5e7b256dac05414d31fa3d78b285e97 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sun, 28 Jan 2018 10:54:13 +1100 Subject: [PATCH] Minor fixes and add unit tests --- CRM/Contact/BAO/Query.php | 16 +++--- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 57 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index c52ab366d1..cc1f614739 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4050,16 +4050,20 @@ WHERE $smartGroupClause } } - $rTypeValues = $relTypes = array(); + $rTypeValues = $relTypes = $relTypesIds = array(); if (!empty($relationType)) { $relationType[2] = (array) $relationType[2]; foreach ($relationType[2] as $relType) { $rel = explode('_', $relType); self::$_relType .= $rel[1]; $params = array('id' => $rel[0]); - $relTypes[] = $rel[0]; $typeValues = array(); - $rTypeValues[] = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues); + $rTypeValue = CRM_Contact_BAO_RelationshipType::retrieve($params, $typeValues); + if (!empty($rTypeValue)) { + $rTypeValues[] = $rTypeValue; + $relTypesIds[] = $rel[0]; + $relTypes[] = $relType; + } } } if (!empty($rTypeValues)) { @@ -4091,7 +4095,7 @@ WHERE $smartGroupClause if ($nameClause || !$targetGroup) { if (!empty($relationType)) { $relQill = ''; - foreach ($relationType[2] as $rel) { + foreach ($relTypes as $rel) { if (!empty($relQill)) { $relQill .= ' OR '; } @@ -4131,7 +4135,7 @@ WHERE $smartGroupClause } if (!empty($relationType)) { $relQill = ''; - foreach ($relationType[2] as $rel) { + foreach ($relTypes as $rel) { if (!empty($relQill)) { $relQill .= ' OR '; } @@ -4188,7 +4192,7 @@ civicrm_relationship.is_permission_a_b = 0 $this->addRelationshipDateClauses($grouping, $where); $this->addRelationshipActivePeriodClauses($grouping, $where); if (!empty($relTypes)) { - $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypes) . ')'; + $where[$grouping][] = 'civicrm_relationship.relationship_type_id IN (' . implode(',', $relTypesIds) . ')'; } $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1; $this->_useDistinct = TRUE; diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 6c8061d240..c9dc80bebc 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -369,6 +369,63 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { } + /** + * Test Relationship Clause + */ + public function testRelationshipClause() { + $today = date('Ymd'); + $where1 = "WHERE ( ( +civicrm_relationship.is_active = 1 AND +( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND +( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} ) +) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8) ) AND (contact_a.is_deleted = 0)"; + $where2 = "WHERE ( ( +civicrm_relationship.is_active = 1 AND +( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND +( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} ) +) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8,10) ) AND (contact_a.is_deleted = 0)"; + // Test Traditional single select format + $params1 = array(array('relation_type_id', '=', '8_a_b', 0, 0)); + $query1 = new CRM_Contact_BAO_Query( + $params1, array('contact_id'), + NULL, TRUE, FALSE, 1, + TRUE, + TRUE, FALSE + ); + $sql1 = $query1->query(FALSE); + $this->assertEquals($where1, $sql1[2]); + // Test single relationship type selected in multiple select. + $params2 = array(array('relation_type_id', 'IN', array('8_a_b'), 0, 0)); + $query2 = new CRM_Contact_BAO_Query( + $params2, array('contact_id'), + NULL, TRUE, FALSE, 1, + TRUE, + TRUE, FALSE + ); + $sql2 = $query2->query(FALSE); + $this->assertEquals($where1, $sql2[2]); + // Test multiple relationship types selected. + $params3 = array(array('relation_type_id', 'IN', array('8_a_b', '10_a_b'), 0, 0)); + $query3 = new CRM_Contact_BAO_Query( + $params3, array('contact_id'), + NULL, TRUE, FALSE, 1, + TRUE, + TRUE, FALSE + ); + $sql3 = $query3->query(FALSE); + $this->assertEquals($where2, $sql3[2]); + // Test Multiple Relationship type selected where one doesn't actually exist. + $params4 = array(array('relation_type_id', 'IN', array('8_a_b', '10_a_b', '14_a_b'), 0, 0)); + $query4 = new CRM_Contact_BAO_Query( + $params4, array('contact_id'), + NULL, TRUE, FALSE, 1, + TRUE, + TRUE, FALSE + ); + $sql4 = $query4->query(FALSE); + $this->assertEquals($where2, $sql4[2]); + } + /** * Test the group contact clause does not contain an OR. * -- 2.25.1