CRM-21043 - Dupe check of on behalf organisation not working
authorJitendra Purohit <jitendra@fuzion.co.nz>
Tue, 8 Aug 2017 10:39:52 +0000 (16:09 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Tue, 8 Aug 2017 10:39:52 +0000 (16:09 +0530)
CRM/Contribute/Form/Contribution/Confirm.php
tests/phpunit/CRM/Contribute/BAO/ContributionTest.php

index e5bfaf06bc0a282a7604c9cb9d2176cffbb835ea..ae9133a1fff5e1ae68202e18716a509a05d7b2ea 100644 (file)
@@ -1189,11 +1189,11 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
 
     if (!$orgID) {
       // check if matching organization contact exists
-      $dupeID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($behalfOrganization, 'Organization', 'Unsupervised', array(), FALSE);
+      $dupeIDs = CRM_Contact_BAO_Contact::getDuplicateContacts($behalfOrganization, 'Organization', 'Unsupervised', array(), FALSE);
 
       // CRM-6243 says to pick the first org even if more than one match
       if (count($dupeIDs) >= 1) {
-        $behalfOrganization['contact_id'] = $orgID = $dupeID;
+        $behalfOrganization['contact_id'] = $orgID = $dupeIDs[0];
         // don't allow name edit
         unset($behalfOrganization['organization_name']);
       }
index a39b1d306eafe0d2d4ba916fbcbdcf1ddbb405e4..64d15fe8c17d613f9576806498a02c193e3b835f 100644 (file)
@@ -1191,6 +1191,84 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
     return array($contribution, $financialAccount);
   }
 
+  /**
+   * Test processOnBehalfOrganization() function.
+   */
+  public function testProcessOnBehalfOrganization() {
+    $orgInfo = array(
+      'phone' => '11111111',
+      'email' => 'testorg@gmail.com',
+      'street_address' => 'test Street',
+      'city' => 'test City',
+      'state_province' => 'AA',
+      'postal_code' => '222222',
+      'country' => 'United States',
+    );
+    $contactID = $this->individualCreate();
+    $orgId = $this->organizationCreate(array('organization_name' => 'testorg1'));
+    $orgCount = $this->callAPISuccessGetCount('Contact', array(
+      'contact_type' => "Organization",
+      'organization_name' => "testorg1",
+    ));
+    $this->assertEquals($orgCount, 1);
+
+    $values = $params = array();
+    $behalfOrganization = array(
+      'organization_name' => 'testorg1',
+      'phone' => array(
+        1 => array(
+          'phone' => $orgInfo['phone'],
+          'is_primary' => 1,
+        ),
+      ),
+      'email' => array(
+        1 => array(
+          'email' => $orgInfo['email'],
+          'is_primary' => 1,
+        ),
+      ),
+      'address' => array(
+        3 => array(
+          'street_address' => $orgInfo['street_address'],
+          'city' => $orgInfo['city'],
+          'location_type_id' => 3,
+          'postal_code' => $orgInfo['postal_code'],
+          'country' => 'US',
+          'state_province' => 'AA',
+          'is_primary' => 1,
+        ),
+      ),
+    );
+    $fields = array(
+      'organization_name' => 1,
+      'phone-3-1' => 1,
+      'email-3' => 1,
+      'street_address-3' => 1,
+      'city-3' => 1,
+      'postal_code-3' => 1,
+      'country-3' => 1,
+      'state_province-3' => 1,
+    );
+    CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
+
+    //Check whether new organisation is not created.
+    $result = $this->callAPISuccess('Contact', 'get', array(
+      'contact_type' => "Organization",
+      'organization_name' => "testorg1",
+    ));
+    $this->assertEquals($result['count'], 1);
+
+    //Assert all org values are updated.
+    foreach ($orgInfo as $key => $val) {
+      $this->assertEquals($result['values'][$orgId][$key], $val);
+    }
+
+    //Check if alert is assigned to params if more than 1 dupe exists.
+    $orgId = $this->organizationCreate(array('organization_name' => 'testorg1', 'email' => 'testorg@gmail.com'));
+    CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
+    $this->assertEquals($params['onbehalf_dupe_alert'], 1);
+  }
+
   /**
    * Test for replaceContributionTokens.
    *   This function tests whether the contribution tokens are replaced with values from contribution.