| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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 | * @file |
| 30 | * File for the CRM_Contact_Imports_Parser_ContactTest class. |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * Test contact import parser. |
| 35 | * |
| 36 | * @package CiviCRM |
| 37 | * @group headless |
| 38 | */ |
| 39 | class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase { |
| 40 | protected $_tablesToTruncate = array(); |
| 41 | |
| 42 | /** |
| 43 | * Setup function. |
| 44 | */ |
| 45 | public function setUp() { |
| 46 | parent::setUp(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Test import parser will update based on a rule match. |
| 51 | * |
| 52 | * In this case the contact has no external identifier. |
| 53 | * |
| 54 | * @throws \Exception |
| 55 | */ |
| 56 | public function testImportParserWithUpdateWithoutExternalIdentifier() { |
| 57 | list($originalValues, $result) = $this->setUpBaseContact(); |
| 58 | $originalValues['nick_name'] = 'Old Bill'; |
| 59 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 60 | $originalValues['id'] = $result['id']; |
| 61 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); |
| 62 | $this->callAPISuccessGetSingle('Contact', $originalValues); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Test import parser will update contacts with an external identifier. |
| 67 | * |
| 68 | * This is the basic test where the identifier matches the import parameters. |
| 69 | * |
| 70 | * @throws \Exception |
| 71 | */ |
| 72 | public function testImportParserWithUpdateWithExternalIdentifier() { |
| 73 | list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows')); |
| 74 | |
| 75 | $this->assertEquals($result['id'], CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE)); |
| 76 | $this->assertEquals('windows', $result['external_identifier']); |
| 77 | |
| 78 | $originalValues['nick_name'] = 'Old Bill'; |
| 79 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 80 | $originalValues['id'] = $result['id']; |
| 81 | |
| 82 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); |
| 83 | $this->callAPISuccessGetSingle('Contact', $originalValues); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Test import parser will fallback to external identifier. |
| 88 | * |
| 89 | * In this case no primary match exists (e.g the details are not supplied) so it falls back on external identifier. |
| 90 | * |
| 91 | * CRM-17275 |
| 92 | * |
| 93 | * @throws \Exception |
| 94 | */ |
| 95 | public function testImportParserWithUpdateWithExternalIdentifierButNoPrimaryMatch() { |
| 96 | list($originalValues, $result) = $this->setUpBaseContact(array( |
| 97 | 'external_identifier' => 'windows', |
| 98 | 'email' => NULL, |
| 99 | )); |
| 100 | |
| 101 | $this->assertEquals('windows', $result['external_identifier']); |
| 102 | |
| 103 | $originalValues['nick_name'] = 'Old Bill'; |
| 104 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 105 | $originalValues['id'] = $result['id']; |
| 106 | |
| 107 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); |
| 108 | $this->callAPISuccessGetSingle('Contact', $originalValues); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Test that the import parser adds the external identifier where none is set. |
| 113 | * |
| 114 | * @throws \Exception |
| 115 | */ |
| 116 | public function testImportParserWithUpdateWithNoExternalIdentifier() { |
| 117 | list($originalValues, $result) = $this->setUpBaseContact(); |
| 118 | $originalValues['nick_name'] = 'Old Bill'; |
| 119 | $originalValues['external_identifier'] = 'windows'; |
| 120 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 121 | $originalValues['id'] = $result['id']; |
| 122 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name'))); |
| 123 | $this->callAPISuccessGetSingle('Contact', $originalValues); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Test that the import parser changes the external identifier when there is a dedupe match. |
| 128 | * |
| 129 | * @throws \Exception |
| 130 | */ |
| 131 | public function testImportParserWithUpdateWithChangedExternalIdentifier() { |
| 132 | list($contactValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows')); |
| 133 | $contact_id = $result['id']; |
| 134 | $contactValues['nick_name'] = 'Old Bill'; |
| 135 | $contactValues['external_identifier'] = 'android'; |
| 136 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 137 | $contactValues['id'] = $contact_id; |
| 138 | $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $contact_id, 'return' => 'nick_name'))); |
| 139 | $this->callAPISuccessGetSingle('Contact', $contactValues); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Test that the import parser adds the address to the right location. |
| 144 | * |
| 145 | * @throws \Exception |
| 146 | */ |
| 147 | public function testImportBillingAddress() { |
| 148 | list($contactValues) = $this->setUpBaseContact(); |
| 149 | $contactValues['nick_name'] = 'Old Bill'; |
| 150 | $contactValues['external_identifier'] = 'android'; |
| 151 | $contactValues['street_address'] = 'Big Mansion'; |
| 152 | $contactValues['phone'] = '911'; |
| 153 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 2, 6 => 2)); |
| 154 | $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion')); |
| 155 | $this->assertEquals(2, $address['location_type_id']); |
| 156 | |
| 157 | $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '911')); |
| 158 | $this->assertEquals(2, $phone['location_type_id']); |
| 159 | |
| 160 | $contact = $this->callAPISuccessGetSingle('Contact', $contactValues); |
| 161 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Test that the import parser adds the address to the primary location. |
| 166 | * |
| 167 | * @throws \Exception |
| 168 | */ |
| 169 | public function testImportPrimaryAddress() { |
| 170 | list($contactValues) = $this->setUpBaseContact(); |
| 171 | $contactValues['nick_name'] = 'Old Bill'; |
| 172 | $contactValues['external_identifier'] = 'android'; |
| 173 | $contactValues['street_address'] = 'Big Mansion'; |
| 174 | $contactValues['phone'] = 12334; |
| 175 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary')); |
| 176 | $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion')); |
| 177 | $this->assertEquals(1, $address['location_type_id']); |
| 178 | $this->assertEquals(1, $address['is_primary']); |
| 179 | |
| 180 | $phone = $this->callAPISuccessGetSingle('Phone', array('phone' => '12334')); |
| 181 | $this->assertEquals(1, $phone['location_type_id']); |
| 182 | |
| 183 | $contact = $this->callAPISuccessGetSingle('Contact', $contactValues); |
| 184 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Test that the import parser adds the address to the primary location. |
| 189 | * |
| 190 | * @throws \Exception |
| 191 | */ |
| 192 | public function testImportDeceased() { |
| 193 | list($contactValues) = $this->setUpBaseContact(); |
| 194 | CRM_Core_Session::singleton()->set("dateTypes", 1); |
| 195 | $contactValues['birth_date'] = '1910-12-17'; |
| 196 | $contactValues['deceased_date'] = '2010-12-17'; |
| 197 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 198 | $contact = $this->callAPISuccessGetSingle('Contact', $contactValues); |
| 199 | $this->assertEquals('1910-12-17', $contact['birth_date']); |
| 200 | $this->assertEquals('2010-12-17', $contact['deceased_date']); |
| 201 | $this->assertEquals(1, $contact['is_deceased']); |
| 202 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * Test that the import parser adds the address to the primary location. |
| 208 | * |
| 209 | * @throws \Exception |
| 210 | */ |
| 211 | public function testImportTwoAddressFirstPrimary() { |
| 212 | list($contactValues) = $this->setUpBaseContact(); |
| 213 | $contactValues['nick_name'] = 'Old Bill'; |
| 214 | $contactValues['external_identifier'] = 'android'; |
| 215 | $contactValues['street_address'] = 'Big Mansion'; |
| 216 | $contactValues['phone'] = 12334; |
| 217 | $fields = array_keys($contactValues); |
| 218 | $contactValues['street_address_2'] = 'Teeny Mansion'; |
| 219 | $contactValues['phone_2'] = 4444; |
| 220 | $fields[] = 'street_address'; |
| 221 | $fields[] = 'phone'; |
| 222 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary', 7 => 3, 8 => 3), $fields); |
| 223 | $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android')); |
| 224 | $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1)); |
| 225 | |
| 226 | $this->assertEquals(3, $address['values'][0]['location_type_id']); |
| 227 | $this->assertEquals(0, $address['values'][0]['is_primary']); |
| 228 | $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']); |
| 229 | |
| 230 | $this->assertEquals(1, $address['values'][1]['location_type_id']); |
| 231 | $this->assertEquals(1, $address['values'][1]['is_primary']); |
| 232 | $this->assertEquals('Big Mansion', $address['values'][1]['street_address']); |
| 233 | |
| 234 | $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1)); |
| 235 | $this->assertEquals(1, $phone['values'][0]['location_type_id']); |
| 236 | $this->assertEquals(1, $phone['values'][0]['is_primary']); |
| 237 | $this->assertEquals(12334, $phone['values'][0]['phone']); |
| 238 | $this->assertEquals(3, $phone['values'][1]['location_type_id']); |
| 239 | $this->assertEquals(0, $phone['values'][1]['is_primary']); |
| 240 | $this->assertEquals(4444, $phone['values'][1]['phone']); |
| 241 | |
| 242 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Test that the import parser adds the address to the primary location. |
| 247 | * |
| 248 | * @throws \Exception |
| 249 | */ |
| 250 | public function testImportTwoAddressSecondPrimary() { |
| 251 | list($contactValues) = $this->setUpBaseContact(); |
| 252 | $contactValues['nick_name'] = 'Old Bill'; |
| 253 | $contactValues['external_identifier'] = 'android'; |
| 254 | $contactValues['street_address'] = 'Big Mansion'; |
| 255 | $contactValues['phone'] = 12334; |
| 256 | $fields = array_keys($contactValues); |
| 257 | $contactValues['street_address_2'] = 'Teeny Mansion'; |
| 258 | $contactValues['phone_2'] = 4444; |
| 259 | $fields[] = 'street_address'; |
| 260 | $fields[] = 'phone'; |
| 261 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 3, 6 => 3, 7 => 'Primary', 8 => 'Primary'), $fields); |
| 262 | $contact = $this->callAPISuccessGetSingle('Contact', array('external_identifier' => 'android')); |
| 263 | $address = $this->callAPISuccess('Address', 'get', array('contact_id' => $contact['id'], 'sequential' => 1)); |
| 264 | |
| 265 | $this->assertEquals(1, $address['values'][0]['location_type_id']); |
| 266 | $this->assertEquals(1, $address['values'][0]['is_primary']); |
| 267 | $this->assertEquals('Teeny Mansion', $address['values'][0]['street_address']); |
| 268 | |
| 269 | $this->assertEquals(3, $address['values'][1]['location_type_id']); |
| 270 | $this->assertEquals(0, $address['values'][1]['is_primary']); |
| 271 | $this->assertEquals('Big Mansion', $address['values'][1]['street_address']); |
| 272 | |
| 273 | $phone = $this->callAPISuccess('Phone', 'get', array('contact_id' => $contact['id'], 'sequential' => 1)); |
| 274 | $this->assertEquals(3, $phone['values'][0]['location_type_id']); |
| 275 | $this->assertEquals(0, $phone['values'][0]['is_primary']); |
| 276 | $this->assertEquals(12334, $phone['values'][0]['phone']); |
| 277 | $this->assertEquals(1, $phone['values'][1]['location_type_id']); |
| 278 | $this->assertEquals(1, $phone['values'][1]['is_primary']); |
| 279 | $this->assertEquals(4444, $phone['values'][1]['phone']); |
| 280 | |
| 281 | $this->callAPISuccess('Contact', 'delete', array('id' => $contact['id'])); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Test that the import parser updates the address on the existing primary location. |
| 286 | * |
| 287 | * @throws \Exception |
| 288 | */ |
| 289 | public function testImportPrimaryAddressUpdate() { |
| 290 | list($contactValues) = $this->setUpBaseContact(array('external_identifier' => 'android')); |
| 291 | $contactValues['nick_name'] = 'Old Bill'; |
| 292 | $contactValues['external_identifier'] = 'android'; |
| 293 | $contactValues['street_address'] = 'Big Mansion'; |
| 294 | $contactValues['city'] = 'Big City'; |
| 295 | $contactID = $this->callAPISuccessGetValue('Contact', array('external_identifier' => 'android', 'return' => 'id')); |
| 296 | $originalAddress = $this->callAPISuccess('Address', 'create', array('location_type_id' => 2, 'street_address' => 'small house', 'contact_id' => $contactID)); |
| 297 | $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, array(0 => NULL, 1 => NULL, 2 => NULL, 3 => NULL, 4 => NULL, 5 => 'Primary', 6 => 'Primary')); |
| 298 | $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion')); |
| 299 | $this->assertEquals(2, $address['location_type_id']); |
| 300 | $this->assertEquals($originalAddress['id'], $address['id']); |
| 301 | $this->assertEquals('Big City', $address['city']); |
| 302 | $this->callAPISuccessGetSingle('Contact', $contactValues); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Run the import parser. |
| 307 | * |
| 308 | * @param array $originalValues |
| 309 | * |
| 310 | * @param int $onDuplicateAction |
| 311 | * @param int $expectedResult |
| 312 | * @param array|null $mapperLocType |
| 313 | * @param array|null $fields |
| 314 | * Array of field names. Will be calculated from $originalValues if not passed in, but |
| 315 | * that method does not cope with duplicates. |
| 316 | */ |
| 317 | protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = NULL, $fields = NULL) { |
| 318 | if (!$fields) { |
| 319 | $fields = array_keys($originalValues); |
| 320 | } |
| 321 | $values = array_values($originalValues); |
| 322 | $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType); |
| 323 | $parser->_contactType = 'Individual'; |
| 324 | $parser->_onDuplicate = $onDuplicateAction; |
| 325 | $parser->init(); |
| 326 | $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected'); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Set up the underlying contact. |
| 331 | * |
| 332 | * @param array $params |
| 333 | * Optional extra parameters to set. |
| 334 | * |
| 335 | * @return array |
| 336 | * @throws \Exception |
| 337 | */ |
| 338 | protected function setUpBaseContact($params = array()) { |
| 339 | $originalValues = array_merge(array( |
| 340 | 'first_name' => 'Bill', |
| 341 | 'last_name' => 'Gates', |
| 342 | 'email' => 'bill.gates@microsoft.com', |
| 343 | 'nick_name' => 'Billy-boy', |
| 344 | ), $params); |
| 345 | $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID); |
| 346 | $result = $this->callAPISuccessGetSingle('Contact', $originalValues); |
| 347 | return array($originalValues, $result); |
| 348 | } |
| 349 | |
| 350 | } |