From e4addeb23f80dadc1a0763c65c9231ddc7571b80 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 5 Aug 2020 15:41:00 -0400 Subject: [PATCH] [REF] change deprecated function to API4 call --- CRM/Contribute/Form/Contribution/Confirm.php | 28 ++++++++++--------- .../CRM/Contribute/BAO/ContributionTest.php | 11 ++++---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 228e285dfd..d1916a14c4 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -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 diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 1a06b3df1f..b73e7c9f8a 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -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); } -- 2.25.1