Fix custom data edit on Relationship
authorJitendra Purohit <jitendra@fuzion.co.nz>
Mon, 3 Apr 2017 06:23:43 +0000 (11:53 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Mon, 3 Apr 2017 08:02:57 +0000 (13:32 +0530)
CRM/Contact/Form/Relationship.php
tests/phpunit/api/v3/RelationshipTest.php

index 7ae3d6fabe957e6d40dce5c47ad87ff62d2b2d03..e90727bb07154fb7c1dc6a364bb9971d7a1ee71a 100644 (file)
@@ -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);
 
index da5164d1b9dcdbd4f65d6575b12c65cd027b85d0..040b98df5a6f04a1c3dec5a2635d457f112ab6d4 100644 (file)
@@ -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',