Add unit test
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 15 Jul 2016 13:23:55 +0000 (18:53 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 15 Jul 2016 13:26:34 +0000 (18:56 +0530)
CRM/Contribute/Form/Task/Status.php
tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php [new file with mode: 0644]
tests/phpunit/CRM/Custom/Page/AjaxTest.php

index e21c6cec51bec1c69704de27d633173a179a0d57..26aefacf3c60911efd07409efaf505614ed1afcb 100644 (file)
@@ -82,6 +82,13 @@ AND    {$this->_componentClause}";
     $this->assign('single', $this->_single);
   }
 
+  /**
+   * Sets contribution Ids for unit test.
+   */
+  public function setContributionIds($contributionIds) {
+    $this->_contributionIds = $contributionIds;
+  }
+
   /**
    * Build the form object.
    */
@@ -209,19 +216,30 @@ AND    co.id IN ( $contribIDs )";
    */
   public function postProcess() {
     $params = $this->controller->exportValues($this->_name);
-    $statusID = CRM_Utils_Array::value('contribution_status_id', $params);
 
+    // submit the form with values.
+    self::processForm($this, $params);
+
+    CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success');
+  }
+
+  /**
+   * Process the form with submitted params.
+   * Also supports unit test.
+   */
+  public static function processForm($form, $params) {
+    $statusID = CRM_Utils_Array::value('contribution_status_id', $params);
     $baseIPN = new CRM_Core_Payment_BaseIPN();
 
     $transaction = new CRM_Core_Transaction();
 
     // get the missing pieces for each contribution
-    $contribIDs = implode(',', $this->_contributionIds);
+    $contribIDs = implode(',', $form->_contributionIds);
     $details = self::getDetails($contribIDs);
     $template = CRM_Core_Smarty::singleton();
 
     // for each contribution id, we just call the baseIPN stuff
-    foreach ($this->_rows as $row) {
+    foreach ($form->_rows as $row) {
       $input = $ids = $objects = array();
       $input['component'] = $details[$row['contribution_id']]['component'];
 
@@ -285,8 +303,6 @@ AND    co.id IN ( $contribIDs )";
       // reset template values before processing next transactions
       $template->clearTemplateVars();
     }
-
-    CRM_Core_Session::setStatus(ts('Contribution status has been updated for selected record(s).'), ts('Status Updated'), 'success');
   }
 
   /**
diff --git a/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php b/tests/phpunit/CRM/Contribute/Form/Task/StatusTest.php
new file mode 100644 (file)
index 0000000..dd9235e
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Contribute_Form_Task_StatusTest
+ */
+class CRM_Contribute_Form_Task_StatusTest extends CiviUnitTestCase {
+  /**
+   * Assume empty database with just civicrm_data.
+   */
+  protected $_individualId;
+
+  /**
+   * Clean up after each test.
+   */
+  public function tearDown() {
+    $this->quickCleanUpFinancialEntities();
+    CRM_Utils_Hook::singleton()->reset();
+  }
+
+  /**
+   * Test update pending contribution
+   */
+  public function testUpdatePendingContribution() {
+    $this->_individualId = $this->individualCreate();
+    $form = new CRM_Contribute_Form_Task_Status();
+
+    // create a pending contribution
+    $contributionParams = array(
+      'contact_id' => $this->_individualId,
+      'total_amount' => 100,
+      'financial_type_id' => 'Donation',
+      'contribution_status_id' => 2,
+    );
+    $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
+    $contributionId = $contribution['id'];
+    $form->setContributionIds(array($contributionId));
+
+    $form->buildQuickForm();
+
+    $params = array(
+      "contribution_status_id" => 1,
+      "trxn_id_{$contributionId}" => NULL,
+      "check_number_{$contributionId}" => NULL,
+      "fee_amount_{$contributionId}" => 0,
+      "trxn_date_{$contributionId}" => date('m/d/Y'),
+      "payment_instrument_id_{$contributionId}" => 4,
+    );
+
+    CRM_Contribute_Form_Task_Status::processForm($form, $params);
+
+    $contribution = $this->callAPISuccess('Contribution', 'get', array('id' => $contributionId));
+    $updatedContribution = $contribution['values'][1];
+
+    $this->assertEquals('', $updatedContribution['contribution_source']);
+    $this->assertEquals(date("Y-m-d"), date("Y-m-d", strtotime($updatedContribution['receive_date'])));
+    $this->assertNotEquals("00:00:00", date("H:i:s", strtotime($updatedContribution['receive_date'])));
+    $this->assertEquals('Completed', $updatedContribution['contribution_status']);
+  }
+
+}
index bbbc8d03b62536ca9c633ef1d0ef447829b8e656..d255094334c7fd27a0a8dab28d3e7ab77257e4ea 100644 (file)
@@ -1,4 +1,29 @@
 <?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
 
 /**
  * Class CRM_Custom_Page_AJAXTest