Skip to content

Commit

Permalink
Merge pull request #13798 from eileenmcnaughton/contribution_validate
Browse files Browse the repository at this point in the history
Improve ContributionPage.validate api
  • Loading branch information
colemanw authored Mar 22, 2019
2 parents 839bb47 + a1a0130 commit 8767e00
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
22 changes: 22 additions & 0 deletions api/v3/ContributionPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ function civicrm_api3_contribution_page_submit($params) {
* API result array
*/
function civicrm_api3_contribution_page_validate($params) {
// If we are calling this as a result of a POST action (e.g validating a form submission before first getting payment
// authorization from a payment processor like Paypal checkout) the lack of a qfKey will not result in a valid
// one being generated so we generate one first.
$originalRequest = $_REQUEST;
$qfKey = CRM_Utils_Array::value('qfKey', $_REQUEST);
if (!$qfKey) {
$_REQUEST['qfKey'] = CRM_Core_Key::get('CRM_Core_Controller', TRUE);
}
$form = new CRM_Contribute_Form_Contribution_Main();
$form->controller = new CRM_Core_Controller();
$form->set('id', $params['id']);
Expand All @@ -122,9 +130,23 @@ function civicrm_api3_contribution_page_validate($params) {
if ($errors === TRUE) {
$errors = [];
}
$_REQUEST = $originalRequest;
return civicrm_api3_create_success($errors, $params, 'ContributionPage', 'validate');
}

/**
* Metadata for validate action.
*
* @param array $params
*/
function _civicrm_api3_contribution_page_validate_spec(&$params) {
$params['id'] = [
'title' => ts('Contribution Page ID'),
'api.required' => TRUE,
'type' => CRM_Utils_Type::T_INT,
];
}

/**
* Set default getlist parameters.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/api/v3/ContributionPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,37 @@ public function testValidate() {
$this->assertEmpty($errors);
}

/**
* Test validating a contribution page submit in POST context.
*
* A likely use case for the validation is when the is being submitted and some handling is
* to be done before processing but the validity of input needs to be checked first.
*
* For example Paypal Checkout will replace the confirm button with it's own but we are able to validate
* before paypal launches it's modal. In this case the $_REQUEST is post but we need validation to succeed.
*/
public function testValidatePost() {
$_SERVER['REQUEST_METHOD'] = 'POST';
$this->setUpContributionPage();
$errors = $this->callAPISuccess('ContributionPage', 'validate', array_merge($this->getBasicSubmitParams(), ['action' => 'submit']))['values'];
$this->assertEmpty($errors);
unset($_SERVER['REQUEST_METHOD']);
}

/**
* Test that an error is generated if required fields are not submitted.
*/
public function testValidateOutputOnMissingRecurFields() {
$this->params['is_recur_interval'] = 1;
$this->setUpContributionPage(TRUE);
$submitParams = array_merge($this->getBasicSubmitParams(), ['action' => 'submit']);
$submitParams['is_recur'] = 1;
$submitParams['frequency_interval'] = '';
$submitParams['frequency_unit'] = '';
$errors = $this->callAPISuccess('ContributionPage', 'validate', $submitParams)['values'];
$this->assertEquals('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).', $errors['frequency_interval']);
}

/**
* Implements hook_civicrm_alterPaymentProcessorParams().
*
Expand Down

0 comments on commit 8767e00

Please sign in to comment.