Merge pull request #12337 from lcdservices/dev-core-190
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
2fe49090 4| CiviCRM version 5 |
b6708aeb 5+--------------------------------------------------------------------+
6b83d5bd 6| Copyright CiviCRM LLC (c) 2004-2019 |
b6708aeb 7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
10| CiviCRM is free software; you can copy, modify, and distribute it |
11| under the terms of the GNU Affero General Public License |
12| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13| |
14| CiviCRM is distributed in the hope that it will be useful, but |
15| WITHOUT ANY WARRANTY; without even the implied warranty of |
16| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17| See the GNU Affero General Public License for more details. |
18| |
19| You should have received a copy of the GNU Affero General Public |
20| License and the CiviCRM Licensing Exception along |
21| with this program; if not, contact CiviCRM LLC |
22| at info[AT]civicrm[DOT]org. If you have questions about the |
23| GNU Affero General Public License or the licensing of CiviCRM, |
24| see the CiviCRM license FAQ at http://civicrm.org/licensing |
25+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class api_v3_PriceFieldValueTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class api_v3_PriceFieldValueTest extends CiviUnitTestCase {
33 protected $_apiversion = 3;
34 protected $_params;
35 protected $id = 0;
36 protected $priceSetID = 0;
37 protected $_entity = 'price_field_value';
b7c9bc4c 38
6a488035
TO
39 public $DBResetRequired = TRUE;
40
78373706
EM
41 /**
42 * @var int
43 */
44 protected $priceFieldID;
45
46 /**
47 * Setup function.
48 */
6a488035
TO
49 public function setUp() {
50 parent::setUp();
78373706
EM
51 // Put stuff here that should happen before all tests in this unit.
52 $priceSetParams = array(
6a488035 53 'name' => 'default_goat_priceset',
78373706 54 'title' => 'Goat accommodation',
6a488035
TO
55 'is_active' => 1,
56 'help_pre' => "Where does your goat sleep",
57 'help_post' => "thank you for your time",
58 'extends' => 2,
59 'financial_type_id' => 1,
60 'is_quick_config' => 1,
61 'is_reserved' => 1,
62 );
63
78373706 64 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetParams);
6a488035
TO
65 $this->priceSetID = $price_set['id'];
66
78373706 67 $priceFieldParams = array(
6a488035
TO
68 'price_set_id' => $this->priceSetID,
69 'name' => 'grassvariety',
70 'label' => 'Grass Variety',
71 'html_type' => 'Text',
72 'is_enter_qty' => 1,
73 'is_active' => 1,
74 );
78373706 75 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldParams);
6a488035
TO
76 $this->priceFieldID = $priceField['id'];
77 $this->_params = array(
6a488035 78 'price_field_id' => $this->priceFieldID,
78373706 79 'name' => 'rye grass',
6a488035 80 'label' => 'juicy and healthy',
21dfd5f5 81 'amount' => 1,
43dbd988 82 'financial_type_id' => 1,
92915c55 83 );
b6708aeb 84
6a488035 85 $membershipOrgId = $this->organizationCreate(NULL);
75638074 86 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $membershipOrgId));
78373706 87 $priceSetParams1 = array(
6a488035
TO
88 'name' => 'priceset',
89 'title' => 'Priceset with Multiple Terms',
90 'is_active' => 1,
91 'extends' => 3,
92 'financial_type_id' => 2,
93 'is_quick_config' => 1,
94 'is_reserved' => 1,
95 );
78373706 96 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
6a488035 97 $this->priceSetID1 = $price_set1['id'];
78373706 98 $priceFieldParams1 = array(
6a488035
TO
99 'price_set_id' => $this->priceSetID1,
100 'name' => 'memtype',
101 'label' => 'memtype',
102 'html_type' => 'Radio',
103 'is_enter_qty' => 1,
104 'is_active' => 1,
105 );
78373706 106 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldParams1);
6a488035
TO
107 $this->priceFieldID1 = $priceField1['id'];
108 }
109
78373706
EM
110 /**
111 * Tear down function.
112 *
113 * @throws \Exception
114 */
00be9182 115 public function tearDown() {
6a488035 116 $tablesToTruncate = array(
92915c55
TO
117 'civicrm_contact',
118 'civicrm_contribution',
6a488035
TO
119 );
120 $this->quickCleanup($tablesToTruncate);
121 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
6c6e6187 122 $this->callAPISuccess('PriceField', 'delete', array(
92915c55 123 'id' => $this->priceFieldID1,
6a488035 124 ));
6c6e6187 125 $this->callAPISuccess('PriceSet', 'delete', array(
6a488035
TO
126 'id' => $this->priceSetID1,
127 ));
6c6e6187 128 $this->callAPISuccess('PriceField', 'delete', array(
92915c55 129 'id' => $this->priceFieldID,
6a488035 130 ));
6c6e6187 131 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
6a488035
TO
132 'id' => $this->priceSetID,
133 ));
134
135 $this->assertAPISuccess($delete);
136 }
137
138 public function testCreatePriceFieldValue() {
ca985406 139 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
a15773db 140 $this->assertAPISuccess($result);
ba4a1892
TM
141 $this->assertEquals(1, $result['count']);
142 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
143 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
144 }
145
146 public function testGetBasicPriceFieldValue() {
ca985406 147 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
6a488035
TO
148 $this->id = $createResult['id'];
149 $this->assertAPISuccess($createResult);
150 $getParams = array(
6a488035
TO
151 'name' => 'contribution_amount',
152 );
ca985406 153 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
ba4a1892 154 $this->assertEquals(1, $getResult['count']);
6c6e6187 155 $this->callAPISuccess('price_field_value', 'delete', array('id' => $createResult['id']));
6a488035
TO
156 }
157
158 public function testDeletePriceFieldValue() {
ca985406 159 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
160 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
161 $deleteParams = array('id' => $createResult['id']);
162 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
163
164 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
a15773db 165 $this->assertEquals($startCount, $endCount);
6a488035
TO
166 }
167
168 public function testGetFieldsPriceFieldValue() {
ca985406 169 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
6a488035
TO
170 $this->assertEquals(1, $result['values']['max_value']['type']);
171 }
172
173 public function testCreatePriceFieldValuewithMultipleTerms() {
174 $params = array(
6a488035 175 'price_field_id' => $this->priceFieldID1,
6c6e6187 176 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
177 'name' => 'memType1',
178 'label' => 'memType1',
179 'amount' => 90,
180 'membership_num_terms' => 2,
181 'is_active' => 1,
182 'financial_type_id' => 2,
92915c55 183 );
ca985406 184 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 185 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
ba4a1892 186 $this->assertEquals(1, $result['count']);
ca985406 187 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
6a488035 188 }
92915c55 189
6a488035
TO
190 public function testGetPriceFieldValuewithMultipleTerms() {
191 $params1 = array(
6a488035 192 'price_field_id' => $this->priceFieldID1,
6c6e6187 193 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
194 'name' => 'memType1',
195 'label' => 'memType1',
196 'amount' => 90,
197 'membership_num_terms' => 2,
198 'is_active' => 1,
199 'financial_type_id' => 2,
92915c55 200 );
6a488035 201 $params2 = array(
6a488035 202 'price_field_id' => $this->priceFieldID1,
6c6e6187 203 'membership_type_id' => $this->_membershipTypeID,
6a488035
TO
204 'name' => 'memType2',
205 'label' => 'memType2',
206 'amount' => 120,
207 'membership_num_terms' => 3,
208 'is_active' => 1,
209 'financial_type_id' => 2,
92915c55 210 );
ca985406 211 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
212 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
481a74f4 213 $result = $this->callAPISuccess($this->_entity, 'get', array('price_field_id' => $this->priceFieldID1));
ba4a1892 214 $this->assertEquals(2, $result['count']);
6c6e6187
TO
215 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result1['id']));
216 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result2['id']));
6a488035 217 }
96025800 218
1019b2fe
SL
219 public function testCreatePriceFieldValueWithDisabledFinancialType() {
220 $financialTypeParams = array(
221 'is_active' => 0,
222 'name' => 'Disabled Donations',
223 );
224 $financialType = $this->callAPISuccess('financial_type', 'create', $financialTypeParams);
225 $params = array(
226 'price_field_id' => $this->priceFieldID,
227 'name' => 'DonType1',
228 'label' => 'DonType1',
229 'amount' => 90,
230 'is_active' => 1,
231 'financial_type_id' => $financialType['id'],
232 );
233 $this->callAPIFailure($this->_entity, 'create', $params);
234 }
235
6a488035 236}