assertNotNull($gc->id); } //cleanup foreach ($groupContacts as $gc) { $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact'); } } /** * Test case for getGroupId( ) */ public function testGetGroupId() { //creates a test groupContact object //force group_id to 1 so we can compare $groupContact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact'); //check the group contact id is not null $this->assertNotNull($groupContact->id); $groupId = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_group'); $this->assertEquals($groupContact->group_id, $groupId, 'Check for group_id'); //cleanup $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact'); } /** * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too. */ public function testContactSearchByParentGroup() { // create a parent group // TODO: This is not an API test!! $groupParams1 = array( 'title' => 'Parent Group', 'description' => 'Parent Group', 'visibility' => 'User and User Admin Only', 'parents' => '', 'is_active' => 1, ); $parentGroup = CRM_Contact_BAO_Group::create($groupParams1); // create a child group $groupParams2 = array( 'title' => 'Child Group', 'description' => 'Child Group', 'visibility' => 'User and User Admin Only', 'parents' => $parentGroup->id, 'is_active' => 1, ); $childGroup = CRM_Contact_BAO_Group::create($groupParams2); // Create a contact within parent group $parentContactParams = array( 'first_name' => 'Parent1 Fname', 'last_name' => 'Parent1 Lname', 'group' => array($parentGroup->id => 1), ); $parentContact = Contact::createIndividual($parentContactParams); // create a contact within child dgroup $childContactParams = array( 'first_name' => 'Child1 Fname', 'last_name' => 'Child2 Lname', 'group' => array($childGroup->id => 1), ); $childContact = Contact::createIndividual($childContactParams); // Check if searching by parent group returns both parent and child group contacts $searchParams = array( 'group' => array($parentGroup->id => 1), 'version' => 3, ); $result = civicrm_api('contact', 'get', $searchParams); $validContactIds = array($parentContact, $childContact); $resultContactIds = array(); foreach ($result['values'] as $k => $v) { $resultContactIds[] = $v['contact_id']; } $this->assertEquals(2, count($resultContactIds), 'Check the count of returned values'); $this->assertEquals(array(), array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array'); // Check if searching by child group returns just child group contacts $searchParams = array( 'group' => array($childGroup->id => 1), 'version' => 3, ); $result = civicrm_api('contact', 'get', $searchParams); $validChildContactIds = array($childContact); $resultChildContactIds = array(); foreach ($result['values'] as $k => $v) { $resultChildContactIds[] = $v['contact_id']; } $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values'); $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array'); } }