672def2d32007cfe5f71d55c9d7993e7b2ad452f
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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';
30 class api_v3_OptionValueTest
extends CiviUnitTestCase
{
31 protected $_apiversion = 3;
32 public $_eNoticeCompliant = TRUE;
37 function tearDown() {}
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'], 'In line ' . __LINE__
);
48 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__
);
51 public function testGetOptionValueByValue() {
52 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
53 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
54 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__
);
60 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 function testGetOptionValueOffSet() {
74 $result = $this->callAPISuccess('option_value', 'get', array(
75 '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 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, 'options' => array(
93 'sort' => 'label ASC',
98 'option_group_id' => 1, 'options' => array(
99 'sort' => 'label DESC',
103 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
104 $this->assertGreaterThan($result['label'], $result2['label']);
108 * 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.
110 function testGetValueOptionPagination() {
112 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), ));
113 $page2 = $this->callAPISuccess('option_value', 'get', array(
114 'options' => array('limit' => $pageSize,
115 // if you use it for pagination, option.offset=pageSize*pageNumber
116 'offset' => $pageSize - 1,
118 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__
);
119 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__
);
121 $last = array_pop($page1['values']);
122 $first = array_shift($page2['values']);
124 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__
);
127 public function testGetOptionGroup() {
128 $params = array('option_group_id' => 1);
129 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__
, __FILE__
);
130 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__
);
133 * test that using option_group_name returns more than 1 & less than all
138 public function testGetOptionGroupByName() {
139 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
140 $params = array('option.limit' => 100);
141 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
142 $result = $this->callAPISuccess('option_value', 'get', $params);
143 $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__
);
144 $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__
);
151 * Check that domain_id is honoured
153 public function testCreateOptionSpecifyDomain() {
154 $result = $this->callAPISuccess('option_group', 'get', array(
155 'name' => 'from_email_address',
157 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
160 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
161 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
162 'id' => $optionValueId,
163 'return' => 'domain_id',
165 $this->assertEquals(2, $domain_id);
168 * Check that component_id is honoured
170 public function testCreateOptionSpecifyComponentID() {
171 $result = $this->callAPISuccess('option_group', 'get', array(
172 'name' => 'from_email_address',
173 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
175 $this->assertAPISuccess($result);
176 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
177 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
178 'id' => $optionValueId, 'return' => 'component_id',
180 $this->assertEquals(2, $component_id);
183 * Check that component continues to be honoured
185 public function testCreateOptionSpecifyComponent() {
186 $result = $this->callAPISuccess('option_group', 'get', array(
187 'name' => 'from_email_address',
188 'sequential' => 1, 'api.option_value.create' => array(
189 'component_id' => 'CiviContribute',
194 $this->assertAPISuccess($result);
195 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
196 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
197 'id' => $optionValueId, 'return' => 'component_id',
199 $this->assertEquals(2, $component_id);
202 * Check that component string is honoured
204 public function testCreateOptionSpecifyComponentString() {
205 $result = $this->callAPISuccess('option_group', 'get', array(
206 'name' => 'from_email_address',
207 'sequential' => 1, 'api.option_value.create' => array(
208 'component_id' => 'CiviContribute',
209 'name' => 'my@y.com'),
212 $this->assertAPISuccess($result);
213 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
214 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
215 'id' => $optionValueId, 'return' => 'component_id',
217 $this->assertEquals(2, $component_id);
220 * Check that domain_id is honoured
222 public function testCRM12133CreateOptionWeightNoValue() {
223 $optionGroup = $this->callAPISuccess(
224 'option_group', 'get', array(
226 'sequential' => 1, ));
227 $this->assertAPISuccess($optionGroup);
229 'option_group_id' => $optionGroup['id'],
230 'label' => 'my@y.com', 'weight' => 3,
232 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
233 $this->assertAPISuccess($optionValue);
234 $params['weight'] = 4;
235 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params );
236 $this->assertAPISuccess($optionValue2);
237 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
238 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
241 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
242 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
246 * Check that domain_id is honoured
248 public function testCreateOptionNoName() {
249 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
251 'sequential' => 1, ));
253 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
254 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
255 $this->assertAPISuccess($optionValue);
256 $this->getAndCheck($params, $optionValue['id'], 'option_value');
260 * Check that pseudoconstant reflects new value added
263 public function testCRM11876CreateOptionPseudoConstantUpdated() {
264 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument',
267 $newOption = (string) time();
268 $apiResult = $this->callAPISuccess('option_value', 'create', array(
269 'option_group_id' => $optionGroupID,
270 'label' => $newOption,
273 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
274 $this->assertTrue(in_array($newOption, $fields['values']));
276 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
278 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
279 $this->assertFalse(in_array($newOption, $fields['values']));