3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class api_v3_OptionValueTest
16 class api_v3_OptionValueTest
extends CiviUnitTestCase
{
17 protected $_apiversion = 3;
19 public function setUp() {
21 $this->useTransaction(TRUE);
24 public function testGetCount() {
25 $result = $this->callAPISuccess('option_value', 'getcount', []);
26 $this->assertGreaterThan(100, $result);
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']);
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']);
44 public function testGetOptionValueLimit() {
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__
);
56 public function testGetOptionValueOffSet() {
58 $result = $this->callAPISuccess('option_value', 'get', [
59 'option_group_id' => 1,
62 $result2 = $this->callAPISuccess('option_value', 'get', [
63 'option_group_id' => 1,
65 'options' => ['offset' => 1],
67 $this->assertGreaterThan($result2['count'], $result['count']);
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,
79 'sort' => 'label ASC',
84 'option_group_id' => 1,
86 'sort' => 'label DESC',
90 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
91 $this->assertGreaterThan($result['label'], $result2['label']);
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.
97 public function testGetValueOptionPagination() {
99 $page1 = $this->callAPISuccess('option_value', 'get', ['options' => ['limit' => $pageSize]]);
100 $page2 = $this->callAPISuccess('option_value', 'get', [
102 'limit' => $pageSize,
103 // if you use it for pagination, option.offset=pageSize*pageNumber
104 'offset' => $pageSize - 1,
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__
);
110 $last = array_pop($page1['values']);
111 $first = array_shift($page2['values']);
113 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
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']);
123 * Test that using option_group_name returns more than 1 & less than all
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']);
134 public function testGetOptionDoesNotExist() {
135 $result = $this->callAPISuccess('option_value', 'get', ['label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB']);
136 $this->assertEquals(0, $result['count']);
140 * Check that domain_id is honoured.
142 public function testCreateOptionSpecifyDomain() {
143 $result = $this->callAPISuccess('option_group', 'get', [
144 'name' => 'from_email_address',
146 'api.option_value.create' => ['domain_id' => 2, 'name' => 'my@y.com', 'value' => '10'],
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',
154 $this->assertEquals(2, $domain_id);
155 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValueId]);
159 * Check that component_id is honoured.
161 public function testCreateOptionSpecifyComponentID() {
162 $result = $this->callAPISuccess('option_group', 'get', [
163 'name' => 'from_email_address',
165 'api.option_value.create' => ['component_id' => 2, 'name' => 'my@y.com'],
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',
173 $this->assertEquals(2, $component_id);
174 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValueId]);
178 * Check that component string is honoured.
180 public function testCreateOptionSpecifyComponentString() {
181 $result = $this->callAPISuccess('option_group', 'get', [
182 'name' => 'from_email_address',
184 'api.option_value.create' => [
185 'component_id' => 'CiviContribute',
186 'name' => 'my@y.com',
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',
195 $this->assertEquals(2, $component_id);
196 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValueId]);
200 * Check that component is honoured when fetching options.
202 public function testGetOptionWithComponent() {
203 $components = Civi
::settings()->get('enable_components');
204 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviContribute');
205 $this->callAPISuccess('option_group', 'get', [
207 'api.option_value.create' => [
208 'component_id' => 'CiviContribute',
212 // Verify new option is present
213 $genders = $this->callAPISuccess('contact', 'getoptions', [
214 'field' => 'gender_id',
215 'context' => 'create',
217 $this->assertContains('Contrib', $genders['values']);
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',
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',
233 $this->assertContains('Contrib', $genders['values']);
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',
243 $this->assertNotContains('Contrib', $genders['values']);
245 // Restore original state
246 CRM_Core_BAO_ConfigSetting
::setEnabledComponents($components);
250 * Check that domain_id is honoured.
252 public function testCRM12133CreateOptionWeightNoValue() {
253 $optionGroup = $this->callAPISuccess(
254 'option_group', 'get', [
259 $this->assertAPISuccess($optionGroup);
261 'option_group_id' => $optionGroup['id'],
262 'label' => 'my@y.com',
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']);
274 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValue['id']]);
275 $this->callAPISuccess('option_value', 'delete', ['id' => $optionValue2['id']]);
279 * Check that domain_id is honoured.
281 public function testCreateOptionNoName() {
282 $optionGroup = $this->callAPISuccess('option_group', 'get', [
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');
294 * Check that pseudoconstant reflects new value added.
296 public function testCRM11876CreateOptionPseudoConstantUpdated() {
297 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', [
298 'name' => 'payment_instrument',
301 $newOption = (string) time();
302 $apiResult = $this->callAPISuccess('option_value', 'create', [
303 'option_group_id' => $optionGroupID,
304 'label' => $newOption,
307 $fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
308 $this->assertTrue(in_array($newOption, $fields['values']));
310 $this->callAPISuccess('option_value', 'delete', ['id' => $apiResult['id']]);
312 $fields = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
313 $this->assertFalse(in_array($newOption, $fields['values']));
317 * Update option value with 'id' parameter and the value to update
318 * and not passing option group id
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']
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', [
331 'return' => 'is_active',
333 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__
);
337 * Update option value with 'id' parameter and the value to update
338 * and as well as option group id
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',
346 // create a option value
347 $ov = $this->callAPISuccess('option_value', 'create',
348 ['option_group_id' => $og['id'], 'label' => 'test option value']
350 // update option value without 'option_group_id'
351 $this->callAPISuccess('option_value', 'create', [
353 'option_group_id' => $og['id'],
356 $val = $this->callAPISuccess('option_value', 'getvalue', [
358 'return' => 'is_active',
360 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__
);
364 * CRM-19346 Ensure that Option Values cannot share same value in the same option value group
366 public function testCreateOptionValueWithSameValue() {
367 $og = $this->callAPISuccess('option_group', 'create', [
368 'name' => 'our test Option Group for with group id',
371 // create a option value
372 $ov = $this->callAPISuccess('option_value', 'create',
373 ['option_group_id' => $og['id'], 'label' => 'test option value']
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']]
382 * CRM-21737 Ensure that language Option Values CAN share same value.
384 public function testCreateOptionValueWithSameValueLanguagesException() {
385 $this->callAPISuccess('option_value', 'create',
386 ['option_group_id' => 'languages', 'label' => 'Quasi English', 'name' => 'en_Qu', 'value' => 'en']
388 $this->callAPISuccess('option_value', 'create',
389 ['option_group_id' => 'languages', 'label' => 'Semi English', 'name' => 'en_Se', 'value' => 'en']
394 public function testCreateOptionValueWithSameValueDiffOptionGroup() {
395 $og = $this->callAPISuccess('option_group', 'create', [
396 'name' => 'our test Option Group',
399 // create a option value
400 $ov = $this->callAPISuccess('option_value', 'create',
401 ['option_group_id' => $og['id'], 'label' => 'test option value']
403 $og2 = $this->callAPISuccess('option_group', 'create', [
404 'name' => 'our test Option Group 2',
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']]
414 * Test to create and update payment method with financial account.
416 public function testCreateUpdateOptionValueForPaymentInstrument() {
417 $assetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [
419 'financial_account_type_id' => "Asset",
420 'options' => ['limit' => 1],
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",
429 //check if relationship is created between Payment method and Financial Account
430 $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId);
432 // update payment method to have different non-asset financial Account
433 $nonAssetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [
435 'financial_account_type_id' => ['NOT IN' => ["Asset"]],
436 'options' => ['limit' => 1],
439 $result = $this->callAPISuccess('OptionValue', 'create', [
440 'financial_account_id' => $nonAssetFinancialAccountId,
443 throw new API_Exception(ts('Should throw error.'));
445 catch (Exception
$e) {
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,
453 throw new API_Exception(ts('Should throw error.'));
455 catch (Exception
$e) {
456 $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId);
459 // update payment method to have different asset financial Account
460 $assetFinancialAccountId = $this->callAPISuccessGetValue('FinancialAccount', [
462 'financial_account_type_id' => "Asset",
463 'options' => ['limit' => 1],
464 'id' => ['NOT IN' => [$assetFinancialAccountId]],
466 $result = $this->callAPISuccess('OptionValue', 'create', [
467 'financial_account_id' => $assetFinancialAccountId,
470 //check if relationship is updated between Payment method and Financial Account
471 $this->checkPaymentMethodFinancialAccountRelationship($ov['id'], $assetFinancialAccountId);
475 * Function to check relationship between FA and Payment method.
477 * @param int $paymentMethodId
478 * @param int $financialAccountId
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,
487 $checkAssetAccountIs = $this->callAPISuccessGetValue('OptionValue', [
489 'option_group_id' => "account_relationship",
490 'name' => "Asset Account is",
491 'value' => $assetAccountRelValue,