Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
972322c5 | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
972322c5 | 5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
6a488035 TO |
27 | |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | class api_v3_OptionValueTest extends CiviUnitTestCase { | |
972322c5 | 31 | protected $_apiversion = 3; |
6a488035 TO |
32 | public $_eNoticeCompliant = TRUE; |
33 | function setUp() { | |
6a488035 TO |
34 | parent::setUp(); |
35 | } | |
36 | ||
37 | function tearDown() {} | |
38 | ||
972322c5 | 39 | |
40 | public function testGetCount() { | |
41 | $result = $this->callAPISuccess('option_value', 'getcount', array()); | |
42 | $this->assertGreaterThan(100, $result); | |
43 | } | |
44 | ||
6a488035 | 45 | public function testGetOptionValueByID() { |
972322c5 | 46 | $result = $this->callAPISuccess('option_value', 'get', array('id' => 1)); |
6a488035 TO |
47 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
48 | $this->assertEquals(1, $result['id'], 'In line ' . __LINE__); | |
49 | } | |
50 | ||
51 | public function testGetOptionValueByValue() { | |
972322c5 | 52 | $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1')); |
6a488035 TO |
53 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
54 | $this->assertEquals(1, $result['id'], 'In line ' . __LINE__); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Test limit param | |
59 | */ | |
60 | function testGetOptionValueLimit() { | |
972322c5 | 61 | $params = array(); |
62 | $result = $this->callAPISuccess('option_value', 'get', $params); | |
63 | $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__); | |
6a488035 | 64 | $params['options']['limit'] = 1; |
972322c5 | 65 | $result = $this->callAPISuccess('option_value', 'get', $params); |
66 | $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__); | |
6a488035 TO |
67 | } |
68 | ||
69 | /** | |
70 | * Test offset param | |
71 | */ | |
72 | function testGetOptionValueOffSet() { | |
73 | ||
972322c5 | 74 | $result = $this->callAPISuccess('option_value', 'get', array( |
6a488035 | 75 | 'option_group_id' => 1, |
972322c5 | 76 | 'value' => '1', )); |
77 | $result2 = $this->callAPISuccess('option_value', 'get', array( | |
6a488035 TO |
78 | 'option_group_id' => 1, |
79 | 'value' => '1', | |
6a488035 TO |
80 | 'options' => array('offset' => 1), |
81 | )); | |
972322c5 | 82 | $this->assertGreaterThan($result2['count'], $result['count']); |
6a488035 TO |
83 | } |
84 | ||
85 | /** | |
86 | * Test offset param | |
87 | */ | |
88 | function testGetSingleValueOptionValueSort() { | |
89 | $description = "demonstrates use of Sort param (available in many api functions). Also, getsingle"; | |
90 | $subfile = 'SortOption'; | |
972322c5 | 91 | $result = $this->callAPISuccess('option_value', 'getsingle', array( |
92 | 'option_group_id' => 1, 'options' => array( | |
6a488035 TO |
93 | 'sort' => 'label ASC', |
94 | 'limit' => 1, | |
95 | ), | |
96 | )); | |
97 | $params = array( | |
972322c5 | 98 | 'option_group_id' => 1, 'options' => array( |
6a488035 TO |
99 | 'sort' => 'label DESC', |
100 | 'limit' => 1, | |
101 | ), | |
102 | ); | |
972322c5 | 103 | $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
104 | $this->assertGreaterThan($result['label'], $result2['label']); |
105 | } | |
106 | ||
107 | /** | |
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. | |
109 | */ | |
110 | function testGetValueOptionPagination() { | |
111 | $pageSize = 10; | |
972322c5 | 112 | $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), )); |
113 | $page2 = $this->callAPISuccess('option_value', 'get', array( | |
6a488035 TO |
114 | 'options' => array('limit' => $pageSize, |
115 | // if you use it for pagination, option.offset=pageSize*pageNumber | |
116 | 'offset' => $pageSize - 1, | |
972322c5 | 117 | ), )); |
6a488035 TO |
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__); | |
120 | ||
121 | $last = array_pop($page1['values']); | |
122 | $first = array_shift($page2['values']); | |
123 | ||
124 | $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__); | |
125 | } | |
126 | ||
127 | public function testGetOptionGroup() { | |
972322c5 | 128 | $params = array('option_group_id' => 1); |
129 | $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__, __FILE__); | |
6a488035 TO |
130 | $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__); |
131 | } | |
132 | /* | |
133 | * test that using option_group_name returns more than 1 & less than all | |
134 | */ | |
135 | ||
136 | ||
137 | ||
138 | public function testGetOptionGroupByName() { | |
972322c5 | 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); | |
6a488035 TO |
143 | $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__); |
144 | $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__); | |
145 | } | |
146 | public function testGetOptionDoesNotExist() { | |
972322c5 | 147 | $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB')); |
6a488035 TO |
148 | $this->assertEquals(0, $result['count'], 'In line ' . __LINE__); |
149 | } | |
150 | /* | |
151 | * Check that domain_id is honoured | |
152 | */ | |
153 | public function testCreateOptionSpecifyDomain() { | |
972322c5 | 154 | $result = $this->callAPISuccess('option_group', 'get', array( |
6a488035 TO |
155 | 'name' => 'from_email_address', |
156 | 'sequential' => 1, | |
c87bbced | 157 | 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'), |
6a488035 | 158 | )); |
972322c5 | 159 | |
6a488035 | 160 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; |
972322c5 | 161 | $domain_id = $this->callAPISuccess('option_value', 'getvalue', array( |
6a488035 | 162 | 'id' => $optionValueId, |
6a488035 TO |
163 | 'return' => 'domain_id', |
164 | )); | |
165 | $this->assertEquals(2, $domain_id); | |
c87bbced | 166 | } |
167 | /* | |
168 | * Check that component_id is honoured | |
169 | */ | |
170 | public function testCreateOptionSpecifyComponentID() { | |
972322c5 | 171 | $result = $this->callAPISuccess('option_group', 'get', array( |
c87bbced | 172 | 'name' => 'from_email_address', |
972322c5 | 173 | 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'), |
c87bbced | 174 | )); |
175 | $this->assertAPISuccess($result); | |
176 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; | |
972322c5 | 177 | $component_id = $this->callAPISuccess('option_value', 'getvalue', array( |
178 | 'id' => $optionValueId, 'return' => 'component_id', | |
c87bbced | 179 | )); |
180 | $this->assertEquals(2, $component_id); | |
181 | } | |
182 | /* | |
183 | * Check that component continues to be honoured | |
184 | */ | |
185 | public function testCreateOptionSpecifyComponent() { | |
972322c5 | 186 | $result = $this->callAPISuccess('option_group', 'get', array( |
c87bbced | 187 | 'name' => 'from_email_address', |
972322c5 | 188 | 'sequential' => 1, 'api.option_value.create' => array( |
c87bbced | 189 | 'component_id' => 'CiviContribute', |
190 | 'name' => 'my@y.com' | |
191 | ), | |
6a488035 | 192 | |
c87bbced | 193 | )); |
194 | $this->assertAPISuccess($result); | |
195 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; | |
972322c5 | 196 | $component_id = $this->callAPISuccess('option_value', 'getvalue', array( |
197 | 'id' => $optionValueId, 'return' => 'component_id', | |
c87bbced | 198 | )); |
199 | $this->assertEquals(2, $component_id); | |
200 | } | |
201 | /* | |
202 | * Check that component string is honoured | |
203 | */ | |
204 | public function testCreateOptionSpecifyComponentString() { | |
972322c5 | 205 | $result = $this->callAPISuccess('option_group', 'get', array( |
c87bbced | 206 | 'name' => 'from_email_address', |
972322c5 | 207 | 'sequential' => 1, 'api.option_value.create' => array( |
c87bbced | 208 | 'component_id' => 'CiviContribute', |
209 | 'name' => 'my@y.com'), | |
210 | ||
211 | )); | |
212 | $this->assertAPISuccess($result); | |
213 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; | |
972322c5 | 214 | $component_id = $this->callAPISuccess('option_value', 'getvalue', array( |
215 | 'id' => $optionValueId, 'return' => 'component_id', | |
c87bbced | 216 | )); |
217 | $this->assertEquals(2, $component_id); | |
218 | } | |
219 | /* | |
220 | * Check that domain_id is honoured | |
221 | */ | |
222 | public function testCRM12133CreateOptionWeightNoValue() { | |
972322c5 | 223 | $optionGroup = $this->callAPISuccess( |
c87bbced | 224 | 'option_group', 'get', array( |
225 | 'name' => 'gender', | |
972322c5 | 226 | 'sequential' => 1, )); |
c87bbced | 227 | $this->assertAPISuccess($optionGroup); |
228 | $params = array( | |
229 | 'option_group_id' => $optionGroup['id'], | |
972322c5 | 230 | 'label' => 'my@y.com', 'weight' => 3, |
c87bbced | 231 | ); |
972322c5 | 232 | $optionValue = $this->callAPISuccess('option_value', 'create', $params); |
c87bbced | 233 | $this->assertAPISuccess($optionValue); |
234 | $params['weight'] = 4; | |
972322c5 | 235 | $optionValue2 = $this->callAPISuccess('option_value', 'create', $params ); |
c87bbced | 236 | $this->assertAPISuccess($optionValue2); |
972322c5 | 237 | $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id'])); |
c87bbced | 238 | $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']); |
239 | ||
240 | //cleanup | |
972322c5 | 241 | $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id'])); |
242 | $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id'])); | |
6a488035 TO |
243 | } |
244 | ||
245 | /* | |
246 | * Check that domain_id is honoured | |
247 | */ | |
248 | public function testCreateOptionNoName() { | |
972322c5 | 249 | $optionGroup = $this->callAPISuccess('option_group', 'get', array( |
6a488035 | 250 | 'name' => 'gender', |
972322c5 | 251 | 'sequential' => 1, )); |
6a488035 | 252 | |
972322c5 | 253 | $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com'); |
254 | $optionValue = $this->callAPISuccess('option_value', 'create', $params); | |
6a488035 TO |
255 | $this->assertAPISuccess($optionValue); |
256 | $this->getAndCheck($params, $optionValue['id'], 'option_value'); | |
257 | } | |
a4a33486 CW |
258 | |
259 | /** | |
6a488035 | 260 | * Check that pseudoconstant reflects new value added |
a4a33486 CW |
261 | * and deleted |
262 | */ | |
6a488035 | 263 | public function testCRM11876CreateOptionPseudoConstantUpdated() { |
972322c5 | 264 | $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument', |
a4a33486 CW |
265 | 'return' => 'id', |
266 | )); | |
267 | $newOption = (string) time(); | |
268 | $apiResult = $this->callAPISuccess('option_value', 'create', array( | |
269 | 'option_group_id' => $optionGroupID, | |
270 | 'label' => $newOption, | |
6a488035 TO |
271 | )); |
272 | ||
a4a33486 CW |
273 | $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); |
274 | $this->assertTrue(in_array($newOption, $fields['values'])); | |
275 | ||
276 | $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id'])); | |
277 | ||
278 | $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); | |
279 | $this->assertFalse(in_array($newOption, $fields['values'])); | |
6a488035 | 280 | } |
6a488035 | 281 | |
89ab5601 PJ |
282 | |
283 | /* | |
284 | * update option value with 'id' paramter and the value to update | |
285 | * and not passing option group id | |
286 | */ | |
287 | public function testUpdateOptionValueNoGroupId() { | |
288 | // create a option group | |
289 | $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1)); | |
290 | // create a option value | |
291 | $ov = $this->callAPISuccess('option_value', 'create', | |
292 | array('option_group_id' => $og['id'], 'label' => 'test option value') | |
293 | ); | |
294 | // update option value without 'option_group_id' | |
295 | $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0)); | |
296 | $val = $this->callAPISuccess('option_value', 'getvalue', array( | |
297 | 'id' => $ov['id'], 'return' => 'is_active', | |
298 | )); | |
299 | $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__); | |
300 | } | |
301 | ||
302 | /* | |
303 | * update option value with 'id' paramter and the value to update | |
304 | * and as well as option group id | |
305 | */ | |
306 | public function testUpdateOptionValueWithGroupId() { | |
307 | // create a option group | |
308 | $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group for with group id', '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') | |
312 | ); | |
313 | // update option value without 'option_group_id' | |
314 | $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'option_group_id' => $og['id'], 'is_active' => 0)); | |
315 | $val = $this->callAPISuccess('option_value', 'getvalue', array( | |
316 | 'id' => $ov['id'], 'return' => 'is_active', | |
317 | )); | |
318 | $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__); | |
319 | } | |
320 | } |