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