* @param int $count get the no of relationships
* $param int $relationshipId relationship id
* @param string $direction the direction we are interested in a_b or b_a
+ * @param array $params array of extra values including relationship_type_id per api spec
*
* return string the query for this diretion
* @static
* @access public
*/
- static function makeURLClause($contactId, $status, $numRelationship, $count, $relationshipId, $direction) {
+ static function makeURLClause($contactId, $status, $numRelationship, $count, $relationshipId, $direction, $params = array()) {
$select = $from = $where = '';
$select = '( ';
// CRM-6181
$where .= ' AND civicrm_contact.is_deleted = 0';
-
+ if(!empty($params['relationship_type_id'])) {
+ if(is_array($params['relationship_type_id'])) {
+ // get our special function from DAO to deal with this
+ // $where .= $this->createSQLFilter('relationship_type_id', $params['relationship_type_id'], 'Integer');
+ }
+ else {
+ $where .= ' AND relationship_type_id = ' . CRM_Utils_Type::escape($params['relationship_type_id'], 'Positive');
+ }
+ }
if ($direction == 'a_b') {
$where .= ' ) UNION ';
}
* $param array $links the list of links to display
* $param int $permissionMask the permission mask to be applied for the actions
* $param boolean $permissionedContact to return only permissioned Contact
- *
+ * $param array $params array of variables consistent with filters supported by the api
* return array $values relationship records
* @static
* @access public
$status = 0, $numRelationship = 0,
$count = 0, $relationshipId = 0,
$links = NULL, $permissionMask = NULL,
- $permissionedContact = FALSE
+ $permissionedContact = FALSE,
+ $params = array()
) {
$values = array();
if (!$contactId && !$relationshipId) {
}
list($select1, $from1, $where1) = self::makeURLClause($contactId, $status, $numRelationship,
- $count, $relationshipId, 'a_b'
+ $count, $relationshipId, 'a_b', $params
);
list($select2, $from2, $where2) = self::makeURLClause($contactId, $status, $numRelationship,
- $count, $relationshipId, 'b_a'
+ $count, $relationshipId, 'b_a', $params
);
$order = $limit = '';
}
}
- function membershipTypeCreate($contactID, $contributionTypeID = 1, $version = 3) {
- require_once 'CRM/Member/PseudoConstant.php';
+ function membershipTypeCreate($params = array()) {
CRM_Member_PseudoConstant::flush('membershipType');
CRM_Core_Config::clearDBCache();
- $params = array(
+ $params = array_merge(array(
'name' => 'General',
'duration_unit' => 'year',
'duration_interval' => 1,
'period_type' => 'rolling',
- 'member_of_contact_id' => $contactID,
+ 'member_of_contact_id' => 1,
'domain_id' => 1,
- // FIXME: I know it's 1, cause it was loaded directly to the db.
- // FIXME: when we load all the data, we'll need to address this to
- // FIXME: avoid hunting numbers around.
- 'financial_type_id' => $contributionTypeID,
+ 'financial_type_id' => 1,
'is_active' => 1,
- 'version' => $version,
'sequential' => 1,
'visibility' => 1,
- );
+ ), $params);
+
+ $result = $this->callAPISuccess('MembershipType', 'Create', $params);
- $result = civicrm_api('MembershipType', 'Create', $params);
- require_once 'CRM/Member/PseudoConstant.php';
CRM_Member_PseudoConstant::flush('membershipType');
CRM_Utils_Cache::singleton()->flush();
- if (CRM_Utils_Array::value('is_error', $result) ||
- (!CRM_Utils_Array::value('id', $result) && !CRM_Utils_Array::value('id', $result['values'][0]))
- ) {
- throw new Exception('Could not create membership type, with message: ' . CRM_Utils_Array::value('error_message', $result));
- }
return $result['id'];
}
return;
}
- function relationshipTypeCreate($params = NULL) {
- if (is_null($params)) {
- $params = array(
+ function relationshipTypeCreate($params = array()) {
+ $params = array_merge(array(
'name_a_b' => 'Relation 1 for relationship type create',
'name_b_a' => 'Relation 2 for relationship type create',
'contact_type_a' => 'Individual',
'contact_type_b' => 'Organization',
'is_reserved' => 1,
'is_active' => 1,
- );
- }
+ ),
+ $params
+ );
$result = $this->callAPISuccess('relationship_type', 'create', $params);
CRM_Core_PseudoConstant::flush('relationshipType');
}
function tearDown() {
- $this->quickCleanup(array('civicrm_relationship'), TRUE);
- $this->relationshipTypeDelete($this->_relTypeID);
$this->contactDelete($this->_cId_a);
$this->contactDelete($this->_cId_b);
+ $this->contactDelete($this->_cId_b2);
+ $this->quickCleanup(array('civicrm_relationship'), TRUE);
+ $this->relationshipTypeDelete($this->_relTypeID);
}
///////////////// civicrm_relationship_create methods
'debug' => 1,
);
$result = $this->callAPISuccess('relationship', 'create', $params);
- $this->assertAPISuccess($result, 'in line ' . __LINE__);
$result = $this->callAPISuccess('relationship', 'get', $params);
$params['id'] = $relationship['id'];
$result = $this->callAPISuccess('relationship', 'delete', $params);
);
$result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__);
- $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+ $this->assertNotNull($result['id']);
$relationParams = array(
'id' => $result['id'],
);
if ($key == 'note') {
continue;
}
- $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
+ $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE));
}
$params['id'] = $result['id'];
$this->callAPISuccess('relationship', 'delete', $params);
);
$result = $this->callAPISuccess('relationship', 'create', $params);
- $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+ $this->assertNotNull($result['id']);
$relationParams = array(
'id' => $result['id'],
);
'is_active' => 1,
);
- $result = $this->callAPIFailure('relationship', 'delete', $params);
- $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
+ $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
}
/**
'relationship_type_id' => 'Breaking Relationship',
);
- $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id', 'in line ' . __LINE__);
+ $result = $this->callAPIFailure('relationship', 'delete', $params, 'Mandatory key(s) missing from params array: id');
$params['id'] = "Invalid";
- $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID', 'in line ' . __LINE__);
+ $result = $this->callAPIFailure('relationship', 'delete', $params, 'Invalid value for relationship ID');
}
/**
* check with empty array
*/
function testRelationshipUpdateEmpty() {
- $result = $this->callAPIFailure('relationship', 'create', array());
- $this->assertEquals('Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id', $result['error_message'], 'In line ' . __LINE__);
+ $result = $this->callAPIFailure('relationship', 'create', array(),
+ 'Mandatory key(s) missing from params array: contact_id_a, contact_id_b, relationship_type_id');
}
/**
$result = $this->callAPISuccess('relationship', 'create', $relParams);
- $this->assertNotNull($result['id'], 'In line ' . __LINE__);
+ $this->assertNotNull($result['id']);
$this->_relationID = $result['id'];
$params = array(
'is_active' => 0,
);
- $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists', 'In line ' . __LINE__);
+ $result = $this->callAPIFailure('relationship', 'create', $params, 'Relationship already exists');
//delete created relationship
$params = array(
'contact_id' => $this->_cId_b,
);
$result = $this->callAPISuccess('relationship', 'get', $params);
- $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
+ $this->assertEquals($result['count'], 1);
$params = array(
'contact_id_a' => $this->_cId_a,
);
$result = $this->callAPISuccess('relationship', 'get', $params);
- $this->assertEquals($result['count'], 1, 'in line ' . __LINE__);
+ $this->assertEquals($result['count'], 1);
// contact_id_a is wrong so should be no matches
$params = array(
'contact_id_a' => $this->_cId_b,
);
$result = $this->callAPISuccess('relationship', 'get', $params);
- $this->assertEquals($result['count'], 0, 'in line ' . __LINE__);
+ $this->assertEquals($result['count'], 0);
}
/**
'contact_type_a' => 'Individual',
'contact_type_b' => 'Organization',
);
- $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
- $this->assertEquals('id is not a valid integer', $result['error_message'], 'in line ' . __LINE__);
+ $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
+ 'id is not a valid integer');
}
/**
);
$result = $this->callAPISuccess('relationship', 'get', $contacts);
- $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__);
+ $this->assertGreaterThan(0, $result['count']);
$params = array(
'id' => $relationship['id'],
);
$result = $this->callAPISuccess('relationship', 'delete', $params);
$this->relationshipTypeDelete($this->_relTypeID);
}
+
+ /**
+ * Checks that passing in 'contact_id' + a relationship type
+ * will filter by relationship type (relationships go in both directions)
+ * as relationship api does a reciprocal check if contact_id provided
+ *
+ * We should get 1 result without or with correct relationship type id & 0 with
+ * an incorrect one
+ */
+ function testGetRelationshipByTypeReciprocal() {
+ $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+ $result = $this->callAPISuccess($this->_entity, 'get', array(
+ 'contact_id' => $this->_cId_a,
+ 'relationship_type_id' => $this->_relTypeID,
+ ));
+ $this->assertEquals(1, $result['count']);
+ $result = $this->callAPISuccess($this->_entity, 'get', array(
+ 'contact_id' => $this->_cId_a,
+ 'relationship_type_id' => $this->_relTypeID + 1,
+ ));
+ $this->assertEquals(0, $result['count']);
+ $this->callAPISuccess($this->_entity, 'delete', array('id' => $created['id']));
+ }
+
+ /**
+ * Checks that passing in 'contact_id_b' + a relationship type
+ * will filter by relationship type for contact b
+ *
+ * We should get 1 result without or with correct relationship type id & 0 with
+ * an incorrect one
+ */
+ function testGetRelationshipByTypeDAO() {
+ $this->ids['relationship'] = $this->callAPISuccess($this->_entity, 'create', array('format.only_id' => TRUE,) + $this->_params);
+ $result = $this->callAPISuccess($this->_entity, 'getcount', array(
+ 'contact_id_a' => $this->_cId_a,),
+ 1);
+ $result = $this->callAPISuccess($this->_entity, 'get', array(
+ 'contact_id_a' => $this->_cId_a,
+ 'relationship_type_id' => $this->_relTypeID,
+ ));
+ $this->assertEquals(1, $result['count']);
+ $result = $this->callAPISuccess($this->_entity, 'get', array(
+ 'contact_id_a' => $this->_cId_a,
+ 'relationship_type_id' => $this->_relTypeID + 1,
+ ));
+ $this->assertEquals(0, $result['count']);
+ }
+
+ /**
+ * Checks that passing in 'contact_id_b' + a relationship type
+ * will filter by relationship type for contact b
+ *
+ * We should get 1 result without or with correct relationship type id & 0 with
+ * an incorrect one
+ */
+ function testGetRelationshipByTypeArrayDAO() {
+ $created = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+ $org3 = $this->organizationCreate();
+ $relType2 = 5; // lets just assume built in ones aren't being messed with!
+ $relType3 = 6; // lets just assume built in ones aren't being messed with!
+
+ //relationshp 2
+ $this->callAPISuccess($this->_entity, 'create',
+ array_merge($this->_params, array(
+ 'relationship_type_id' => $relType2,
+ 'contact_id_b' => $this->_cId_b2))
+ );
+
+ //relationshp 3
+ $this->callAPISuccess($this->_entity, 'create',
+ array_merge($this->_params, array(
+ 'relationship_type_id' => $relType3,
+ 'contact_id_b' => $org3))
+ );
+
+ $result = $this->callAPISuccess($this->_entity, 'get', array(
+ 'contact_id_a' => $this->_cId_a,
+ 'relationship_type_id' => array('IN' => array($this->_relTypeID, $relType3)),
+ ));
+
+ $this->assertEquals(2, $result['count']);
+ foreach ($result['values'] as $key => $value) {
+ $this->assertTrue(in_array($value['relationship_type_id'], array($this->_relTypeID, $relType3)));
+ }
+ }
}