Merge pull request #21561 from civicrm/5.42
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Contribution / MainTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_contribute_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
17 * @group headless
18 */
19 class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
20
21 /**
22 * Clean up DB.
23 */
24 public function tearDown(): void {
25 $this->quickCleanUpFinancialEntities();
26 parent::tearDown();
27 }
28
29 /**
30 * Test that the membership is set to recurring if the membership type is always autorenew.
31 */
32 public function testSetRecurFunction() {
33 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
34 $form = $this->getContributionForm();
35 $form->testSubmit([
36 'selectMembership' => $membershipTypeID,
37 ]);
38 $this->assertEquals(1, $form->_params['is_recur']);
39 }
40
41 /**
42 * Test that the membership is set to recurring if the membership type is always autorenew.
43 */
44 public function testSetRecurFunctionOptionalYes() {
45 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
46 $form = $this->getContributionForm();
47 $form->testSubmit([
48 'selectMembership' => $membershipTypeID,
49 'is_recur' => 1,
50 ]);
51 $this->assertEquals(1, $form->_params['is_recur']);
52 }
53
54 /**
55 * Test that the membership is set to recurring if the membership type is always autorenew.
56 */
57 public function testSetRecurFunctionOptionalNo() {
58 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
59 $form = $this->getContributionForm();
60 $form->testSubmit([
61 'selectMembership' => $membershipTypeID,
62 'is_recur' => 0,
63 ]);
64 $this->assertEquals(0, $form->_params['is_recur']);
65 }
66
67 /**
68 * Test that the membership is set to recurring if the membership type is always autorenew.
69 */
70 public function testSetRecurFunctionNotAvailable() {
71 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
72 $form = $this->getContributionForm();
73 $form->testSubmit([
74 'selectMembership' => $membershipTypeID,
75 ]);
76 $this->assertArrayNotHasKey('is_recur', $form->_params);
77 }
78
79 /**
80 * Get a contribution form object for testing.
81 *
82 * @return \CRM_Contribute_Form_Contribution_Main
83 */
84 protected function getContributionForm($params = []) {
85 $params['priceSetID'] = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
86 'name' => 'default_membership_type_amount',
87 'return' => 'id',
88 ]);
89
90 $contributionPageParams = (array_merge($params, [
91 'currency' => 'NZD',
92 'goal_amount' => 6000,
93 'is_pay_later' => 0,
94 'is_monetary' => 1,
95 'pay_later_text' => 'Front up',
96 'pay_later_receipt' => 'Ta',
97 'is_email_receipt' => 1,
98 'payment_processor' => $this->paymentProcessorCreate([
99 'payment_processor_type_id' => 'Dummy',
100 'is_test' => 0,
101 ]),
102 'amount_block_is_active' => 1,
103 ]));
104
105 /** @var \CRM_Contribute_Form_Contribution_Main $form */
106 $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main');
107 $contributionPage = reset($this->contributionPageCreate($contributionPageParams)['values']);
108 $form->set('id', $contributionPage['id']);
109 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $params['priceSetID']);
110 $form->preProcess();
111 $form->buildQuickForm();
112 return $form;
113 }
114
115 /**
116 * Test expired priceset are not returned from buildPriceSet() Function
117 */
118 public function testExpiredPriceSet() {
119 $priceSetParams1 = [
120 'name' => 'priceset',
121 'title' => 'Priceset with Multiple Terms',
122 'is_active' => 1,
123 'extends' => 3,
124 'financial_type_id' => 2,
125 'is_quick_config' => 1,
126 'is_reserved' => 1,
127 ];
128 $priceSet = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
129
130 // Create valid price field.
131 $params = [
132 'price_set_id' => $priceSet['id'],
133 'name' => 'testvalidpf',
134 'label' => 'test valid pf',
135 'html_type' => 'Radio',
136 'is_enter_qty' => 1,
137 'is_active' => 1,
138 ];
139 $priceField1 = $this->callAPISuccess('PriceField', 'create', $params);
140
141 // Create expired price field.
142 $params = [
143 'price_set_id' => $priceSet['id'],
144 'name' => 'testexpiredpf',
145 'label' => 'test expired pf',
146 'html_type' => 'Radio',
147 'is_enter_qty' => 1,
148 'is_active' => 1,
149 'expire_on' => date('Y-m-d', strtotime("-1 days")),
150 ];
151 $priceField2 = $this->callAPISuccess('PriceField', 'create', $params);
152
153 //Create price options.
154 $membershipOrgId = $this->organizationCreate(NULL);
155 $memtype = $this->membershipTypeCreate(['member_of_contact_id' => $membershipOrgId]);
156 foreach ([$priceField1, $priceField2] as $priceField) {
157 $priceFieldValueParams = [
158 'price_field_id' => $priceField['id'],
159 'name' => 'rye grass',
160 'membership_type_id' => $memtype,
161 'label' => 'juicy and healthy',
162 'amount' => 1,
163 'membership_num_terms' => 2,
164 'financial_type_id' => 1,
165 ];
166 $this->callAPISuccess('PriceFieldValue', 'create', $priceFieldValueParams);
167 }
168
169 $form = $this->getContributionForm(['priceSetID' => $priceSet['id']]);
170 foreach ($form->_priceSet['fields'] as $pField) {
171 foreach ($pField['options'] as $opId => $opValues) {
172 $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id'];
173 }
174 }
175 $form->_membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($form, $membershipTypeIds);
176
177 //This function should not update form priceSet with the expired one.
178 CRM_Price_BAO_PriceSet::buildPriceSet($form);
179
180 $this->assertEquals(1, count($form->_priceSet['fields']));
181 $field = current($form->_priceSet['fields']);
182 $this->assertEquals('testvalidpf', $field['name']);
183 }
184
185 }