Merge branch '4.7.21-rc' into master
[civicrm-core.git] / tests / phpunit / CRM / Contact / Page / AjaxTest.php
index 6b9873491458fec031fc18b238996d64255b6238..acc0eb8cfee4ea0a627c75d492bd775afda7dce1 100644 (file)
@@ -210,4 +210,138 @@ class CRM_Contact_Page_AjaxTest extends CiviUnitTestCase {
     $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
   }
 
+  /**
+   * CRM-20621 : Test to check usage count of Tag tree
+   */
+  public function testGetTagTree() {
+    $contacts = array();
+    // create three contacts
+    for ($i = 0; $i < 3; $i++) {
+      $contacts[] = $this->individualCreate();
+    }
+
+    // Create Tag called as 'Parent Tag'
+    $parentTag = $this->tagCreate(array(
+      'name' => 'Parent Tag',
+      'used_for' => 'civicrm_contact',
+    ));
+    //assign first contact to parent tag
+    $params = array(
+      'entity_id' => $contacts[0],
+      'entity_table' => 'civicrm_contact',
+      'tag_id' => $parentTag['id'],
+    );
+    // TODO: EntityTag.create API is not working
+    CRM_Core_BAO_EntityTag::add($params);
+
+    // Create child Tag of $parentTag
+    $childTag1 = $this->tagCreate(array(
+      'name' => 'Child Tag Level 1',
+      'parent_id' => $parentTag['id'],
+      'used_for' => 'civicrm_contact',
+    ));
+    //assign contact to this level 1 child tag
+    $params = array(
+      'entity_id' => $contacts[1],
+      'entity_table' => 'civicrm_contact',
+      'tag_id' => $childTag1['id'],
+    );
+    CRM_Core_BAO_EntityTag::add($params);
+
+    // Create child Tag of $childTag1
+    $childTag2 = $this->tagCreate(array(
+      'name' => 'Child Tag Level 2',
+      'parent_id' => $childTag1['id'],
+      'used_for' => 'civicrm_contact',
+    ));
+    //assign contact to this level 2 child tag
+    $params = array(
+      'entity_id' => $contacts[2],
+      'entity_table' => 'civicrm_contact',
+      'tag_id' => $childTag2['id'],
+    );
+    CRM_Core_BAO_EntityTag::add($params);
+
+    // CASE I : check the usage count of parent tag which need to be 1
+    //  as the one contact added
+    $_REQUEST['is_unit_test'] = TRUE;
+    $parentTagTreeResult = CRM_Admin_Page_AJAX::getTagTree();
+    foreach ($parentTagTreeResult as $result) {
+      if ($result['id'] == $parentTag['id']) {
+        $this->assertEquals(1, $result['data']['usages']);
+      }
+    }
+
+    // CASE 2 : check the usage count of level 1 child tag, which needs to be 1
+    //  as it should include the count of added one contact
+    $_GET['parent_id'] = $parentTag['id'];
+    $childTagTree = CRM_Admin_Page_AJAX::getTagTree();
+    $this->assertEquals(1, $childTagTree[0]['data']['usages']);
+
+    // CASE 2 : check the usage count of child tag at level 2
+    //which needs to be 1 as it has no child tag
+    $_GET['parent_id'] = $childTag1['id'];
+    $childTagTree = CRM_Admin_Page_AJAX::getTagTree();
+    $this->assertEquals(1, $childTagTree[0]['data']['usages']);
+
+    //cleanup
+    foreach ($contacts as $id) {
+      $this->callAPISuccess('Contact', 'delete', array('id' => $id));
+    }
+    $this->callAPISuccess('Tag', 'delete', array('id' => $childTag2['id']));
+    $this->callAPISuccess('Tag', 'delete', array('id' => $childTag1['id']));
+    $this->callAPISuccess('Tag', 'delete', array('id' => $parentTag['id']));
+  }
+
+  /**
+   * Test to check contact reference field
+   */
+  public function testContactReference() {
+    //create group
+    $groupId1 = $this->groupCreate();
+    $groupId2 = $this->groupCreate(array(
+      'name' => 'Test Group 2',
+      'domain_id' => 1,
+      'title' => 'New Test Group2 Created',
+      'description' => 'New Test Group2 Created',
+      'is_active' => 1,
+      'visibility' => 'User and User Admin Only',
+    ));
+
+    $contactIds = array();
+    foreach (array($groupId1, $groupId2) as $groupId) {
+      $this->groupContactCreate($groupId);
+      $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group::getGroupContacts($groupId));
+    }
+    $contactIds = CRM_Utils_Array::collect('contact_id', $contactIds);
+
+    // create custom group with contact reference field
+    $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
+    $params = array(
+      'custom_group_id' => $customGroup['id'],
+      'name' => 'Worker_Lookup',
+      'label' => 'Worker Lookup',
+      // limit this field to two groups created above
+      'filter' => "action=lookup&group={$groupId1},{$groupId2}",
+      'html_type' => 'Autocomplete-Select',
+      'data_type' => 'ContactReference',
+      'weight' => 4,
+      'is_searchable' => 1,
+      'is_active' => 1,
+    );
+    $customField = $this->callAPISuccess('custom_field', 'create', $params);
+
+    $_GET = array(
+      'id' => $customField['id'],
+      'is_unit_test' => TRUE,
+    );
+    $contactList = CRM_Contact_Page_AJAX::contactReference();
+    $contactList = CRM_Utils_Array::collect('id', $contactList);
+
+    //assert each returned contact id to be present in group contact
+    foreach ($contactList as $contactId) {
+      $this->assertTrue(in_array($contactId, $contactIds));
+    }
+  }
+
 }