| 1 | <?php |
| 2 | // $Id$ |
| 3 | |
| 4 | /* |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | CiviCRM version 4.3 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | | This file is a part of CiviCRM. | |
| 11 | | | |
| 12 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 13 | | under the terms of the GNU Affero General Public License | |
| 14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 15 | | | |
| 16 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 19 | | See the GNU Affero General Public License for more details. | |
| 20 | | | |
| 21 | | You should have received a copy of the GNU Affero General Public | |
| 22 | | License and the CiviCRM Licensing Exception along | |
| 23 | | with this program; if not, contact CiviCRM LLC | |
| 24 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 25 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 27 | +--------------------------------------------------------------------+ |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * Test APIv3 civicrm_activity_* functions |
| 32 | * |
| 33 | * @package CiviCRM_APIv3 |
| 34 | * @subpackage API_Contact |
| 35 | */ |
| 36 | |
| 37 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 38 | class api_v3_AddressTest extends CiviUnitTestCase { |
| 39 | protected $_apiversion; |
| 40 | protected $_contactID; |
| 41 | protected $_locationType; |
| 42 | protected $_params; |
| 43 | public $_eNoticeCompliant = TRUE; |
| 44 | protected $_entity; |
| 45 | |
| 46 | function setUp() { |
| 47 | $this->_apiversion = 3; |
| 48 | $this->_entity = 'Address'; |
| 49 | parent::setUp(); |
| 50 | |
| 51 | $this->_contactID = $this->organizationCreate(); |
| 52 | $this->_locationType = $this->locationTypeCreate(); |
| 53 | CRM_Core_PseudoConstant::flush('locationType'); |
| 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 | 'version' => $this->_apiversion, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | function tearDown() { |
| 70 | $this->locationTypeDelete($this->_locationType->id); |
| 71 | $this->contactDelete($this->_contactID); |
| 72 | } |
| 73 | |
| 74 | public function testCreateAddress() { |
| 75 | |
| 76 | $result = civicrm_api('address', 'create', $this->_params); |
| 77 | $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__); |
| 78 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
| 79 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
| 80 | $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); |
| 81 | $this->getAndCheck($this->_params, $result['id'], 'address'); |
| 82 | } |
| 83 | public function testCreateAddressParsing() { |
| 84 | $params = array( |
| 85 | 'version' => $this->_apiversion, |
| 86 | 'street_parsing' => 1, |
| 87 | 'street_address' => '54A Excelsior Ave. Apt 1C', |
| 88 | 'location_type_id' => $this->_locationType->id, |
| 89 | 'contact_id' => $this->_contactID, |
| 90 | ); |
| 91 | $subfile = "AddressParse"; |
| 92 | $description = "Demonstrates Use of address parsing param"; |
| 93 | $result = civicrm_api('address', 'create', $params); |
| 94 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); |
| 95 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
| 96 | $this->assertEquals(54, $result['values'][$result['id']]['street_number'], 'In line ' . __LINE__); |
| 97 | $this->assertEquals('A', $result['values'][$result['id']]['street_number_suffix'], 'In line ' . __LINE__); |
| 98 | $this->assertEquals('Excelsior Ave.', $result['values'][$result['id']]['street_name'], 'In line ' . __LINE__); |
| 99 | $this->assertEquals('Apt 1C', $result['values'][$result['id']]['street_unit'], 'In line ' . __LINE__); |
| 100 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id'])); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * is_primary should be set as a default |
| 106 | */ |
| 107 | |
| 108 | |
| 109 | |
| 110 | public function testCreateAddressTestDefaults() { |
| 111 | $params = $this->_params; |
| 112 | unset($params['is_primary']); |
| 113 | $result = civicrm_api('address', 'create', $params); |
| 114 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
| 115 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
| 116 | $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__); |
| 117 | $this->getAndCheck($this->_params, $result['id'], 'address'); |
| 118 | } |
| 119 | |
| 120 | public function testCreateAddressTooLongSuffix() { |
| 121 | $params = $this->_params; |
| 122 | $params['street_number_suffix'] = 'really long string'; |
| 123 | $result = civicrm_api('address', 'create', $params); |
| 124 | $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__); |
| 125 | $this->assertEquals(2100, $result['error_code']); |
| 126 | } |
| 127 | /* |
| 128 | * is_primary shoule be set as a default. ie. create the address, unset the params & recreate. |
| 129 | * is_primary should be 0 before & after the update. ie - having no other address |
| 130 | * is_primary is invalid |
| 131 | */ |
| 132 | |
| 133 | |
| 134 | |
| 135 | public function testCreateAddressTestDefaultWithID() { |
| 136 | $params = $this->_params; |
| 137 | $params['is_primary'] = 0; |
| 138 | $result = civicrm_api('address', 'create', $params); |
| 139 | unset($params['is_primary']); |
| 140 | $params['id'] = $result['id']; |
| 141 | $result = civicrm_api('address', 'create', $params); |
| 142 | $address = civicrm_api('address', 'get', array('version' => 3, 'contact_id' => $params['contact_id'])); |
| 143 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); |
| 144 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
| 145 | $this->assertEquals(1, $result['values'][$result['id']]['is_primary'], 'In line ' . __LINE__); |
| 146 | $this->getAndCheck($params, $result['id'], 'address', __FUNCTION__); |
| 147 | } |
| 148 | public function testDeleteAddress() { |
| 149 | |
| 150 | //check there are no addresss to start with |
| 151 | $get = civicrm_api('address', 'get', array( |
| 152 | 'version' => 3, |
| 153 | 'location_type_id' => $this->_locationType->id, |
| 154 | )); |
| 155 | $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__); |
| 156 | $this->assertEquals(0, $get['count'], 'Contact already exists ' . __LINE__); |
| 157 | |
| 158 | //create one |
| 159 | $create = civicrm_api('address', 'create', $this->_params); |
| 160 | |
| 161 | $this->assertEquals(0, $create['is_error'], 'In line ' . __LINE__); |
| 162 | |
| 163 | $result = civicrm_api('address', 'delete', array('id' => $create['id'], 'version' => 3)); |
| 164 | $this->documentMe($this->_params, $result, __FUNCTION__, __FILE__); |
| 165 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); |
| 166 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
| 167 | $get = civicrm_api('address', 'get', array( |
| 168 | 'version' => 3, |
| 169 | 'location_type_id' => $this->_locationType->id, |
| 170 | )); |
| 171 | $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__); |
| 172 | $this->assertEquals(0, $get['count'], 'Contact not successfully deleted In line ' . __LINE__); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Test civicrm_address_get - success expected. |
| 177 | */ |
| 178 | public function testGetAddress() { |
| 179 | $address = civicrm_api('address', 'create', $this->_params); |
| 180 | $this->assertAPISuccess($address, 'In line ' . __LINE__); |
| 181 | |
| 182 | $params = array( |
| 183 | 'contact_id' => $this->_contactID, |
| 184 | 'street_name' => $address['values'][$address['id']]['street_name'], |
| 185 | 'version' => $this->_apiversion, |
| 186 | ); |
| 187 | $result = civicrm_api('Address', 'Get', ($params)); |
| 188 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); |
| 189 | civicrm_api('Address', 'delete', array('version' => 3, 'id' => $result['id'])); |
| 190 | $this->assertAPISuccess($result, 'In line ' . __LINE__); |
| 191 | $this->assertEquals($address['values'][$address['id']]['location_type_id'], $result['values'][$address['id']]['location_type_id'], 'In line ' . __LINE__); |
| 192 | $this->assertEquals($address['values'][$address['id']]['is_primary'], $result['values'][$address['id']]['is_primary'], 'In line ' . __LINE__); |
| 193 | $this->assertEquals($address['values'][$address['id']]['street_address'], $result['values'][$address['id']]['street_address'], 'In line ' . __LINE__); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Test civicrm_address_get - success expected. |
| 198 | */ |
| 199 | public function testGetSingleAddress() { |
| 200 | civicrm_api('address', 'create', $this->_params); |
| 201 | $params = array( |
| 202 | 'contact_id' => $this->_contactID, |
| 203 | 'version' => $this->_apiversion, |
| 204 | ); |
| 205 | $address = civicrm_api('Address', 'getsingle', ($params)); |
| 206 | $this->assertEquals($address['location_type_id'], $this->_params['location_type_id'], 'In line ' . __LINE__); |
| 207 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $address['id'])); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Test civicrm_address_get with sort option- success expected. |
| 212 | */ |
| 213 | public function testGetAddressSort() { |
| 214 | $create = civicrm_api('address', 'create', $this->_params); |
| 215 | $subfile = "AddressSort"; |
| 216 | $description = "Demonstrates Use of sort filter"; |
| 217 | $params = array( |
| 218 | 'options' => array( |
| 219 | 'sort' => 'street_address DESC', |
| 220 | 'limit' => 2, |
| 221 | ), |
| 222 | 'version' => $this->_apiversion, |
| 223 | 'sequential' => 1, |
| 224 | ); |
| 225 | $result = civicrm_api('Address', 'Get', ($params)); |
| 226 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); |
| 227 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); |
| 228 | $this->assertEquals(2, $result['count'], 'In line ' . __LINE__); |
| 229 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__); |
| 230 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id'])); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Test civicrm_address_get with sort option- success expected. |
| 235 | */ |
| 236 | public function testGetAddressLikeSuccess() { |
| 237 | civicrm_api('address', 'create', $this->_params); |
| 238 | $subfile = "AddressLike"; |
| 239 | $description = "Demonstrates Use of Like"; |
| 240 | $params = array('street_address' => array('LIKE' => '%mb%'), |
| 241 | 'version' => $this->_apiversion, |
| 242 | 'sequential' => 1, |
| 243 | ); |
| 244 | $result = civicrm_api('Address', 'Get', ($params)); |
| 245 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); |
| 246 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); |
| 247 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
| 248 | $this->assertEquals('Ambachtstraat 23', $result['values'][0]['street_address'], 'In line ' . __LINE__); |
| 249 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id'])); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Test civicrm_address_get with sort option- success expected. |
| 254 | */ |
| 255 | public function testGetAddressLikeFail() { |
| 256 | $create = civicrm_api('address', 'create', $this->_params); |
| 257 | $subfile = "AddressLike"; |
| 258 | $description = "Demonstrates Use of Like"; |
| 259 | $params = array('street_address' => array('LIKE' => "'%xy%'"), |
| 260 | 'version' => $this->_apiversion, |
| 261 | 'sequential' => 1, |
| 262 | ); |
| 263 | $result = civicrm_api('Address', 'Get', ($params)); |
| 264 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); |
| 265 | $this->assertEquals(0, $result['count'], 'In line ' . __LINE__); |
| 266 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $create['id'])); |
| 267 | } |
| 268 | |
| 269 | function testGetWithCustom() { |
| 270 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
| 271 | |
| 272 | $params = $this->_params; |
| 273 | $params['custom_' . $ids['custom_field_id']] = "custom string"; |
| 274 | |
| 275 | $result = civicrm_api($this->_entity, 'create', $params); |
| 276 | $this->assertAPISuccess($result, ' in line ' . __LINE__); |
| 277 | |
| 278 | $getParams = array('version' => 3, 'id' => $result['id'], 'return' => array('custom')); |
| 279 | $check = civicrm_api($this->_entity, 'get', $getParams); |
| 280 | |
| 281 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
| 282 | |
| 283 | $this->customFieldDelete($ids['custom_field_id']); |
| 284 | $this->customGroupDelete($ids['custom_group_id']); |
| 285 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $result['id'])); |
| 286 | } |
| 287 | |
| 288 | public function testCreateAddressPrimaryHandlingChangeToPrimary() { |
| 289 | $params = $this->_params; |
| 290 | unset($params['is_primary']); |
| 291 | $address1 = civicrm_api('address', 'create', $params); |
| 292 | $this->assertApiSuccess($address1, 'In line ' . __LINE__); |
| 293 | //now we check & make sure it has been set to primary |
| 294 | $check = civicrm_api('address', 'getcount', array( |
| 295 | 'version' => 3, |
| 296 | 'is_primary' => 1, |
| 297 | 'id' => $address1['id'], |
| 298 | )); |
| 299 | $this->assertEquals(1, $check); |
| 300 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id'])); |
| 301 | } |
| 302 | public function testCreateAddressPrimaryHandlingChangeExisting() { |
| 303 | $address1 = civicrm_api('address', 'create', $this->_params); |
| 304 | $this->assertApiSuccess($address1, 'In line ' . __LINE__); |
| 305 | $address2 = civicrm_api('address', 'create', $this->_params); |
| 306 | $this->assertApiSuccess($address2, 'In line ' . __LINE__); |
| 307 | $check = civicrm_api('address', 'getcount', array( |
| 308 | 'version' => 3, |
| 309 | 'is_primary' => 1, |
| 310 | 'contact_id' => $this->_contactID, |
| 311 | )); |
| 312 | $this->assertEquals(1, $check); |
| 313 | civicrm_api('address', 'delete', array('version' => 3, 'id' => $address1['id'])); |
| 314 | } |
| 315 | } |
| 316 | |