Merge pull request #10603 from JMAConsulting/CRM-20640
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Parser / ContactTest.php
CommitLineData
e87ff4ce 1<?php
2/*
3+--------------------------------------------------------------------+
3435af9a 4| CiviCRM version 4.7 |
e87ff4ce 5+--------------------------------------------------------------------+
15a4309a 6| Copyright CiviCRM LLC (c) 2004-2017 |
e87ff4ce 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
e87ff4ce 33/**
34 * Test contact import parser.
35 *
36 * @package CiviCRM
acb109b7 37 * @group headless
e87ff4ce 38 */
39class 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 /**
8fd37b20 50 * Test import parser will update based on a rule match.
51 *
52 * In this case the contact has no external identifier.
e87ff4ce 53 *
54 * @throws \Exception
55 */
56 public function testImportParserWithUpdateWithoutExternalIdentifier() {
8fd37b20 57 list($originalValues, $result) = $this->setUpBaseContact();
e87ff4ce 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 /**
8fd37b20 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.
e87ff4ce 69 *
70 * @throws \Exception
71 */
72 public function testImportParserWithUpdateWithExternalIdentifier() {
8fd37b20 73 list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
74
e87ff4ce 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']);
8fd37b20 77
e87ff4ce 78 $originalValues['nick_name'] = 'Old Bill';
79 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
80 $originalValues['id'] = $result['id'];
8fd37b20 81
e87ff4ce 82 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
83 $this->callAPISuccessGetSingle('Contact', $originalValues);
84 }
85
65070890 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
eb5f7260 111 /**
8fd37b20 112 * Test that the import parser adds the external identifier where none is set.
eb5f7260 113 *
114 * @throws \Exception
115 */
8fd37b20 116 public function testImportParserWithUpdateWithNoExternalIdentifier() {
117 list($originalValues, $result) = $this->setUpBaseContact();
eb5f7260 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
65070890 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
10741f35 142 /**
143 * Test that the import parser changes the external identifier when there is a dedupe match.
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 $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));
153 $address = $this->callAPISuccessGetSingle('Address', array('street_address' => 'Big Mansion'));
154 $this->assertEquals(2, $address['location_type_id']);
155
156 $this->callAPISuccessGetSingle('Contact', $contactValues);
157 }
158
e87ff4ce 159 /**
160 * Run the import parser.
161 *
162 * @param array $originalValues
163 *
164 * @param int $onDuplicateAction
165 * @param int $expectedResult
10741f35 166 * @param array|null $mapperLocType
e87ff4ce 167 */
10741f35 168 protected function runImport($originalValues, $onDuplicateAction, $expectedResult, $mapperLocType = NULL) {
e87ff4ce 169 $fields = array_keys($originalValues);
170 $values = array_values($originalValues);
10741f35 171 $parser = new CRM_Contact_Import_Parser_Contact($fields, $mapperLocType);
e87ff4ce 172 $parser->_contactType = 'Individual';
173 $parser->_onDuplicate = $onDuplicateAction;
174 $parser->init();
65070890 175 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
e87ff4ce 176 }
ffe87781 177
8fd37b20 178 /**
179 * Set up the underlying contact.
180 *
181 * @param array $params
182 * Optional extra parameters to set.
183 *
184 * @return array
185 * @throws \Exception
186 */
187 protected function setUpBaseContact($params = array()) {
188 $originalValues = array_merge(array(
189 'first_name' => 'Bill',
190 'last_name' => 'Gates',
191 'email' => 'bill.gates@microsoft.com',
192 'nick_name' => 'Billy-boy',
193 ), $params);
194 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
195 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
196 return array($originalValues, $result);
197 }
198
e87ff4ce 199}