From c3137c08168665e27a18cd6a5f41ec3b5375b205 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 30 Jun 2016 18:54:02 +1200 Subject: [PATCH] CRM-17213 add tests for group search This test ensures we don't break the group search while fixing CRM-17213 --- CRM/Contact/BAO/Group.php | 2 +- CRM/Contact/BAO/Query.php | 6 ++--- .../CRM/Contact/BAO/GroupContactTest.php | 11 +++----- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 26 +++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 19 ++++++++++++++ 5 files changed, 53 insertions(+), 11 deletions(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index df0ae58ae8..7125c2a139 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -346,7 +346,7 @@ class CRM_Contact_BAO_Group extends CRM_Contact_DAO_Group { * @return CRM_Contact_BAO_Group|NULL * The new group BAO (if created) */ - public static function &create(&$params) { + public static function create(&$params) { if (!empty($params['id'])) { CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params); diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 498c2dcfe7..59ec38668d 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -2878,11 +2878,11 @@ class CRM_Contact_BAO_Query { } /** - * Where / qill clause for groups + * Where / qill clause for groups. * * @param $values */ - public function group(&$values) { + public function group($values) { list($name, $op, $value, $grouping, $wildcard) = $values; // If the $value is in OK (operator as key) array format we need to extract the key as operator and value first @@ -2910,7 +2910,7 @@ class CRM_Contact_BAO_Query { } $groupIds = NULL; - $names = array(); + $isSmart = FALSE; $isNotOp = ($op == 'NOT IN' || $op == '!='); diff --git a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php index 719948faeb..01a90fad3a 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php @@ -96,7 +96,6 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { */ public function testContactSearchByParentGroup() { // create a parent group - // TODO: This is not an API test!! $groupParams1 = array( 'title' => 'Parent Group', 'description' => 'Parent Group', @@ -104,7 +103,7 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { 'parents' => '', 'is_active' => 1, ); - $parentGroup = CRM_Contact_BAO_Group::create($groupParams1); + $parentGroup = $this->callAPISuccess('Group', 'create', $groupParams1); // create a child group $groupParams2 = array( @@ -114,7 +113,7 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { 'parents' => $parentGroup->id, 'is_active' => 1, ); - $childGroup = CRM_Contact_BAO_Group::create($groupParams2); + $childGroup = $this->callAPISuccess('Group', 'create', $groupParams2); // Create a contact within parent group $parentContactParams = array( @@ -135,9 +134,8 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { // Check if searching by parent group returns both parent and child group contacts $searchParams = array( 'group' => $parentGroup->id, - 'version' => 3, ); - $result = civicrm_api('contact', 'get', $searchParams); + $result = $this->callAPISuccess('contact', 'get', $searchParams); $validContactIds = array($parentContact, $childContact); $resultContactIds = array(); foreach ($result['values'] as $k => $v) { @@ -149,9 +147,8 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { // Check if searching by child group returns just child group contacts $searchParams = array( 'group' => $childGroup->id, - 'version' => 3, ); - $result = civicrm_api('contact', 'get', $searchParams); + $result = $this->callAPISuccess('contact', 'get', $searchParams); $validChildContactIds = array($childContact); $resultChildContactIds = array(); foreach ($result['values'] as $k => $v) { diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index e81c3427aa..b0233c1c5b 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -275,4 +275,30 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { } + /** + * Test the group contact clause does not contain an OR. + * + * The search should return 3 contacts - 2 households in the smart group of + * Contact Type = Household and one Individual hard-added to it. The + * Household that meets both criteria should be returned once. + */ + public function testGroupClause() { + $this->householdCreate(); + $householdID = $this->householdCreate(); + $individualID = $this->individualCreate(); + $groupID = $this->smartGroupCreate(); + $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $individualID, 'status' => 'Added')); + $this->callAPISuccess('GroupContact', 'create', array('group_id' => $groupID, 'contact_id' => $householdID, 'status' => 'Added')); + + $query = new CRM_Contact_BAO_Query( + array(array('group', 'IN', array($groupID), 0, 0)), + array('contact_id') + ); + + $sql = $query->query(); + $queryString = implode(' ', $sql); + $dao = CRM_Core_DAO::executeQuery($queryString); + $this->assertEquals(3, $dao->N); + } + } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index a62f04531d..577be0e07a 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1788,6 +1788,25 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { return $result['id']; } + /** + * Create a smart group. + * + * By default it will be a group of households. + * + * @param array $smartGroupParams + * @param array $groupParams + * @return int + */ + public function smartGroupCreate($smartGroupParams = array(), $groupParams = array()) { + $smartGroupParams = array_merge(array( + 'formValues' => array('contact_type' => array('IN' => array('Household'))), + ), + $smartGroupParams); + $savedSearch = CRM_Contact_BAO_SavedSearch::create($smartGroupParams); + + $groupParams['saved_search_id'] = $savedSearch->id; + return $this->groupCreate($groupParams); + } /** * Function to add a Group. -- 2.25.1