version fixes
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
CommitLineData
6a488035 1<?php
972322c5 2/*
3 +--------------------------------------------------------------------+
39de6fd5 4| CiviCRM version 4.6 |
972322c5 5+--------------------------------------------------------------------+
e7112fa7 6| Copyright CiviCRM LLC (c) 2004-2015 |
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));
ba4a1892
TM
48 $this->assertEquals(1, $result['count']);
49 $this->assertEquals(1, $result['id']);
6a488035
TO
50 }
51
52 public function testGetOptionValueByValue() {
972322c5 53 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
ba4a1892
TM
54 $this->assertEquals(1, $result['count']);
55 $this->assertEquals(1, $result['id']);
6a488035
TO
56 }
57
58 /**
eceb18cc 59 * Test limit param.
6a488035 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 /**
eceb18cc 71 * Test offset param.
6a488035 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 /**
eceb18cc 88 * Test offset param.
6a488035 89 */
00be9182 90 public function testGetSingleValueOptionValueSort() {
5c49fee0 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__);
ba4a1892 136 $this->assertGreaterThan(1, $result['count']);
6a488035 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);
ba4a1892
TM
147 $this->assertGreaterThan(1, $activityTypes['count']);
148 $this->assertGreaterThan($activityTypes['count'], $result['count']);
6a488035 149 }
c490a46a 150
6a488035 151 public function testGetOptionDoesNotExist() {
972322c5 152 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
ba4a1892 153 $this->assertEquals(0, $result['count']);
6a488035 154 }
c490a46a
CW
155
156 /**
fe482240 157 * Check that domain_id is honoured.
c490a46a 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 /**
fe482240 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 /**
eceb18cc 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 /**
eceb18cc 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 /**
fe482240 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 /**
fe482240 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 /**
fe482240 281 * Check that pseudoconstant reflects new value added.
a4a33486 282 */
6a488035 283 public function testCRM11876CreateOptionPseudoConstantUpdated() {
6c6e6187 284 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
5896d037 285 'name' => 'payment_instrument',
a4a33486
CW
286 'return' => 'id',
287 ));
288 $newOption = (string) time();
289 $apiResult = $this->callAPISuccess('option_value', 'create', array(
290 'option_group_id' => $optionGroupID,
291 'label' => $newOption,
6a488035
TO
292 ));
293
a4a33486
CW
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']));
6a488035 301 }
6a488035 302
89ab5601 303
c490a46a 304 /**
e4f46be0 305 * Update option value with 'id' parameter and the value to update
89ab5601
PJ
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'
6c6e6187 316 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
89ab5601 317 $val = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 318 'id' => $ov['id'],
5896d037 319 'return' => 'is_active',
89ab5601
PJ
320 ));
321 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__);
322 }
323
c490a46a 324 /**
e4f46be0 325 * Update option value with 'id' parameter and the value to update
89ab5601
PJ
326 * and as well as option group id
327 */
328 public function testUpdateOptionValueWithGroupId() {
329 // create a option group
5896d037 330 $og = $this->callAPISuccess('option_group', 'create', array(
92915c55
TO
331 'name' => 'our test Option Group for with group id',
332 'is_active' => 1,
333 ));
89ab5601
PJ
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'
5896d037 339 $this->callAPISuccess('option_value', 'create', array(
92915c55
TO
340 'id' => $ov['id'],
341 'option_group_id' => $og['id'],
342 'is_active' => 0,
343 ));
89ab5601 344 $val = $this->callAPISuccess('option_value', 'getvalue', array(
6c6e6187 345 'id' => $ov['id'],
5896d037 346 'return' => 'is_active',
89ab5601
PJ
347 ));
348 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__);
349 }
96025800 350
e9479dcf 351}