3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_contribute_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
19 class CRM_Contribute_Form_Contribution_MainTest
extends CiviUnitTestCase
{
24 public function tearDown(): void
{
25 $this->quickCleanUpFinancialEntities();
29 * Test that the membership is set to recurring if the membership type is always autorenew.
31 public function testSetRecurFunction() {
32 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
33 $form = $this->getContributionForm();
35 'selectMembership' => $membershipTypeID,
37 $this->assertEquals(1, $form->_params
['is_recur']);
41 * Test that the membership is set to recurring if the membership type is always autorenew.
43 public function testSetRecurFunctionOptionalYes() {
44 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
45 $form = $this->getContributionForm();
47 'selectMembership' => $membershipTypeID,
50 $this->assertEquals(1, $form->_params
['is_recur']);
54 * Test that the membership is set to recurring if the membership type is always autorenew.
56 public function testSetRecurFunctionOptionalNo() {
57 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
58 $form = $this->getContributionForm();
60 'selectMembership' => $membershipTypeID,
63 $this->assertEquals(0, $form->_params
['is_recur']);
67 * Test that the membership is set to recurring if the membership type is always autorenew.
69 public function testSetRecurFunctionNotAvailable() {
70 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
71 $form = $this->getContributionForm();
73 'selectMembership' => $membershipTypeID,
75 $this->assertArrayNotHasKey('is_recur', $form->_params
);
79 * Get a contribution form object for testing.
81 * @return \CRM_Contribute_Form_Contribution_Main
83 protected function getContributionForm($params = []) {
84 $params['priceSetID'] = $params['priceSetID'] ??
$this->callAPISuccessGetValue('PriceSet', [
85 'name' => 'default_membership_type_amount',
89 $contributionPageParams = (array_merge($params, [
91 'goal_amount' => 6000,
94 'pay_later_text' => 'Front up',
95 'pay_later_receipt' => 'Ta',
96 'is_email_receipt' => 1,
97 'payment_processor' => $this->paymentProcessorCreate([
98 'payment_processor_type_id' => 'Dummy',
101 'amount_block_is_active' => 1,
104 /** @var \CRM_Contribute_Form_Contribution_Main $form */
105 $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main');
106 $contributionPage = reset($this->contributionPageCreate($contributionPageParams)['values']);
107 $form->set('id', $contributionPage['id']);
108 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPage['id'], $params['priceSetID']);
110 $form->buildQuickForm();
115 * Test expired priceset are not returned from buildPriceSet() Function
117 public function testExpiredPriceSet() {
119 'name' => 'priceset',
120 'title' => 'Priceset with Multiple Terms',
123 'financial_type_id' => 2,
124 'is_quick_config' => 1,
127 $priceSet = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
129 // Create valid price field.
131 'price_set_id' => $priceSet['id'],
132 'name' => 'testvalidpf',
133 'label' => 'test valid pf',
134 'html_type' => 'Radio',
138 $priceField1 = $this->callAPISuccess('PriceField', 'create', $params);
140 // Create expired price field.
142 'price_set_id' => $priceSet['id'],
143 'name' => 'testexpiredpf',
144 'label' => 'test expired pf',
145 'html_type' => 'Radio',
148 'expire_on' => date('Y-m-d', strtotime("-1 days")),
150 $priceField2 = $this->callAPISuccess('PriceField', 'create', $params);
152 //Create price options.
153 $membershipOrgId = $this->organizationCreate(NULL);
154 $memtype = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]);
155 foreach ([$priceField1, $priceField2] as $priceField) {
156 $priceFieldValueParams = [
157 'price_field_id' => $priceField['id'],
158 'name' => 'rye grass',
159 'membership_type_id' => $memtype,
160 'label' => 'juicy and healthy',
162 'membership_num_terms' => 2,
163 'financial_type_id' => 1,
165 $this->callAPISuccess('PriceFieldValue', 'create', $priceFieldValueParams);
168 $form = $this->getContributionForm(['priceSetID' => $priceSet['id']]);
169 foreach ($form->_priceSet
['fields'] as $pField) {
170 foreach ($pField['options'] as $opId => $opValues) {
171 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
174 $form->_membershipTypeValues
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($form, $membershipTypeIds);
176 //This function should not update form priceSet with the expired one.
177 CRM_Price_BAO_PriceSet
::buildPriceSet($form);
179 $this->assertEquals(1, count($form->_priceSet
['fields']));
180 $field = current($form->_priceSet
['fields']);
181 $this->assertEquals('testvalidpf', $field['name']);