From 3fb990f40bf67975c2bb032c291c04fe86bc6560 Mon Sep 17 00:00:00 2001
From: Ravish Nair <ravish.nair@webaccess.co.in>
Date: Mon, 10 Jun 2013 20:12:49 +0530
Subject: [PATCH] -- CRM-12771 some more fixes

----------------------------------------
* CRM-12771: contribution pages with a zero amount are not generating receipts or creating contribution records
  http://issues.civicrm.org/jira/browse/CRM-12771
---
 CRM/Contribute/BAO/Contribution.php           |   8 +-
 CRM/Contribute/Form/Contribution/Main.php     |   2 +-
 CRM/Contribute/Form/ContributionBase.php      |   5 +-
 .../Contribute/OfflineContributionTest.php    |  36 +++++
 .../Contribute/OnlineContributionTest.php     | 134 ++++++++++++++++++
 5 files changed, 181 insertions(+), 4 deletions(-)

diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php
index 9e19eb531d..b41d2c0f0c 100644
--- a/CRM/Contribute/BAO/Contribution.php
+++ b/CRM/Contribute/BAO/Contribution.php
@@ -1204,10 +1204,14 @@ LEFT JOIN civicrm_option_value contribution_status ON (civicrm_contribution.cont
     $addressParams = array();
     $addressParams['location_type_id'] = $billingLocationTypeID;
     $addressParams['is_billing'] = 1;
-    $addressParams['address_name'] = "{$params['billing_first_name']}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$params['billing_middle_name']}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$params['billing_last_name']}";
+
+    $billingFirstName = CRM_Utils_Array::value('billing_first_name', $params);
+    $billingMiddleName = CRM_Utils_Array::value('billing_middle_name', $params);
+    $billingLastName = CRM_Utils_Array::value('billing_last_name', $params);
+    $addressParams['address_name'] = "{$billingFirstName}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$billingMiddleName}" . CRM_Core_DAO::VALUE_SEPARATOR . "{$billingLastName}";
 
     foreach ($billingFields as $value) {
-      $addressParams[$value] = $params["billing_{$value}-{$billingLocationTypeID}"];
+      $addressParams[$value] = CRM_Utils_Array::value("billing_{$value}-{$billingLocationTypeID}", $params);
     }
 
     $address = CRM_Core_BAO_Address::add($addressParams, FALSE);
diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php
index 6cafa02d0c..8112cda30e 100644
--- a/CRM/Contribute/Form/Contribution/Main.php
+++ b/CRM/Contribute/Form/Contribution/Main.php
@@ -762,7 +762,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
           }
         }
         if ($priceField->name == 'other_amount') {
-          if ($self->_quickConfig && !isset($fields["price_{$priceField->id}"]) &&
+          if ($self->_quickConfig && !CRM_Utils_Array::value("price_{$priceField->id}", $fields) &&
             array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])) {
             $otherAmount = $priceField->id;
           }
diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php
index 901527e098..ea07642459 100644
--- a/CRM/Contribute/Form/ContributionBase.php
+++ b/CRM/Contribute/Form/ContributionBase.php
@@ -537,13 +537,16 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     }
 
     foreach ($vars as $v) {
-      if (CRM_Utils_Array::value($v, $this->_params)) {
+      if (isset($this->_params[$v])) {
         if ($v == 'frequency_unit' || $v == 'pledge_frequency_unit') {
           $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
           if (array_key_exists($this->_params[$v], $frequencyUnits)) {
             $this->_params[$v] = $frequencyUnits[$this->_params[$v]];
           }
         }
+        if ($v == "amount") {
+          $this->_params[$v] = CRM_Utils_Money::format($this->_params[$v], ' ');
+        }
         $this->assign($v, $this->_params[$v]);
       }
     }
diff --git a/tests/phpunit/WebTest/Contribute/OfflineContributionTest.php b/tests/phpunit/WebTest/Contribute/OfflineContributionTest.php
index 0e177fb7c7..7b299cf322 100644
--- a/tests/phpunit/WebTest/Contribute/OfflineContributionTest.php
+++ b/tests/phpunit/WebTest/Contribute/OfflineContributionTest.php
@@ -355,4 +355,40 @@ class WebTest_Contribute_OfflineContributionTest extends CiviSeleniumTestCase {
     $this->click("_qf_ContributionView_cancel-top");
     $this->waitForPageToLoad($this->getTimeoutMsec());
   }
+
+  function testOnlineContributionWithZeroAmount () {
+    $this->webtestLogin();
+
+    // Create a contact to be used as soft creditor
+    $firstName = 'John'.substr(sha1(rand()), 0, 7);
+    $lastName = 'Peterson'.substr(sha1(rand()), 0, 7);
+    $this->webtestAddContact( $firstName, $lastName);
+    $this->waitForElementPresent("css=li#tab_contribute a");
+    $this->click("css=li#tab_contribute a");
+    $this->waitForElementPresent("link=Record Contribution (Check, Cash, EFT ...)");
+    $this->clickLink("link=Record Contribution (Check, Cash, EFT ...)");
+
+     // select financial type
+    $this->select("financial_type_id", "value=1");
+
+    // total amount
+    $this->type("total_amount", "0.00");
+
+    // select payment instrument
+    $this->select("payment_instrument_id", "value=1");
+
+    $this->type("trxn_id", "X20901X1" . rand(100, 10000));
+    $this->clickLink('_qf_Contribution_upload-bottom');
+    $this->waitForText("crm-notification-container", "The contribution record has been saved.");
+
+    $this->waitForElementPresent( "xpath=//div[@id='Contributions']//table//tbody/tr[1]/td[8]/span/a[text()='View']" );
+    $this->clickLink( "xpath=//div[@id='Contributions']//table/tbody/tr[1]/td[8]/span/a[text()='View']" );
+    $expected = array(
+      'Financial Type'   => 'Donation',
+      'Total Amount'        => '0.00',
+      'Contribution Status' => 'Completed',
+      'Paid By'             => 'Credit Card'
+    );
+    $this->webtestVerifyTabularData($expected);
+  }
 }
diff --git a/tests/phpunit/WebTest/Contribute/OnlineContributionTest.php b/tests/phpunit/WebTest/Contribute/OnlineContributionTest.php
index 5d38797a06..12f63dc29d 100644
--- a/tests/phpunit/WebTest/Contribute/OnlineContributionTest.php
+++ b/tests/phpunit/WebTest/Contribute/OnlineContributionTest.php
@@ -171,5 +171,139 @@ class WebTest_Contribute_OnlineContributionTest extends CiviSeleniumTestCase {
     $this->assertTrue($this->isTextPresent("$honorDisplayName"), "Honoree contact not found.");
 
     }
