From 26dcc9eb552feb0e1a7bf47a16a5afb8af8df748 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 22 Jun 2013 15:55:16 +1200 Subject: [PATCH] Make relationship api test ENotice Compliant --- CRM/Contact/BAO/Relationship.php | 26 ++- api/v3/Relationship.php | 25 ++- .../Relationship/BetweenRelationshipType.php | 59 +++--- .../Relationship/INRelationshipType.php | 45 ++--- .../NotBetweenRelationshipType.php | 29 ++- .../Relationship/NotInRelationshipType.php | 45 ++--- .../examples/Relationship/filterIsCurrent.php | 25 +-- api/v3/examples/RelationshipCreate.php | 22 ++- api/v3/examples/RelationshipDelete.php | 19 +- api/v3/examples/RelationshipGet.php | 21 +-- tests/phpunit/api/v3/RelationshipTest.php | 171 ++++++------------ 11 files changed, 190 insertions(+), 297 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 492df4b0bb..c020ea9530 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -47,22 +47,30 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids + * per http://wiki.civicrm.org/confluence/display/CRM/Database+layer + * "we are moving away from the $ids param " * * @return object CRM_Contact_BAO_Relationship object * @access public * @static */ - static function create(&$params, &$ids) { + static function create(&$params, $ids = array()) { $valid = $invalid = $duplicate = $saved = 0; - $relationshipId = CRM_Utils_Array::value('relationship', $ids); + $relationships = array(); + $relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params)); //CRM-9015 - the hooks are called here & in add (since add doesn't call create) // but in future should be tidied per ticket - if (CRM_Utils_Array::value('relationship', $ids)) { - CRM_Utils_Hook::pre('edit', 'Relationship', $ids['relationship'], $params); + if(empty($relationshipId)){ + $hook = 'create'; + $action = CRM_Core_Action::ADD; } - else { - CRM_Utils_Hook::pre('create', 'Relationship', NULL, $params); + else{ + $hook = 'edit'; + $action = CRM_Core_Action::UPDATE; } + + CRM_Utils_Hook::pre($hook, 'Relationship', $relationshipId, $params); + if (!$relationshipId) { // creating a new relationship $dataExists = self::dataExists($params); @@ -98,6 +106,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $relationship = self::add($params, $ids, $key); $relationshipIds[] = $relationship->id; + $relationships[$relationship->id] = $relationship; $valid++; } // editing the relationship @@ -131,6 +140,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { // editing an existing relationship $relationship = self::add($params, $ids, $ids['contactTarget']); $relationshipIds[] = $relationship->id; + $relationships[$relationship->id] = $relationship; $saved++; } } @@ -157,11 +167,11 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { ), ); } + CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a, $params, $ids, $action); $title = CRM_Contact_BAO_Contact::displayName($relationship->contact_id_a) . ' (' . CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $relationship->relationship_type_id, 'label_a_b' ) . ' ' . CRM_Contact_BAO_Contact::displayName($relationship->contact_id_b) . ')'; - // add the recently created Relationship CRM_Utils_Recent::add($title, $url, @@ -173,7 +183,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { ); } - return array($valid, $invalid, $duplicate, $saved, $relationshipIds); + return array($valid, $invalid, $duplicate, $saved, $relationshipIds, $relationships); } /** diff --git a/api/v3/Relationship.php b/api/v3/Relationship.php index ff5e5515a4..e6ba8153f5 100644 --- a/api/v3/Relationship.php +++ b/api/v3/Relationship.php @@ -55,31 +55,30 @@ function civicrm_api3_relationship_create($params) { $values = array(); _civicrm_api3_relationship_format_params($params, $values); $ids = array(); - $action = CRM_Core_Action::ADD; if (CRM_Utils_Array::value('id', $params)) { - $ids['relationship'] = $params['id']; $ids['contactTarget'] = $values['contact_id_b']; - $action = CRM_Core_Action::UPDATE; } $values['relationship_type_id'] = $values['relationship_type_id'] . '_a_b'; - $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']); - $ids['contact'] = $values['contact_id_a']; + if(!empty($params['contact_id_b'])){ + $values['contact_check'] = array($params['contact_id_b'] => $params['contact_id_b']); + } + if(!empty($values['contact_id_a'])){ + $ids['contact'] = $values['contact_id_a']; + } $relationshipBAO = CRM_Contact_BAO_Relationship::create($values, $ids); if ($relationshipBAO[1]) { - return civicrm_api3_create_error('Relationship is not valid'); + throw new API_Exception('Relationship is not valid'); } elseif ($relationshipBAO[2]) { - return civicrm_api3_create_error('Relationship already exists'); + throw new API_Exception('Relationship already exists'); } - CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $values, $ids, $action); - $relationID = $relationshipBAO[4][0]; - return civicrm_api3_create_success(array( - $relationID => array('id' => $relationID, - 'moreIDs' => implode(',', $relationshipBAO[4]), - ))); + $id = $relationshipBAO[4][0]; + $values = array(); + _civicrm_api3_object_to_array($relationshipBAO[5][$id], $values[$id]); + return civicrm_api3_create_success($values, $params, 'relationship', 'create'); } /** diff --git a/api/v3/examples/Relationship/BetweenRelationshipType.php b/api/v3/examples/Relationship/BetweenRelationshipType.php index c0e4c2b73b..5cd8d920c1 100644 --- a/api/v3/examples/Relationship/BetweenRelationshipType.php +++ b/api/v3/examples/Relationship/BetweenRelationshipType.php @@ -4,14 +4,15 @@ demonstrates use of BETWEEN filter */ function relationship_get_example(){ -$params = array( - 'version' => 3, - 'relationship_type_id' => array( - 'BETWEEN' => array( - '0' => 34, - '1' => 36, +$params = array( + 'relationship_type_id' => array( + 'BETWEEN' => array( + '0' => 33, + '1' => 35, ), ), + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'relationship','get',$params ); @@ -24,61 +25,43 @@ $params = array( */ function relationship_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 3, - 'values' => array( - '2' => array( + 'values' => array( + '2' => array( 'id' => '2', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '34', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '33', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), - '3' => array( + '3' => array( 'id' => '3', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '35', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '34', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), - '4' => array( + '4' => array( 'id' => '4', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '36', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '35', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), ), ); diff --git a/api/v3/examples/Relationship/INRelationshipType.php b/api/v3/examples/Relationship/INRelationshipType.php index 68b07c521c..8014f90a1e 100644 --- a/api/v3/examples/Relationship/INRelationshipType.php +++ b/api/v3/examples/Relationship/INRelationshipType.php @@ -4,14 +4,15 @@ demonstrates use of IN filter */ function relationship_get_example(){ -$params = array( - 'version' => 3, - 'relationship_type_id' => array( - 'IN' => array( - '0' => 34, - '1' => 35, +$params = array( + 'relationship_type_id' => array( + 'IN' => array( + '0' => 33, + '1' => 34, ), ), + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'relationship','get',$params ); @@ -24,44 +25,32 @@ $params = array( */ function relationship_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 2, - 'values' => array( - '2' => array( + 'values' => array( + '2' => array( 'id' => '2', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '34', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '33', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), - '3' => array( + '3' => array( 'id' => '3', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '35', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '34', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), ), ); diff --git a/api/v3/examples/Relationship/NotBetweenRelationshipType.php b/api/v3/examples/Relationship/NotBetweenRelationshipType.php index 1866ff1f2f..d81e512007 100644 --- a/api/v3/examples/Relationship/NotBetweenRelationshipType.php +++ b/api/v3/examples/Relationship/NotBetweenRelationshipType.php @@ -4,14 +4,15 @@ demonstrates use of Not BETWEEN filter */ function relationship_get_example(){ -$params = array( +$params = array( 'version' => 3, - 'relationship_type_id' => array( - 'NOT BETWEEN' => array( - '0' => 34, - '1' => 36, + 'relationship_type_id' => array( + 'NOT BETWEEN' => array( + '0' => 33, + '1' => 35, ), ), + 'debug' => 0, ); $result = civicrm_api( 'relationship','get',$params ); @@ -24,28 +25,22 @@ $params = array( */ function relationship_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 1, 'id' => 1, - 'values' => array( - '1' => array( + 'values' => array( + '1' => array( 'id' => '1', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '33', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '32', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), ), ); diff --git a/api/v3/examples/Relationship/NotInRelationshipType.php b/api/v3/examples/Relationship/NotInRelationshipType.php index 751755f640..531f30fa08 100644 --- a/api/v3/examples/Relationship/NotInRelationshipType.php +++ b/api/v3/examples/Relationship/NotInRelationshipType.php @@ -4,14 +4,15 @@ demonstrates use of NOT IN filter */ function relationship_get_example(){ -$params = array( - 'version' => 3, - 'relationship_type_id' => array( - 'NOT IN' => array( - '0' => 34, - '1' => 35, +$params = array( + 'relationship_type_id' => array( + 'NOT IN' => array( + '0' => 33, + '1' => 34, ), ), + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'relationship','get',$params ); @@ -24,44 +25,32 @@ $params = array( */ function relationship_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 2, - 'values' => array( - '1' => array( + 'values' => array( + '1' => array( 'id' => '1', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '33', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '32', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), - '4' => array( + '4' => array( 'id' => '4', - 'contact_id_a' => '72', - 'contact_id_b' => '73', - 'relationship_type_id' => '36', + 'contact_id_a' => '66', + 'contact_id_b' => '67', + 'relationship_type_id' => '35', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), ), ); diff --git a/api/v3/examples/Relationship/filterIsCurrent.php b/api/v3/examples/Relationship/filterIsCurrent.php index 82709917aa..9e441ac4eb 100644 --- a/api/v3/examples/Relationship/filterIsCurrent.php +++ b/api/v3/examples/Relationship/filterIsCurrent.php @@ -4,11 +4,12 @@ demonstrates is_current filter */ function relationship_get_example(){ -$params = array( - 'version' => 3, - 'filters' => array( +$params = array( + 'filters' => array( 'is_current' => 1, ), + 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'relationship','get',$params ); @@ -21,28 +22,22 @@ $params = array( */ function relationship_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 1, 'id' => 2, - 'values' => array( - '2' => array( + 'values' => array( + '2' => array( 'id' => '2', - 'contact_id_a' => '69', - 'contact_id_b' => '70', - 'relationship_type_id' => '32', + 'contact_id_a' => '63', + 'contact_id_b' => '64', + 'relationship_type_id' => '31', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', ), ), ); diff --git a/api/v3/examples/RelationshipCreate.php b/api/v3/examples/RelationshipCreate.php index a31e4051b1..5181ba72e5 100644 --- a/api/v3/examples/RelationshipCreate.php +++ b/api/v3/examples/RelationshipCreate.php @@ -4,15 +4,16 @@ */ function relationship_create_example(){ -$params = array( +$params = array( 'contact_id_a' => 27, 'contact_id_b' => 28, - 'relationship_type_id' => 18, + 'relationship_type_id' => 19, 'start_date' => '2010-10-30', 'end_date' => '2010-12-30', 'is_active' => 1, 'note' => 'note', 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'relationship','create',$params ); @@ -25,15 +26,24 @@ $params = array( */ function relationship_create_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 1, 'id' => 1, - 'values' => array( - '1' => array( + 'values' => array( + '1' => array( 'id' => '1', - 'moreIDs' => '1', + 'contact_id_a' => '27', + 'contact_id_b' => '28', + 'relationship_type_id' => '19', + 'start_date' => '20101030000000', + 'end_date' => '20101230000000', + 'is_active' => '1', + 'description' => '', + 'is_permission_a_b' => 0, + 'is_permission_b_a' => 0, + 'case_id' => '', ), ), ); diff --git a/api/v3/examples/RelationshipDelete.php b/api/v3/examples/RelationshipDelete.php index e02ba7f724..06187c027b 100644 --- a/api/v3/examples/RelationshipDelete.php +++ b/api/v3/examples/RelationshipDelete.php @@ -4,13 +4,10 @@ */ function relationship_delete_example(){ -$params = array( - 'contact_id_a' => 51, - 'contact_id_b' => 52, - 'relationship_type_id' => 26, - 'start_date' => '2008-12-20', - 'is_active' => 1, +$params = array( + 'id' => 1, 'version' => 3, + 'debug' => 0, ); $result = civicrm_api( 'relationship','delete',$params ); @@ -23,17 +20,11 @@ $params = array( */ function relationship_delete_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 1, - 'id' => 1, - 'values' => array( - '1' => array( - 'id' => '1', - 'moreIDs' => '1', - ), - ), + 'values' => 'Deleted relationship successfully', ); return $expectedResult ; diff --git a/api/v3/examples/RelationshipGet.php b/api/v3/examples/RelationshipGet.php index df0cdec48e..b47bd7f526 100644 --- a/api/v3/examples/RelationshipGet.php +++ b/api/v3/examples/RelationshipGet.php @@ -4,9 +4,10 @@ */ function relationship_get_example(){ -$params = array( +$params = array( 'version' => 3, 'id' => 1, + 'debug' => 0, ); $result = civicrm_api( 'relationship','get',$params ); @@ -19,30 +20,24 @@ $params = array( */ function relationship_get_expectedresult(){ - $expectedResult = array( + $expectedResult = array( 'is_error' => 0, 'version' => 3, 'count' => 1, 'id' => 1, - 'values' => array( - '1' => array( + 'values' => array( + '1' => array( 'id' => '1', 'contact_id_a' => '36', 'contact_id_b' => '37', - 'relationship_type_id' => '21', + 'relationship_type_id' => '22', 'start_date' => '2008-12-20', 'is_active' => '1', 'description' => '', 'is_permission_a_b' => 0, 'is_permission_b_a' => 0, - 'custom_5' => 'custom string', - 'custom_5_1' => 'custom string', - 'custom_1' => 'xyz', - 'custom_1_-1' => 'xyz', - 'custom_3' => '07/11/2009', - 'custom_3_-1' => '07/11/2009', - 'custom_4' => 'http://civicrm.org', - 'custom_4_-1' => 'http://civicrm.org', + 'custom_1' => 'custom string', + 'custom_1_1' => 'custom string', ), ), ); diff --git a/tests/phpunit/api/v3/RelationshipTest.php b/tests/phpunit/api/v3/RelationshipTest.php index c8d6f08b78..5010b105cc 100644 --- a/tests/phpunit/api/v3/RelationshipTest.php +++ b/tests/phpunit/api/v3/RelationshipTest.php @@ -41,7 +41,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { protected $_customGroupId = NULL; protected $_customFieldId = NULL; protected $_params; - public $_eNoticeCompliant = FALSE; + public $_eNoticeCompliant = TRUE; protected $_entity; function get_info() { return array( @@ -82,7 +82,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { } function tearDown() { - $this->quickCleanup(array('civicrm_relationship')); + $this->quickCleanup(array('civicrm_relationship'), TRUE); $this->relationshipTypeDelete($this->_relTypeID); $this->contactDelete($this->_cId_a); $this->contactDelete($this->_cId_b); @@ -221,22 +221,19 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'start_date' => '2008-12-20', 'end_date' => NULL, 'is_active' => 1, - 'version' => $this->_apiversion, - ); - $relationship = civicrm_api('relationship', 'create', $params); + ); + $relationship = $this->callAPISuccess('relationship', 'create', $params); $params = array( 'id' => $relationship['id'], 'is_active' => 0, - 'version' => $this->_apiversion, 'debug' => 1, ); - $result = civicrm_api('relationship', 'create', $params); + $result = $this->callAPISuccess('relationship', 'create', $params); $this->assertAPISuccess($result, 'in line ' . __LINE__); - $result = civicrm_api('relationship', 'get', $params); - $this->assertEquals(0, $result['values'][$result['id']]['is_active'], 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $params); $params['id'] = $relationship['id']; - $result = civicrm_api('relationship', 'delete', $params); + $result = $this->callAPISuccess('relationship', 'delete', $params); } /** @@ -248,22 +245,20 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'contact_id_b' => $this->_cId_b, 'relationship_type_id' => $this->_relTypeID, 'start_date' => '2008-12-20', - 'end_date' => NULL, 'is_active' => 1, 'is_permission_a_b' => 1, 'description' => 'my desc', 'version' => $this->_apiversion, ); - $relationship = civicrm_api('relationship', 'create', $params); + $relationship = $this->callAPISuccess('relationship', 'create', $params); $updateparams = array( 'id' => $relationship['id'], 'version' => $this->_apiversion, 'relationship_type_id' => $this->_relTypeID, ); - $result = civicrm_api('relationship', 'create', $updateparams); + $result = $this->callAPISuccess('relationship', 'create', $updateparams); - $this->assertAPISuccess($result, 'in line ' . __LINE__); //make sure the orig params didn't get changed $this->getAndCheck($params, $relationship['id'], 'relationship'); @@ -283,12 +278,9 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'end_date' => '2010-12-30', 'is_active' => 1, 'note' => 'note', - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'create', $params); - $this->documentMe($params, $result, __FUNCTION__, __FILE__); - $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__); + $result = $this->callAPIAndDocument('relationship', 'create', $params, __FUNCTION__, __FILE__); $this->assertNotNull($result['id'], 'in line ' . __LINE__); $relationParams = array( 'id' => $result['id'], @@ -296,7 +288,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { // assertDBState compares expected values in $result to actual values in the DB $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); - $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id'])); + $result = $this->callAPISuccess('relationship', 'get', array('version' => 3, 'id' => $result['id'])); $values = $result['values'][$result['id']]; foreach ($params as $key => $value) { if ($key == 'version' || $key == 'note') { @@ -305,7 +297,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); } $params['id'] = $result['id']; - civicrm_api('relationship', 'delete', $params); + $this->callAPISuccess('relationship', 'delete', $params); } /** @@ -323,9 +315,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'create', $params); - - $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'create', $params); $this->assertNotNull($result['id'], 'in line ' . __LINE__); $relationParams = array( 'id' => $result['id'], @@ -333,7 +323,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { // assertDBState compares expected values in $result to actual values in the DB $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); - $result = civicrm_api('relationship', 'get', array('version' => 3, 'id' => $result['id'])); + $result = $this->callAPISuccess('relationship', 'get', array('id' => $result['id'])); $values = $result['values'][$result['id']]; foreach ($params as $key => $value) { if ($key == 'version' || $key == 'note') { @@ -346,7 +336,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $this->assertEquals($value, $values[$key], $key . " doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); } $params['id'] = $result['id']; - civicrm_api('relationship', 'delete', $params); + $this->callAPISuccess('relationship', 'delete', $params); } /** @@ -372,9 +362,8 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); $params = array_merge($params, $custom_params); - $result = civicrm_api('relationship', 'create', $params); + $result = $this->callAPISuccess('relationship', 'create', $params); - $this->assertNotNull($result['id']); $relationParams = array( 'id' => $result['id'], ); @@ -382,7 +371,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); $params['id'] = $result['id']; - $result = civicrm_api('relationship', 'delete', $params); + $result = $this->callAPISuccess('relationship', 'delete', $params); $this->relationshipTypeDelete($this->_relTypeID); } @@ -398,13 +387,11 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $params = $this->_params; $params['custom_' . $ids['custom_field_id']] = "custom string"; - $result = civicrm_api($this->_entity, 'create', $params); + $result = $this->callAPISuccess($this->_entity, 'create', $params); $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); - $this->assertAPISuccess($result, ' in line ' . __LINE__); $getParams = array('version' => 3, 'id' => $result['id']); - $check = civicrm_api($this->_entity, 'get', $getParams); - $this->documentMe($getParams, $check, __FUNCTION__, __FILE__); + $check = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__); $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); $this->customFieldDelete($ids['custom_field_id']); @@ -421,7 +408,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'max_multiple' => 0, 'version' => $this->_apiversion, ); - $customGroup = civicrm_api('custom_group', 'create', $params); + $customGroup = $this->callAPISuccess('custom_group', 'create', $params); $this->_customGroupId = $customGroup['id']; return $customGroup['id']; } @@ -442,7 +429,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { ); - $result = civicrm_api('CustomField', 'create', $params); + $result = $this->callAPISuccess('CustomField', 'create', $params); $customField = NULL; $ids[] = $customField['result']['customFieldId']; @@ -479,7 +466,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); - $customField = civicrm_api('custom_field', 'create', $params); + $customField = $this->callAPISuccess('custom_field', 'create', $params); $ids[] = $customField['id']; $params = array( @@ -496,7 +483,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); - $customField = civicrm_api('custom_field', 'create', $params); + $customField = $this->callAPISuccess('custom_field', 'create', $params); $ids[] = $customField['id']; $params = array( @@ -513,7 +500,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); - $customField = civicrm_api('custom_field', 'create', $params); + $customField = $this->callAPISuccess('custom_field', 'create', $params); $ids[] = $customField['id']; return $ids; } @@ -575,15 +562,13 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'create', $params); - $this->documentMe($params, $result, __FUNCTION__, __FILE__); - $this->assertNotNull($result['id']); + $result = $this->callAPISuccess('relationship', 'create', $params); //Delete relationship $params = array(); $params['id'] = $result['id']; - $result = civicrm_api('relationship', 'delete', $params); + $result = $this->callAPIAndDocument('relationship', 'delete', $params, __FUNCTION__, __FILE__); $this->relationshipTypeDelete($this->_relTypeID); } @@ -612,10 +597,9 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'start_date' => '20081214', 'end_date' => '20091214', 'is_active' => 1, - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'create', $relParams); + $result = $this->callAPISuccess('relationship', 'create', $relParams); $this->assertNotNull($result['id'], 'In line ' . __LINE__); $this->_relationID = $result['id']; @@ -664,26 +648,20 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { //get relationship $params = array( 'contact_id' => $this->_cId_b, - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'get', $params); - - $this->assertAPISuccess($result, 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $params); $this->assertEquals($result['count'], 1, 'in line ' . __LINE__); $params = array( 'contact_id_a' => $this->_cId_a, - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'get', $params); - $this->assertAPISuccess($result, 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $params); $this->assertEquals($result['count'], 1, 'in line ' . __LINE__); // contact_id_a is wrong so should be no matches $params = array( 'contact_id_a' => $this->_cId_b, 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'get', $params); - $this->assertAPISuccess($result, 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $params); $this->assertEquals($result['count'], 0, 'in line ' . __LINE__); } @@ -702,15 +680,13 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'create', $relParams); + $result = $this->callAPISuccess('relationship', 'create', $relParams); //get relationship $params = array( 'contact_id_b' => $this->_cId_b, - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'get', $params); - $this->assertAPISuccess($result, 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $params); } function testGetIsCurrent() { @@ -720,29 +696,25 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'relationship_type_id' => $this->_relTypeID, 'start_date' => '2008-12-20', 'is_active' => 0, - 'version' => $this->_apiversion, ); - $rel2 = civicrm_api('relationship', 'create', $rel2Params); - $this->assertAPISuccess($rel2); - $rel1 = civicrm_api('relationship', 'create', $this->_params); - $this->assertAPISuccess($rel1); + $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); + $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); + $getParams = array( - 'version' => $this->_apiversion, 'filters' => array('is_current' => 1) ); $description = "demonstrates is_current filter"; $subfile = 'filterIsCurrent'; //no relationship has been created - $result = civicrm_api('relationship', 'get', $getParams); - $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile); + $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); $this->assertEquals($result['count'], 1); $this->AssertEquals($rel1['id'], $result['id']); // now try not started $rel2Params['is_active'] =1; $rel2Params['start_date'] ='tomorrow'; - $rel2 = civicrm_api('relationship', 'create', $rel2Params); - $result = civicrm_api('relationship', 'get', $getParams); + $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); + $result = $this->callAPISuccess('relationship', 'get', $getParams); $this->assertEquals($result['count'], 1); $this->AssertEquals($rel1['id'], $result['id']); @@ -750,7 +722,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $rel2Params['is_active'] =1; $rel2Params['start_date'] ='last week'; $rel2Params['end_date'] ='yesterday'; - $rel2 = civicrm_api('relationship', 'create', $rel2Params); + $rel2 = $this->callAPISuccess('relationship', 'create', $rel2Params); } /* * Test using various operators @@ -791,50 +763,40 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { ); $relationType4 = $this->relationshipTypeCreate($relTypeParams); - $rel1 = civicrm_api('relationship', 'create', $this->_params); - $this->assertAPISuccess($rel1); - $rel2 = civicrm_api('relationship', 'create', array_merge($this->_params, + $rel1 = $this->callAPISuccess('relationship', 'create', $this->_params); + $rel2 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, array('relationship_type_id' => $relationType2,))); - $this->assertAPISuccess($rel2); - $rel3 = civicrm_api('relationship', 'create', array_merge($this->_params, + $rel3 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, array('relationship_type_id' => $relationType3,))); - $this->assertAPISuccess($rel3); - $rel4 = civicrm_api('relationship', 'create', array_merge($this->_params, + $rel4 = $this->callAPISuccess('relationship', 'create', array_merge($this->_params, array('relationship_type_id' => $relationType4,))); - $this->assertAPISuccess($rel4); $getParams = array( - 'version' => $this->_apiversion, - 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)) + 'relationship_type_id' => array('IN' => array($relationType2, $relationType3)) ); $description = "demonstrates use of IN filter"; $subfile = 'INRelationshipType'; - $result = civicrm_api('relationship', 'get', $getParams); - $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile); + $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); $this->assertEquals($result['count'], 2); $this->AssertEquals(array($rel2['id'], $rel3['id']), array_keys($result['values'])); $description = "demonstrates use of NOT IN filter"; $subfile = 'NotInRelationshipType'; $getParams = array( - 'version' => $this->_apiversion, 'relationship_type_id' => array('NOT IN' => array($relationType2, $relationType3)) ); - $result = civicrm_api('relationship', 'get', $getParams); - $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile); + $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); $this->assertEquals($result['count'], 2); $this->AssertEquals(array($rel1['id'], $rel4['id']), array_keys($result['values'])); $description = "demonstrates use of BETWEEN filter"; $subfile = 'BetweenRelationshipType'; $getParams = array( - 'version' => $this->_apiversion, 'relationship_type_id' => array('BETWEEN' => array($relationType2, $relationType4)) ); - $result = civicrm_api('relationship', 'get', $getParams); - $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile); + $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); $this->assertEquals($result['count'], 3); $this->AssertEquals(array($rel2['id'], $rel3['id'], $rel4['id']), array_keys($result['values'])); @@ -844,8 +806,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'version' => $this->_apiversion, 'relationship_type_id' => array('NOT BETWEEN' => array($relationType2, $relationType4)) ); - $result = civicrm_api('relationship', 'get', $getParams); - $this->documentMe($getParams, $result, __FUNCTION__, __FILE__, $description, $subfile); + $result = $this->callAPIAndDocument('relationship', 'get', $getParams, __FUNCTION__, __FILE__, $description, $subfile); $this->assertEquals($result['count'], 1); $this->AssertEquals(array($rel1['id'],), array_keys($result['values'])); @@ -865,20 +826,6 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { $this->assertEquals('id is not a valid integer', $result['error_message'], 'in line ' . __LINE__); } - ///////////////// civicrm_get_relationships - - /** - * check with invalid data - */ - function testGetRelationshipInvalidData() { - $contact_a = array('contact_id' => $this->_cId_a); - $contact_b = array('contact_id' => $this->_cId_b); - - //no relationship has been created - $result = civicrm_api('relationship', 'get', $contact_a, $contact_b, NULL, 'asc'); - $this->assertAPIFailure($result); - } - /** * check with valid data with contact_b */ @@ -890,24 +837,20 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'start_date' => '2011-01-01', 'end_date' => '2013-01-01', 'is_active' => 1, - 'version' => $this->_apiversion, ); - $relationship = civicrm_api('relationship', 'create', $relParams); + $relationship = $this->callAPISuccess('relationship', 'create', $relParams); $contacts = array( 'contact_id' => $this->_cId_a, - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'get', $contacts); - $this->assertAPISuccess($result, 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $contacts); $this->assertGreaterThan(0, $result['count'], 'in line ' . __LINE__); $params = array( 'id' => $relationship['id'], - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'delete', $params); + $result = $this->callAPISuccess('relationship', 'delete', $params); $this->relationshipTypeDelete($this->_relTypeID); } @@ -922,25 +865,19 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'start_date' => '2011-01-01', 'end_date' => '2013-01-01', 'is_active' => 1, - 'version' => $this->_apiversion, ); - $relationship = civicrm_api('relationship', 'create', $relParams); + $relationship = $this->callAPISuccess('relationship', 'create', $relParams); $contact_a = array( 'contact_id' => $this->_cId_a, - 'version' => $this->_apiversion, ); - - $result = civicrm_api('relationship', 'get', $contact_a); - - $this->assertAPISuccess($result, 'in line ' . __LINE__); + $result = $this->callAPISuccess('relationship', 'get', $contact_a); $params = array( 'id' => $relationship['id'], - 'version' => $this->_apiversion, ); - $result = civicrm_api('relationship', 'delete', $params); + $result = $this->callAPISuccess('relationship', 'delete', $params); $this->relationshipTypeDelete($this->_relTypeID); } } -- 2.25.1