3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class api_v3_OptionValueTest
33 class api_v3_OptionValueTest
extends CiviUnitTestCase
{
34 protected $_apiversion = 3;
40 function tearDown() {}
43 public function testGetCount() {
44 $result = $this->callAPISuccess('option_value', 'getcount', array());
45 $this->assertGreaterThan(100, $result);
48 public function testGetOptionValueByID() {
49 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
50 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
51 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__
);
54 public function testGetOptionValueByValue() {
55 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
56 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
57 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__
);
63 function testGetOptionValueLimit() {
65 $result = $this->callAPISuccess('option_value', 'get', $params);
66 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__
);
67 $params['options']['limit'] = 1;
68 $result = $this->callAPISuccess('option_value', 'get', $params);
69 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__
);
75 function testGetOptionValueOffSet() {
77 $result = $this->callAPISuccess('option_value', 'get', array(
78 'option_group_id' => 1,
80 $result2 = $this->callAPISuccess('option_value', 'get', array(
81 'option_group_id' => 1,
83 'options' => array('offset' => 1),
85 $this->assertGreaterThan($result2['count'], $result['count']);
91 function testGetSingleValueOptionValueSort() {
92 $description = "demonstrates use of Sort param (available in many api functions). Also, getsingle";
93 $subfile = 'SortOption';
94 $result = $this->callAPISuccess('option_value', 'getsingle', array(
95 'option_group_id' => 1, 'options' => array(
96 'sort' => 'label ASC',
101 'option_group_id' => 1, 'options' => array(
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 function testGetValueOptionPagination() {
115 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), ));
116 $page2 = $this->callAPISuccess('option_value', 'get', array(
117 'options' => array('limit' => $pageSize,
118 // if you use it for pagination, option.offset=pageSize*pageNumber
119 'offset' => $pageSize - 1,
121 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__
);
122 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__
);
124 $last = array_pop($page1['values']);
125 $first = array_shift($page2['values']);
127 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
130 public function testGetOptionGroup() {
131 $params = array('option_group_id' => 1);
132 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__
, __FILE__
);
133 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__
);
137 * Test that using option_group_name returns more than 1 & less than all
139 public function testGetOptionGroupByName() {
140 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
141 $params = array('option.limit' => 100);
142 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
143 $result = $this->callAPISuccess('option_value', 'get', $params);
144 $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__
);
145 $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__
);
148 public function testGetOptionDoesNotExist() {
149 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
150 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
154 * Check that domain_id is honoured
156 public function testCreateOptionSpecifyDomain() {
157 $result = $this->callAPISuccess('option_group', 'get', array(
158 'name' => 'from_email_address',
160 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
163 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
164 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
165 'id' => $optionValueId,
166 'return' => 'domain_id',
168 $this->assertEquals(2, $domain_id);
172 * Check that component_id is honoured
174 public function testCreateOptionSpecifyComponentID() {
175 $result = $this->callAPISuccess('option_group', 'get', array(
176 'name' => 'from_email_address',
177 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
179 $this->assertAPISuccess($result);
180 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
181 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
182 'id' => $optionValueId, 'return' => 'component_id',
184 $this->assertEquals(2, $component_id);
188 * Check that component continues to be honoured
190 public function testCreateOptionSpecifyComponent() {
191 $result = $this->callAPISuccess('option_group', 'get', array(
192 'name' => 'from_email_address',
193 'sequential' => 1, 'api.option_value.create' => array(
194 'component_id' => 'CiviContribute',
199 $this->assertAPISuccess($result);
200 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
201 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
202 'id' => $optionValueId, 'return' => 'component_id',
204 $this->assertEquals(2, $component_id);
208 * Check that component string is honoured
210 public function testCreateOptionSpecifyComponentString() {
211 $result = $this->callAPISuccess('option_group', 'get', array(
212 'name' => 'from_email_address',
213 'sequential' => 1, 'api.option_value.create' => array(
214 'component_id' => 'CiviContribute',
215 'name' => 'my@y.com'),
218 $this->assertAPISuccess($result);
219 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
220 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
221 'id' => $optionValueId, 'return' => 'component_id',
223 $this->assertEquals(2, $component_id);
227 * Check that domain_id is honoured
229 public function testCRM12133CreateOptionWeightNoValue() {
230 $optionGroup = $this->callAPISuccess(
231 'option_group', 'get', array(
233 'sequential' => 1, ));
234 $this->assertAPISuccess($optionGroup);
236 'option_group_id' => $optionGroup['id'],
237 'label' => 'my@y.com', 'weight' => 3,
239 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
240 $this->assertAPISuccess($optionValue);
241 $params['weight'] = 4;
242 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params );
243 $this->assertAPISuccess($optionValue2);
244 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
245 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
248 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
249 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
253 * Check that domain_id is honoured
255 public function testCreateOptionNoName() {
256 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
258 'sequential' => 1, ));
260 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
261 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
262 $this->assertAPISuccess($optionValue);
263 $this->getAndCheck($params, $optionValue['id'], 'option_value');
267 * Check that pseudoconstant reflects new value added
270 public function testCRM11876CreateOptionPseudoConstantUpdated() {
271 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument',
274 $newOption = (string) time();
275 $apiResult = $this->callAPISuccess('option_value', 'create', array(
276 'option_group_id' => $optionGroupID,
277 'label' => $newOption,
280 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
281 $this->assertTrue(in_array($newOption, $fields['values']));
283 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
285 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
286 $this->assertFalse(in_array($newOption, $fields['values']));
291 * Update option value with 'id' paramter and the value to update
292 * and not passing option group id
294 public function testUpdateOptionValueNoGroupId() {
295 // create a option group
296 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1));
297 // create a option value
298 $ov = $this->callAPISuccess('option_value', 'create',
299 array('option_group_id' => $og['id'], 'label' => 'test option value')
301 // update option value without 'option_group_id'
302 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
303 $val = $this->callAPISuccess('option_value', 'getvalue', array(
304 'id' => $ov['id'], 'return' => 'is_active',
306 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__
);
310 * Update option value with 'id' paramter and the value to update
311 * and as well as option group id
313 public function testUpdateOptionValueWithGroupId() {
314 // create a option group
315 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group for with group id', 'is_active' => 1));
316 // create a option value
317 $ov = $this->callAPISuccess('option_value', 'create',
318 array('option_group_id' => $og['id'], 'label' => 'test option value')
320 // update option value without 'option_group_id'
321 $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'option_group_id' => $og['id'], 'is_active' => 0));
322 $val = $this->callAPISuccess('option_value', 'getvalue', array(
323 'id' => $ov['id'], 'return' => 'is_active',
325 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__
);