Merge pull request #19806 from eileenmcnaughton/msg_compat
[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 }
27
28 /**
29 * Test that the membership is set to recurring if the membership type is always autorenew.
30 */
31 public function testSetRecurFunction() {
32 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
33 $form = $this->getContributionForm();
34 $form->testSubmit([
35 'selectMembership' => $membershipTypeID,
36 ]);
37 $this->assertEquals(1, $form->_params['is_recur']);
38 }
39
40 /**
41 * Test that the membership is set to recurring if the membership type is always autorenew.
42 */
43 public function testSetRecurFunctionOptionalYes() {
44 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
45 $form = $this->getContributionForm();
46 $form->testSubmit([
47 'selectMembership' => $membershipTypeID,
48 'is_recur' => 1,
49 ]);
50 $this->assertEquals(1, $form->_params['is_recur']);
51 }
52
53 /**
54 * Test that the membership is set to recurring if the membership type is always autorenew.
55 */
56 public function testSetRecurFunctionOptionalNo() {
57 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
58 $form = $this->getContributionForm();
59 $form->testSubmit([
60 'selectMembership' => $membershipTypeID,
61 'is_recur' => 0,
62 ]);
63 $this->assertEquals(0, $form->_params['is_recur']);
64 }
65
66 /**
67 * Test that the membership is set to recurring if the membership type is always autorenew.
68 */
69 public function testSetRecurFunctionNotAvailable() {
70 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
71 $form = $this->getContributionForm();
72 $form->testSubmit([
73 'selectMembership' => $membershipTypeID,
74 ]);
75 $this->assertArrayNotHasKey('is_recur', $form->_params);
76 }
77
78 /**
79 * Get a contribution form object for testing.
80 *
81 * @return \CRM_Contribute_Form_Contribution_Main
82 */
83 protected function getContributionForm($params = []) {
84 $params['priceSetID'] = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [
85 'name' => 'default_membership_type_amount',
86 'return' => 'id',
87 ]);
88
89 $contributionPageParams = (array_merge($params, [
90 'currency' => 'NZD',
91 'goal_amount' => 6000,
92 'is_pay_later' => 0,
93 'is_monetary' => 1,
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',
99 'is_test' => 0,
100 ]),
101 'amount_block_is_active' => 1,
102 ]));
103
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']);
109 $form->preProcess();
110 $form->buildQuickForm();
111 return $form;
112 }
113
114 /**
115 * Test expired priceset are not returned from buildPriceSet() Function
116 */
117 public function testExpiredPriceSet() {
118 $priceSetParams1 = [
119 'name' => 'priceset',
120 'title' => 'Priceset with Multiple Terms',
121 'is_active' => 1,
122 'extends' => 3,
123 'financial_type_id' => 2,
124 'is_quick_config' => 1,
125 'is_reserved' => 1,
126 ];
127 $priceSet = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
128
129 // Create valid price field.
130 $params = [
131 'price_set_id' => $priceSet['id'],
132 'name' => 'testvalidpf',
133 'label' => 'test valid pf',
134 'html_type' => 'Radio',
135 'is_enter_qty' => 1,
136 'is_active' => 1,
137 ];
138 $priceField1 = $this->callAPISuccess('PriceField', 'create', $params);
139
140 // Create expired price field.
141 $params = [
142 'price_set_id' => $priceSet['id'],
143 'name' => 'testexpiredpf',
144 'label' => 'test expired pf',
145 'html_type' => 'Radio',
146 'is_enter_qty' => 1,
147 'is_active' => 1,
148 'expire_on' => date('Y-m-d', strtotime("-1 days")),
149 ];
150 $priceField2 = $this->callAPISuccess('PriceField', 'create', $params);
151
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',
161 'amount' => 1,
162 'membership_num_terms' => 2,
163 'financial_type_id' => 1,
164 ];
165 $this->callAPISuccess('PriceFieldValue', 'create', $priceFieldValueParams);
166 }
167
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'];
172 }
173 }
174 $form->_membershipTypeValues = CRM_Member_BAO_Membership::buildMembershipTypeValues($form, $membershipTypeIds);
175
176 //This function should not update form priceSet with the expired one.
177 CRM_Price_BAO_PriceSet::buildPriceSet($form);
178
179 $this->assertEquals(1, count($form->_priceSet['fields']));
180 $field = current($form->_priceSet['fields']);
181 $this->assertEquals('testvalidpf', $field['name']);
182 }
183
184 }