3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * File for the CRM_Contact_Imports_Parser_ContactTest class.
34 * Test contact import parser.
39 class CRM_Contact_Imports_Parser_ContactTest
extends CiviUnitTestCase
{
40 protected $_tablesToTruncate = array();
45 public function setUp() {
50 * Test import parser will update based on a rule match.
52 * In this case the contact has no external identifier.
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);
66 * Test import parser will update contacts with an external identifier.
68 * This is the basic test where the identifier matches the import parameters.
72 public function testImportParserWithUpdateWithExternalIdentifier() {
73 list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
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']);
78 $originalValues['nick_name'] = 'Old Bill';
79 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
80 $originalValues['id'] = $result['id'];
82 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
83 $this->callAPISuccessGetSingle('Contact', $originalValues);
87 * Test import parser will fallback to external identifier.
89 * In this case no primary match exists (e.g the details are not supplied) so it falls back on external identifier.
95 public function testImportParserWithUpdateWithExternalIdentifierButNoPrimaryMatch() {
96 list($originalValues, $result) = $this->setUpBaseContact(array(
97 'external_identifier' => 'windows',
101 $this->assertEquals('windows', $result['external_identifier']);
103 $originalValues['nick_name'] = 'Old Bill';
104 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
105 $originalValues['id'] = $result['id'];
107 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
108 $this->callAPISuccessGetSingle('Contact', $originalValues);
112 * Test that the import parser adds the external identifier where none is set.
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);
127 * Test that the import parser changes the external identifier when there is a dedupe match.
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);
143 * Run the import parser.
145 * @param array $originalValues
147 * @param int $onDuplicateAction
148 * @param int $expectedResult
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;
157 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values), 'Return code from parser import was not as expected');
161 * Set up the underlying contact.
163 * @param array $params
164 * Optional extra parameters to set.
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',
176 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
177 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
178 return array($originalValues, $result);