INFRA-132 - Drupal.Array.Array.CommaLastItem
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 public function setUp() {
43 parent::setUp();
44 // put stuff here that should happen before all tests in this unit
45 $priceSetparams = array(
46 # [domain_id] =>
47 'name' => 'default_goat_priceset',
48 'title' => 'Goat accomodation',
49 'is_active' => 1,
50 'help_pre' => "Where does your goat sleep",
51 'help_post' => "thank you for your time",
52 'extends' => 2,
53 'financial_type_id' => 1,
54 'is_quick_config' => 1,
55 'is_reserved' => 1,
56 );
57
58 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetparams);
59 $this->priceSetID = $price_set['id'];
60
61 $priceFieldparams = array(
62 'price_set_id' => $this->priceSetID,
63 'name' => 'grassvariety',
64 'label' => 'Grass Variety',
65 'html_type' => 'Text',
66 'is_enter_qty' => 1,
67 'is_active' => 1,
68 );
69 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldparams);
70 $this->priceFieldID = $priceField['id'];
71 $this->_params = array(
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(array('member_of_contact_id' => $membershipOrgId));
80 $priceSetparams1 = array(
81 'name' => 'priceset',
82 'title' => 'Priceset with Multiple Terms',
83 'is_active' => 1,
84 'extends' => 3,
85 'financial_type_id' => 2,
86 'is_quick_config' => 1,
87 'is_reserved' => 1,
88 );
89 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetparams1);
90 $this->priceSetID1 = $price_set1['id'];
91 $priceFieldparams1 = array(
92 'price_set_id' => $this->priceSetID1,
93 'name' => 'memtype',
94 'label' => 'memtype',
95 'html_type' => 'Radio',
96 'is_enter_qty' => 1,
97 'is_active' => 1,
98 );
99 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldparams1);
100 $this->priceFieldID1 = $priceField1['id'];
101 }
102
103 public function tearDown() {
104 $tablesToTruncate = array(
105 'civicrm_contact',
106 'civicrm_contribution',
107 );
108 $this->quickCleanup($tablesToTruncate);
109 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
110 $this->callAPISuccess('PriceField', 'delete', array(
111 'id' => $this->priceFieldID1,
112 ));
113 $this->callAPISuccess('PriceSet', 'delete', array(
114 'id' => $this->priceSetID1,
115 ));
116 $this->callAPISuccess('PriceField', 'delete', array(
117 'id' => $this->priceFieldID,
118 ));
119 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
120 'id' => $this->priceSetID,
121 ));
122
123 $this->assertAPISuccess($delete);
124 }
125
126 public function testCreatePriceFieldValue() {
127 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
128 $this->assertAPISuccess($result, 'In line ' . __LINE__);
129 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
130 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
131 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
132 }
133
134 public function testGetBasicPriceFieldValue() {
135 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
136 $this->id = $createResult['id'];
137 $this->assertAPISuccess($createResult);
138 $getParams = array(
139 'name' => 'contribution_amount',
140 );
141 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
142 $this->assertEquals(1, $getResult['count'], 'In line ' . __LINE__);
143 $this->callAPISuccess('price_field_value', 'delete', array('id' => $createResult['id']));
144 }
145
146 public function testDeletePriceFieldValue() {
147 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
148 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
149 $deleteParams = array('id' => $createResult['id']);
150 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
151
152 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
153 $this->assertEquals($startCount, $endCount, 'In line ' . __LINE__);
154 }
155
156 public function testGetFieldsPriceFieldValue() {
157 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
158 $this->assertEquals(1, $result['values']['max_value']['type']);
159 }
160
161 public function testCreatePriceFieldValuewithMultipleTerms() {
162 $params = array(
163 'price_field_id' => $this->priceFieldID1,
164 'membership_type_id' => $this->_membershipTypeID,
165 'name' => 'memType1',
166 'label' => 'memType1',
167 'amount' => 90,
168 'membership_num_terms' => 2,
169 'is_active' => 1,
170 'financial_type_id' => 2,
171 );
172 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
173 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
174 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
175 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
176 }
177
178 public function testGetPriceFieldValuewithMultipleTerms() {
179 $params1 = array(
180 'price_field_id' => $this->priceFieldID1,
181 'membership_type_id' => $this->_membershipTypeID,
182 'name' => 'memType1',
183 'label' => 'memType1',
184 'amount' => 90,
185 'membership_num_terms' => 2,
186 'is_active' => 1,
187 'financial_type_id' => 2,
188 );
189 $params2 = array(
190 'price_field_id' => $this->priceFieldID1,
191 'membership_type_id' => $this->_membershipTypeID,
192 'name' => 'memType2',
193 'label' => 'memType2',
194 'amount' => 120,
195 'membership_num_terms' => 3,
196 'is_active' => 1,
197 'financial_type_id' => 2,
198 );
199 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
200 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
201 $result = $this->callAPISuccess($this->_entity, 'get', array('price_field_id' => $this->priceFieldID1));
202 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
203 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result1['id']));
204 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result2['id']));
205 }
206
207 }