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();
30 * Test that the membership is set to recurring if the membership type is always autorenew.
32 public function testSetRecurFunction() {
33 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]);
34 $form = $this->getContributionForm();
36 'selectMembership' => $membershipTypeID,
38 $this->assertEquals(1, $form->_params
['is_recur']);
42 * Test that the membership is set to recurring if the membership type is always autorenew.
44 public function testSetRecurFunctionOptionalYes() {
45 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
46 $form = $this->getContributionForm();
48 'selectMembership' => $membershipTypeID,
51 $this->assertEquals(1, $form->_params
['is_recur']);
55 * Test that the membership is set to recurring if the membership type is always autorenew.
57 public function testSetRecurFunctionOptionalNo() {
58 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]);
59 $form = $this->getContributionForm();
61 'selectMembership' => $membershipTypeID,
64 $this->assertEquals(0, $form->_params
['is_recur']);
68 * Test that the membership is set to recurring if the membership type is always autorenew.
70 public function testSetRecurFunctionNotAvailable() {
71 $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 0, 'minimum_fee' => 80]);
72 $form = $this->getContributionForm();
74 'selectMembership' => $membershipTypeID,
76 $this->assertArrayNotHasKey('is_recur', $form->_params
);
80 * Get a contribution form object for testing.
82 * @return \CRM_Contribute_Form_Contribution_Main
84 protected function getContributionForm($params = []) {
85 $params['priceSetID'] = $params['priceSetID'] ??
$this->callAPISuccessGetValue('PriceSet', [
86 'name' => 'default_membership_type_amount',
90 $contributionPageParams = (array_merge($params, [
92 'goal_amount' => 6000,
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',
102 'amount_block_is_active' => 1,
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']);
111 $form->buildQuickForm();
116 * Test expired priceset are not returned from buildPriceSet() Function
118 public function testExpiredPriceSet() {
120 'name' => 'priceset',
121 'title' => 'Priceset with Multiple Terms',
124 'financial_type_id' => 2,
125 'is_quick_config' => 1,
128 $priceSet = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
130 // Create valid price field.
132 'price_set_id' => $priceSet['id'],
133 'name' => 'testvalidpf',
134 'label' => 'test valid pf',
135 'html_type' => 'Radio',
139 $priceField1 = $this->callAPISuccess('PriceField', 'create', $params);
141 // Create expired price field.
143 'price_set_id' => $priceSet['id'],
144 'name' => 'testexpiredpf',
145 'label' => 'test expired pf',
146 'html_type' => 'Radio',
149 'expire_on' => date('Y-m-d', strtotime("-1 days")),
151 $priceField2 = $this->callAPISuccess('PriceField', 'create', $params);
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',
163 'membership_num_terms' => 2,
164 'financial_type_id' => 1,
166 $this->callAPISuccess('PriceFieldValue', 'create', $priceFieldValueParams);
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'];
175 $form->_membershipTypeValues
= CRM_Member_BAO_Membership
::buildMembershipTypeValues($form, $membershipTypeIds);
177 //This function should not update form priceSet with the expired one.
178 CRM_Price_BAO_PriceSet
::buildPriceSet($form);
180 $this->assertEquals(1, count($form->_priceSet
['fields']));
181 $field = current($form->_priceSet
['fields']);
182 $this->assertEquals('testvalidpf', $field['name']);