+
+  function testOnlineContributionWithZeroAmount () {
+    $this->webtestLogin();
+
+    // We need a payment processor
+    $processorName = "Webtest Dummy" . substr(sha1(rand()), 0, 7);
+    $processorType = 'Dummy';
+    $pageTitle = substr(sha1(rand()), 0, 7);
+    $rand = 2 * rand(10, 50);
+    $hash = substr(sha1(rand()), 0, 7);
+    $amountSection = TRUE;
+    $payLater = FALSE;
+    $onBehalf = FALSE;
+    $pledges = FALSE;
+    $recurring = FALSE;
+    $memberships = FALSE;
+    $friend = FALSE;
+    $profilePreId = NULL;
+    $profilePostId = NULL;
+    $premiums = FALSE;
+    $widget = FALSE;
+    $pcp = FALSE;
+    $memPriceSetId = NULL;
+
+    // create a new online contribution page
+    // create contribution page with randomized title and default params
+    $pageId = $this->webtestAddContributionPage($hash,
+      $rand,
+      $pageTitle,
+      array($processorName => $processorType),
+      $amountSection,
+      $payLater,
+      $onBehalf,
+      $pledges,
+      $recurring,
+      $memberships,
+      $memPriceSetId,
+      $friend,
+      $profilePreId,
+      $profilePostId,
+      $premiums,
+      $widget,
+      $pcp
+    );
+    
+    $this->openCiviPage("admin/contribute/amount", "reset=1&action=update&id=$pageId", '_qf_Amount_cancel-bottom');
+    $this->type('label_1', "Label $hash");
+    $this->type('value_1', 0);
+    $this->clickLink('_qf_Amount_upload_done-top');
+
+    //Contribution using Contribution Options
+    $this->_doContributionAndVerifyData($pageId);
+    
+    //add priceset
+    $this->openCiviPage("admin/price", "reset=1&action=add", '_qf_Set_next-bottom');
+    $this->type('title', "Test Priceset $rand");
+    $this->check('extends_2');
+    $this->select("financial_type_id", "label=Donation");
+    $this->clickLink('_qf_Set_next-bottom', '_qf_Field_next-bottom');
+    $sid = $this->urlArg('sid');
+    //add field
+    $this->type('label', "Testfield");
+    $this->select('html_type', "value=Radio");
+    $this->type('option_label_1', 'test Label');
+    $this->type('option_amount_1', 0.00);
+    $this->clickLink('_qf_Field_next_new-bottom', '_qf_Field_next-bottom');
+    $this->openCiviPage("admin/contribute/amount", "reset=1&action=update&id=$pageId", '_qf_Amount_cancel-bottom');
+    $this->select('price_set_id', "value=$sid");
+    $this->clickLink('_qf_Amount_upload_done-bottom');
+
+    //Contribution using priceset
+    $this->_doContributionAndVerifyData($pageId, TRUE);   
+  }
+
+  function _doContributionAndVerifyData($pageId, $priceSet = FALSE) {
+    //logout
+    $this->webtestLogout();
+    $amountLabel = 'Total Amount';
+    $amountValue = '0.00';
+    //Open Live Contribution Page
+    $this->openCiviPage("contribute/transact", "reset=1&id=$pageId", "_qf_Main_upload-bottom");
+
+    $firstName = 'Ma' . substr(sha1(rand()), 0, 4);
+    $lastName = 'An' . substr(sha1(rand()), 0, 7);
+
+    $this->type("email-5", $firstName . "@example.com");
+
+    if ($priceSet) {
+      $this->click("xpath=//div[@id='priceset']/div/div[2]/div/span/input");
+      $amountLabel = 'Contribution Amount';
+      $amountValue = 'Contribution Total: $ 0.00';
+    }
+
+    //Credit Card Info
+    $this->select("credit_card_type", "value=Visa");
+    $this->type("credit_card_number", "4111111111111111");
+    $this->type("cvv2", "000");
+    $this->select("credit_card_exp_date[M]", "value=1");
+    $this->select("credit_card_exp_date[Y]", "value=2020");
+
+    //Billing Info
+    $this->type("billing_first_name", $firstName);
+    $this->type("billing_last_name", $lastName);
+    $this->type("billing_street_address-5", "15 Main St.");
+    $this->type(" billing_city-5", "San Jose");
+    $this->select("billing_country_id-5", "value=1228");
+    $this->select("billing_state_province_id-5", "value=1004");
+    $this->type("billing_postal_code-5", "94129");
+    $this->clickLink("_qf_Main_upload-bottom", "_qf_Confirm_next-bottom");
+
+    $this->clickLink("_qf_Confirm_next-bottom", NULL);
+
+      
+    //login to check contribution
+
+    // Log in using webtestLogin() method
+    $this->webtestLogin();
+
+      //Find Contribution
+    $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low");
+
+    $this->type("sort_name", "$firstName $lastName");
+    $this->clickLink("_qf_Search_refresh", "xpath=//div[@id='contributionSearch']//table//tbody/tr[1]/td[11]/span/a[text()='View']");
+    $this->clickLink("xpath=//div[@id='contributionSearch']//table//tbody/tr[1]/td[11]/span/a[text()='View']", "_qf_ContributionView_cancel-bottom");
+
+    //View Contribution Record and verify data
+    $expected = array(
+      'From' => "{$firstName} {$lastName}",
+      'Financial Type' => 'Donation',
+      $amountLabel => $amountValue,
+      'Contribution Status' => 'Completed'
+    );
+    $this->webtestVerifyTabularData($expected);
   }
+}
 
-- 
GitLab