Add validate api action for ContributionPage.submit
authoreileen <emcnaughton@wikimedia.org>
Mon, 17 Sep 2018 02:58:42 +0000 (14:58 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 7 Oct 2018 14:59:37 +0000 (15:59 +0100)
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php
api/v3/ContributionPage.php
tests/phpunit/api/v3/ContributionPageTest.php

index 4a302b82b70a19e761745512bccbaa737e23d24a..ed56fd99e680f87b6f0618930f6cfc8623abe8e4 100644 (file)
@@ -626,7 +626,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
    *   The input form values.
    * @param array $files
    *   The uploaded files if any.
-   * @param CRM_Core_Form $self
+   * @param \CRM_Contribute_Form_Contribution_Main $self
    *
    * @return bool|array
    *   true if no errors, else array of errors
@@ -670,7 +670,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       $previousId = $otherAmount = FALSE;
       while ($priceField->fetch()) {
 
-        if ($self->_quickConfig && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) {
+        if ($self->isQuickConfig() && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) {
           $previousId = $priceField->id;
           if ($priceField->name == 'membership_amount' && !$priceField->is_active) {
             $membershipIsActive = FALSE;
@@ -951,7 +951,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       return $errors;
     }
 
-    if (CRM_Utils_Array::value('payment_processor_id', $fields) == NULL) {
+    if (CRM_Utils_Array::value('payment_processor_id', $fields) === NULL) {
       $errors['payment_processor_id'] = ts('Payment Method is a required field.');
     }
     else {
index ec174f47baa4e70501a4b8640ffb976714c02e54..9b378cc4a260befd9074c767c00c2b0a8125f4d6 100644 (file)
@@ -211,6 +211,14 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    */
   public $paymentInstrumentID;
 
+  /**
+   * Is the price set quick config.
+   * @return bool
+   */
+  public function isQuickConfig() {
+    return isset(self::$_quickConfig) ? self::$_quickConfig : FALSE;
+  }
+
   /**
    * Set variables up before form is built.
    *
index 8506143621980477071890bf0e370c7faf9f7648..461116c4972b24613a5c01670431259bda040a98 100644 (file)
@@ -104,6 +104,26 @@ function civicrm_api3_contribution_page_submit($params) {
   return civicrm_api3_create_success($result, $params, 'ContributionPage', 'submit');
 }
 
+/**
+ * Validate ContributionPage submission parameters.
+ *
+ * @param array $params
+ *   Array per getfields metadata.
+ *
+ * @return array
+ *   API result array
+ */
+function civicrm_api3_contribution_page_validate($params) {
+  $form = new CRM_Contribute_Form_Contribution_Main();
+  $form->controller = new CRM_Core_Controller();
+  $form->set('id', $params['id']);
+  $form->preProcess();
+  $errors = CRM_Contribute_Form_Contribution_Main::formRule($params, [], $form);
+  if ($errors === TRUE) {
+    $errors = [];
+  }
+  return civicrm_api3_create_success($errors, $params, 'ContributionPage', 'validate');
+}
 
 /**
  * Set default getlist parameters.
index 2d908fb363bb147dc89835a55e93c5c6ec8d5b6d..a9cafed9c7cb9978fcacd3df7ab6fac002eec796 100644 (file)
@@ -139,13 +139,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    */
   public function testSubmit() {
     $this->setUpContributionPage();
-    $priceFieldID = reset($this->_ids['price_field']);
-    $priceFieldValueID = reset($this->_ids['price_field_value']);
-    $submitParams = array(
-      'price_' . $priceFieldID => $priceFieldValueID,
-      'id' => (int) $this->_ids['contribution_page'],
-      'amount' => 10,
-    );
+    $submitParams = $this->getBasicSubmitParams();
 
     $this->callAPISuccess('contribution_page', 'submit', $submitParams);
     $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
@@ -1887,6 +1881,21 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals($lineItem_TaxAmount, round(180 * 16.95 * 0.10, 2), 'Wrong Sales Tax Amount is calculated and stored.');
   }
 
+
+  /**
+   * Test validating a contribution page submit.
+   */
+  public function testValidate() {
+    $this->setUpContributionPage();
+    $errors = $this->callAPISuccess('ContributionPage', 'validate', array_merge($this->getBasicSubmitParams(), ['action' => 'submit']))['values'];
+    $this->assertEmpty($errors);
+  }
+
+  /**
+   * Implements hook_civicrm_alterPaymentProcessorParams().
+   *
+   * @throws \Exception
+   */
   public function hook_civicrm_alterPaymentProcessorParams($paymentObj, &$rawParams, &$cookedParams) {
     // Ensure total_amount are the same if they're both given.
     $total_amount = CRM_Utils_Array::value('total_amount', $rawParams);
@@ -1907,4 +1916,22 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $log->debug($message, $_REQUEST);
   }
 
+  /**
+   * Get the params for a basic simple submit.
+   *
+   * @return array
+   */
+  protected function getBasicSubmitParams() {
+    $priceFieldID = reset($this->_ids['price_field']);
+    $priceFieldValueID = reset($this->_ids['price_field_value']);
+    $submitParams = [
+      'price_' . $priceFieldID => $priceFieldValueID,
+      'id' => (int) $this->_ids['contribution_page'],
+      'amount' => 10,
+      'priceSetId' => $this->_ids['price_set'][0],
+      'payment_processor_id' => 0,
+    ];
+    return $submitParams;
+  }
+
 }