3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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;
38 $this->useTransaction(TRUE);
41 public function testGetCount() {
42 $result = $this->callAPISuccess('option_value', 'getcount', array());
43 $this->assertGreaterThan(100, $result);
46 public function testGetOptionValueByID() {
47 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
48 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
49 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__
);
52 public function testGetOptionValueByValue() {
53 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
54 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
55 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__
);
61 function testGetOptionValueLimit() {
63 $result = $this->callAPISuccess('option_value', 'get', $params);
64 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__
);
65 $params['options']['limit'] = 1;
66 $result = $this->callAPISuccess('option_value', 'get', $params);
67 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__
);
73 function testGetOptionValueOffSet() {
75 $result = $this->callAPISuccess('option_value', 'get', array(
76 '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 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, 'options' => array(
94 'sort' => 'label ASC',
99 'option_group_id' => 1, 'options' => array(
100 'sort' => 'label DESC',
104 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
105 $this->assertGreaterThan($result['label'], $result2['label']);
109 * 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.
111 function testGetValueOptionPagination() {
113 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), ));
114 $page2 = $this->callAPISuccess('option_value', 'get', array(
115 'options' => array('limit' => $pageSize,
116 // if you use it for pagination, option.offset=pageSize*pageNumber
117 'offset' => $pageSize - 1,
119 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__
);
120 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__
);
122 $last = array_pop($page1['values']);
123 $first = array_shift($page2['values']);
125 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
128 public function testGetOptionGroup() {
129 $params = array('option_group_id' => 1);
130 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__
, __FILE__
);
131 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__
);
135 * Test that using option_group_name returns more than 1 & less than all
137 public function testGetOptionGroupByName() {
138 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
139 $params = array('option.limit' => 100);
140 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
141 $result = $this->callAPISuccess('option_value', 'get', $params);
142 $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__
);
143 $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__
);
146 public function testGetOptionDoesNotExist() {
147 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
148 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__
);
152 * Check that domain_id is honoured
154 public function testCreateOptionSpecifyDomain() {
155 $result = $this->callAPISuccess('option_group', 'get', array(
156 'name' => 'from_email_address',
158 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
161 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
162 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
163 'id' => $optionValueId,
164 'return' => 'domain_id',
166 $this->assertEquals(2, $domain_id);
170 * Check that component_id is honoured
172 public function testCreateOptionSpecifyComponentID() {
173 $result = $this->callAPISuccess('option_group', 'get', array(
174 'name' => 'from_email_address',
175 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
177 $this->assertAPISuccess($result);
178 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
179 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
180 'id' => $optionValueId, 'return' => 'component_id',
182 $this->assertEquals(2, $component_id);
186 * Check that component continues to be honoured
188 public function testCreateOptionSpecifyComponent() {
189 $result = $this->callAPISuccess('option_group', 'get', array(
190 'name' => 'from_email_address',
191 'sequential' => 1, 'api.option_value.create' => array(
192 'component_id' => 'CiviContribute',
197 $this->assertAPISuccess($result);
198 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
199 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
200 'id' => $optionValueId, 'return' => 'component_id',
202 $this->assertEquals(2, $component_id);
206 * Check that component string is honoured
208 public function testCreateOptionSpecifyComponentString() {
209 $result = $this->callAPISuccess('option_group', 'get', array(
210 'name' => 'from_email_address',
211 'sequential' => 1, 'api.option_value.create' => array(
212 'component_id' => 'CiviContribute',
213 'name' => 'my@y.com'),
216 $this->assertAPISuccess($result);
217 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
218 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
219 'id' => $optionValueId, 'return' => 'component_id',
221 $this->assertEquals(2, $component_id);
225 * Check that domain_id is honoured
227 public function testCRM12133CreateOptionWeightNoValue() {
228 $optionGroup = $this->callAPISuccess(
229 'option_group', 'get', array(
231 'sequential' => 1, ));
232 $this->assertAPISuccess($optionGroup);
234 'option_group_id' => $optionGroup['id'],
235 'label' => 'my@y.com', 'weight' => 3,
237 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
238 $this->assertAPISuccess($optionValue);
239 $params['weight'] = 4;
240 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params );
241 $this->assertAPISuccess($optionValue2);
242 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
243 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
246 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
247 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
251 * Check that domain_id is honoured
253 public function testCreateOptionNoName() {
254 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
256 'sequential' => 1, ));
258 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
259 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
260 $this->assertAPISuccess($optionValue);
261 $this->getAndCheck($params, $optionValue['id'], 'option_value');
265 * Check that pseudoconstant reflects new value added
268 public function testCRM11876CreateOptionPseudoConstantUpdated() {
269 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument',
272 $newOption = (string) time();
273 $apiResult = $this->callAPISuccess('option_value', 'create', array(
274 'option_group_id' => $optionGroupID,
275 'label' => $newOption,
278 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
279 $this->assertTrue(in_array($newOption, $fields['values']));
281 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
283 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
284 $this->assertFalse(in_array($newOption, $fields['values']));
289 * Update option value with 'id' paramter and the value to update
290 * and not passing option group id
292 public function testUpdateOptionValueNoGroupId() {
293 // create a option group
294 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1));
295 // create a option value
296 $ov = $this->callAPISuccess('option_value', 'create',
297 array('option_group_id' => $og['id'], 'label' => 'test option value')
299 // update option value without 'option_group_id'
300 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
301 $val = $this->callAPISuccess('option_value', 'getvalue', array(
302 'id' => $ov['id'], 'return' => 'is_active',
304 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__
);
308 * Update option value with 'id' paramter and the value to update
309 * and as well as option group id
311 public function testUpdateOptionValueWithGroupId() {
312 // create a option group
313 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group for with group id', 'is_active' => 1));
314 // create a option value
315 $ov = $this->callAPISuccess('option_value', 'create',
316 array('option_group_id' => $og['id'], 'label' => 'test option value')
318 // update option value without 'option_group_id'
319 $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'option_group_id' => $og['id'], 'is_active' => 0));
320 $val = $this->callAPISuccess('option_value', 'getvalue', array(
321 'id' => $ov['id'], 'return' => 'is_active',
323 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__
);