Merge pull request #8525 from twomice/CRM-18251b
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Parser / ContactTest.php
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 * Run the import parser.
144 *
145 * @param array $originalValues
146 *
147 * @param int $onDuplicateAction
148 * @param int $expectedResult
149 */
150 protected function runImport($originalValues, $onDuplicateAction, $expectedResult) {
151 $fields = array_keys($originalValues);
152 $values = array_values($originalValues);
153 $parser = new CRM_Contact_Import_Parser_Contact($fields);
154 $parser->_contactType = 'Individual';
155 $parser->_onDuplicate = $onDuplicateAction;
156 $parser->init();
157 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
158 }
159
160 /**
161 * Set up the underlying contact.
162 *
163 * @param array $params
164 * Optional extra parameters to set.
165 *
166 * @return array
167 * @throws \Exception
168 */
169 protected function setUpBaseContact($params = array()) {
170 $originalValues = array_merge(array(
171 'first_name' => 'Bill',
172 'last_name' => 'Gates',
173 'email' => 'bill.gates@microsoft.com',
174 'nick_name' => 'Billy-boy',
175 ), $params);
176 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
177 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
178 return array($originalValues, $result);
179 }
180
181 }