Fix token subscriber to format the display of the custom tokens
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class api_v3_OptionValueTest
14 * @group headless
15 */
16 class api_v3_OptionValueTest extends CiviUnitTestCase {
17 protected $_apiversion = 3;
18
19 public function setUp() {
20 parent::setUp();
21 $this->useTransaction(TRUE);
22 }
23
24 public function testGetCount() {
25 $result = $this->callAPISuccess('option_value', 'getcount', []);
26 $this->assertGreaterThan(100, $result);
27 }
28
29 public function testGetOptionValueByID() {
30 $result = $this->callAPISuccess('option_value', 'get', ['id' => 1]);
31 $this->assertEquals(1, $result['count']);
32 $this->assertEquals(1, $result['id']);
33 }
34
35 public function testGetOptionValueByValue() {
36 $result = $this->callAPISuccess('option_value', 'get', ['option_group_id' => 1, 'value' => '1']);
37 $this->assertEquals(1, $result['count']);
38 $this->assertEquals(1, $result['id']);
39 }
40
41 /**
42 * Test limit param.
43 */
44 public function testGetOptionValueLimit() {
45 $params = [];
46 $result = $this->callAPISuccess('option_value', 'get', $params);
47 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__);
48 $params['options']['limit'] = 1;
49 $result = $this->callAPISuccess('option_value', 'get', $params);
50 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__);
51 }
52
53 /**
54 * Test offset param.
55 */
56 public function testGetOptionValueOffSet() {
57
58 $result = $this->callAPISuccess('option_value', 'get', [
59 'option_group_id' => 1,
60 'value' => '1',
61 ]);
62 $result2 = $this->callAPISuccess('option_value', 'get', [
63 'option_group_id' => 1,
64 'value' => '1',
65 'options' => ['offset' => 1],
66 ]);
67 $this->assertGreaterThan($result2['count'], $result['count']);
68 }
69
70 /**
71 * Test offset param.
72 */
73 public function testGetSingleValueOptionValueSort() {
74 $description = "Demonstrates use of Sort param (available in many api functions). Also, getsingle.";
75 $subfile = 'SortOption';
76 $result = $this->callAPISuccess('option_value', 'getsingle', [
77 'option_group_id' => 1,
78 'options' => [
79 'sort' => 'label ASC',
80 'limit' => 1,
81 ],
82 ]);
83 $params = [
84 'option_group_id' => 1,
85 'options' => [
86 'sort' => 'label DESC',
87 'limit' => 1,
88 ],
89 ];
90 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
91 $this->assertGreaterThan($result['label'], $result2['label']);
92 }
93
94 /**
95 * 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.
96 */
97 public function testGetValueOptionPagination() {
98 $pageSize = 10;
99 $page1 = $this->callAPISuccess('option_value', 'get', ['options' => ['limit' => $pageSize]]);
100 $page2 = $this->callAPISuccess('option_value', 'get', [
101 'options' => [
102 'limit' => $pageSize,
103 // if you use it for pagination, option.offset=pageSize*pageNumber
104 'offset' => $pageSize - 1,
105 ],
106 ]);
107 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__);
108 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__);
109
110 $last = array_pop($page1['values']);
111 $first = array_shift($page2['values']);
112
113 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__);
114 }
115
116 public function testGetOptionGroup() {
117 $params = ['option_group_id' => 1];
118 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__, __FILE__);
119 $this->assertGreaterThan(1, $result['count']);
120 }
121
122 /**
123 * Test that using option_group_name returns more than 1 & less than all
124 */
125 public function testGetOptionGroupByName() {
126 $activityTypesParams = ['option_group_name' => 'activity_type', 'option.limit' => 100];
127 $params = ['option.limit' => 100];
128 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
129 $result = $this->callAPISuccess('option_value', 'get', $params);
130 $this->assertGreaterThan(1, $activityTypes['count']);
131 $this->assertGreaterThan($activityTypes['count'], $result['count']);
132 }
133
134 public function testGetOptionDoesNotExist() {
135 $result = $this->callAPISuccess('option_value', 'get', ['label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB']);
136 $this->assertEquals(0, $result['count']);
137 }
138
139 /**
140 * Check that domain_id is honoured.
141 */
142 public function testCreateOptionSpecifyDomain() {
143 $result = $this->callAPISuccess('option_group', 'get', [
144 'name' => 'from_email_address',
145 'sequential' => 1,
146 'api.option_value.create' => ['domain_id' => 2, 'name' => 'my@y.com', 'value' => '10'],
147 ]);
148
149 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
150 $domain_id = $this->callAPISuccess('option_value', 'getvalue', [
151 'id' => $optionValueId,
152 'return' => 'domain_id',
153 ]);
154 $this->assertEquals(2, $domain_id);
155 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValueId]);
156 }
157
158 /**
159 * Check that component_id is honoured.
160 */
161 public function testCreateOptionSpecifyComponentID() {
162 $result = $this->callAPISuccess('option_group', 'get', [
163 'name' => 'from_email_address',
164 'sequential' => 1,
165 'api.option_value.create' => ['component_id' => 2, 'name' => 'my@y.com'],
166 ]);
167 $this->assertAPISuccess($result);
168 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
169 $component_id = $this->callAPISuccess('option_value', 'getvalue', [
170 'id' => $optionValueId,
171 'return' => 'component_id',
172 ]);
173 $this->assertEquals(2, $component_id);
174 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValueId]);
175 }
176
177 /**
178 * Check that component string is honoured.
179 */
180 public function testCreateOptionSpecifyComponentString() {
181 $result = $this->callAPISuccess('option_group', 'get', [
182 'name' => 'from_email_address',
183 'sequential' => 1,
184 'api.option_value.create' => [
185 'component_id' => 'CiviContribute',
186 'name' => 'my@y.com',
187 ],
188 ]);
189 $this->assertAPISuccess($result);
190 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
191 $component_id = $this->callAPISuccess('option_value', 'getvalue', [
192 'id' => $optionValueId,
193 'return' => 'component_id',
194 ]);
195 $this->assertEquals(2, $component_id);
196 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValueId]);
197 }
198
199 /**
200 * Check that component is honoured when fetching options.
201 */
202 public function testGetOptionWithComponent() {
203 $components = Civi::settings()->get('enable_components');
204 CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute');
205 $this->callAPISuccess('option_group', 'get', [
206 'name' => 'gender',
207 'api.option_value.create' => [
208 'component_id' => 'CiviContribute',
209 'name' => 'Contrib',
210 ],
211 ]);
212 // Verify new option is present
213 $genders = $this->callAPISuccess('contact', 'getoptions', [
214 'field' => 'gender_id',
215 'context' => 'create',
216 ]);
217 $this->assertContains('Contrib', $genders['values']);
218
219 // Disable relevant component
220 CRM_Core_BAO_ConfigSetting::disableComponent('CiviContribute');
221 CRM_Core_PseudoConstant::flush();
222 // New option should now be hidden for "create" context
223 $genders = $this->callAPISuccess('contact', 'getoptions', [
224 'field' => 'gender_id',
225 'context' => 'create',
226 ]);
227 $this->assertNotContains('Contrib', $genders['values']);
228 // New option should be visible for "get" context even with component disabled
229 $genders = $this->callAPISuccess('contact', 'getoptions', [
230 'field' => 'gender_id',
231 'context' => 'get',
232 ]);
233 $this->assertContains('Contrib', $genders['values']);
234
235 // Now disable all components and ensure we can still fetch options with no errors
236 CRM_Core_BAO_ConfigSetting::setEnabledComponents([]);
237 CRM_Core_PseudoConstant::flush();
238 // New option should still be hidden for "create" context
239 $genders = $this->callAPISuccess('contact', 'getoptions', [
240 'field' => 'gender_id',
241 'context' => 'create',
242 ]);
243 $this->assertNotContains('Contrib', $genders['values']);
244
245 // Restore original state
246 CRM_Core_BAO_ConfigSetting::setEnabledComponents($components);
247 }
248
249 /**
250 * Check that domain_id is honoured.
251 */
252 public function testCRM12133CreateOptionWeightNoValue() {
253 $optionGroup = $this->callAPISuccess(
254 'option_group', 'get', [
255 'name' => 'gender',
256 'sequential' => 1,
257 ]
258 );
259 $this->assertAPISuccess($optionGroup);
260 $params = [
261 'option_group_id' => $optionGroup['id'],
262 'label' => 'my@y.com',
263 'weight' => 3,
264 ];
265 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
266 $this->assertAPISuccess($optionValue);
267 $params['weight'] = 4;
268 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params);
269 $this->assertAPISuccess($optionValue2);
270 $options = $this->callAPISuccess('option_value', 'get', ['option_group_id' => $optionGroup['id']]);
271 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
272
273 //cleanup
274 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValue['id']]);
275 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValue2['id']]);
276 }
277
278 /**
279 * Check that domain_id is honoured.
280 */
281 public function testCreateOptionNoName() {
282 $optionGroup = $this->callAPISuccess('option_group', 'get', [
283 'name' => 'gender',
284 'sequential' => 1,
285 ]);
286
287 $params = ['option_group_id' => $optionGroup['id'], 'label' => 'my@y.com'];
288 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
289 $this->assertAPISuccess($optionValue);
290 $this->getAndCheck($params, $optionValue['id'], 'option_value');
291 }
292
293 /**
294 * Check that pseudoconstant reflects new value added.
295 */
296 public function testCRM11876CreateOptionPseudoConstantUpdated() {
297 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', [
298 'name' => 'payment_instrument',
299 'return' => 'id',
300 ]);
301 $newOption = (string) time();
302 $apiResult = $this->callAPISuccess('option_value', 'create', [
303 'option_group_id' => $optionGroupID,
304 'label' => $newOption,
305 ]);
306
307 $fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
308 $this->assertTrue(in_array($newOption, $fields['values']));
309
310 $this->callAPISuccess('option_value', 'delete', ['id' => $apiResult['id']]);
311
312 $fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
313 $this->assertFalse(in_array($newOption, $fields['values']));
314 }
315
316 /**
317 * Update option value with 'id' parameter and the value to update
318 * and not passing option group id
319 */
320 public function testUpdateOptionValueNoGroupId() {
321 // create a option group
322 $og = $this->callAPISuccess('option_group', 'create', ['name' => 'our test Option Group', 'is_active' => 1]);
323 // create a option value
324 $ov = $this->callAPISuccess('option_value', 'create',
325 ['option_group_id' => $og['id'], 'label' => 'test option value']
326 );
327 // update option value without 'option_group_id'
328 $res = $this->callAPISuccess('option_value', 'create', ['id' => $ov['id'], 'is_active' => 0]);
329 $val = $this->callAPISuccess('option_value', 'getvalue', [
330 'id' => $ov['id'],
331 'return' => 'is_active',
332 ]);
333 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__);
334 }
335
336 /**
337 * Update option value with 'id' parameter and the value to update
338 * and as well as option group id
339 */
340 public function testUpdateOptionValueWithGroupId() {
341 // create a option group
342 $og = $this->callAPISuccess('option_group', 'create', [
343 'name' => 'our test Option Group for with group id',
344 'is_active' => 1,
345 ]);
346 // create a option value
347 $ov = $this->callAPISuccess('option_value', 'create',
348 ['option_group_id' => $og['id'], 'label' => 'test option value']
349 );
350 // update option value without 'option_group_id'
351 $this->callAPISuccess('option_value', 'create', [
352 'id' => $ov['id'],
353 'option_group_id' => $og['id'],
354 'is_active' => 0,
355 ]);
356 $val = $this->callAPISuccess('option_value', 'getvalue', [
357 'id' => $ov['id'],
358 'return' => 'is_active',
359 ]);
360 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__);
361 }
362
363 /**
364 * CRM-19346 Ensure that Option Values cannot share same value in the same option value group
365 */
366 public function testCreateOptionValueWithSameValue() {
367 $og = $this->callAPISuccess('option_group', 'create', [
368 'name' => 'our test Option Group for with group id',
369 'is_active' => 1,
370 ]);
371 // create a option value
372 $ov = $this->callAPISuccess('option_value', 'create',
373 ['option_group_id' => $og['id'], 'label' => 'test option value']
374 );
375 // update option value without 'option_group_id'
376 $this->callAPIFailure('option_value', 'create',
377 ['option_group_id' => $og['id'], 'label' => 'Test 2nd option value', 'value' => $ov['values'][$ov['id']]['value']]
378 );
379 }
380
381 /**
382 * CRM-21737 Ensure that language Option Values CAN share same value.
383 */
384 public function testCreateOptionValueWithSameValueLanguagesException() {
385 $this->callAPISuccess('option_value', 'create',
386 ['option_group_id' => 'languages', 'label' => 'Quasi English', 'name' => 'en_Qu', 'value' => 'en']
387 );
388 $this->callAPISuccess('option_value', 'create',
389 ['option_group_id' => 'languages', 'label' => 'Semi English', 'name' => 'en_Se', 'value' => 'en']
390 );
391
392 }
393
394 public function testCreateOptionValueWithSameValueDiffOptionGroup() {
395 $og = $this->callAPISuccess('option_group', 'create', [
396 'name' => 'our test Option Group',
397 'is_active' => 1,
398 ]);
399 // create a option value
400 $ov = $this->callAPISuccess('option_value', 'create',
401 ['option_group_id' => $og['id'], 'label' => 'test option value']
402 );
403 $og2 = $this->callAPISuccess('option_group', 'create', [
404 'name' => 'our test Option Group 2',
405 'is_active' => 1,
406 ]);
407 // update option value without 'option_group_id'
408 $ov2 = $this->callAPISuccess('option_value', 'create',
409 ['option_group_id' => $og2['id'], 'label' => 'Test 2nd option value', 'value' => $ov['values'][$ov['id']]['value']]
410 );
411 }
412
413 /**
414 * Test to create and update payment method with financial account.
415 */
416 public function testCreateUpdateOptionValueForPaymentInstrument() {
417 $assetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [
418 'return' => "id",
419 'financial_account_type_id' => "Asset",
420 'options' => ['limit' => 1],
421 ]);
422 // create new payment method with financial account
423 $ov = $this->callAPISuccess('OptionValue', 'create', [
424 'financial_account_id' => $assetFinancialAccountId,
425 'option_group_id' => "payment_instrument",
426 'label' => "Dummy Payment Method",
427 ]);
428
429 //check if relationship is created between Payment method and Financial Account
430 $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId);
431
432 // update payment method to have different non-asset financial Account
433 $nonAssetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [
434 'return' => "id",
435 'financial_account_type_id' => ['NOT IN' => ["Asset"]],
436 'options' => ['limit' => 1],
437 ]);
438 try {
439 $result = $this->callAPISuccess('OptionValue', 'create', [
440 'financial_account_id' => $nonAssetFinancialAccountId,
441 'id' => $ov['id'],
442 ]);
443 throw new API_Exception(ts('Should throw error.'));
444 }
445 catch (Exception $e) {
446 try {
447 $assetAccountRelValue = $this->callAPISuccessGetValue('EntityFinancialAccount', [
448 'return' => "account_relationship",
449 'entity_table' => "civicrm_option_value",
450 'entity_id' => $ov['id'],
451 'financial_account_id' => $nonAssetFinancialAccountId,
452 ]);
453 throw new API_Exception(ts('Should throw error.'));
454 }
455 catch (Exception $e) {
456 $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId);
457 }
458 }
459 // update payment method to have different asset financial Account
460 $assetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [
461 'return' => "id",
462 'financial_account_type_id' => "Asset",
463 'options' => ['limit' => 1],
464 'id' => ['NOT IN' => [$assetFinancialAccountId]],
465 ]);
466 $result = $this->callAPISuccess('OptionValue', 'create', [
467 'financial_account_id' => $assetFinancialAccountId,
468 'id' => $ov['id'],
469 ]);
470 //check if relationship is updated between Payment method and Financial Account
471 $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId);
472 }
473
474 /**
475 * Function to check relationship between FA and Payment method.
476 *
477 * @param int $paymentMethodId
478 * @param int $financialAccountId
479 */
480 protected function checkPaymentMethodFinancialAccountRelationship($paymentMethodId, $financialAccountId) {
481 $assetAccountRelValue = $this->callAPISuccessGetValue('EntityFinancialAccount', [
482 'return' => "account_relationship",
483 'entity_table' => "civicrm_option_value",
484 'entity_id' => $paymentMethodId,
485 'financial_account_id' => $financialAccountId,
486 ]);
487 $checkAssetAccountIs = $this->callAPISuccessGetValue('OptionValue', [
488 'return' => "id",
489 'option_group_id' => "account_relationship",
490 'name' => "Asset Account is",
491 'value' => $assetAccountRelValue,
492 ]);
493 }
494
495 }