3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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;
36 public function setUp() {
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']);
49 $this->assertEquals(1, $result['id']);
52 public function testGetOptionValueByValue() {
53 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
54 $this->assertEquals(1, $result['count']);
55 $this->assertEquals(1, $result['id']);
61 public 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 public function testGetOptionValueOffSet() {
75 $result = $this->callAPISuccess('option_value', 'get', array(
76 'option_group_id' => 1,
79 $result2 = $this->callAPISuccess('option_value', 'get', array(
80 'option_group_id' => 1,
82 'options' => array('offset' => 1),
84 $this->assertGreaterThan($result2['count'], $result['count']);
90 public function testGetSingleValueOptionValueSort() {
91 $description = "Demonstrates use of Sort param (available in many api functions). Also, getsingle.";
92 $subfile = 'SortOption';
93 $result = $this->callAPISuccess('option_value', 'getsingle', array(
94 'option_group_id' => 1,
96 'sort' => 'label ASC',
101 'option_group_id' => 1,
103 'sort' => 'label DESC',
107 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
108 $this->assertGreaterThan($result['label'], $result2['label']);
112 * 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.
114 public function testGetValueOptionPagination() {
116 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize)));
117 $page2 = $this->callAPISuccess('option_value', 'get', array(
119 'limit' => $pageSize,
120 // if you use it for pagination, option.offset=pageSize*pageNumber
121 'offset' => $pageSize - 1,
124 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__
);
125 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__
);
127 $last = array_pop($page1['values']);
128 $first = array_shift($page2['values']);
130 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
133 public function testGetOptionGroup() {
134 $params = array('option_group_id' => 1);
135 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__
, __FILE__
);
136 $this->assertGreaterThan(1, $result['count']);
140 * Test that using option_group_name returns more than 1 & less than all
142 public function testGetOptionGroupByName() {
143 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
144 $params = array('option.limit' => 100);
145 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
146 $result = $this->callAPISuccess('option_value', 'get', $params);
147 $this->assertGreaterThan(1, $activityTypes['count']);
148 $this->assertGreaterThan($activityTypes['count'], $result['count']);
151 public function testGetOptionDoesNotExist() {
152 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
153 $this->assertEquals(0, $result['count']);
157 * Check that domain_id is honoured.
159 public function testCreateOptionSpecifyDomain() {
160 $result = $this->callAPISuccess('option_group', 'get', array(
161 'name' => 'from_email_address',
163 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
166 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
167 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
168 'id' => $optionValueId,
169 'return' => 'domain_id',
171 $this->assertEquals(2, $domain_id);
175 * Check that component_id is honoured.
177 public function testCreateOptionSpecifyComponentID() {
178 $result = $this->callAPISuccess('option_group', 'get', array(
179 'name' => 'from_email_address',
181 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
183 $this->assertAPISuccess($result);
184 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
185 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
186 'id' => $optionValueId,
187 'return' => 'component_id',
189 $this->assertEquals(2, $component_id);
193 * Check that component continues to be honoured.
195 public function testCreateOptionSpecifyComponent() {
196 $result = $this->callAPISuccess('option_group', 'get', array(
197 'name' => 'from_email_address',
199 'api.option_value.create' => array(
200 'component_id' => 'CiviContribute',
201 'name' => 'my@y.com',
205 $this->assertAPISuccess($result);
206 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
207 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
208 'id' => $optionValueId,
209 'return' => 'component_id',
211 $this->assertEquals(2, $component_id);
215 * Check that component string is honoured.
217 public function testCreateOptionSpecifyComponentString() {
218 $result = $this->callAPISuccess('option_group', 'get', array(
219 'name' => 'from_email_address',
221 'api.option_value.create' => array(
222 'component_id' => 'CiviContribute',
223 'name' => 'my@y.com',
227 $this->assertAPISuccess($result);
228 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
229 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
230 'id' => $optionValueId,
231 'return' => 'component_id',
233 $this->assertEquals(2, $component_id);
237 * Check that domain_id is honoured.
239 public function testCRM12133CreateOptionWeightNoValue() {
240 $optionGroup = $this->callAPISuccess(
241 'option_group', 'get', array(
246 $this->assertAPISuccess($optionGroup);
248 'option_group_id' => $optionGroup['id'],
249 'label' => 'my@y.com',
252 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
253 $this->assertAPISuccess($optionValue);
254 $params['weight'] = 4;
255 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params);
256 $this->assertAPISuccess($optionValue2);
257 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
258 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
261 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
262 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
266 * Check that domain_id is honoured.
268 public function testCreateOptionNoName() {
269 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
274 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
275 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
276 $this->assertAPISuccess($optionValue);
277 $this->getAndCheck($params, $optionValue['id'], 'option_value');
281 * Check that pseudoconstant reflects new value added.
283 public function testCRM11876CreateOptionPseudoConstantUpdated() {
284 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
285 'name' => 'payment_instrument',
288 $newOption = (string) time();
289 $apiResult = $this->callAPISuccess('option_value', 'create', array(
290 'option_group_id' => $optionGroupID,
291 'label' => $newOption,
294 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
295 $this->assertTrue(in_array($newOption, $fields['values']));
297 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
299 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
300 $this->assertFalse(in_array($newOption, $fields['values']));
305 * Update option value with 'id' parameter and the value to update
306 * and not passing option group id
308 public function testUpdateOptionValueNoGroupId() {
309 // create a option group
310 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1));
311 // create a option value
312 $ov = $this->callAPISuccess('option_value', 'create',
313 array('option_group_id' => $og['id'], 'label' => 'test option value')
315 // update option value without 'option_group_id'
316 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
317 $val = $this->callAPISuccess('option_value', 'getvalue', array(
319 'return' => 'is_active',
321 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__
);
325 * Update option value with 'id' parameter and the value to update
326 * and as well as option group id
328 public function testUpdateOptionValueWithGroupId() {
329 // create a option group
330 $og = $this->callAPISuccess('option_group', 'create', array(
331 'name' => 'our test Option Group for with group id',
334 // create a option value
335 $ov = $this->callAPISuccess('option_value', 'create',
336 array('option_group_id' => $og['id'], 'label' => 'test option value')
338 // update option value without 'option_group_id'
339 $this->callAPISuccess('option_value', 'create', array(
341 'option_group_id' => $og['id'],
344 $val = $this->callAPISuccess('option_value', 'getvalue', array(
346 'return' => 'is_active',
348 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__
);