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