INFRA-132 - Drupal.Array.Array.CommaLastItem
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
CommitLineData
6a488035 1<?php
972322c5 2/*
3 +--------------------------------------------------------------------+
39de6fd5 4| CiviCRM version 4.6 |
972322c5 5+--------------------------------------------------------------------+
06a1bc01 6| Copyright CiviCRM LLC (c) 2004-2014 |
972322c5 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+--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
e9479dcf
EM
30/**
31 * Class api_v3_OptionValueTest
32 */
6a488035 33class api_v3_OptionValueTest extends CiviUnitTestCase {
972322c5 34 protected $_apiversion = 3;
b7c9bc4c 35
00be9182 36 public function setUp() {
6a488035 37 parent::setUp();
a73525a7 38 $this->useTransaction(TRUE);
6a488035
TO
39 }
40
972322c5 41 public function testGetCount() {
42 $result = $this->callAPISuccess('option_value', 'getcount', array());
43 $this->assertGreaterThan(100, $result);
44 }
45
6a488035 46 public function testGetOptionValueByID() {
972322c5 47 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
6a488035
TO
48 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
49 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
50 }
51
52 public function testGetOptionValueByValue() {
972322c5 53 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
6a488035
TO
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 */
00be9182 61 public function testGetOptionValueLimit() {
972322c5 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__);
6a488035 65 $params['options']['limit'] = 1;
972322c5 66 $result = $this->callAPISuccess('option_value', 'get', $params);
67 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__);
6a488035
TO
68 }
69
70 /**
71 * Test offset param
72 */
00be9182 73 public function testGetOptionValueOffSet() {
6a488035 74
972322c5 75 $result = $this->callAPISuccess('option_value', 'get', array(
6a488035 76 'option_group_id' => 1,
5896d037
TO
77 'value' => '1',
78 ));
972322c5 79 $result2 = $this->callAPISuccess('option_value', 'get', array(
6a488035 80 'option_group_id' => 1,
5896d037
TO
81 'value' => '1',
82 'options' => array('offset' => 1),
83 ));
972322c5 84 $this->assertGreaterThan($result2['count'], $result['count']);
6a488035
TO
85 }
86
87 /**
88 * Test offset param
89 */
00be9182 90 public function testGetSingleValueOptionValueSort() {
6a488035 91 $description = "demonstrates use of Sort param (available in many api functions). Also, getsingle";
5896d037
TO
92 $subfile = 'SortOption';
93 $result = $this->callAPISuccess('option_value', 'getsingle', array(
6c6e6187 94 'option_group_id' => 1,
5896d037
TO
95 'options' => array(
96 'sort' => 'label ASC',
97 'limit' => 1,
98 ),
99 ));
6a488035 100 $params = array(
6c6e6187 101 'option_group_id' => 1,
5896d037 102 'options' => array(
6a488035
TO
103 'sort' => 'label DESC',
104 'limit' => 1,
105 ),
106 );
972322c5 107 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
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 */
00be9182 114 public function testGetValueOptionPagination() {
6a488035 115 $pageSize = 10;
5896d037 116 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize)));
972322c5 117 $page2 = $this->callAPISuccess('option_value', 'get', array(
6c6e6187 118 'options' => array(
5896d037
TO
119 'limit' => $pageSize,
120 // if you use it for pagination, option.offset=pageSize*pageNumber
121 'offset' => $pageSize - 1,
122 ),
123 ));
6a488035
TO
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() {
972322c5 134 $params = array('option_group_id' => 1);
135 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
136 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
137 }
6a488035 138
c490a46a 139 /**
100fef9d 140 * Test that using option_group_name returns more than 1 & less than all
c490a46a 141 */
6a488035 142 public function testGetOptionGroupByName() {
972322c5 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);
6a488035
TO
147 $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__);
148 $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__);
149 }
c490a46a 150
6a488035 151 public function testGetOptionDoesNotExist() {
972322c5 152 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
6a488035
TO
153 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
154 }
c490a46a
CW
155
156 /**
157 * Check that domain_id is honoured
158 */
6a488035 159 public function testCreateOptionSpecifyDomain() {
972322c5 160 $result = $this->callAPISuccess('option_group', 'get', array(
6a488035
TO
161 'name' => 'from_email_address',
162 'sequential' => 1,
c87bbced 163 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
5896d037 164 ));
972322c5 165
6a488035 166 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
972322c5 167 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
6a488035 168 'id' => $optionValueId,
6a488035
TO
169 'return' => 'domain_id',
170 ));
171 $this->assertEquals(2, $domain_id);
c87bbced 172 }
c490a46a
CW
173
174 /**
c87bbced 175 * Check that component_id is honoured
c490a46a 176 */
c87bbced 177 public function testCreateOptionSpecifyComponentID() {
972322c5 178 $result = $this->callAPISuccess('option_group', 'get', array(
c87bbced 179 'name' => 'from_email_address',
6c6e6187 180 'sequential' => 1,
5896d037 181 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
c87bbced 182 ));
183 $this->assertAPISuccess($result);
184 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
972322c5 185 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 186 'id' => $optionValueId,
5896d037 187 'return' => 'component_id',
c87bbced 188 ));
189 $this->assertEquals(2, $component_id);
190 }
c490a46a
CW
191
192 /**
c87bbced 193 * Check that component continues to be honoured
c490a46a 194 */
c87bbced 195 public function testCreateOptionSpecifyComponent() {
972322c5 196 $result = $this->callAPISuccess('option_group', 'get', array(
c87bbced 197 'name' => 'from_email_address',
6c6e6187 198 'sequential' => 1,
5896d037 199 'api.option_value.create' => array(
c87bbced 200 'component_id' => 'CiviContribute',
21dfd5f5 201 'name' => 'my@y.com',
5896d037 202 ),
6a488035 203
c87bbced 204 ));
205 $this->assertAPISuccess($result);
206 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
972322c5 207 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 208 'id' => $optionValueId,
5896d037 209 'return' => 'component_id',
c87bbced 210 ));
211 $this->assertEquals(2, $component_id);
212 }
c490a46a
CW
213
214 /**
c87bbced 215 * Check that component string is honoured
c490a46a 216 */
c87bbced 217 public function testCreateOptionSpecifyComponentString() {
972322c5 218 $result = $this->callAPISuccess('option_group', 'get', array(
c87bbced 219 'name' => 'from_email_address',
6c6e6187 220 'sequential' => 1,
5896d037 221 'api.option_value.create' => array(
c87bbced 222 'component_id' => 'CiviContribute',
21dfd5f5 223 'name' => 'my@y.com',
5896d037 224 ),
c87bbced 225
226 ));
227 $this->assertAPISuccess($result);
228 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
972322c5 229 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 230 'id' => $optionValueId,
5896d037 231 'return' => 'component_id',
c87bbced 232 ));
233 $this->assertEquals(2, $component_id);
234 }
c490a46a
CW
235
236 /**
c87bbced 237 * Check that domain_id is honoured
c490a46a 238 */
c87bbced 239 public function testCRM12133CreateOptionWeightNoValue() {
972322c5 240 $optionGroup = $this->callAPISuccess(
c87bbced 241 'option_group', 'get', array(
5896d037
TO
242 'name' => 'gender',
243 'sequential' => 1,
e70a7fc0
TO
244 )
245 );
c87bbced 246 $this->assertAPISuccess($optionGroup);
247 $params = array(
248 'option_group_id' => $optionGroup['id'],
6c6e6187 249 'label' => 'my@y.com',
5896d037 250 'weight' => 3,
c87bbced 251 );
6c6e6187 252 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
c87bbced 253 $this->assertAPISuccess($optionValue);
254 $params['weight'] = 4;
5896d037 255 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params);
c87bbced 256 $this->assertAPISuccess($optionValue2);
972322c5 257 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
c87bbced 258 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
259
6c6e6187 260 //cleanup
972322c5 261 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
262 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
6a488035
TO
263 }
264
c490a46a 265 /**
6a488035 266 * Check that domain_id is honoured
c490a46a 267 */
6a488035 268 public function testCreateOptionNoName() {
972322c5 269 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
6a488035 270 'name' => 'gender',
5896d037
TO
271 'sequential' => 1,
272 ));
6a488035 273
972322c5 274 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
6c6e6187 275 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
6a488035
TO
276 $this->assertAPISuccess($optionValue);
277 $this->getAndCheck($params, $optionValue['id'], 'option_value');
278 }
a4a33486
CW
279
280 /**
6a488035 281 * Check that pseudoconstant reflects new value added
a4a33486
CW
282 * and deleted
283 */
6a488035 284 public function testCRM11876CreateOptionPseudoConstantUpdated() {
6c6e6187 285 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
5896d037 286 'name' => 'payment_instrument',
a4a33486
CW
287 'return' => 'id',
288 ));
289 $newOption = (string) time();
290 $apiResult = $this->callAPISuccess('option_value', 'create', array(
291 'option_group_id' => $optionGroupID,
292 'label' => $newOption,
6a488035
TO
293 ));
294
a4a33486
CW
295 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
296 $this->assertTrue(in_array($newOption, $fields['values']));
297
298 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
299
300 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
301 $this->assertFalse(in_array($newOption, $fields['values']));
6a488035 302 }
6a488035 303
89ab5601 304
c490a46a 305 /**
100fef9d 306 * Update option value with 'id' paramter and the value to update
89ab5601
PJ
307 * and not passing option group id
308 */
309 public function testUpdateOptionValueNoGroupId() {
310 // create a option group
311 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1));
312 // create a option value
313 $ov = $this->callAPISuccess('option_value', 'create',
314 array('option_group_id' => $og['id'], 'label' => 'test option value')
315 );
316 // update option value without 'option_group_id'
6c6e6187 317 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
89ab5601 318 $val = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 319 'id' => $ov['id'],
5896d037 320 'return' => 'is_active',
89ab5601
PJ
321 ));
322 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__);
323 }
324
c490a46a 325 /**
100fef9d 326 * Update option value with 'id' paramter and the value to update
89ab5601
PJ
327 * and as well as option group id
328 */
329 public function testUpdateOptionValueWithGroupId() {
330 // create a option group
5896d037 331 $og = $this->callAPISuccess('option_group', 'create', array(
92915c55
TO
332 'name' => 'our test Option Group for with group id',
333 'is_active' => 1,
334 ));
89ab5601
PJ
335 // create a option value
336 $ov = $this->callAPISuccess('option_value', 'create',
337 array('option_group_id' => $og['id'], 'label' => 'test option value')
338 );
339 // update option value without 'option_group_id'
5896d037 340 $this->callAPISuccess('option_value', 'create', array(
92915c55
TO
341 'id' => $ov['id'],
342 'option_group_id' => $og['id'],
343 'is_active' => 0,
344 ));
89ab5601 345 $val = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 346 'id' => $ov['id'],
5896d037 347 'return' => 'is_active',
89ab5601
PJ
348 ));
349 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__);
350 }
96025800 351
e9479dcf 352}