| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | * $Id$ |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * Class CRM_Core_BAO_LocationTest |
| 22 | * @group headless |
| 23 | */ |
| 24 | class CRM_Core_BAO_LocationTest extends CiviUnitTestCase { |
| 25 | |
| 26 | public function setUp() { |
| 27 | parent::setUp(); |
| 28 | |
| 29 | $this->quickCleanup([ |
| 30 | 'civicrm_contact', |
| 31 | 'civicrm_address', |
| 32 | 'civicrm_loc_block', |
| 33 | 'civicrm_email', |
| 34 | 'civicrm_phone', |
| 35 | 'civicrm_im', |
| 36 | ]); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Tears down the fixture, for example, closes a network connection. |
| 41 | * This method is called after a test is executed. |
| 42 | */ |
| 43 | public function tearDown() { |
| 44 | $tablesToTruncate = [ |
| 45 | 'civicrm_contact', |
| 46 | 'civicrm_openid', |
| 47 | 'civicrm_loc_block', |
| 48 | ]; |
| 49 | $this->quickCleanup($tablesToTruncate); |
| 50 | } |
| 51 | |
| 52 | public function testCreateWithMissingParams() { |
| 53 | $contactId = $this->individualCreate(); |
| 54 | $params = [ |
| 55 | 'contact_id' => $contactId, |
| 56 | 'street_address' => 'Saint Helier St', |
| 57 | ]; |
| 58 | |
| 59 | CRM_Core_BAO_Location::create($params); |
| 60 | |
| 61 | //Now check DB for Address |
| 62 | $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address', |
| 63 | 'Database check, Address created successfully.' |
| 64 | ); |
| 65 | |
| 66 | $this->contactDelete($contactId); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Create() method |
| 71 | * create various elements of location block |
| 72 | * without civicrm_loc_block entry |
| 73 | */ |
| 74 | public function testCreateWithoutLocBlock() { |
| 75 | $contactId = $this->individualCreate(); |
| 76 | |
| 77 | //create various element of location block |
| 78 | //like address, phone, email, openid, im. |
| 79 | $params = [ |
| 80 | 'address' => [ |
| 81 | '1' => [ |
| 82 | 'street_address' => 'Saint Helier St', |
| 83 | 'supplemental_address_1' => 'Hallmark Ct', |
| 84 | 'supplemental_address_2' => 'Jersey Village', |
| 85 | 'supplemental_address_3' => 'My Town', |
| 86 | 'city' => 'Newark', |
| 87 | 'postal_code' => '01903', |
| 88 | 'country_id' => 1228, |
| 89 | 'state_province_id' => 1029, |
| 90 | 'geo_code_1' => '18.219023', |
| 91 | 'geo_code_2' => '-105.00973', |
| 92 | 'is_primary' => 1, |
| 93 | 'location_type_id' => 1, |
| 94 | ], |
| 95 | ], |
| 96 | 'email' => [ |
| 97 | '1' => [ |
| 98 | 'email' => 'john.smith@example.org', |
| 99 | 'is_primary' => 1, |
| 100 | 'location_type_id' => 1, |
| 101 | ], |
| 102 | ], |
| 103 | 'phone' => [ |
| 104 | '1' => [ |
| 105 | 'phone_type_id' => 1, |
| 106 | 'phone' => '303443689', |
| 107 | 'is_primary' => 1, |
| 108 | 'location_type_id' => 1, |
| 109 | ], |
| 110 | '2' => [ |
| 111 | 'phone_type_id' => 2, |
| 112 | 'phone' => '9833910234', |
| 113 | 'location_type_id' => 1, |
| 114 | ], |
| 115 | ], |
| 116 | 'openid' => [ |
| 117 | '1' => [ |
| 118 | 'openid' => 'http://civicrm.org/', |
| 119 | 'location_type_id' => 1, |
| 120 | 'is_primary' => 1, |
| 121 | ], |
| 122 | ], |
| 123 | 'im' => [ |
| 124 | '1' => [ |
| 125 | 'name' => 'jane.doe', |
| 126 | 'provider_id' => 1, |
| 127 | 'location_type_id' => 1, |
| 128 | 'is_primary' => 1, |
| 129 | ], |
| 130 | ], |
| 131 | ]; |
| 132 | |
| 133 | $params['contact_id'] = $contactId; |
| 134 | |
| 135 | $locBlockId = CRM_Core_BAO_Location::create($params); |
| 136 | |
| 137 | //Now check DB for contact |
| 138 | $searchParams = [ |
| 139 | 'contact_id' => $contactId, |
| 140 | 'location_type_id' => 1, |
| 141 | 'is_primary' => 1, |
| 142 | ]; |
| 143 | $compareParams = [ |
| 144 | 'street_address' => 'Saint Helier St', |
| 145 | 'supplemental_address_1' => 'Hallmark Ct', |
| 146 | 'supplemental_address_2' => 'Jersey Village', |
| 147 | 'supplemental_address_3' => 'My Town', |
| 148 | 'city' => 'Newark', |
| 149 | 'postal_code' => '01903', |
| 150 | 'country_id' => 1228, |
| 151 | 'state_province_id' => 1029, |
| 152 | 'geo_code_1' => '18.219023', |
| 153 | 'geo_code_2' => '-105.00973', |
| 154 | ]; |
| 155 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
| 156 | |
| 157 | $compareParams = ['email' => 'john.smith@example.org']; |
| 158 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
| 159 | |
| 160 | $compareParams = ['openid' => 'http://civicrm.org/']; |
| 161 | $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams); |
| 162 | |
| 163 | $compareParams = [ |
| 164 | 'name' => 'jane.doe', |
| 165 | 'provider_id' => 1, |
| 166 | ]; |
| 167 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
| 168 | |
| 169 | $searchParams = [ |
| 170 | 'contact_id' => $contactId, |
| 171 | 'location_type_id' => 1, |
| 172 | 'is_primary' => 1, |
| 173 | 'phone_type_id' => 1, |
| 174 | ]; |
| 175 | $compareParams = ['phone' => '303443689']; |
| 176 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 177 | |
| 178 | $searchParams = [ |
| 179 | 'contact_id' => $contactId, |
| 180 | 'location_type_id' => 1, |
| 181 | 'phone_type_id' => 2, |
| 182 | ]; |
| 183 | $compareParams = ['phone' => '9833910234']; |
| 184 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 185 | |
| 186 | $this->contactDelete($contactId); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Create() method |
| 191 | * create various elements of location block |
| 192 | * with civicrm_loc_block |
| 193 | */ |
| 194 | public function testCreateWithLocBlock() { |
| 195 | $this->_contactId = $this->individualCreate(); |
| 196 | $event = $this->eventCreate(); |
| 197 | $params = [ |
| 198 | 'address' => [ |
| 199 | '1' => [ |
| 200 | 'street_address' => 'Saint Helier St', |
| 201 | 'supplemental_address_1' => 'Hallmark Ct', |
| 202 | 'supplemental_address_2' => 'Jersey Village', |
| 203 | 'supplemental_address_3' => 'My Town', |
| 204 | 'city' => 'Newark', |
| 205 | 'postal_code' => '01903', |
| 206 | 'country_id' => 1228, |
| 207 | 'state_province_id' => 1029, |
| 208 | 'geo_code_1' => '18.219023', |
| 209 | 'geo_code_2' => '-105.00973', |
| 210 | 'is_primary' => 1, |
| 211 | 'location_type_id' => 1, |
| 212 | ], |
| 213 | ], |
| 214 | 'email' => [ |
| 215 | '1' => [ |
| 216 | 'email' => 'john.smith@example.org', |
| 217 | 'is_primary' => 1, |
| 218 | 'location_type_id' => 1, |
| 219 | ], |
| 220 | ], |
| 221 | 'phone' => [ |
| 222 | '1' => [ |
| 223 | 'phone_type_id' => 1, |
| 224 | 'phone' => '303443689', |
| 225 | 'is_primary' => 1, |
| 226 | 'location_type_id' => 1, |
| 227 | ], |
| 228 | '2' => [ |
| 229 | 'phone_type_id' => 2, |
| 230 | 'phone' => '9833910234', |
| 231 | 'location_type_id' => 1, |
| 232 | ], |
| 233 | ], |
| 234 | 'im' => [ |
| 235 | '1' => [ |
| 236 | 'name' => 'jane.doe', |
| 237 | 'provider_id' => 1, |
| 238 | 'location_type_id' => 1, |
| 239 | 'is_primary' => 1, |
| 240 | ], |
| 241 | ], |
| 242 | ]; |
| 243 | |
| 244 | $params['entity_id'] = $event['id']; |
| 245 | $params['entity_table'] = 'civicrm_event'; |
| 246 | |
| 247 | //create location block. |
| 248 | //with various element of location block |
| 249 | //like address, phone, email, im. |
| 250 | $locBlockId = CRM_Core_BAO_Location::create($params, NULL, TRUE)['id']; |
| 251 | |
| 252 | //update event record with location block id |
| 253 | $eventParams = [ |
| 254 | 'id' => $event['id'], |
| 255 | 'loc_block_id' => $locBlockId, |
| 256 | ]; |
| 257 | |
| 258 | CRM_Event_BAO_Event::add($eventParams); |
| 259 | |
| 260 | //Now check DB for location block |
| 261 | |
| 262 | $this->assertDBCompareValue('CRM_Event_DAO_Event', |
| 263 | $event['id'], |
| 264 | 'loc_block_id', |
| 265 | 'id', |
| 266 | $locBlockId, |
| 267 | 'Checking database for the record.' |
| 268 | ); |
| 269 | $locElementIds = []; |
| 270 | $locParams = ['id' => $locBlockId]; |
| 271 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock', |
| 272 | $locParams, |
| 273 | $locElementIds |
| 274 | ); |
| 275 | |
| 276 | //Now check DB for location elements. |
| 277 | $searchParams = [ |
| 278 | 'id' => $locElementIds['address_id'] ?? NULL, |
| 279 | 'location_type_id' => 1, |
| 280 | 'is_primary' => 1, |
| 281 | ]; |
| 282 | $compareParams = [ |
| 283 | 'street_address' => 'Saint Helier St', |
| 284 | 'supplemental_address_1' => 'Hallmark Ct', |
| 285 | 'supplemental_address_2' => 'Jersey Village', |
| 286 | 'supplemental_address_3' => 'My Town', |
| 287 | 'city' => 'Newark', |
| 288 | 'postal_code' => '01903', |
| 289 | 'country_id' => 1228, |
| 290 | 'state_province_id' => 1029, |
| 291 | 'geo_code_1' => '18.219023', |
| 292 | 'geo_code_2' => '-105.00973', |
| 293 | ]; |
| 294 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
| 295 | |
| 296 | $searchParams = [ |
| 297 | 'id' => $locElementIds['email_id'] ?? NULL, |
| 298 | 'location_type_id' => 1, |
| 299 | 'is_primary' => 1, |
| 300 | ]; |
| 301 | $compareParams = ['email' => 'john.smith@example.org']; |
| 302 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
| 303 | |
| 304 | $searchParams = [ |
| 305 | 'id' => $locElementIds['phone_id'] ?? NULL, |
| 306 | 'location_type_id' => 1, |
| 307 | 'is_primary' => 1, |
| 308 | 'phone_type_id' => 1, |
| 309 | ]; |
| 310 | $compareParams = ['phone' => '303443689']; |
| 311 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 312 | |
| 313 | $searchParams = [ |
| 314 | 'id' => $locElementIds['phone_2_id'] ?? NULL, |
| 315 | 'location_type_id' => 1, |
| 316 | 'phone_type_id' => 2, |
| 317 | ]; |
| 318 | $compareParams = ['phone' => '9833910234']; |
| 319 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 320 | |
| 321 | $searchParams = [ |
| 322 | 'id' => $locElementIds['im_id'] ?? NULL, |
| 323 | 'location_type_id' => 1, |
| 324 | 'is_primary' => 1, |
| 325 | ]; |
| 326 | $compareParams = [ |
| 327 | 'name' => 'jane.doe', |
| 328 | 'provider_id' => 1, |
| 329 | ]; |
| 330 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
| 331 | |
| 332 | // Cleanup. |
| 333 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); |
| 334 | $this->eventDelete($event['id']); |
| 335 | $this->contactDelete($this->_contactId); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * GetValues() method |
| 340 | * get the values of various location elements |
| 341 | */ |
| 342 | public function testLocBlockgetValues() { |
| 343 | $contactId = $this->individualCreate(); |
| 344 | |
| 345 | //create various element of location block |
| 346 | //like address, phone, email, openid, im. |
| 347 | $params = [ |
| 348 | 'address' => [ |
| 349 | '1' => [ |
| 350 | 'street_address' => 'Saint Helier St', |
| 351 | 'supplemental_address_1' => 'Hallmark Ct', |
| 352 | 'supplemental_address_2' => 'Jersey Village', |
| 353 | 'supplemental_address_3' => 'My Town', |
| 354 | 'city' => 'Newark', |
| 355 | 'postal_code' => '01903', |
| 356 | 'country_id' => 1228, |
| 357 | 'state_province_id' => 1029, |
| 358 | 'geo_code_1' => '18.219023', |
| 359 | 'geo_code_2' => '-105.00973', |
| 360 | 'is_primary' => 1, |
| 361 | 'location_type_id' => 1, |
| 362 | ], |
| 363 | ], |
| 364 | 'email' => [ |
| 365 | '1' => [ |
| 366 | 'email' => 'john.smith@example.org', |
| 367 | 'is_primary' => 1, |
| 368 | 'location_type_id' => 1, |
| 369 | ], |
| 370 | ], |
| 371 | 'phone' => [ |
| 372 | '1' => [ |
| 373 | 'phone_type_id' => 1, |
| 374 | 'phone' => '303443689', |
| 375 | 'is_primary' => 1, |
| 376 | 'location_type_id' => 1, |
| 377 | ], |
| 378 | '2' => [ |
| 379 | 'phone_type_id' => 2, |
| 380 | 'phone' => '9833910234', |
| 381 | 'location_type_id' => 1, |
| 382 | ], |
| 383 | ], |
| 384 | 'openid' => [ |
| 385 | '1' => [ |
| 386 | 'openid' => 'http://civicrm.org/', |
| 387 | 'location_type_id' => 1, |
| 388 | 'is_primary' => 1, |
| 389 | ], |
| 390 | ], |
| 391 | 'im' => [ |
| 392 | '1' => [ |
| 393 | 'name' => 'jane.doe', |
| 394 | 'provider_id' => 1, |
| 395 | 'location_type_id' => 1, |
| 396 | 'is_primary' => 1, |
| 397 | ], |
| 398 | ], |
| 399 | ]; |
| 400 | |
| 401 | $params['contact_id'] = $contactId; |
| 402 | |
| 403 | //create location elements. |
| 404 | CRM_Core_BAO_Location::create($params); |
| 405 | |
| 406 | //get the values from DB |
| 407 | $values = CRM_Core_BAO_Location::getValues($params); |
| 408 | |
| 409 | //Now check values of address |
| 410 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']), |
| 411 | CRM_Utils_Array::value('1', $values['address']) |
| 412 | ); |
| 413 | |
| 414 | //Now check values of email |
| 415 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']), |
| 416 | CRM_Utils_Array::value('1', $values['email']) |
| 417 | ); |
| 418 | |
| 419 | //Now check values of phone |
| 420 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']), |
| 421 | CRM_Utils_Array::value('1', $values['phone']) |
| 422 | ); |
| 423 | |
| 424 | //Now check values of mobile |
| 425 | $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']), |
| 426 | CRM_Utils_Array::value('2', $values['phone']) |
| 427 | ); |
| 428 | |
| 429 | //Now check values of openid |
| 430 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']), |
| 431 | CRM_Utils_Array::value('1', $values['openid']) |
| 432 | ); |
| 433 | |
| 434 | //Now check values of im |
| 435 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']), |
| 436 | CRM_Utils_Array::value('1', $values['im']) |
| 437 | ); |
| 438 | $this->contactDelete($contactId); |
| 439 | } |
| 440 | |
| 441 | } |