api_v3_OptionValueTest - useTransaction()
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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_OptionValueTest
32 */
33 class api_v3_OptionValueTest extends CiviUnitTestCase {
34 protected $_apiversion = 3;
35
36 function setUp() {
37 parent::setUp();
38 $this->useTransaction(TRUE);
39 }
40
41 public function testGetCount() {
42 $result = $this->callAPISuccess('option_value', 'getcount', array());
43 $this->assertGreaterThan(100, $result);
44 }
45
46 public function testGetOptionValueByID() {
47 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
48 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
49 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
50 }
51
52 public function testGetOptionValueByValue() {
53 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
54 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
55 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
56 }
57
58 /**
59 * Test limit param
60 */
61 function testGetOptionValueLimit() {
62 $params = array();
63 $result = $this->callAPISuccess('option_value', 'get', $params);
64 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__);
65 $params['options']['limit'] = 1;
66 $result = $this->callAPISuccess('option_value', 'get', $params);
67 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__);
68 }
69
70 /**
71 * Test offset param
72 */
73 function testGetOptionValueOffSet() {
74
75 $result = $this->callAPISuccess('option_value', 'get', array(
76 'option_group_id' => 1,
77 'value' => '1', ));
78 $result2 = $this->callAPISuccess('option_value', 'get', array(
79 'option_group_id' => 1,
80 'value' => '1',
81 'options' => array('offset' => 1),
82 ));
83 $this->assertGreaterThan($result2['count'], $result['count']);
84 }
85
86 /**
87 * Test offset param
88 */
89 function testGetSingleValueOptionValueSort() {
90 $description = "demonstrates use of Sort param (available in many api functions). Also, getsingle";
91 $subfile = 'SortOption';
92 $result = $this->callAPISuccess('option_value', 'getsingle', array(
93 'option_group_id' => 1, 'options' => array(
94 'sort' => 'label ASC',
95 'limit' => 1,
96 ),
97 ));
98 $params = array(
99 'option_group_id' => 1, 'options' => array(
100 'sort' => 'label DESC',
101 'limit' => 1,
102 ),
103 );
104 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
105 $this->assertGreaterThan($result['label'], $result2['label']);
106 }
107
108 /**
109 * Try to emulate a pagination: fetch the first page of 10 options, then fetch the second page with an offset of 9 (instead of 10) and check the start of the second page is the end of the 1st one.
110 */
111 function testGetValueOptionPagination() {
112 $pageSize = 10;
113 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), ));
114 $page2 = $this->callAPISuccess('option_value', 'get', array(
115 'options' => array('limit' => $pageSize,
116 // if you use it for pagination, option.offset=pageSize*pageNumber
117 'offset' => $pageSize - 1,
118 ), ));
119 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__);
120 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__);
121
122 $last = array_pop($page1['values']);
123 $first = array_shift($page2['values']);
124
125 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__);
126 }
127
128 public function testGetOptionGroup() {
129 $params = array('option_group_id' => 1);
130 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__, __FILE__);
131 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
132 }
133
134 /**
135 * Test that using option_group_name returns more than 1 & less than all
136 */
137 public function testGetOptionGroupByName() {
138 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
139 $params = array('option.limit' => 100);
140 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
141 $result = $this->callAPISuccess('option_value', 'get', $params);
142 $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__);
143 $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__);
144 }
145
146 public function testGetOptionDoesNotExist() {
147 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
148 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
149 }
150
151 /**
152 * Check that domain_id is honoured
153 */
154 public function testCreateOptionSpecifyDomain() {
155 $result = $this->callAPISuccess('option_group', 'get', array(
156 'name' => 'from_email_address',
157 'sequential' => 1,
158 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
159 ));
160
161 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
162 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
163 'id' => $optionValueId,
164 'return' => 'domain_id',
165 ));
166 $this->assertEquals(2, $domain_id);
167 }
168
169 /**
170 * Check that component_id is honoured
171 */
172 public function testCreateOptionSpecifyComponentID() {
173 $result = $this->callAPISuccess('option_group', 'get', array(
174 'name' => 'from_email_address',
175 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
176 ));
177 $this->assertAPISuccess($result);
178 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
179 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
180 'id' => $optionValueId, 'return' => 'component_id',
181 ));
182 $this->assertEquals(2, $component_id);
183 }
184
185 /**
186 * Check that component continues to be honoured
187 */
188 public function testCreateOptionSpecifyComponent() {
189 $result = $this->callAPISuccess('option_group', 'get', array(
190 'name' => 'from_email_address',
191 'sequential' => 1, 'api.option_value.create' => array(
192 'component_id' => 'CiviContribute',
193 'name' => 'my@y.com'
194 ),
195
196 ));
197 $this->assertAPISuccess($result);
198 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
199 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
200 'id' => $optionValueId, 'return' => 'component_id',
201 ));
202 $this->assertEquals(2, $component_id);
203 }
204
205 /**
206 * Check that component string is honoured
207 */
208 public function testCreateOptionSpecifyComponentString() {
209 $result = $this->callAPISuccess('option_group', 'get', array(
210 'name' => 'from_email_address',
211 'sequential' => 1, 'api.option_value.create' => array(
212 'component_id' => 'CiviContribute',
213 'name' => 'my@y.com'),
214
215 ));
216 $this->assertAPISuccess($result);
217 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
218 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
219 'id' => $optionValueId, 'return' => 'component_id',
220 ));
221 $this->assertEquals(2, $component_id);
222 }
223
224 /**
225 * Check that domain_id is honoured
226 */
227 public function testCRM12133CreateOptionWeightNoValue() {
228 $optionGroup = $this->callAPISuccess(
229 'option_group', 'get', array(
230 'name' => 'gender',
231 'sequential' => 1, ));
232 $this->assertAPISuccess($optionGroup);
233 $params = array(
234 'option_group_id' => $optionGroup['id'],
235 'label' => 'my@y.com', 'weight' => 3,
236 );
237 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
238 $this->assertAPISuccess($optionValue);
239 $params['weight'] = 4;
240 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params );
241 $this->assertAPISuccess($optionValue2);
242 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
243 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
244
245 //cleanup
246 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
247 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
248 }
249
250 /**
251 * Check that domain_id is honoured
252 */
253 public function testCreateOptionNoName() {
254 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
255 'name' => 'gender',
256 'sequential' => 1, ));
257
258 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
259 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
260 $this->assertAPISuccess($optionValue);
261 $this->getAndCheck($params, $optionValue['id'], 'option_value');
262 }
263
264 /**
265 * Check that pseudoconstant reflects new value added
266 * and deleted
267 */
268 public function testCRM11876CreateOptionPseudoConstantUpdated() {
269 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument',
270 'return' => 'id',
271 ));
272 $newOption = (string) time();
273 $apiResult = $this->callAPISuccess('option_value', 'create', array(
274 'option_group_id' => $optionGroupID,
275 'label' => $newOption,
276 ));
277
278 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
279 $this->assertTrue(in_array($newOption, $fields['values']));
280
281 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
282
283 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
284 $this->assertFalse(in_array($newOption, $fields['values']));
285 }
286
287
288 /**
289 * Update option value with 'id' paramter and the value to update
290 * and not passing option group id
291 */
292 public function testUpdateOptionValueNoGroupId() {
293 // create a option group
294 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1));
295 // create a option value
296 $ov = $this->callAPISuccess('option_value', 'create',
297 array('option_group_id' => $og['id'], 'label' => 'test option value')
298 );
299 // update option value without 'option_group_id'
300 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
301 $val = $this->callAPISuccess('option_value', 'getvalue', array(
302 'id' => $ov['id'], 'return' => 'is_active',
303 ));
304 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__);
305 }
306
307 /**
308 * Update option value with 'id' paramter and the value to update
309 * and as well as option group id
310 */
311 public function testUpdateOptionValueWithGroupId() {
312 // create a option group
313 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group for with group id', 'is_active' => 1));
314 // create a option value
315 $ov = $this->callAPISuccess('option_value', 'create',
316 array('option_group_id' => $og['id'], 'label' => 'test option value')
317 );
318 // update option value without 'option_group_id'
319 $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'option_group_id' => $og['id'], 'is_active' => 0));
320 $val = $this->callAPISuccess('option_value', 'getvalue', array(
321 'id' => $ov['id'], 'return' => 'is_active',
322 ));
323 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__);
324 }
325 }