From c59e7960cc476524db0aac9b58c8cfa964bea194 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 17 Sep 2018 14:58:42 +1200 Subject: [PATCH] Add validate api action for ContributionPage.submit --- CRM/Contribute/Form/Contribution/Main.php | 6 +-- CRM/Contribute/Form/ContributionBase.php | 8 ++++ api/v3/ContributionPage.php | 20 +++++++++ tests/phpunit/api/v3/ContributionPageTest.php | 41 +++++++++++++++---- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 4a302b82b7..ed56fd99e6 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -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 { diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index ec174f47ba..9b378cc4a2 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -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. * diff --git a/api/v3/ContributionPage.php b/api/v3/ContributionPage.php index 8506143621..461116c497 100644 --- a/api/v3/ContributionPage.php +++ b/api/v3/ContributionPage.php @@ -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. diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 2d908fb363..a9cafed9c7 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -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; + } + } -- 2.25.1