| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | * Test APIv3 civicrm_activity_* functions |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | * @subpackage API_Contact |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * Class api_v3_AddressTest |
| 37 | * @group headless |
| 38 | */ |
| 39 | class api_v3_AddressTest extends CiviUnitTestCase { |
| 40 | protected $_apiversion = 3; |
| 41 | protected $_contactID; |
| 42 | protected $_locationType; |
| 43 | protected $_params; |
| 44 | |
| 45 | protected $_entity; |
| 46 | |
| 47 | public function setUp() { |
| 48 | $this->_entity = 'Address'; |
| 49 | parent::setUp(); |
| 50 | |
| 51 | $this->_contactID = $this->organizationCreate(); |
| 52 | $this->_locationType = $this->locationTypeCreate(); |
| 53 | CRM_Core_PseudoConstant::flush(); |
| 54 | |
| 55 | $this->_params = array( |
| 56 | 'contact_id' => $this->_contactID, |
| 57 | 'location_type_id' => $this->_locationType->id, |
| 58 | 'street_name' => 'Ambachtstraat', |
| 59 | 'street_number' => '23', |
| 60 | 'street_address' => 'Ambachtstraat 23', |
| 61 | 'postal_code' => '6971 BN', |
| 62 | 'country_id' => '1152', |
| 63 | 'city' => 'Brummen', |
| 64 | 'is_primary' => 1, |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | public function tearDown() { |
| 69 | $this->locationTypeDelete($this->_locationType->id); |
| 70 | $this->contactDelete($this->_contactID); |
| 71 | $this->quickCleanup(array('civicrm_address', 'civicrm_relationship')); |
| 72 | } |
| 73 | |
| 74 | public function testCreateAddress() { |
| 75 | $result = $this->callAPIAndDocument('address', 'create', $this->_params, __FUNCTION__, __FILE__); |
| 76 | $this->assertEquals(1, $result['count']); |
| 77 | $this->assertNotNull($result['values'][$result['id']]['id']); |
| 78 | $this->getAndCheck($this->_params, $result['id'], 'address'); |
| 79 | } |
| 80 | |
| 81 | public function testCreateAddressParsing() { |
| 82 | $params = array( |
| 83 | 'street_parsing' => 1, |
| 84 | 'street_address' => '54A Excelsior Ave. Apt 1C', |
| 85 | 'location_type_id' => $this->_locationType->id, |
| 86 | 'contact_id' => $this->_contactID, |
| 87 | ); |
| 88 | $subfile = "AddressParse"; |
| 89 | $description = "Demonstrates Use of address parsing param."; |
| 90 | $result = $this->callAPIAndDocument('address', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 91 | $this->assertEquals(54, $result['values'][$result['id']]['street_number']); |
| 92 | $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix']); |
| 93 | $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name']); |
| 94 | $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit']); |
| 95 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Is_primary should be set as a default. |
| 101 | */ |
| 102 | public function testCreateAddressTestDefaults() { |
| 103 | $params = $this->_params; |
| 104 | unset($params['is_primary']); |
| 105 | $result = $this->callAPISuccess('address', 'create', $params); |
| 106 | $this->assertEquals(1, $result['count']); |
| 107 | $this->assertEquals(1, $result['values'][$result['id']]['is_primary']); |
| 108 | $this->getAndCheck($this->_params, $result['id'], 'address'); |
| 109 | } |
| 110 | |
| 111 | public function testCreateAddressTooLongSuffix() { |
| 112 | $params = $this->_params; |
| 113 | $params['street_number_suffix'] = 'really long string'; |
| 114 | $this->callAPIFailure('address', 'create', $params); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Create an address with a master ID and ensure that a relationship is created. |
| 119 | */ |
| 120 | public function testCreateAddressWithMasterRelationshipHousehold() { |
| 121 | $householdID = $this->householdCreate(); |
| 122 | $address = $this->callAPISuccess('address', 'create', array_merge($this->_params, $this->_params, array('contact_id' => $householdID))); |
| 123 | $individualID = $this->individualCreate(); |
| 124 | $individualParams = array( |
| 125 | 'contact_id' => $individualID, |
| 126 | 'master_id' => $address['id'], |
| 127 | ); |
| 128 | $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams)); |
| 129 | $this->callAPISuccess('relationship', 'getcount', array( |
| 130 | 'contact_id_a' => $individualID, |
| 131 | 'contact_id_b' => $this->_contactID, |
| 132 | )); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Create an address with a master ID and ensure that a relationship is created. |
| 137 | */ |
| 138 | public function testCreateAddressWithMasterRelationshipOrganization() { |
| 139 | $address = $this->callAPISuccess('address', 'create', $this->_params); |
| 140 | $individualID = $this->individualCreate(); |
| 141 | $individualParams = array( |
| 142 | 'contact_id' => $individualID, |
| 143 | 'master_id' => $address['id'], |
| 144 | ); |
| 145 | $this->callAPISuccess('address', 'create', array_merge($this->_params, $individualParams)); |
| 146 | $this->callAPISuccess('relationship', 'getcount', array( |
| 147 | 'contact_id_a' => $individualID, |
| 148 | 'contact_id_b' => $this->_contactID, |
| 149 | )); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Create an address with a master ID and ensure that a relationship is created. |
| 154 | */ |
| 155 | public function testCreateAddressWithMasterRelationshipChangingOrganization() { |
| 156 | $address = $this->callAPISuccess('address', 'create', $this->_params); |
| 157 | $organisation2ID = $this->organizationCreate(); |
| 158 | $address2 = $this->callAPISuccess('address', 'create', array_merge($this->_params, array('contact_id' => $organisation2ID))); |
| 159 | $individualID = $this->individualCreate(); |
| 160 | $individualParams = array_merge($this->_params, array( |
| 161 | 'contact_id' => $individualID, |
| 162 | 'master_id' => $address['id'], |
| 163 | )); |
| 164 | $individualAddress = $this->callAPISuccess('address', 'create', $individualParams); |
| 165 | $individualParams['master_id'] = $address2['id']; |
| 166 | $individualParams['id'] = $individualAddress['id']; |
| 167 | $this->callAPISuccess('address', 'create', $individualParams); |
| 168 | $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID), 2); |
| 169 | $this->markTestIncomplete('Remainder of test checks that employer relationship is disabled when new one is created but turns out to be not happening - by design?'); |
| 170 | $this->callAPISuccessGetCount('relationship', array('contact_id_a' => $individualID, 'is_active' => FALSE), 1); |
| 171 | $this->callAPISuccessGetCount('relationship', array( |
| 172 | 'contact_id_a' => $individualID, |
| 173 | 'is_active' => TRUE, |
| 174 | 'contact_id_b' => $organisation2ID, |
| 175 | ), 1); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Is_primary should be set as a default. |
| 181 | * |
| 182 | * ie. create the address, unset the params & recreate. |
| 183 | * is_primary should be 0 before & after the update. ie - having no other address |
| 184 | * is_primary is invalid. |
| 185 | */ |
| 186 | public function testCreateAddressTestDefaultWithID() { |
| 187 | $params = $this->_params; |
| 188 | $params['is_primary'] = 0; |
| 189 | $result = $this->callAPISuccess('address', 'create', $params); |
| 190 | unset($params['is_primary']); |
| 191 | $params['id'] = $result['id']; |
| 192 | $result = $this->callAPISuccess('address', 'create', $params); |
| 193 | $this->callAPISuccess('address', 'get', array('contact_id' => $params['contact_id'])); |
| 194 | $this->assertEquals(1, $result['count']); |
| 195 | $this->assertEquals(1, $result['values'][$result['id']]['is_primary']); |
| 196 | $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * test address deletion. |
| 201 | */ |
| 202 | public function testDeleteAddress() { |
| 203 | //check there are no address to start with |
| 204 | $get = $this->callAPISuccess('address', 'get', array( |
| 205 | 'location_type_id' => $this->_locationType->id, |
| 206 | )); |
| 207 | $this->assertEquals(0, $get['count'], 'Contact already exists '); |
| 208 | |
| 209 | //create one |
| 210 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
| 211 | |
| 212 | $result = $this->callAPIAndDocument('address', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__); |
| 213 | $this->assertEquals(1, $result['count']); |
| 214 | $get = $this->callAPISuccess('address', 'get', array( |
| 215 | 'location_type_id' => $this->_locationType->id, |
| 216 | )); |
| 217 | $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Test civicrm_address_get - success expected. |
| 222 | */ |
| 223 | public function testGetAddress() { |
| 224 | $address = $this->callAPISuccess('address', 'create', $this->_params); |
| 225 | |
| 226 | $params = array( |
| 227 | 'contact_id' => $this->_contactID, |
| 228 | 'street_name' => $address['values'][$address['id']]['street_name'], |
| 229 | ); |
| 230 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__); |
| 231 | $this->callAPISuccess('Address', 'delete', array('id' => $result['id'])); |
| 232 | $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id']); |
| 233 | $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary']); |
| 234 | $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address']); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Test civicrm_address_get - success expected. |
| 239 | */ |
| 240 | public function testGetSingleAddress() { |
| 241 | $this->callAPISuccess('address', 'create', $this->_params); |
| 242 | $params = array( |
| 243 | 'contact_id' => $this->_contactID, |
| 244 | ); |
| 245 | $address = $this->callAPISuccess('Address', 'getsingle', ($params)); |
| 246 | $this->assertEquals($address['location_type_id'], $this->_params['location_type_id']); |
| 247 | $this->callAPISuccess('address', 'delete', array('id' => $address['id'])); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Test civicrm_address_get with sort option- success expected. |
| 252 | */ |
| 253 | public function testGetAddressSort() { |
| 254 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
| 255 | $this->callAPISuccess('address', 'create', array_merge($this->_params, array('street_address' => 'yzy'))); |
| 256 | $subfile = "AddressSort"; |
| 257 | $description = "Demonstrates Use of sort filter."; |
| 258 | $params = array( |
| 259 | 'options' => array( |
| 260 | 'sort' => 'street_address DESC', |
| 261 | 'limit' => 2, |
| 262 | ), |
| 263 | 'sequential' => 1, |
| 264 | ); |
| 265 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 266 | $this->assertEquals(2, $result['count']); |
| 267 | $this->assertEquals('Ambachtstraat 23', $result['values'][1]['street_address']); |
| 268 | $this->callAPISuccess('address', 'delete', array('id' => $create['id'])); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Test civicrm_address_get with sort option- success expected. |
| 273 | */ |
| 274 | public function testGetAddressLikeSuccess() { |
| 275 | $this->callAPISuccess('address', 'create', $this->_params); |
| 276 | $subfile = "AddressLike"; |
| 277 | $description = "Demonstrates Use of Like."; |
| 278 | $params = array( |
| 279 | 'street_address' => array('LIKE' => '%mb%'), |
| 280 | 'sequential' => 1, |
| 281 | ); |
| 282 | $result = $this->callAPIAndDocument('Address', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 283 | $this->assertEquals(1, $result['count']); |
| 284 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address']); |
| 285 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Test civicrm_address_get with sort option- success expected. |
| 290 | */ |
| 291 | public function testGetAddressLikeFail() { |
| 292 | $create = $this->callAPISuccess('address', 'create', $this->_params); |
| 293 | $params = array( |
| 294 | 'street_address' => array('LIKE' => "'%xy%'"), |
| 295 | 'sequential' => 1, |
| 296 | ); |
| 297 | $result = $this->callAPISuccess('Address', 'Get', ($params)); |
| 298 | $this->assertEquals(0, $result['count']); |
| 299 | $this->callAPISuccess('address', 'delete', array('id' => $create['id'])); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | */ |
| 304 | public function testGetWithCustom() { |
| 305 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
| 306 | |
| 307 | $params = $this->_params; |
| 308 | $params['custom_' . $ids['custom_field_id']] = "custom string"; |
| 309 | |
| 310 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
| 311 | |
| 312 | $getParams = array('id' => $result['id'], 'return' => array('custom')); |
| 313 | $check = $this->callAPISuccess($this->_entity, 'get', $getParams); |
| 314 | |
| 315 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
| 316 | |
| 317 | $this->customFieldDelete($ids['custom_field_id']); |
| 318 | $this->customGroupDelete($ids['custom_group_id']); |
| 319 | $this->callAPISuccess('address', 'delete', array('id' => $result['id'])); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | */ |
| 324 | public function testCreateAddressPrimaryHandlingChangeToPrimary() { |
| 325 | $params = $this->_params; |
| 326 | unset($params['is_primary']); |
| 327 | $address1 = $this->callAPISuccess('address', 'create', $params); |
| 328 | $this->assertApiSuccess($address1); |
| 329 | //now we check & make sure it has been set to primary |
| 330 | $check = $this->callAPISuccess('address', 'getcount', array( |
| 331 | 'is_primary' => 1, |
| 332 | 'id' => $address1['id'], |
| 333 | )); |
| 334 | $this->assertEquals(1, $check); |
| 335 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | */ |
| 340 | public function testCreateAddressPrimaryHandlingChangeExisting() { |
| 341 | $address1 = $this->callAPISuccess('address', 'create', $this->_params); |
| 342 | $this->callAPISuccess('address', 'create', $this->_params); |
| 343 | $check = $this->callAPISuccess('address', 'getcount', array( |
| 344 | 'is_primary' => 1, |
| 345 | 'contact_id' => $this->_contactID, |
| 346 | )); |
| 347 | $this->assertEquals(1, $check); |
| 348 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Test Creating address of same type alreay ind the database |
| 353 | * This is legacy API v3 behaviour and not correct behaviour |
| 354 | * however we are too far down the path wiwth v3 to fix this |
| 355 | * @link https://chat.civicrm.org/civicrm/pl/zcq3jkg69jdt5g4aqze6bbe9pc |
| 356 | * @todo vis this in v4 api |
| 357 | */ |
| 358 | public function testCreateDuplicateLocationTypes() { |
| 359 | $address1 = $this->callAPISuccess('address', 'create', $this->_params); |
| 360 | $address2 = $this->callAPISuccess('address', 'create', array( |
| 361 | 'location_type_id' => $this->_locationType->id, |
| 362 | 'street_address' => '1600 Pensilvania Avenue', |
| 363 | 'city' => 'Washington DC', |
| 364 | 'is_primary' => 0, |
| 365 | 'is_billing' => 0, |
| 366 | 'contact_id' => $this->_contactID, |
| 367 | )); |
| 368 | $check = $this->callAPISuccess('address', 'getcount', array( |
| 369 | 'contact_id' => $this->_contactID, |
| 370 | 'location_type_id' => $this->_locationType->id, |
| 371 | )); |
| 372 | $this->assertEquals(2, $check); |
| 373 | $this->callAPISuccess('address', 'delete', array('id' => $address1['id'])); |
| 374 | $this->callAPISuccess('address', 'delete', array('id' => $address2['id'])); |
| 375 | } |
| 376 | |
| 377 | public function testGetWithJoin() { |
| 378 | $cid = $this->individualCreate(array( |
| 379 | 'api.Address.create' => array( |
| 380 | 'street_address' => __FUNCTION__, |
| 381 | 'location_type_id' => $this->_locationType->id, |
| 382 | ), |
| 383 | )); |
| 384 | $result = $this->callAPISuccess('address', 'getsingle', array( |
| 385 | 'check_permissions' => TRUE, |
| 386 | 'contact_id' => $cid, |
| 387 | 'street_address' => __FUNCTION__, |
| 388 | 'return' => 'contact_id.contact_type', |
| 389 | )); |
| 390 | $this->assertEquals('Individual', $result['contact_id.contact_type']); |
| 391 | } |
| 392 | |
| 393 | } |