add unit test
authordeb.monish <monish.deb@jmaconsulting.biz>
Mon, 24 Jul 2017 09:42:42 +0000 (15:12 +0530)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Tue, 5 Sep 2017 20:22:33 +0000 (01:52 +0530)
CRM/Contact/BAO/Group.php
tests/phpunit/CRM/Contact/BAO/GroupTest.php

index 1a7b71e40db006984623a0256812b02593a24be4..397c8b255414e48c147094f4072cab3028ebd81f 100644 (file)
@@ -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');
index 93dde59a6123cad4cde22640109531158d230d34..6d9de6e030d4a1b71f3844df462d59668e20e299 100644 (file)
@@ -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, '&nbsp;&nbsp;', TRUE);
+    // check if child group is present in the tree with formatted group title prepended with spacer '&nbsp;&nbsp;'
+    $this->assertEquals('&nbsp;&nbsp;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, '&nbsp;&nbsp;', TRUE);
+    $this->assertFalse(array_key_exists($group3->id, $groupsHierarchy));
+  }
+
   /**
    * Test adding a smart group.
    */