From b901b84e97197f03f89b0ba34674fb1ac08fdb33 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Mon, 24 Jul 2017 15:12:42 +0530 Subject: [PATCH] add unit test --- CRM/Contact/BAO/Group.php | 2 +- tests/phpunit/CRM/Contact/BAO/GroupTest.php | 52 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/BAO/Group.php b/CRM/Contact/BAO/Group.php index 1a7b71e40d..397c8b2554 100644 --- a/CRM/Contact/BAO/Group.php +++ b/CRM/Contact/BAO/Group.php @@ -1073,7 +1073,7 @@ FROM civicrm_group WHERE id IN $groupIdString "; if ($parents) { - // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). We just grab and match on 1st parent. + // group can have > 1 parent so parents may be comma separated list (eg. '1,2,5'). $parentArray = explode(',', $parents); $parent = self::filterActiveGroups($parentArray); $args[2] = array($parent, 'Integer'); diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index 93dde59a61..6d9de6e030 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -72,6 +72,58 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { ); } + /** + * Test case to ensure child group is present in the hierarchy + * if it has multiple parent groups and not all are disabled. + */ + public function testGroupHirearchy() { + // Use-case : + // 1. Create two parent group A and B and disable B + // 2. Create a child group C + // 3. Ensure that Group C is present in the group hierarchy + $params = array( + 'name' => uniqid(), + 'title' => 'Parent Group A', + 'description' => 'Parent Group One', + 'visibility' => 'User and User Admin Only', + 'is_active' => 1, + ); + $group1 = CRM_Contact_BAO_Group::create($params); + + $params = array_merge($params, array( + 'name' => uniqid(), + 'title' => 'Parent Group B', + 'description' => 'Parent Group Two', + 'is_active' => 0, // disable + )); + $group2 = CRM_Contact_BAO_Group::create($params); + + $params = array_merge($params, array( + 'name' => uniqid(), + 'title' => 'Child Group C', + 'description' => 'Child Group C', + 'parents' => array( + $group1->id => 1, + $group2->id => 1, + ), + )); + $group3 = CRM_Contact_BAO_Group::create($params); + + $params = array( + $group1->id => 1, + $group3->id => 1, + ); + $groupsHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($params, NULL, '  ', TRUE); + // check if child group is present in the tree with formatted group title prepended with spacer '  ' + $this->assertEquals('  Child Group C', $groupsHierarchy[$group3->id]); + + // Disable parent group A and ensure that child group C is not present as both of its parent groups are disabled + $group1->is_active = 0; + $group1->save(); + $groupsHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($params, NULL, '  ', TRUE); + $this->assertFalse(array_key_exists($group3->id, $groupsHierarchy)); + } + /** * Test adding a smart group. */ -- 2.25.1