+--------------------------------------------------------------------+
*/
+use Civi\Api4\PriceField;
+
/**
* Test APIv3 civicrm_contribute_* functions
*
/**
* Given a membership type ID, return the price field value.
*/
- private function getPriceFieldValue($membershipTypeId) {
- return $this->callAPISuccessGetValue('PriceFieldValue', ['membership_type_id' => $membershipTypeId, 'return' => 'id']);
+ private function getPriceFieldValue($membershipTypeID): int {
+ return (int) $this->callAPISuccessGetSingle('PriceFieldValue', ['membership_type_id' => $membershipTypeID, 'return' => 'id', 'price_field_id' => $this->ids['PriceField']['membership']])['id'];
}
/**
'credit_card_exp_date' => ['M' => 9, 'Y' => 2040],
'cvv2' => 123,
'auto_renew' => 1,
- 'priceSetId' => $this->priceSetId,
+ 'priceSetId' => $this->ids['PriceSet']['membership'],
];
}
* Test that the membership is set to recurring if the membership type is always autorenew.
*/
public function testSetRecurFunction(): void {
- $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
+ $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
- $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
- $form->testSubmit(array_merge($this->getSubmitParams(), [
- 'price_' . $this->priceSetId => $priceFieldValueId,
- ]));
- $this->assertEquals(1, $form->_params['is_recur']);
+ $form->postProcess();
+ $this->assertIsRecur(1);
}
/**
* Test that the membership is set to recurring if the membership type is optionally autorenew and is_recur is true.
*/
public function testSetRecurFunctionOptionalYes(): void {
- $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
+ $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
- $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
- $form->testSubmit(array_merge($this->getSubmitParams(), [
- 'price_' . $this->priceSetId => $priceFieldValueId,
- ]));
- $this->assertEquals(1, $form->_params['is_recur']);
+ $form->postProcess();
+ $this->assertIsRecur(1);
}
/**
* Test that the membership is not set to recurring if the membership type is optionally autorenew and is_recur is false.
*/
public function testSetRecurFunctionOptionalNo(): void {
- $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
- $form = $this->getContributionForm();
- $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
- $form->testSubmit(array_merge($this->getSubmitParams(), [
- 'price_' . $this->priceSetId => $priceFieldValueId,
- 'auto_renew' => 0,
- ]));
- $this->assertEquals(0, $form->_params['is_recur']);
+ $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
+ $form = $this->getContributionForm(['auto_renew' => 0]);
+ $form->postProcess();
+ $this->assertIsRecur(0);
}
/**
* Test that the membership doesn't have an "is_recur" key if the membership type can never autorenew.
*/
- public function testSetRecurFunctionNotAvailable() {
- $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
+ public function testSetRecurFunctionNotAvailable(): void {
+ $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
$form = $this->getContributionForm();
- $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
- $form->testSubmit(array_merge($this->getSubmitParams(), [
- 'price_' . $this->priceSetId => $priceFieldValueId,
- ]));
- $this->assertArrayNotHasKey('is_recur', $form->_params);
+ $form->postProcess();
+ $this->assertIsRecur(NULL);
}
/**
*/
protected function getContributionForm(array $submittedValues = [], $params = []): CRM_Contribute_Form_Contribution_Main {
try {
- $this->priceSetId = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
+ $this->ids['PriceSet']['membership'] = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
'name' => 'default_membership_type_amount',
'return' => 'id',
]);
+ $this->ids['PriceField']['membership'] = PriceField::get(FALSE)->addWhere('price_set_id', '=', $this->ids['PriceSet']['membership'])->execute()->first()['id'];
$paymentProcessor = $submittedValues['payment_processor_id'] = $this->paymentProcessorCreate([
'payment_processor_type_id' => 'Dummy',
'amount_block_is_active' => 1,
]));
$contributionPage = $this->contributionPageCreate($contributionPageParams);
+ CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->ids['PriceSet']['membership']);
+
+ $submittedValues = array_merge($this->getSubmitParams(), [
+ 'price_' . $this->ids['PriceField']['membership'] => $this->getPriceFieldValue($this->ids['MembershipType']['test']),
+ ], $submittedValues);
$submittedValues['id'] = $_REQUEST['id'] = (int) $contributionPage['id'];
/** @var \CRM_Contribute_Form_Contribution_Main $form */
$form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main', $submittedValues);
- $form->set('id', $contributionPage['id']);
- CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->priceSetId);
$form->preProcess();
+ $form->_paymentProcessor['object']->setSupports(['PreApproval' => TRUE, 'BackOffice' => TRUE]);
$form->buildQuickForm();
// Need these values to create more realistic submit params (in getSubmitParams).
$this->paymentProcessorId = $paymentProcessor;
}
/**
- * Test expired priceset are not returned from buildPriceSet() Function
+ * Test expired priceset are not returned from buildPriceSet() Function.
*/
public function testExpiredPriceSet(): void {
$priceSetParams1 = [
'is_quick_config' => 1,
'is_reserved' => 1,
];
- $priceSet = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
+ $priceSet = $this->createTestEntity('PriceSet', $priceSetParams1);
// Create valid price field.
$params = [
- 'price_set_id' => $priceSet['id'],
- 'name' => 'testvalidpf',
- 'label' => 'test valid pf',
+ 'price_set_id' => $this->ids['PriceSet']['default'],
+ 'name' => 'test_valid_price_field',
+ 'label' => 'test valid price field',
'html_type' => 'Radio',
'is_enter_qty' => 1,
'is_active' => 1,
];
- $priceField1 = $this->callAPISuccess('PriceField', 'create', $params);
+ $priceField1 = $this->createTestEntity('PriceField', $params);
// Create expired price field.
$params = [
'price_set_id' => $priceSet['id'],
- 'name' => 'testexpiredpf',
- 'label' => 'test expired pf',
+ 'name' => 'test_expired_price_field',
+ 'label' => 'test expired price field',
'html_type' => 'Radio',
'is_enter_qty' => 1,
'is_active' => 1,
- 'expire_on' => date('Y-m-d', strtotime("-1 days")),
+ 'expire_on' => date('Y-m-d', strtotime('-1 days')),
];
- $priceField2 = $this->callAPISuccess('PriceField', 'create', $params);
+ $priceField2 = $this->createTestEntity('PriceField', $params, 'expired');
//Create price options.
- $membershipOrgId = $this->organizationCreate();
- $memtype = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]);
+ $this->membershipTypeCreate(['member_of_contact_id' => $this->organizationCreate()]);
foreach ([$priceField1, $priceField2] as $priceField) {
$priceFieldValueParams = [
'price_field_id' => $priceField['id'],
'name' => 'rye grass',
- 'membership_type_id' => $memtype,
+ 'membership_type_id' => $this->ids['MembershipType']['test'],
'label' => 'juicy and healthy',
'amount' => 1,
'membership_num_terms' => 2,
}
$form = $this->getContributionForm([], ['priceSetID' => $priceSet['id']]);
- foreach ($form->_priceSet['fields'] as $pField) {
- foreach ($pField['options'] as $opId => $opValues) {
+ foreach ($form->_priceSet['fields'] as $priceField) {
+ foreach ($priceField['options'] as $opValues) {
$membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
}
}
//This function should not update form priceSet with the expired one.
CRM_Price_BAO_PriceSet::buildPriceSet($form);
- $this->assertEquals(1, count($form->_priceSet['fields']));
+ $this->assertCount(1, $form->_priceSet['fields']);
$field = current($form->_priceSet['fields']);
- $this->assertEquals('testvalidpf', $field['name']);
+ $this->assertEquals('test_valid_price_field', $field['name']);
+ }
+
+ /**
+ * @param int|null $expected
+ */
+ protected function assertIsRecur(?int $expected): void {
+ $isRecur = \Civi::$statics['CRM_Core_Payment_Dummy']['doPreApproval']['is_recur'] ?? NULL;
+ $this->assertEquals($expected, $isRecur);
}
}