Skip to content

Commit

Permalink
Merge pull request #29348 from eileenmcnaughton/crm_member
Browse files Browse the repository at this point in the history
Fix BackOffice Membership forms getPriceSetID() to be standard
  • Loading branch information
colemanw authored Feb 10, 2024
2 parents 06df990 + e6e6891 commit 7b6ec92
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
25 changes: 23 additions & 2 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
*/
public $_tagsetInfo;

/**
* @var true
*/
private bool $isValidated = FALSE;

/**
* @return string
*/
Expand Down Expand Up @@ -734,7 +739,7 @@ public function validate() {
if (!empty($hookErrors)) {
$this->_errors += $hookErrors;
}

$this->isValidated = TRUE;
return (0 == count($this->_errors));
}

Expand Down Expand Up @@ -3087,8 +3092,12 @@ protected function getContactIDIfAccessingOwnRecord() {
*
* These values have been validated against the fields added to the form.
* https://pear.php.net/manual/en/package.html.html-quickform.html-quickform.exportvalues.php
* unless the function is being called during before the submission has
* been validated. In which the values are not yet validated & hence
* taking directly from $_POST.
*
* Any money processing has also been done.
* Fields with money or number formats are converted from localised formats
* before returning.
*
* @param string $fieldName
*
Expand All @@ -3100,6 +3109,18 @@ protected function getContactIDIfAccessingOwnRecord() {
*/
public function getSubmittedValue(string $fieldName) {
if (empty($this->exportedValues)) {
if (!$this->isValidated) {
// Trying to access the submitted value before during during validate.
// In this case we get the submitValue which is equivalent to the value
// in $_POST. By contrast exportValues will filter out fields
// that have not been added to QuickForm.
$value = $this->getSubmitValue($fieldName);
if (is_string($value)) {
// Precaution since we are dealing with values directly in $_POST.
$value = CRM_Utils_String::purifyHTML($value);
}
return $this->getUnLocalizedSubmittedValue($fieldName, $value);
}
$this->exportedValues = $this->controller->exportValues($this->_name);
}
$value = $this->exportedValues[$fieldName] ?? NULL;
Expand Down
21 changes: 11 additions & 10 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,20 +551,21 @@ protected function getPriceSetDetails(array $params): ?array {
}

/**
* Get the selected price set id.
* Get the price set ID.
*
* @param array $params
* Parameters submitted to the form.
* @api Supported for external use.
*
* @return int
*/
protected function getPriceSetID(array $params): int {
$priceSetID = $params['price_set_id'] ?? NULL;
if (!$priceSetID) {
$priceSetDetails = $this->getPriceSetDetails($params);
return (int) key($priceSetDetails);
public function getPriceSetID(): int {
$this->_priceSetId = $this->getSubmittedValue('price_set_id') ?? NULL;
if (!$this->_priceSetId) {
$priceSet = CRM_Price_BAO_PriceSet::getDefaultPriceSet('membership');
$priceSet = reset($priceSet);
$priceSetDetails = CRM_Price_BAO_PriceSet::getSetDetail($priceSet['setID']);
$this->_priceSetId = key($priceSetDetails);
}
return (int) $priceSetID;
return (int) $this->_priceSetId;
}

/**
Expand All @@ -577,7 +578,7 @@ protected function getPriceSetID(array $params): int {
*/
protected function setPriceSetParameters(array $formValues): array {
// process price set and get total amount and line items.
$this->_priceSetId = $this->getPriceSetID($formValues);
$this->getPriceSetID();
$this->ensurePriceParamsAreSet($formValues);
$priceSetDetails = $this->getPriceSetDetails($formValues);
$this->_priceSet = $priceSetDetails[$this->_priceSetId];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public function buildQuickForm() {
public static function formRule($params, $files, $self) {
$errors = [];

$priceSetId = $self->getPriceSetID($params);
$priceSetId = $self->getPriceSetID();
$priceSetDetails = $self->getPriceSetDetails($params);

$selectedMemberships = self::getSelectedMemberships($priceSetDetails[$priceSetId], $params);
Expand Down
16 changes: 7 additions & 9 deletions ext/contributioncancelactions/tests/phpunit/CancelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Civi\Api4\Contribution;
use Civi\Test\Api3TestTrait;
use Civi\Test\CiviEnvBuilder;
use Civi\Test\FormTrait;
use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;
Expand Down Expand Up @@ -35,6 +36,7 @@ class CancelTest extends TestCase implements HeadlessInterface, HookInterface, T

use Api3TestTrait;
use ContactTestTrait;
use FormTrait;

/**
* Created ids.
Expand Down Expand Up @@ -271,17 +273,13 @@ public function testCancelFromContributionForm(): void {
$this->createLoggedInUser();
$formValues = [
'contact_id' => $this->ids['contact'][0],
'financial_type_id' => 1,
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Cancelled'),
];
$form = new CRM_Contribute_Form_Contribution();
$_SERVER['REQUEST_METHOD'] = 'GET';
$form->controller = new CRM_Core_Controller();
$form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
$_SESSION['_' . $form->controller->_name . '_container']['values']['Contribution'] = $formValues;
$_REQUEST['action'] = 'update';
$_REQUEST['id'] = $this->ids['Contribution'][0];
$form->buildForm();
$form->postProcess();
$this->getTestForm('CRM_Contribute_Form_Contribution', $formValues, [
'action' => 'update',
'id' => $this->ids['Contribution'][0],
])->processForm();

$contribution = Contribution::get()
->addWhere('id', '=', $this->ids['Contribution'][0])
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ protected function getForm(array $eventParams = [], array $submittedValues = [],
$event = $this->eventCreateUnpaid($eventParams);
}
$submittedValues['event_id'] = $event['id'];
$submittedValues['_qf_default'] = 'Builder:refresh';
$submittedValues['receipt_text'] = 'Contact the Development Department if you need to make any changes to your registration.';
return $this->getTestForm('CRM_Event_Form_Participant', $submittedValues, ['cid' => $submittedValues['contact_id']])->processForm(FormWrapper::BUILT);
}
Expand All @@ -403,7 +404,7 @@ protected function getForm(array $eventParams = [], array $submittedValues = [],
*/
protected function submitForm(array $eventParams = [], array $submittedValues = [], bool $isQuickConfig = FALSE): EventFormParticipant {
$form = $this->getForm($eventParams, $submittedValues, $isQuickConfig);
$form->postProcess();
$form->processForm();
return $form;
}

Expand Down

0 comments on commit 7b6ec92

Please sign in to comment.