commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tests / phpunit / api / v3 / PriceFieldValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 );
84
85 $membershipOrgId = $this->organizationCreate(NULL);
86 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $membershipOrgId));
87 $priceSetParams1 = array(
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 );
96 $price_set1 = $this->callAPISuccess('price_set', 'create', $priceSetParams1);
97 $this->priceSetID1 = $price_set1['id'];
98 $priceFieldParams1 = array(
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 );
106 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldParams1);
107 $this->priceFieldID1 = $priceField1['id'];
108 }
109
110 /**
111 * Tear down function.
112 *
113 * @throws \Exception
114 */
115 public function tearDown() {
116 $tablesToTruncate = array(
117 'civicrm_contact',
118 'civicrm_contribution',
119 );
120 $this->quickCleanup($tablesToTruncate);
121 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
122 $this->callAPISuccess('PriceField', 'delete', array(
123 'id' => $this->priceFieldID1,
124 ));
125 $this->callAPISuccess('PriceSet', 'delete', array(
126 'id' => $this->priceSetID1,
127 ));
128 $this->callAPISuccess('PriceField', 'delete', array(
129 'id' => $this->priceFieldID,
130 ));
131 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
132 'id' => $this->priceSetID,
133 ));
134
135 $this->assertAPISuccess($delete);
136 }
137
138 public function testCreatePriceFieldValue() {
139 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
140 $this->assertAPISuccess($result, 'In line ' . __LINE__);
141 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
142 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
143 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
144 }
145
146 public function testGetBasicPriceFieldValue() {
147 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
148 $this->id = $createResult['id'];
149 $this->assertAPISuccess($createResult);
150 $getParams = array(
151 'name' => 'contribution_amount',
152 );
153 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
154 $this->assertEquals(1, $getResult['count'], 'In line ' . __LINE__);
155 $this->callAPISuccess('price_field_value', 'delete', array('id' => $createResult['id']));
156 }
157
158 public function testDeletePriceFieldValue() {
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());
165 $this->assertEquals($startCount, $endCount, 'In line ' . __LINE__);
166 }
167
168 public function testGetFieldsPriceFieldValue() {
169 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
170 $this->assertEquals(1, $result['values']['max_value']['type']);
171 }
172
173 public function testCreatePriceFieldValuewithMultipleTerms() {
174 $params = array(
175 'price_field_id' => $this->priceFieldID1,
176 'membership_type_id' => $this->_membershipTypeID,
177 'name' => 'memType1',
178 'label' => 'memType1',
179 'amount' => 90,
180 'membership_num_terms' => 2,
181 'is_active' => 1,
182 'financial_type_id' => 2,
183 );
184 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
185 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
186 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
187 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
188 }
189
190 public function testGetPriceFieldValuewithMultipleTerms() {
191 $params1 = array(
192 'price_field_id' => $this->priceFieldID1,
193 'membership_type_id' => $this->_membershipTypeID,
194 'name' => 'memType1',
195 'label' => 'memType1',
196 'amount' => 90,
197 'membership_num_terms' => 2,
198 'is_active' => 1,
199 'financial_type_id' => 2,
200 );
201 $params2 = array(
202 'price_field_id' => $this->priceFieldID1,
203 'membership_type_id' => $this->_membershipTypeID,
204 'name' => 'memType2',
205 'label' => 'memType2',
206 'amount' => 120,
207 'membership_num_terms' => 3,
208 'is_active' => 1,
209 'financial_type_id' => 2,
210 );
211 $result1 = $this->callAPISuccess($this->_entity, 'create', $params1);
212 $result2 = $this->callAPISuccess($this->_entity, 'create', $params2);
213 $result = $this->callAPISuccess($this->_entity, 'get', array('price_field_id' => $this->priceFieldID1));
214 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
215 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result1['id']));
216 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result2['id']));
217 }
218
219 }