| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 37 | require_once 'CiviTest/Contact.php'; |
| 38 | require_once 'CiviTest/Event.php'; |
| 39 | |
| 40 | /** |
| 41 | * Class CRM_Core_BAO_LocationTest |
| 42 | */ |
| 43 | class CRM_Core_BAO_LocationTest extends CiviUnitTestCase { |
| 44 | public function setUp() { |
| 45 | parent::setUp(); |
| 46 | |
| 47 | $this->quickCleanup(array( |
| 48 | 'civicrm_contact', |
| 49 | 'civicrm_address', |
| 50 | 'civicrm_loc_block', |
| 51 | 'civicrm_email', |
| 52 | 'civicrm_phone', |
| 53 | 'civicrm_im', |
| 54 | )); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tears down the fixture, for example, closes a network connection. |
| 59 | * This method is called after a test is executed. |
| 60 | */ |
| 61 | public function tearDown() { |
| 62 | $tablesToTruncate = array( |
| 63 | 'civicrm_contact', |
| 64 | 'civicrm_openid', |
| 65 | 'civicrm_loc_block', |
| 66 | ); |
| 67 | $this->quickCleanup($tablesToTruncate); |
| 68 | } |
| 69 | |
| 70 | public function testCreateWithMissingParams() { |
| 71 | $contactId = Contact::createIndividual(); |
| 72 | $params = array( |
| 73 | 'contact_id' => $contactId, |
| 74 | 'street_address' => 'Saint Helier St', |
| 75 | ); |
| 76 | |
| 77 | CRM_Core_BAO_Location::create($params); |
| 78 | |
| 79 | //Now check DB for Address |
| 80 | $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address', |
| 81 | 'Database check, Address created successfully.' |
| 82 | ); |
| 83 | |
| 84 | //cleanup DB by deleting the contact |
| 85 | Contact::delete($contactId); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Create() method |
| 90 | * create various elements of location block |
| 91 | * without civicrm_loc_block entry |
| 92 | */ |
| 93 | public function testCreateWithoutLocBlock() { |
| 94 | $contactId = Contact::createIndividual(); |
| 95 | |
| 96 | //create various element of location block |
| 97 | //like address, phone, email, openid, im. |
| 98 | $params = array( |
| 99 | 'address' => array( |
| 100 | '1' => array( |
| 101 | 'street_address' => 'Saint Helier St', |
| 102 | 'supplemental_address_1' => 'Hallmark Ct', |
| 103 | 'supplemental_address_2' => 'Jersey Village', |
| 104 | 'city' => 'Newark', |
| 105 | 'postal_code' => '01903', |
| 106 | 'country_id' => 1228, |
| 107 | 'state_province_id' => 1029, |
| 108 | 'geo_code_1' => '18.219023', |
| 109 | 'geo_code_2' => '-105.00973', |
| 110 | 'is_primary' => 1, |
| 111 | 'location_type_id' => 1, |
| 112 | ), |
| 113 | ), |
| 114 | 'email' => array( |
| 115 | '1' => array( |
| 116 | 'email' => 'john.smith@example.org', |
| 117 | 'is_primary' => 1, |
| 118 | 'location_type_id' => 1, |
| 119 | ), |
| 120 | ), |
| 121 | 'phone' => array( |
| 122 | '1' => array( |
| 123 | 'phone_type_id' => 1, |
| 124 | 'phone' => '303443689', |
| 125 | 'is_primary' => 1, |
| 126 | 'location_type_id' => 1, |
| 127 | ), |
| 128 | '2' => array( |
| 129 | 'phone_type_id' => 2, |
| 130 | 'phone' => '9833910234', |
| 131 | 'location_type_id' => 1, |
| 132 | ), |
| 133 | ), |
| 134 | 'openid' => array( |
| 135 | '1' => array( |
| 136 | 'openid' => 'http://civicrm.org/', |
| 137 | 'location_type_id' => 1, |
| 138 | 'is_primary' => 1, |
| 139 | ), |
| 140 | ), |
| 141 | 'im' => array( |
| 142 | '1' => array( |
| 143 | 'name' => 'jane.doe', |
| 144 | 'provider_id' => 1, |
| 145 | 'location_type_id' => 1, |
| 146 | 'is_primary' => 1, |
| 147 | ), |
| 148 | ), |
| 149 | ); |
| 150 | |
| 151 | $params['contact_id'] = $contactId; |
| 152 | |
| 153 | $location = CRM_Core_BAO_Location::create($params); |
| 154 | |
| 155 | $locBlockId = CRM_Utils_Array::value('id', $location); |
| 156 | |
| 157 | //Now check DB for contact |
| 158 | $searchParams = array( |
| 159 | 'contact_id' => $contactId, |
| 160 | 'location_type_id' => 1, |
| 161 | 'is_primary' => 1, |
| 162 | ); |
| 163 | $compareParams = array( |
| 164 | 'street_address' => 'Saint Helier St', |
| 165 | 'supplemental_address_1' => 'Hallmark Ct', |
| 166 | 'supplemental_address_2' => 'Jersey Village', |
| 167 | 'city' => 'Newark', |
| 168 | 'postal_code' => '01903', |
| 169 | 'country_id' => 1228, |
| 170 | 'state_province_id' => 1029, |
| 171 | 'geo_code_1' => '18.219023', |
| 172 | 'geo_code_2' => '-105.00973', |
| 173 | ); |
| 174 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
| 175 | |
| 176 | $compareParams = array('email' => 'john.smith@example.org'); |
| 177 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
| 178 | |
| 179 | $compareParams = array('openid' => 'http://civicrm.org/'); |
| 180 | $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams); |
| 181 | |
| 182 | $compareParams = array( |
| 183 | 'name' => 'jane.doe', |
| 184 | 'provider_id' => 1, |
| 185 | ); |
| 186 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
| 187 | |
| 188 | $searchParams = array( |
| 189 | 'contact_id' => $contactId, |
| 190 | 'location_type_id' => 1, |
| 191 | 'is_primary' => 1, |
| 192 | 'phone_type_id' => 1, |
| 193 | ); |
| 194 | $compareParams = array('phone' => '303443689'); |
| 195 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 196 | |
| 197 | $searchParams = array( |
| 198 | 'contact_id' => $contactId, |
| 199 | 'location_type_id' => 1, |
| 200 | 'phone_type_id' => 2, |
| 201 | ); |
| 202 | $compareParams = array('phone' => '9833910234'); |
| 203 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 204 | |
| 205 | //delete the location block |
| 206 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); |
| 207 | |
| 208 | //cleanup DB by deleting the contact |
| 209 | Contact::delete($contactId); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Create() method |
| 214 | * create various elements of location block |
| 215 | * with civicrm_loc_block |
| 216 | */ |
| 217 | public function testCreateWithLocBlock() { |
| 218 | $this->_contactId = Contact::createIndividual(); |
| 219 | //create test event record. |
| 220 | $eventId = Event::create($this->_contactId); |
| 221 | $params = array( |
| 222 | 'address' => array( |
| 223 | '1' => array( |
| 224 | 'street_address' => 'Saint Helier St', |
| 225 | 'supplemental_address_1' => 'Hallmark Ct', |
| 226 | 'supplemental_address_2' => 'Jersey Village', |
| 227 | 'city' => 'Newark', |
| 228 | 'postal_code' => '01903', |
| 229 | 'country_id' => 1228, |
| 230 | 'state_province_id' => 1029, |
| 231 | 'geo_code_1' => '18.219023', |
| 232 | 'geo_code_2' => '-105.00973', |
| 233 | 'is_primary' => 1, |
| 234 | 'location_type_id' => 1, |
| 235 | ), |
| 236 | ), |
| 237 | 'email' => array( |
| 238 | '1' => array( |
| 239 | 'email' => 'john.smith@example.org', |
| 240 | 'is_primary' => 1, |
| 241 | 'location_type_id' => 1, |
| 242 | ), |
| 243 | ), |
| 244 | 'phone' => array( |
| 245 | '1' => array( |
| 246 | 'phone_type_id' => 1, |
| 247 | 'phone' => '303443689', |
| 248 | 'is_primary' => 1, |
| 249 | 'location_type_id' => 1, |
| 250 | ), |
| 251 | '2' => array( |
| 252 | 'phone_type_id' => 2, |
| 253 | 'phone' => '9833910234', |
| 254 | 'location_type_id' => 1, |
| 255 | ), |
| 256 | ), |
| 257 | 'im' => array( |
| 258 | '1' => array( |
| 259 | 'name' => 'jane.doe', |
| 260 | 'provider_id' => 1, |
| 261 | 'location_type_id' => 1, |
| 262 | 'is_primary' => 1, |
| 263 | ), |
| 264 | ), |
| 265 | ); |
| 266 | |
| 267 | $params['entity_id'] = $eventId; |
| 268 | $params['entity_table'] = 'civicrm_event'; |
| 269 | |
| 270 | //create location block. |
| 271 | //with various element of location block |
| 272 | //like address, phone, email, im. |
| 273 | $location = CRM_Core_BAO_Location::create($params, NULL, TRUE); |
| 274 | $locBlockId = CRM_Utils_Array::value('id', $location); |
| 275 | |
| 276 | //update event record with location block id |
| 277 | $eventParams = array( |
| 278 | 'id' => $eventId, |
| 279 | 'loc_block_id' => $locBlockId, |
| 280 | ); |
| 281 | |
| 282 | CRM_Event_BAO_Event::add($eventParams); |
| 283 | |
| 284 | //Now check DB for location block |
| 285 | |
| 286 | $this->assertDBCompareValue('CRM_Event_DAO_Event', |
| 287 | $eventId, |
| 288 | 'loc_block_id', |
| 289 | 'id', |
| 290 | $locBlockId, |
| 291 | 'Checking database for the record.' |
| 292 | ); |
| 293 | $locElementIds = array(); |
| 294 | $locParams = array('id' => $locBlockId); |
| 295 | CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock', |
| 296 | $locParams, |
| 297 | $locElementIds |
| 298 | ); |
| 299 | |
| 300 | //Now check DB for location elements. |
| 301 | $searchParams = array( |
| 302 | 'id' => CRM_Utils_Array::value('address_id', $locElementIds), |
| 303 | 'location_type_id' => 1, |
| 304 | 'is_primary' => 1, |
| 305 | ); |
| 306 | $compareParams = array( |
| 307 | 'street_address' => 'Saint Helier St', |
| 308 | 'supplemental_address_1' => 'Hallmark Ct', |
| 309 | 'supplemental_address_2' => 'Jersey Village', |
| 310 | 'city' => 'Newark', |
| 311 | 'postal_code' => '01903', |
| 312 | 'country_id' => 1228, |
| 313 | 'state_province_id' => 1029, |
| 314 | 'geo_code_1' => '18.219023', |
| 315 | 'geo_code_2' => '-105.00973', |
| 316 | ); |
| 317 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
| 318 | |
| 319 | $searchParams = array( |
| 320 | 'id' => CRM_Utils_Array::value('email_id', $locElementIds), |
| 321 | 'location_type_id' => 1, |
| 322 | 'is_primary' => 1, |
| 323 | ); |
| 324 | $compareParams = array('email' => 'john.smith@example.org'); |
| 325 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
| 326 | |
| 327 | $searchParams = array( |
| 328 | 'id' => CRM_Utils_Array::value('phone_id', $locElementIds), |
| 329 | 'location_type_id' => 1, |
| 330 | 'is_primary' => 1, |
| 331 | 'phone_type_id' => 1, |
| 332 | ); |
| 333 | $compareParams = array('phone' => '303443689'); |
| 334 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 335 | |
| 336 | $searchParams = array( |
| 337 | 'id' => CRM_Utils_Array::value('phone_2_id', $locElementIds), |
| 338 | 'location_type_id' => 1, |
| 339 | 'phone_type_id' => 2, |
| 340 | ); |
| 341 | $compareParams = array('phone' => '9833910234'); |
| 342 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
| 343 | |
| 344 | $searchParams = array( |
| 345 | 'id' => CRM_Utils_Array::value('im_id', $locElementIds), |
| 346 | 'location_type_id' => 1, |
| 347 | 'is_primary' => 1, |
| 348 | ); |
| 349 | $compareParams = array( |
| 350 | 'name' => 'jane.doe', |
| 351 | 'provider_id' => 1, |
| 352 | ); |
| 353 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
| 354 | |
| 355 | //delete the location block |
| 356 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); |
| 357 | |
| 358 | //cleanup DB by deleting the record. |
| 359 | Event::delete($eventId); |
| 360 | Contact::delete($this->_contactId); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * DeleteLocBlock() method |
| 365 | * delete the location block |
| 366 | * created with various elements. |
| 367 | */ |
| 368 | public function testDeleteLocBlock() { |
| 369 | $this->_contactId = Contact::createIndividual(); |
| 370 | //create test event record. |
| 371 | $eventId = Event::create($this->_contactId); |
| 372 | $params['location'][1] = array( |
| 373 | 'location_type_id' => 1, |
| 374 | 'is_primary' => 1, |
| 375 | 'address' => array( |
| 376 | 'street_address' => 'Saint Helier St', |
| 377 | 'supplemental_address_1' => 'Hallmark Ct', |
| 378 | 'supplemental_address_2' => 'Jersey Village', |
| 379 | 'city' => 'Newark', |
| 380 | 'postal_code' => '01903', |
| 381 | 'country_id' => 1228, |
| 382 | 'state_province_id' => 1029, |
| 383 | 'geo_code_1' => '18.219023', |
| 384 | 'geo_code_2' => '-105.00973', |
| 385 | ), |
| 386 | 'email' => array( |
| 387 | '1' => array('email' => 'john.smith@example.org'), |
| 388 | ), |
| 389 | 'phone' => array( |
| 390 | '1' => array( |
| 391 | 'phone_type_id' => 1, |
| 392 | 'phone' => '303443689', |
| 393 | ), |
| 394 | '2' => array( |
| 395 | 'phone_type_id' => 2, |
| 396 | 'phone' => '9833910234', |
| 397 | ), |
| 398 | ), |
| 399 | 'im' => array( |
| 400 | '1' => array( |
| 401 | 'name' => 'jane.doe', |
| 402 | 'provider_id' => 1, |
| 403 | ), |
| 404 | ), |
| 405 | ); |
| 406 | $params['entity_id'] = $eventId; |
| 407 | $params['entity_table'] = 'civicrm_event'; |
| 408 | |
| 409 | //create location block. |
| 410 | //with various elements |
| 411 | //like address, phone, email, im. |
| 412 | $location = CRM_Core_BAO_Location::create($params, NULL, TRUE); |
| 413 | $locBlockId = CRM_Utils_Array::value('id', $location); |
| 414 | //update event record with location block id |
| 415 | $eventParams = array( |
| 416 | 'id' => $eventId, |
| 417 | 'loc_block_id' => $locBlockId, |
| 418 | ); |
| 419 | CRM_Event_BAO_Event::add($eventParams); |
| 420 | |
| 421 | //delete the location block |
| 422 | CRM_Core_BAO_Location::deleteLocBlock($locBlockId); |
| 423 | |
| 424 | //Now check DB for location elements. |
| 425 | //Now check DB for Address |
| 426 | $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address', |
| 427 | 'Database check, Address deleted successfully.' |
| 428 | ); |
| 429 | //Now check DB for Email |
| 430 | $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email', |
| 431 | 'Database check, Email deleted successfully.' |
| 432 | ); |
| 433 | //Now check DB for Phone |
| 434 | $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone', |
| 435 | 'Database check, Phone deleted successfully.' |
| 436 | ); |
| 437 | //Now check DB for Mobile |
| 438 | $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone', |
| 439 | 'Database check, Mobile deleted successfully.' |
| 440 | ); |
| 441 | //Now check DB for IM |
| 442 | $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name', |
| 443 | 'Database check, IM deleted successfully.' |
| 444 | ); |
| 445 | |
| 446 | //cleanup DB by deleting the record. |
| 447 | Event::delete($eventId); |
| 448 | Contact::delete($this->_contactId); |
| 449 | |
| 450 | //Now check DB for Event |
| 451 | $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id', |
| 452 | 'Database check, Event deleted successfully.' |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * GetValues() method |
| 458 | * get the values of various location elements |
| 459 | */ |
| 460 | public function testLocBlockgetValues() { |
| 461 | $contactId = Contact::createIndividual(); |
| 462 | |
| 463 | //create various element of location block |
| 464 | //like address, phone, email, openid, im. |
| 465 | $params = array( |
| 466 | 'address' => array( |
| 467 | '1' => array( |
| 468 | 'street_address' => 'Saint Helier St', |
| 469 | 'supplemental_address_1' => 'Hallmark Ct', |
| 470 | 'supplemental_address_2' => 'Jersey Village', |
| 471 | 'city' => 'Newark', |
| 472 | 'postal_code' => '01903', |
| 473 | 'country_id' => 1228, |
| 474 | 'state_province_id' => 1029, |
| 475 | 'geo_code_1' => '18.219023', |
| 476 | 'geo_code_2' => '-105.00973', |
| 477 | 'is_primary' => 1, |
| 478 | 'location_type_id' => 1, |
| 479 | ), |
| 480 | ), |
| 481 | 'email' => array( |
| 482 | '1' => array( |
| 483 | 'email' => 'john.smith@example.org', |
| 484 | 'is_primary' => 1, |
| 485 | 'location_type_id' => 1, |
| 486 | ), |
| 487 | ), |
| 488 | 'phone' => array( |
| 489 | '1' => array( |
| 490 | 'phone_type_id' => 1, |
| 491 | 'phone' => '303443689', |
| 492 | 'is_primary' => 1, |
| 493 | 'location_type_id' => 1, |
| 494 | ), |
| 495 | '2' => array( |
| 496 | 'phone_type_id' => 2, |
| 497 | 'phone' => '9833910234', |
| 498 | 'location_type_id' => 1, |
| 499 | ), |
| 500 | ), |
| 501 | 'openid' => array( |
| 502 | '1' => array( |
| 503 | 'openid' => 'http://civicrm.org/', |
| 504 | 'location_type_id' => 1, |
| 505 | 'is_primary' => 1, |
| 506 | ), |
| 507 | ), |
| 508 | 'im' => array( |
| 509 | '1' => array( |
| 510 | 'name' => 'jane.doe', |
| 511 | 'provider_id' => 1, |
| 512 | 'location_type_id' => 1, |
| 513 | 'is_primary' => 1, |
| 514 | ), |
| 515 | ), |
| 516 | ); |
| 517 | |
| 518 | $params['contact_id'] = $contactId; |
| 519 | |
| 520 | //create location elements. |
| 521 | CRM_Core_BAO_Location::create($params); |
| 522 | |
| 523 | //get the values from DB |
| 524 | $values = CRM_Core_BAO_Location::getValues($params); |
| 525 | |
| 526 | //Now check values of address |
| 527 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']), |
| 528 | CRM_Utils_Array::value('1', $values['address']) |
| 529 | ); |
| 530 | |
| 531 | //Now check values of email |
| 532 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']), |
| 533 | CRM_Utils_Array::value('1', $values['email']) |
| 534 | ); |
| 535 | |
| 536 | //Now check values of phone |
| 537 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']), |
| 538 | CRM_Utils_Array::value('1', $values['phone']) |
| 539 | ); |
| 540 | |
| 541 | //Now check values of mobile |
| 542 | $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']), |
| 543 | CRM_Utils_Array::value('2', $values['phone']) |
| 544 | ); |
| 545 | |
| 546 | //Now check values of openid |
| 547 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']), |
| 548 | CRM_Utils_Array::value('1', $values['openid']) |
| 549 | ); |
| 550 | |
| 551 | //Now check values of im |
| 552 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']), |
| 553 | CRM_Utils_Array::value('1', $values['im']) |
| 554 | ); |
| 555 | |
| 556 | //cleanup DB by deleting the contact |
| 557 | Contact::delete($contactId); |
| 558 | } |
| 559 | |
| 560 | } |