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