| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Class api_v3_OptionValueTest |
| 32 | */ |
| 33 | class api_v3_OptionValueTest extends CiviUnitTestCase { |
| 34 | protected $_apiversion = 3; |
| 35 | |
| 36 | function setUp() { |
| 37 | parent::setUp(); |
| 38 | } |
| 39 | |
| 40 | function tearDown() {} |
| 41 | |
| 42 | |
| 43 | public function testGetCount() { |
| 44 | $result = $this->callAPISuccess('option_value', 'getcount', array()); |
| 45 | $this->assertGreaterThan(100, $result); |
| 46 | } |
| 47 | |
| 48 | public function testGetOptionValueByID() { |
| 49 | $result = $this->callAPISuccess('option_value', 'get', array('id' => 1)); |
| 50 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
| 51 | $this->assertEquals(1, $result['id'], 'In line ' . __LINE__); |
| 52 | } |
| 53 | |
| 54 | public function testGetOptionValueByValue() { |
| 55 | $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1')); |
| 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() { |
| 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__); |
| 67 | $params['options']['limit'] = 1; |
| 68 | $result = $this->callAPISuccess('option_value', 'get', $params); |
| 69 | $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Test offset param |
| 74 | */ |
| 75 | function testGetOptionValueOffSet() { |
| 76 | |
| 77 | $result = $this->callAPISuccess('option_value', 'get', array( |
| 78 | 'option_group_id' => 1, |
| 79 | 'value' => '1', )); |
| 80 | $result2 = $this->callAPISuccess('option_value', 'get', array( |
| 81 | 'option_group_id' => 1, |
| 82 | 'value' => '1', |
| 83 | 'options' => array('offset' => 1), |
| 84 | )); |
| 85 | $this->assertGreaterThan($result2['count'], $result['count']); |
| 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'; |
| 94 | $result = $this->callAPISuccess('option_value', 'getsingle', array( |
| 95 | 'option_group_id' => 1, 'options' => array( |
| 96 | 'sort' => 'label ASC', |
| 97 | 'limit' => 1, |
| 98 | ), |
| 99 | )); |
| 100 | $params = array( |
| 101 | 'option_group_id' => 1, 'options' => array( |
| 102 | 'sort' => 'label DESC', |
| 103 | 'limit' => 1, |
| 104 | ), |
| 105 | ); |
| 106 | $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 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; |
| 115 | $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), )); |
| 116 | $page2 = $this->callAPISuccess('option_value', 'get', array( |
| 117 | 'options' => array('limit' => $pageSize, |
| 118 | // if you use it for pagination, option.offset=pageSize*pageNumber |
| 119 | 'offset' => $pageSize - 1, |
| 120 | ), )); |
| 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() { |
| 131 | $params = array('option_group_id' => 1); |
| 132 | $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__, __FILE__); |
| 133 | $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Test that using option_group_name returns more than 1 & less than all |
| 138 | */ |
| 139 | public function testGetOptionGroupByName() { |
| 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); |
| 144 | $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__); |
| 145 | $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__); |
| 146 | } |
| 147 | |
| 148 | public function testGetOptionDoesNotExist() { |
| 149 | $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB')); |
| 150 | $this->assertEquals(0, $result['count'], 'In line ' . __LINE__); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Check that domain_id is honoured |
| 155 | */ |
| 156 | public function testCreateOptionSpecifyDomain() { |
| 157 | $result = $this->callAPISuccess('option_group', 'get', array( |
| 158 | 'name' => 'from_email_address', |
| 159 | 'sequential' => 1, |
| 160 | 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'), |
| 161 | )); |
| 162 | |
| 163 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; |
| 164 | $domain_id = $this->callAPISuccess('option_value', 'getvalue', array( |
| 165 | 'id' => $optionValueId, |
| 166 | 'return' => 'domain_id', |
| 167 | )); |
| 168 | $this->assertEquals(2, $domain_id); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Check that component_id is honoured |
| 173 | */ |
| 174 | public function testCreateOptionSpecifyComponentID() { |
| 175 | $result = $this->callAPISuccess('option_group', 'get', array( |
| 176 | 'name' => 'from_email_address', |
| 177 | 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'), |
| 178 | )); |
| 179 | $this->assertAPISuccess($result); |
| 180 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; |
| 181 | $component_id = $this->callAPISuccess('option_value', 'getvalue', array( |
| 182 | 'id' => $optionValueId, 'return' => 'component_id', |
| 183 | )); |
| 184 | $this->assertEquals(2, $component_id); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Check that component continues to be honoured |
| 189 | */ |
| 190 | public function testCreateOptionSpecifyComponent() { |
| 191 | $result = $this->callAPISuccess('option_group', 'get', array( |
| 192 | 'name' => 'from_email_address', |
| 193 | 'sequential' => 1, 'api.option_value.create' => array( |
| 194 | 'component_id' => 'CiviContribute', |
| 195 | 'name' => 'my@y.com' |
| 196 | ), |
| 197 | |
| 198 | )); |
| 199 | $this->assertAPISuccess($result); |
| 200 | $optionValueId = $result['values'][0]['api.option_value.create']['id']; |
| 201 | $component_id = $this->callAPISuccess('option_value', 'getvalue', array( |
| 202 | 'id' => $optionValueId, 'return' => 'component_id', |
| 203 | )); |
| 204 | $this->assertEquals(2, $component_id); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Check that component string is honoured |
| 209 | */ |
| 210 | public function testCreateOptionSpecifyComponentString() { |
| 211 | $result = $this->callAPISuccess('option_group', 'get', array( |
| 212 | 'name' => 'from_email_address', |
| 213 | 'sequential' => 1, 'api.option_value.create' => array( |
| 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']; |
| 220 | $component_id = $this->callAPISuccess('option_value', 'getvalue', array( |
| 221 | 'id' => $optionValueId, 'return' => 'component_id', |
| 222 | )); |
| 223 | $this->assertEquals(2, $component_id); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Check that domain_id is honoured |
| 228 | */ |
| 229 | public function testCRM12133CreateOptionWeightNoValue() { |
| 230 | $optionGroup = $this->callAPISuccess( |
| 231 | 'option_group', 'get', array( |
| 232 | 'name' => 'gender', |
| 233 | 'sequential' => 1, )); |
| 234 | $this->assertAPISuccess($optionGroup); |
| 235 | $params = array( |
| 236 | 'option_group_id' => $optionGroup['id'], |
| 237 | 'label' => 'my@y.com', 'weight' => 3, |
| 238 | ); |
| 239 | $optionValue = $this->callAPISuccess('option_value', 'create', $params); |
| 240 | $this->assertAPISuccess($optionValue); |
| 241 | $params['weight'] = 4; |
| 242 | $optionValue2 = $this->callAPISuccess('option_value', 'create', $params ); |
| 243 | $this->assertAPISuccess($optionValue2); |
| 244 | $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id'])); |
| 245 | $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']); |
| 246 | |
| 247 | //cleanup |
| 248 | $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id'])); |
| 249 | $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id'])); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Check that domain_id is honoured |
| 254 | */ |
| 255 | public function testCreateOptionNoName() { |
| 256 | $optionGroup = $this->callAPISuccess('option_group', 'get', array( |
| 257 | 'name' => 'gender', |
| 258 | 'sequential' => 1, )); |
| 259 | |
| 260 | $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com'); |
| 261 | $optionValue = $this->callAPISuccess('option_value', 'create', $params); |
| 262 | $this->assertAPISuccess($optionValue); |
| 263 | $this->getAndCheck($params, $optionValue['id'], 'option_value'); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Check that pseudoconstant reflects new value added |
| 268 | * and deleted |
| 269 | */ |
| 270 | public function testCRM11876CreateOptionPseudoConstantUpdated() { |
| 271 | $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument', |
| 272 | 'return' => 'id', |
| 273 | )); |
| 274 | $newOption = (string) time(); |
| 275 | $apiResult = $this->callAPISuccess('option_value', 'create', array( |
| 276 | 'option_group_id' => $optionGroupID, |
| 277 | 'label' => $newOption, |
| 278 | )); |
| 279 | |
| 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'])); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Update option value with 'id' paramter and the value to update |
| 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 | |
| 309 | /** |
| 310 | * Update option value with 'id' paramter and the value to update |
| 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 | } |
| 327 | } |