_groupID = $this->groupCreate(); $config = CRM_Core_Config::singleton(); $config->userPermissionClass->permissions = array(); } /** * Clean up after test. */ public function tearDown() { $this->groupDelete($this->_groupID); CRM_Utils_Hook::singleton()->reset(); $config = CRM_Core_Config::singleton(); unset($config->userPermissionClass->permissions); } /** * Test missing required title parameter results in an error. */ public function testGroupCreateNoTitle() { $params = array( 'name' => 'Test Group No title ', 'domain_id' => 1, 'description' => 'New Test Group Created', 'is_active' => 1, 'visibility' => 'Public Pages', 'group_type' => array( '1' => 1, '2' => 1, ), ); $this->callAPIFailure('group', 'create', $params, 'Mandatory key(s) missing from params array: title'); } public function testGetGroupWithEmptyParams() { $group = $this->callAPISuccess('group', 'get', array()); $group = $group["values"]; $this->assertNotNull(count($group)); $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1"); $this->assertEquals($group[$this->_groupID]['is_active'], 1); $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages'); } public function testGetGroupParamsWithGroupId() { $params = array('id' => $this->_groupID); $group = $this->callAPISuccess('group', 'get', $params); foreach ($group['values'] as $v) { $this->assertEquals($v['name'], "Test Group 1"); $this->assertEquals($v['title'], 'New Test Group Created'); $this->assertEquals($v['description'], 'New Test Group Created'); $this->assertEquals($v['is_active'], 1); $this->assertEquals($v['visibility'], 'Public Pages'); } } public function testGetGroupParamsWithGroupName() { $params = array( 'name' => "Test Group 1", ); $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__); $group = $group['values']; foreach ($group as $v) { $this->assertEquals($v['id'], $this->_groupID); $this->assertEquals($v['title'], 'New Test Group Created'); $this->assertEquals($v['description'], 'New Test Group Created'); $this->assertEquals($v['is_active'], 1); $this->assertEquals($v['visibility'], 'Public Pages'); } } public function testGetGroupParamsWithReturnName() { $params = array(); $params['id'] = $this->_groupID; $params['return.name'] = 1; $group = $this->callAPISuccess('group', 'get', $params); $this->assertEquals($group['values'][$this->_groupID]['name'], "Test Group 1" ); } public function testGetGroupParamsWithGroupTitle() { $params = array(); $params['title'] = 'New Test Group Created'; $group = $this->callAPISuccess('group', 'get', $params); foreach ($group['values'] as $v) { $this->assertEquals($v['id'], $this->_groupID); $this->assertEquals($v['name'], "Test Group 1"); $this->assertEquals($v['description'], 'New Test Group Created'); $this->assertEquals($v['is_active'], 1); $this->assertEquals($v['visibility'], 'Public Pages'); } } /** * Test Group create with Group Type and Parent */ public function testGroupCreateWithTypeAndParent() { $params = array( 'name' => 'Test Group type', 'title' => 'Test Group Type', 'description' => 'Test Group with Group Type', 'is_active' => 1, //check for empty parent 'parents' => "", 'visibility' => 'Public Pages', 'group_type' => array(1, 2), ); $result = $this->callAPISuccess('Group', 'create', $params); $group = $result['values'][$result['id']]; $this->assertEquals($group['name'], "Test Group type"); $this->assertEquals($group['is_active'], 1); $this->assertEquals($group['parents'], ""); $this->assertEquals($group['group_type'], $params['group_type']); //Pass group_type param in checkbox format. $params = array_merge($params, array( 'name' => 'Test Checkbox Format', 'title' => 'Test Checkbox Format', 'group_type' => array(2 => 1), ) ); $result = $this->callAPISuccess('Group', 'create', $params); $group = $result['values'][$result['id']]; $this->assertEquals($group['name'], "Test Checkbox Format"); $this->assertEquals($group['group_type'], array_keys($params['group_type'])); //assert single value for group_type and parent $params = array_merge($params, array( 'name' => 'Test Group 2', 'title' => 'Test Group 2', 'group_type' => 2, 'parents' => $result['id'], ) ); $result = $this->callAPISuccess('Group', 'create', $params); $group = $result["values"][$result['id']]; $this->assertEquals($group['group_type'], array($params['group_type'])); $this->assertEquals($group['parents'], $params['parents']); } public function testGetNonExistingGroup() { $params = array(); $params['title'] = 'No such group Exist'; $group = $this->callAPISuccess('group', 'get', $params); $this->assertEquals(0, $group['count']); } public function testgroupdeleteParamsnoId() { $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id'); } public function testgetfields() { $description = "Demonstrate use of getfields to interrogate api."; $params = array('action' => 'create'); $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description); $this->assertEquals(1, $result['values']['is_active']['api.default']); } public function testIllegalParentsParams() { $params = array( 'title' => 'Test illegal Group', 'domain_id' => 1, 'description' => 'Testing illegal Parents params', 'is_active' => 1, 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)", ); $this->callAPIFailure('group', 'create', $params); unset($params['parents']); $this->callAPISuccess('group', 'create', $params); $group1 = $this->callAPISuccess('group', 'get', array( 'title' => 'Test illegal Group', 'parents' => array('IS NOT NULL' => 1), )); $this->assertEquals(0, $group1['count']); $params['title'] = 'Test illegal Group 2'; $params['parents'] = array(); $params['parents'][$this->_groupID] = 'test Group'; $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test"; $this->callAPIFailure('group', 'create', $params); unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]); $group2 = $this->callAPISuccess('group', 'create', $params); $this->assertEquals(count($group2['values'][$group2['id']]['parents']), 1); } /** * Test that ACLs are applied to group.get calls. */ public function testGroupGetACLs() { $this->createLoggedInUser(); CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 0); $this->hookClass->setHook('civicrm_aclGroup', array($this, 'aclGroupAllGroups')); unset(Civi::$statics['CRM_ACL_API']['group_permission']); $this->callAPISuccessGetCount('Group', array('check_permissions' => 1), 1); } /** * Implement hook to restrict to test group 1. * * @param string $type * @param int $contactID * @param string $tableName * @param array $allGroups * @param array $ids */ public function aclGroupAllGroups($type, $contactID, $tableName, $allGroups, &$ids) { $group = $this->callAPISuccess('Group', 'get', array('name' => 'Test Group 1')); $ids = array_keys($group['values']); } }