added unit test
authordeb.monish <monish.deb@webaccessglobal.com>
Wed, 13 Apr 2016 15:32:20 +0000 (21:02 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Wed, 13 Apr 2016 15:32:20 +0000 (21:02 +0530)
CRM/Contribute/BAO/Contribution.php
tests/phpunit/api/v3/ContributionTest.php

index f8cc5df3ef984fe955fa15239dfe43e7b80df632..acd9d3e56e7bc1eeb2b16505e42f0fe38d7922d5 100644 (file)
@@ -2552,17 +2552,18 @@ INNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_ac
       $addressParams = array('id' => $this->address_id);
       $addressDetails = CRM_Core_BAO_Address::getValues($addressParams, FALSE, 'id');
       $addressDetails = array_values($addressDetails);
-      $values['address'] = $addressDetails[0]['display'];
     }
     // Else we assign the billing address of the contribution contact.
     else {
       $addressParams = array('contact_id' => $this->contact_id, 'is_billing' => 1);
-      $addressDetails = CRM_Core_BAO_Address::getValues($addressParams);
-      if (!is_null($addressDetails)) {
-        $addressDetails = array_values($addressDetails);
-        $values['address'] = $addressDetails[0]['display'];
-      }
+      $addressDetails = (array) CRM_Core_BAO_Address::getValues($addressParams);
+      $addressDetails = array_values($addressDetails);
     }
+
+    if (!empty($addressDetails[0]['display'])) {
+      $values['address'] = $addressDetails[0]['display'];
+    }
+
     if ($this->_component == 'contribute') {
       //get soft contributions
       $softContributions = CRM_Contribute_BAO_ContributionSoft::getSoftContribution($this->id, TRUE);
index 7d482022300fb6cc3c9b5bf24805aec6bba61f18..fa90194452a90d2d5d70a16dfd64a5eab08e4954 100644 (file)
@@ -1603,6 +1603,61 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->revertTemplateToReservedTemplate();
   }
 
+  /**
+   * Test to check whether contact billing address is used when no contribution address
+   */
+  public function testBillingAddress() {
+    $mut = new CiviMailUtils($this, TRUE);
+    $this->swapMessageTemplateForTestTemplate();
+    $this->createLoggedInUser();
+
+    //Scenario 1: When Contact don't have any address
+    $params = array_merge($this->_params, array('contribution_status_id' => 2));
+    $contribution = $this->callAPISuccess('contribution', 'create', $params);
+    $this->callAPISuccess('contribution', 'completetransaction', array(
+      'id' => $contribution['id'],
+    ));
+    $mut->checkMailLog(array(
+      'address:::',
+    ));
+
+    // Scenario 2: Contribution using address
+    $address = $this->callAPISuccess('address', 'create', array(
+      'street_address' => 'contribution billing st',
+      'location_type_id' => 2,
+      'contact_id' => $this->_params['contact_id'],
+    ));
+    $params = array_merge($this->_params, array('contribution_status_id' => 2,
+      'address_id' => $address['id'],
+      )
+    );
+    $contribution = $this->callAPISuccess('contribution', 'create', $params);
+    $this->callAPISuccess('contribution', 'completetransaction', array(
+      'id' => $contribution['id'],
+    ));
+    $mut->checkMailLog(array(
+      'address:::contribution billing st',
+    ));
+
+    // Scenario 3: Contribution wtth no address but contact has a billing address
+    $this->callAPISuccess('address', 'create', array(
+      'id' => $address['id'],
+      'street_address' => 'is billing st',
+      'contact_id' => $this->_params['contact_id'],
+    ));
+    $params = array_merge($this->_params, array('contribution_status_id' => 2));
+    $contribution = $this->callAPISuccess('contribution', 'create', $params);
+    $this->callAPISuccess('contribution', 'completetransaction', array(
+      'id' => $contribution['id'],
+    ));
+    $mut->checkMailLog(array(
+      'address:::is billing st',
+    ));
+
+    $mut->stop();
+    $this->revertTemplateToReservedTemplate();
+  }
+
   /**
    * Test completing a transaction via the API.
    *