From 00d84e9bc6856e5b2a09f25da400d3c244b0b75e Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Mon, 3 Apr 2017 11:53:43 +0530 Subject: [PATCH] Fix custom data edit on Relationship --- CRM/Contact/Form/Relationship.php | 39 ++++++++++++++++------- tests/phpunit/api/v3/RelationshipTest.php | 23 ++++++++++--- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 7ae3d6fabe..e90727bb07 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -357,25 +357,37 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { } /** - * This function is called when the form is submitted. + * This function is called when the form is submitted and also from unit test. + * @param array $params + * + * @return array */ - public function postProcess() { - // Store the submitted values in an array. - $params = $this->controller->exportValues($this->_name); - + public function submit($params) { switch ($this->getAction()) { case CRM_Core_Action::DELETE: $this->deleteAction($this->_relationshipId); - return; + return array(); case CRM_Core_Action::UPDATE: - list ($params, $relationshipIds) = $this->updateAction($params); - break; + return $this->updateAction($params); default: - list ($params, $relationshipIds) = $this->createAction($params); - break; + return $this->createAction($params); } + } + + /** + * This function is called when the form is submitted. + */ + public function postProcess() { + // Store the submitted values in an array. + $params = $this->controller->exportValues($this->_name); + + $values = $this->submit($params); + if (empty($values)) { + return; + } + list ($params, $relationshipIds) = $values; // if this is called from case view, //create an activity for case role removal.CRM-4480 @@ -513,7 +525,12 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { $params = $this->preparePostProcessParameters($params); $params = $params[0]; - CRM_Contact_BAO_Relationship::create($params); + try { + civicrm_api3('relationship', 'create', $params); + } + catch (CiviCRM_API3_Exception $e) { + throw new CRM_Core_Exception('Relationship create error ' . $e->getMessage()); + } $this->clearCurrentEmployer($params); diff --git a/tests/phpunit/api/v3/RelationshipTest.php b/tests/phpunit/api/v3/RelationshipTest.php index da5164d1b9..040b98df5a 100644 --- a/tests/phpunit/api/v3/RelationshipTest.php +++ b/tests/phpunit/api/v3/RelationshipTest.php @@ -356,7 +356,7 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { /** * Check relationship creation with custom data. */ - public function testRelationshipCreateWithCustomData() { + public function testRelationshipCreateEditWithCustomData() { $this->createCustomGroup(); $this->_ids = $this->createCustomField(); //few custom Values for comparing @@ -382,6 +382,21 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { ); $this->assertDBState('CRM_Contact_DAO_Relationship', $result['id'], $relationParams); + //Test Edit of custom field from the form. + $getParams = array('id' => $result['id']); + $updateParams = array_merge($getParams, array( + "custom_{$this->_ids[0]}" => 'Edited Text Value', + 'relationship_type_id' => $this->_relTypeID . '_b_a', + 'related_contact_id' => $this->_cId_a, + )); + $reln = new CRM_Contact_Form_Relationship(); + $reln->_action = CRM_Core_Action::UPDATE; + $reln->_relationshipId = $result['id']; + $reln->submit($updateParams); + + $check = $this->callAPISuccess('relationship', 'get', $getParams); + $this->assertEquals("Edited Text Value", $check['values'][$check['id']]["custom_{$this->_ids[0]}"]); + $params['id'] = $result['id']; $this->callAPISuccess('relationship', 'delete', $params); $this->relationshipTypeDelete($this->_relTypeID); @@ -444,10 +459,8 @@ class api_v3_RelationshipTest extends CiviUnitTestCase { 'is_active' => 1, ); - $this->callAPISuccess('CustomField', 'create', $params); - - $customField = NULL; - $ids[] = $customField['result']['customFieldId']; + $customField = $this->callAPISuccess('CustomField', 'create', $params); + $ids[] = $customField['id']; $optionValue[] = array( 'label' => 'Red', -- 2.25.1