tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 */
27
28 /**
29 * Class api_v3_OptionValueTest
30 */
31 class api_v3_OptionValueTest extends CiviUnitTestCase {
32 protected $_apiversion = 3;
33
34 public function setUp() {
35 parent::setUp();
36 $this->useTransaction(TRUE);
37 }
38
39 public function testGetCount() {
40 $result = $this->callAPISuccess('option_value', 'getcount', array());
41 $this->assertGreaterThan(100, $result);
42 }
43
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']);
48 }
49
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']);
54 }
55
56 /**
57 * Test limit param.
58 */
59 public function testGetOptionValueLimit() {
60 $params = array();
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__);
66 }
67
68 /**
69 * Test offset param.
70 */
71 public function testGetOptionValueOffSet() {
72
73 $result = $this->callAPISuccess('option_value', 'get', array(
74 'option_group_id' => 1,
75 'value' => '1',
76 ));
77 $result2 = $this->callAPISuccess('option_value', 'get', array(
78 'option_group_id' => 1,
79 'value' => '1',
80 'options' => array('offset' => 1),
81 ));
82 $this->assertGreaterThan($result2['count'], $result['count']);
83 }
84
85 /**
86 * Test offset param.
87 */
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,
93 'options' => array(
94 'sort' => 'label ASC',
95 'limit' => 1,
96 ),
97 ));
98 $params = array(
99 'option_group_id' => 1,
100 'options' => array(
101 'sort' => 'label DESC',
102 'limit' => 1,
103 ),
104 );
105 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
106 $this->assertGreaterThan($result['label'], $result2['label']);
107 }
108
109 /**
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.
111 */
112 public function testGetValueOptionPagination() {
113 $pageSize = 10;
114 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize)));
115 $page2 = $this->callAPISuccess('option_value', 'get', array(
116 'options' => array(
117 'limit' => $pageSize,
118 // if you use it for pagination, option.offset=pageSize*pageNumber
119 'offset' => $pageSize - 1,
120 ),
121 ));
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__);
124
125 $last = array_pop($page1['values']);
126 $first = array_shift($page2['values']);
127
128 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__);
129 }
130
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']);
135 }
136
137 /**
138 * Test that using option_group_name returns more than 1 & less than all
139 */
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']);
147 }
148
149 public function testGetOptionDoesNotExist() {
150 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
151 $this->assertEquals(0, $result['count']);
152 }
153
154 /**
155 * Check that domain_id is honoured.
156 */
157 public function testCreateOptionSpecifyDomain() {
158 $result = $this->callAPISuccess('option_group', 'get', array(
159 'name' => 'from_email_address',
160 'sequential' => 1,
161 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
162 ));
163
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',
168 ));
169 $this->assertEquals(2, $domain_id);
170 }
171
172 /**
173 * Check that component_id is honoured.
174 */
175 public function testCreateOptionSpecifyComponentID() {
176 $result = $this->callAPISuccess('option_group', 'get', array(
177 'name' => 'from_email_address',
178 'sequential' => 1,
179 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
180 ));
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',
186 ));
187 $this->assertEquals(2, $component_id);
188 }
189
190 /**
191 * Check that component continues to be honoured.
192 */
193 public function testCreateOptionSpecifyComponent() {
194 $result = $this->callAPISuccess('option_group', 'get', array(
195 'name' => 'from_email_address',
196 'sequential' => 1,
197 'api.option_value.create' => array(
198 'component_id' => 'CiviContribute',
199 'name' => 'my@y.com',
200 ),
201
202 ));
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',
208 ));
209 $this->assertEquals(2, $component_id);
210 }
211
212 /**
213 * Check that component string is honoured.
214 */
215 public function testCreateOptionSpecifyComponentString() {
216 $result = $this->callAPISuccess('option_group', 'get', array(
217 'name' => 'from_email_address',
218 'sequential' => 1,
219 'api.option_value.create' => array(
220 'component_id' => 'CiviContribute',
221 'name' => 'my@y.com',
222 ),
223
224 ));
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',
230 ));
231 $this->assertEquals(2, $component_id);
232 }
233
234 /**
235 * Check that domain_id is honoured.
236 */
237 public function testCRM12133CreateOptionWeightNoValue() {
238 $optionGroup = $this->callAPISuccess(
239 'option_group', 'get', array(
240 'name' => 'gender',
241 'sequential' => 1,
242 )
243 );
244 $this->assertAPISuccess($optionGroup);
245 $params = array(
246 'option_group_id' => $optionGroup['id'],
247 'label' => 'my@y.com',
248 'weight' => 3,
249 );
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']);
257
258 //cleanup
259 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
260 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
261 }
262
263 /**
264 * Check that domain_id is honoured.
265 */
266 public function testCreateOptionNoName() {
267 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
268 'name' => 'gender',
269 'sequential' => 1,
270 ));
271
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');
276 }
277
278 /**
279 * Check that pseudoconstant reflects new value added.
280 */
281 public function testCRM11876CreateOptionPseudoConstantUpdated() {
282 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array(
283 'name' => 'payment_instrument',
284 'return' => 'id',
285 ));
286 $newOption = (string) time();
287 $apiResult = $this->callAPISuccess('option_value', 'create', array(
288 'option_group_id' => $optionGroupID,
289 'label' => $newOption,
290 ));
291
292 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
293 $this->assertTrue(in_array($newOption, $fields['values']));
294
295 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
296
297 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
298 $this->assertFalse(in_array($newOption, $fields['values']));
299 }
300
301
302 /**
303 * Update option value with 'id' parameter and the value to update
304 * and not passing option group id
305 */
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')
312 );
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(
316 'id' => $ov['id'],
317 'return' => 'is_active',
318 ));
319 $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__);
320 }
321
322 /**
323 * Update option value with 'id' parameter and the value to update
324 * and as well as option group id
325 */
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',
330 'is_active' => 1,
331 ));
332 // create a option value
333 $ov = $this->callAPISuccess('option_value', 'create',
334 array('option_group_id' => $og['id'], 'label' => 'test option value')
335 );
336 // update option value without 'option_group_id'
337 $this->callAPISuccess('option_value', 'create', array(
338 'id' => $ov['id'],
339 'option_group_id' => $og['id'],
340 'is_active' => 0,
341 ));
342 $val = $this->callAPISuccess('option_value', 'getvalue', array(
343 'id' => $ov['id'],
344 'return' => 'is_active',
345 ));
346 $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__);
347 }
348
349 }