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