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