--CRM-17240, fixed tests
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_PriceFieldValueTest
32 */
33 class api_v3_PriceFieldValueTest extends CiviUnitTestCase {
34 protected $_apiversion = 3;
35 protected $_params;
36 protected $id = 0;
37 protected $priceSetID = 0;
38 protected $_entity = 'price_field_value';
39
40 public $DBResetRequired = TRUE;
41
42 /**
43 * @var int
44 */
45 protected $priceFieldID;
46
47 /**
48 * Setup function.
49 */
50 public function setUp() {
51 parent::setUp();
52 // Put stuff here that should happen before all tests in this unit.
53 $priceSetParams = array(
54 'name' => 'default_goat_priceset',
55 'title' => 'Goat accommodation',
56 'is_active' => 1,
57 'help_pre' => "Where does your goat sleep",
58 'help_post' => "thank you for your time",
59 'extends' => 2,
60 'financial_type_id' => 1,
61 'is_quick_config' => 1,
62 'is_reserved' => 1,
63 );
64
65 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetParams);
66 $this->priceSetID = $price_set['id'];
67
68 $priceFieldParams = array(
69 'price_set_id' => $this->priceSetID,
70 'name' => 'grassvariety',
71 'label' => 'Grass Variety',
72 'html_type' => 'Text',
73 'is_enter_qty' => 1,
74 'is_active' => 1,
75 );
76 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldParams);
77 $this->priceFieldID = $priceField['id'];
78 $this->_params = array(
79 'price_field_id' => $this->priceFieldID,
80 'name' => 'rye grass',
81 'label' => 'juicy and healthy',
82 'amount' => 1,
83 'financial_type_id' => 1,
84 );
85
86 $membershipOrgId = $this->organizationCreate(NULL);
87 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $membershipOrgId));
88 $priceSetParams1 = array(
89 'name' => 'priceset',
90 'title' => 'Priceset with Multiple Terms',
91 'is_active' => 1,
92 'extends' => 3,
93 'financial_type_id' => 2,
94 'is_quick_config' => 1,
95 'is_reserved' => 1,
96 );
97 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
98 $this->priceSetID1 = $price_set1['id'];
99 $priceFieldParams1 = array(
100 'price_set_id' => $this->priceSetID1,
101 'name' => 'memtype',
102 'label' => 'memtype',
103 'html_type' => 'Radio',
104 'is_enter_qty' => 1,
105 'is_active' => 1,
106 );
107 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldParams1);
108 $this->priceFieldID1 = $priceField1['id'];
109 }
110
111 /**
112 * Tear down function.
113 *
114 * @throws \Exception
115 */
116 public function tearDown() {
117 $tablesToTruncate = array(
118 'civicrm_contact',
119 'civicrm_contribution',
120 );
121 $this->quickCleanup($tablesToTruncate);
122 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
123 $this->callAPISuccess('PriceField', 'delete', array(
124 'id' => $this->priceFieldID1,
125 ));
126 $this->callAPISuccess('PriceSet', 'delete', array(
127 'id' => $this->priceSetID1,
128 ));
129 $this->callAPISuccess('PriceField', 'delete', array(
130 'id' => $this->priceFieldID,
131 ));
132 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
133 'id' => $this->priceSetID,
134 ));
135
136 $this->assertAPISuccess($delete);
137 }
138
139 public function testCreatePriceFieldValue() {
140 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
141 $this->assertAPISuccess($result);
142 $this->assertEquals(1, $result['count']);
143 $this->assertNotNull($result['values'][$result['id']]['id']);
144 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
145 }
146
147 public function testGetBasicPriceFieldValue() {
148 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
149 $this->id = $createResult['id'];
150 $this->assertAPISuccess($createResult);
151 $getParams = array(
152 'name' => 'contribution_amount',
153 );
154 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
155 $this->assertEquals(1, $getResult['count']);
156 $this->callAPISuccess('price_field_value', 'delete', array('id' => $createResult['id']));
157 }
158
159 public function testDeletePriceFieldValue() {
160 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
161 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
162 $deleteParams = array('id' => $createResult['id']);
163 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
164
165 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
166 $this->assertEquals($startCount, $endCount);
167 }
168
169 public function testGetFieldsPriceFieldValue() {
170 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
171 $this->assertEquals(1, $result['values']['max_value']['type']);
172 }
173
174 public function testCreatePriceFieldValuewithMultipleTerms() {
175 $params = array(
176 'price_field_id' => $this->priceFieldID1,
177 'membership_type_id' => $this->_membershipTypeID,
178 'name' => 'memType1',
179 'label' => 'memType1',
180 'amount' => 90,
181 'membership_num_terms' => 2,
182 'is_active' => 1,
183 'financial_type_id' => 2,
184 );
185 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
186 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
187 $this->assertEquals(1, $result['count']);
188 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
189 }
190
191 public function testGetPriceFieldValuewithMultipleTerms() {
192 $params1 = array(
193 'price_field_id' => $this->priceFieldID1,
194 'membership_type_id' => $this->_membershipTypeID,
195 'name' => 'memType1',
196 'label' => 'memType1',
197 'amount' => 90,
198 'membership_num_terms' => 2,
199 'is_active' => 1,
200 'financial_type_id' => 2,
201 );
202 $params2 = array(
203 'price_field_id' => $this->priceFieldID1,
204 'membership_type_id' => $this->_membershipTypeID,
205 'name' => 'memType2',
206 'label' => 'memType2',
207 'amount' => 120,
208 'membership_num_terms' => 3,
209 'is_active' => 1,
210 'financial_type_id' => 2,
211 );
212 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
213 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
214 $result = $this->callAPISuccess($this->_entity, 'get', array('price_field_id' => $this->priceFieldID1));
215 $this->assertEquals(2, $result['count']);
216 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result1['id']));
217 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result2['id']));
218 }
219
220 }