[REF] change deprecated function to API4 call
authorJon Goldberg <jon@megaphonetech.com>
Wed, 5 Aug 2020 19:41:00 +0000 (15:41 -0400)
committerJon Goldberg <jon@megaphonetech.com>
Sun, 6 Sep 2020 22:38:00 +0000 (18:38 -0400)
CRM/Contribute/Form/Contribution/Confirm.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php

index 228e285dfdee18ffeef735d6bf931e6d444bc852..d1916a14c42cd968bb346c095d7efc461db824bf 100644 (file)
@@ -1145,19 +1145,9 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
     // create employer relationship with $contactID only when new organization is there
     // else retain the existing relationship
     else {
-      // get the Employee relationship type id
-      $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
-
-      // keep relationship params ready
-      $relParams['relationship_type_id'] = $relTypeId . '_a_b';
-      $relParams['is_permission_a_b'] = 1;
-      $relParams['is_active'] = 1;
       $isNotCurrentEmployer = TRUE;
     }
 
-    // formalities for creating / editing organization.
-    $behalfOrganization['contact_type'] = 'Organization';
-
     if (!$orgID) {
       // check if matching organization contact exists
       $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts($behalfOrganization, 'Organization', 'Unsupervised', [], FALSE);
@@ -1182,14 +1172,26 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
     }
 
     // create organization, add location
+    $behalfOrganization['contact_type'] = 'Organization';
     $orgID = CRM_Contact_BAO_Contact::createProfileContact($behalfOrganization, $fields, $orgID,
       NULL, NULL, 'Organization'
     );
     // create relationship
     if ($isNotCurrentEmployer) {
-      $relParams['contact_check'][$orgID] = 1;
-      $cid = ['contact' => $contactID];
-      CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid);
+      try {
+        \Civi\Api4\Relationship::create(FALSE)
+          ->addValue('contact_id_a', $contactID)
+          ->addValue('contact_id_b', $orgID)
+          ->addValue('relationship_type_id', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b'))
+          ->addValue('is_permission_a_b:name', 'View and update')
+          ->execute();
+      }
+      catch (CRM_Core_Exception $e) {
+        // Ignore if duplicate relationship.
+        if ($e->getMessage() !== 'Duplicate Relationship') {
+          throw $e;
+        }
+      }
     }
 
     // if multiple match - send a duplicate alert
index 1a06b3df1ff778fa78ce98e4406201c9981d5dd9..b73e7c9f8a046ff4284428e43d90e084c1f8d6cf 100644 (file)
@@ -1338,7 +1338,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
       'postal_code' => '222222',
       'country' => 'United States',
     ];
-    $contactID = $this->individualCreate();
+    $originalContactId = $contactID = $this->individualCreate();
     $orgId = $this->organizationCreate(['organization_name' => 'testorg1']);
     $orgCount = $this->callAPISuccessGetCount('Contact', [
       'contact_type' => "Organization",
@@ -1347,7 +1347,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
     $this->assertEquals($orgCount, 1);
 
     $values = $params = [];
-    $behalfOrganization = [
+    $originalBehalfOrganization = $behalfOrganization = [
       'organization_name' => 'testorg1',
       'phone' => [
         1 => [
@@ -1383,9 +1383,10 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
       'country-3' => 1,
       'state_province-3' => 1,
     ];
-    CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
+    $empty = [];
+    CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $empty, $empty, $empty);
 
-    //Check whether new organisation is not created.
+    //Check whether new organisation is created.
     $result = $this->callAPISuccess('Contact', 'get', [
       'contact_type' => "Organization",
       'organization_name' => "testorg1",
@@ -1399,7 +1400,7 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
 
     //Check if alert is assigned to params if more than 1 dupe exists.
     $orgId = $this->organizationCreate(['organization_name' => 'testorg1', 'email' => 'testorg@gmail.com']);
-    CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
+    CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($originalBehalfOrganization, $originalContactId, $values, $params, $fields);
     $this->assertEquals($params['onbehalf_dupe_alert'], 1);
   }