CRM-14778 add preliminary unit test on postProcess function
authorEileen McNaughton <eileen@fuzion.co.nz>
Tue, 3 Jun 2014 22:59:40 +0000 (10:59 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Tue, 3 Jun 2014 22:59:40 +0000 (10:59 +1200)
CRM/Contribute/Form/Contribution/Confirm.php
api/v3/ContributionPage.php
tests/phpunit/api/v3/ContributionPageTest.php

index 1213979019eee18150efbe48dcc555608acd2cb5..88d951705b23b907452370af338f2da1b029baf8 100644 (file)
@@ -1818,4 +1818,44 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       }
     }
   }
+
+  /**
+   * Static submit function allowing tests (& api access although this is being built slowly)
+   * @param $params
+   */
+  static function submit($params) {
+    $form = new CRM_Contribute_Form_Contribution_Confirm();
+    $form->_id = $params['id'];
+    //this way the mocked up controller ignores the session stuff
+    $_SERVER['REQUEST_METHOD'] = 'GET';
+    $form->controller = new CRM_Contribute_Controller_Contribution();
+    $params['invoiceID'] = md5(uniqid(rand(), TRUE));
+    $paramsProcessedForForm = $form->_params = self::getFormParams($params['id'], $params);
+
+    $priceSetID = $form->_params['priceSetId'] = $paramsProcessedForForm['price_set_id'];
+    $priceFields = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID);
+    $form->setFormAmountFields($priceSetID);
+    $priceFields = $priceFields[$priceSetID]['fields'];
+    CRM_Price_BAO_PriceSet::processAmount($priceFields, $paramsProcessedForForm, $lineItems, 'civicrm_contribution');
+    $form->_lineItem = array($priceSetID => $lineItems);
+    $form->postProcess();
+  }
+
+  /**
+   * Helper function for static submit function - set relevant params - help us to build up an array that we can pass in
+   * @param $id
+   * @param array $params
+   *
+   * @return array
+   * @throws CiviCRM_API3_Exception
+   */
+  static function getFormParams($id, array $params) {
+    if(!isset($params['is_pay_later'])) {
+      $params['is_pay_later'] = civicrm_api3('contribution_page', 'getvalue', array('id' => $id, 'return' => 'is_pay_later'));
+    }
+    if(empty($params['price_set_id'])) {
+      $params['price_set_id'] = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $params['id']);
+    }
+    return $params;
+  }
 }
index bd11bdda317c76d1b1e7f85dc933499de8c325c5..360128ba95f7a0e981f830caff18517a430bca9e 100644 (file)
@@ -92,3 +92,22 @@ function civicrm_api3_contribution_page_get($params) {
 function civicrm_api3_contribution_page_delete($params) {
   return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
 }
+
+/**
+ * delete an existing contribution_page
+ *
+ * This method is used to delete any existing contribution_page. id of the group
+ * to be deleted is required field in $params array
+ *
+ * @param array $params  (reference) array containing id of the group
+ *                       to be deleted
+ *
+ * @return array  (referance) returns flag true if successfull, error
+ *                message otherwise
+ * {@getfields contribution_page_delete}
+ * @access public
+ */
+function civicrm_api3_contribution_page_submit($params) {
+  $result = CRM_Contribute_Form_Contribution_Confirm::submit($params);
+  return civicrm_api3_create_success($result, $params, 'contribution_page', 'submit');
+}
index bf726abde79a15ab18aa2a8f41a9c0b7436a0b34..ae5041d3ba54d5dba6136b8fdd7b6b521d9cfc3f 100644 (file)
@@ -108,6 +108,47 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals(12, $result['values']['start_date']['type']);
   }
 
+
+  public function testSubmit() {
+    $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $priceSetParams = array(
+      'is_quick_config' => 1,
+      'extends' => 'CiviContribute',
+      'financial_type_id' => 'Donation',
+      'title' => 'my Page'
+    );
+
+    $priceSet = $this->callAPISuccess('price_set', 'create', $priceSetParams);
+    CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSet['id']);
+    $priceField = $this->callAPISuccess('price_field', 'create', array(
+      'price_set_id' => $priceSet['id'],
+      'label' => 'Goat Breed',
+      'html_type' => 'Radio',
+    ));
+    $this->callAPISuccess('price_field_value', 'create', array(
+      'price_set_id' => $priceSet['id'],
+      'price_field_id' => $priceField['id'],
+      'label' => 'Long Haired Goat',
+      'amount' => 20,
+      )
+    );
+    $priceFieldValue2 = $this->callAPISuccess('price_field_value', 'create', array(
+      'price_set_id' => $priceSet['id'],
+      'price_field_id' => $priceField['id'],
+      'label' => 'Shoe-eating Goat',
+      'amount' => 10,
+      )
+    );
+    $submitParams = array(
+      'price_' . $priceField['id'] => $priceFieldValue2['id'],
+      'id' => (int) $contributionPageResult['id'],
+      'amount' => 10
+    );
+
+    $this->callAPISuccess('contribution_page', 'submit', $submitParams);
+    $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $contributionPageResult['id']));
+  }
+
   public static function setUpBeforeClass() {
       // put stuff here that should happen before all tests in this unit
   }