INFRA-132 - tests/ - phpcbf
[civicrm-core.git] / tests / phpunit / api / v3 / PriceFieldValueTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 /**
32 * Class api_v3_PriceFieldValueTest
33 */
34 class api_v3_PriceFieldValueTest extends CiviUnitTestCase {
35 protected $_apiversion = 3;
36 protected $_params;
37 protected $id = 0;
38 protected $priceSetID = 0;
39 protected $_entity = 'price_field_value';
40
41 public $DBResetRequired = TRUE;
42
43 public function setUp() {
44 parent::setUp();
45 // put stuff here that should happen before all tests in this unit
46 $priceSetparams = array(
47 # [domain_id] =>
48 'name' => 'default_goat_priceset',
49 'title' => 'Goat accomodation',
50 'is_active' => 1,
51 'help_pre' => "Where does your goat sleep",
52 'help_post' => "thank you for your time",
53 'extends' => 2,
54 'financial_type_id' => 1,
55 'is_quick_config' => 1,
56 'is_reserved' => 1,
57 );
58
59 $price_set = $this->callAPISuccess('price_set', 'create', $priceSetparams);
60 $this->priceSetID = $price_set['id'];
61
62 $priceFieldparams = array(
63 'price_set_id' => $this->priceSetID,
64 'name' => 'grassvariety',
65 'label' => 'Grass Variety',
66 'html_type' => 'Text',
67 'is_enter_qty' => 1,
68 'is_active' => 1,
69 );
70 $priceField = $this->callAPISuccess('price_field', 'create', $priceFieldparams);
71 $this->priceFieldID = $priceField['id'];
72 $this->_params = array(
73 'price_field_id' => $this->priceFieldID,
74 'name' => 'ryegrass',
75 'label' => 'juicy and healthy',
76 'amount' => 1
77 );
78
79 $membershipOrgId = $this->organizationCreate(NULL);
80 $this->_membershipTypeID = $this->membershipTypeCreate(array('member_of_contact_id' => $membershipOrgId));
81 $priceSetparams1 = array(
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 = $this->callAPISuccess('price_set', 'create', $priceSetparams1);
91 $this->priceSetID1 = $price_set1['id'];
92 $priceFieldparams1 = array(
93 'price_set_id' => $this->priceSetID1,
94 'name' => 'memtype',
95 'label' => 'memtype',
96 'html_type' => 'Radio',
97 'is_enter_qty' => 1,
98 'is_active' => 1,
99 );
100 $priceField1 = $this->callAPISuccess('price_field', 'create', $priceFieldparams1);
101 $this->priceFieldID1 = $priceField1['id'];
102 }
103
104 public function tearDown() {
105 $tablesToTruncate = array(
106 'civicrm_contact',
107 'civicrm_contribution',
108 );
109 $this->quickCleanup($tablesToTruncate);
110 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
111 $this->callAPISuccess('PriceField', 'delete', array(
112 'id' => $this->priceFieldID1,
113 ));
114 $this->callAPISuccess('PriceSet', 'delete', array(
115 'id' => $this->priceSetID1,
116 ));
117 $this->callAPISuccess('PriceField', 'delete', array(
118 'id' => $this->priceFieldID,
119 ));
120 $delete = $this->callAPISuccess('PriceSet', 'delete', array(
121 'id' => $this->priceSetID,
122 ));
123
124 $this->assertAPISuccess($delete);
125 }
126
127 public function testCreatePriceFieldValue() {
128 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
129 $this->assertAPISuccess($result, 'In line ' . __LINE__);
130 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
131 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
132 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
133 }
134
135 public function testGetBasicPriceFieldValue() {
136 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
137 $this->id = $createResult['id'];
138 $this->assertAPISuccess($createResult);
139 $getParams = array(
140 'name' => 'contribution_amount',
141 );
142 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
143 $this->assertEquals(1, $getResult['count'], 'In line ' . __LINE__);
144 $this->callAPISuccess('price_field_value', 'delete', array('id' => $createResult['id']));
145 }
146
147 public function testDeletePriceFieldValue() {
148 $startCount = $this->callAPISuccess($this->_entity, 'getcount', array());
149 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->_params);
150 $deleteParams = array('id' => $createResult['id']);
151 $deleteResult = $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
152
153 $endCount = $this->callAPISuccess($this->_entity, 'getcount', array());
154 $this->assertEquals($startCount, $endCount, 'In line ' . __LINE__);
155 }
156
157 public function testGetFieldsPriceFieldValue() {
158 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
159 $this->assertEquals(1, $result['values']['max_value']['type']);
160 }
161
162 public function testCreatePriceFieldValuewithMultipleTerms() {
163 $params = array(
164 'price_field_id' => $this->priceFieldID1,
165 'membership_type_id' => $this->_membershipTypeID,
166 'name' => 'memType1',
167 'label' => 'memType1',
168 'amount' => 90,
169 'membership_num_terms' => 2,
170 'is_active' => 1,
171 'financial_type_id' => 2,
172 );
173 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
174 $this->assertEquals($result['values'][$result['id']]['membership_num_terms'], 2);
175 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
176 $this->callAPISuccess($this->_entity, 'delete', array('id' => $result['id']));
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 }