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 +--------------------------------------------------------------------+
29 * Class api_v3_OptionValueTest
31 class api_v3_OptionValueTest
extends CiviUnitTestCase
{
32 protected $_apiversion = 3;
34 public function setUp() {
36 $this->useTransaction(TRUE);
39 public function testGetCount() {
40 $result = $this->callAPISuccess('option_value', 'getcount', array());
41 $this->assertGreaterThan(100, $result);
44 public function testGetOptionValueByID() {
45 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
46 $this->assertEquals(1, $result['count']);
47 $this->assertEquals(1, $result['id']);
50 public function testGetOptionValueByValue() {
51 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
52 $this->assertEquals(1, $result['count']);
53 $this->assertEquals(1, $result['id']);
59 public function testGetOptionValueLimit() {
61 $result = $this->callAPISuccess('option_value', 'get', $params);
62 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__
);
63 $params['options']['limit'] = 1;
64 $result = $this->callAPISuccess('option_value', 'get', $params);
65 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__
);
71 public function testGetOptionValueOffSet() {
73 $result = $this->callAPISuccess('option_value', 'get', array(
74 'option_group_id' => 1,
77 $result2 = $this->callAPISuccess('option_value', 'get', array(
78 'option_group_id' => 1,
80 'options' => array('offset' => 1),
82 $this->assertGreaterThan($result2['count'], $result['count']);
88 public function testGetSingleValueOptionValueSort() {
89 $description = "Demonstrates use of Sort param (available in many api functions). Also, getsingle.";
90 $subfile = 'SortOption';
91 $result = $this->callAPISuccess('option_value', 'getsingle', array(
92 'option_group_id' => 1,
94 'sort' => 'label ASC',
99 'option_group_id' => 1,
101 'sort' => 'label DESC',
105 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
106 $this->assertGreaterThan($result['label'], $result2['label']);
110 * 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.
112 public function testGetValueOptionPagination() {
114 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize)));
115 $page2 = $this->callAPISuccess('option_value', 'get', array(
117 'limit' => $pageSize,
118 // if you use it for pagination, option.offset=pageSize*pageNumber
119 'offset' => $pageSize - 1,
122 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__
);
123 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__
);
125 $last = array_pop($page1['values']);
126 $first = array_shift($page2['values']);
128 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
131 public function testGetOptionGroup() {
132 $params = array('option_group_id' => 1);
133 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__
, __FILE__
);
134 $this->assertGreaterThan(1, $result['count']);
138 * Test that using option_group_name returns more than 1 & less than all
140 public function testGetOptionGroupByName() {
141 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
142 $params = array('option.limit' => 100);
143 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
144 $result = $this->callAPISuccess('option_value', 'get', $params);
145 $this->assertGreaterThan(1, $activityTypes['count']);
146 $this->assertGreaterThan($activityTypes['count'], $result['count']);
149 public function testGetOptionDoesNotExist() {
150 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
151 $this->assertEquals(0, $result['count']);
155 * Check that domain_id is honoured.
157 public function testCreateOptionSpecifyDomain() {
158 $result = $this->callAPISuccess('option_group', 'get', array(
159 'name' => 'from_email_address',
161 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
164 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
165 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
166 'id' => $optionValueId,
167 'return' => 'domain_id',
169 $this->assertEquals(2, $domain_id);
173 * Check that component_id is honoured.
175 public function testCreateOptionSpecifyComponentID() {
176 $result = $this->callAPISuccess('option_group', 'get', array(
177 'name' => 'from_email_address',
179 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
181 $this->assertAPISuccess($result);
182 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
183 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
184 'id' => $optionValueId,
185 'return' => 'component_id',
187 $this->assertEquals(2, $component_id);
191 * Check that component continues to be honoured.
193 public function testCreateOptionSpecifyComponent() {
194 $result = $this->callAPISuccess('option_group', 'get', array(
195 'name' => 'from_email_address',
197 'api.option_value.create' => array(
198 'component_id' => 'CiviContribute',
199 'name' => 'my@y.com',
203 $this->assertAPISuccess($result);
204 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
205 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
206 'id' => $optionValueId,
207 'return' => 'component_id',
209 $this->assertEquals(2, $component_id);
213 * Check that component string is honoured.
215 public function testCreateOptionSpecifyComponentString() {
216 $result = $this->callAPISuccess('option_group', 'get', array(
217 'name' => 'from_email_address',
219 'api.option_value.create' => array(
220 'component_id' => 'CiviContribute',
221 'name' => 'my@y.com',
225 $this->assertAPISuccess($result);
226 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
227 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
228 'id' => $optionValueId,
229 'return' => 'component_id',
231 $this->assertEquals(2, $component_id);
235 * Check that domain_id is honoured.
237 public function testCRM12133CreateOptionWeightNoValue() {
238 $optionGroup = $this->callAPISuccess(
239 'option_group', 'get', array(
244 $this->assertAPISuccess($optionGroup);
246 'option_group_id' => $optionGroup['id'],
247 'label' => 'my@y.com',
250 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
251 $this->assertAPISuccess($optionValue);
252 $params['weight'] = 4;
253 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params);
254 $this->assertAPISuccess($optionValue2);
255 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
256 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
259 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
260 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
264 * Check that domain_id is honoured.
266 public function testCreateOptionNoName() {
267 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
272 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
273 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
274 $this->assertAPISuccess($optionValue);
275 $this->getAndCheck($params, $optionValue['id'], 'option_value');
279 * Check that pseudoconstant reflects new value added.
281 public function testCRM11876CreateOptionPseudoConstantUpdated() {
282 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
283 'name' => 'payment_instrument',
286 $newOption = (string) time();
287 $apiResult = $this->callAPISuccess('option_value', 'create', array(
288 'option_group_id' => $optionGroupID,
289 'label' => $newOption,
292 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
293 $this->assertTrue(in_array($newOption, $fields['values']));
295 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
297 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
298 $this->assertFalse(in_array($newOption, $fields['values']));
303 * Update option value with 'id' parameter and the value to update
304 * and not passing option group id
306 public function testUpdateOptionValueNoGroupId() {
307 // create a option group
308 $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1));
309 // create a option value
310 $ov = $this->callAPISuccess('option_value', 'create',
311 array('option_group_id' => $og['id'], 'label' => 'test option value')
313 // update option value without 'option_group_id'
314 $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0));
315 $val = $this->callAPISuccess('option_value', 'getvalue', array(
317 'return' => 'is_active',
319 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__
);
323 * Update option value with 'id' parameter and the value to update
324 * and as well as option group id
326 public function testUpdateOptionValueWithGroupId() {
327 // create a option group
328 $og = $this->callAPISuccess('option_group', 'create', array(
329 'name' => 'our test Option Group for with group id',
332 // create a option value
333 $ov = $this->callAPISuccess('option_value', 'create',
334 array('option_group_id' => $og['id'], 'label' => 'test option value')
336 // update option value without 'option_group_id'
337 $this->callAPISuccess('option_value', 'create', array(
339 'option_group_id' => $og['id'],
342 $val = $this->callAPISuccess('option_value', 'getvalue', array(
344 'return' => 'is_active',
346 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__
);