3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_OptionValueTest
32 class api_v3_OptionValueTest
extends CiviUnitTestCase
{
33 protected $_apiversion = 3;
35 public function setUp() {
37 $this->useTransaction(TRUE);
40 public function testGetCount() {
41 $result = $this->callAPISuccess('option_value', 'getcount', array());
42 $this->assertGreaterThan(100, $result);
45 public function testGetOptionValueByID() {
46 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
47 $this->assertEquals(1, $result['count']);
48 $this->assertEquals(1, $result['id']);
51 public function testGetOptionValueByValue() {
52 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
53 $this->assertEquals(1, $result['count']);
54 $this->assertEquals(1, $result['id']);
60 public function testGetOptionValueLimit() {
62 $result = $this->callAPISuccess('option_value', 'get', $params);
63 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__
);
64 $params['options']['limit'] = 1;
65 $result = $this->callAPISuccess('option_value', 'get', $params);
66 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__
);
72 public function testGetOptionValueOffSet() {
74 $result = $this->callAPISuccess('option_value', 'get', array(
75 'option_group_id' => 1,
78 $result2 = $this->callAPISuccess('option_value', 'get', array(
79 'option_group_id' => 1,
81 'options' => array('offset' => 1),
83 $this->assertGreaterThan($result2['count'], $result['count']);
89 public function testGetSingleValueOptionValueSort() {
90 $description = "Demonstrates use of Sort param (available in many api functions). Also, getsingle.";
91 $subfile = 'SortOption';
92 $result = $this->callAPISuccess('option_value', 'getsingle', array(
93 'option_group_id' => 1,
95 'sort' => 'label ASC',
100 'option_group_id' => 1,
102 'sort' => 'label DESC',
106 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
107 $this->assertGreaterThan($result['label'], $result2['label']);
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.
113 public function testGetValueOptionPagination() {
115 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize)));
116 $page2 = $this->callAPISuccess('option_value', 'get', array(
118 'limit' => $pageSize,
119 // if you use it for pagination, option.offset=pageSize*pageNumber
120 'offset' => $pageSize - 1,
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__
);
126 $last = array_pop($page1['values']);
127 $first = array_shift($page2['values']);
129 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
132 public function testGetOptionGroup() {
133 $params = array('option_group_id' => 1);
134 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__
, __FILE__
);
135 $this->assertGreaterThan(1, $result['count']);
139 * Test that using option_group_name returns more than 1 & less than all
141 public function testGetOptionGroupByName() {
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);
146 $this->assertGreaterThan(1, $activityTypes['count']);
147 $this->assertGreaterThan($activityTypes['count'], $result['count']);
150 public function testGetOptionDoesNotExist() {
151 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
152 $this->assertEquals(0, $result['count']);
156 * Check that domain_id is honoured.
158 public function testCreateOptionSpecifyDomain() {
159 $result = $this->callAPISuccess('option_group', 'get', array(
160 'name' => 'from_email_address',
162 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
165 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
166 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
167 'id' => $optionValueId,
168 'return' => 'domain_id',
170 $this->assertEquals(2, $domain_id);
174 * Check that component_id is honoured.
176 public function testCreateOptionSpecifyComponentID() {
177 $result = $this->callAPISuccess('option_group', 'get', array(
178 'name' => 'from_email_address',
180 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
182 $this->assertAPISuccess($result);
183 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
184 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
185 'id' => $optionValueId,
186 'return' => 'component_id',
188 $this->assertEquals(2, $component_id);
192 * Check that component continues to be honoured.
194 public function testCreateOptionSpecifyComponent() {
195 $result = $this->callAPISuccess('option_group', 'get', array(
196 'name' => 'from_email_address',
198 'api.option_value.create' => array(
199 'component_id' => 'CiviContribute',
200 'name' => 'my@y.com',
204 $this->assertAPISuccess($result);
205 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
206 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
207 'id' => $optionValueId,
208 'return' => 'component_id',
210 $this->assertEquals(2, $component_id);
214 * Check that component string is honoured.
216 public function testCreateOptionSpecifyComponentString() {
217 $result = $this->callAPISuccess('option_group', 'get', array(
218 'name' => 'from_email_address',
220 'api.option_value.create' => array(
221 'component_id' => 'CiviContribute',
222 'name' => 'my@y.com',
226 $this->assertAPISuccess($result);
227 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
228 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
229 'id' => $optionValueId,
230 'return' => 'component_id',
232 $this->assertEquals(2, $component_id);
236 * Check that domain_id is honoured.
238 public function testCRM12133CreateOptionWeightNoValue() {
239 $optionGroup = $this->callAPISuccess(
240 'option_group', 'get', array(
245 $this->assertAPISuccess($optionGroup);
247 'option_group_id' => $optionGroup['id'],
248 'label' => 'my@y.com',
251 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
252 $this->assertAPISuccess($optionValue);
253 $params['weight'] = 4;
254 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params);
255 $this->assertAPISuccess($optionValue2);
256 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
257 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
260 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
261 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
265 * Check that domain_id is honoured.
267 public function testCreateOptionNoName() {
268 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
273 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
274 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
275 $this->assertAPISuccess($optionValue);
276 $this->getAndCheck($params, $optionValue['id'], 'option_value');
280 * Check that pseudoconstant reflects new value added.
282 public function testCRM11876CreateOptionPseudoConstantUpdated() {
283 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
284 'name' => 'payment_instrument',
287 $newOption = (string) time();
288 $apiResult = $this->callAPISuccess('option_value', 'create', array(
289 'option_group_id' => $optionGroupID,
290 'label' => $newOption,
293 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
294 $this->assertTrue(in_array($newOption, $fields['values']));
296 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
298 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
299 $this->assertFalse(in_array($newOption, $fields['values']));
304 * Update option value with 'id' parameter and the value to update
305 * and not passing option group id
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')
314 // update option value without 'option_group_id'
315 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
316 $val = $this->callAPISuccess('option_value', 'getvalue', array(
318 'return' => 'is_active',
320 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__
);
324 * Update option value with 'id' parameter and the value to update
325 * and as well as option group id
327 public function testUpdateOptionValueWithGroupId() {
328 // create a option group
329 $og = $this->callAPISuccess('option_group', 'create', array(
330 'name' => 'our test Option Group for with group id',
333 // create a option value
334 $ov = $this->callAPISuccess('option_value', 'create',
335 array('option_group_id' => $og['id'], 'label' => 'test option value')
337 // update option value without 'option_group_id'
338 $this->callAPISuccess('option_value', 'create', array(
340 'option_group_id' => $og['id'],
343 $val = $this->callAPISuccess('option_value', 'getvalue', array(
345 'return' => 'is_active',
347 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__
);