3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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.
33 require_once 'CiviTest/CiviUnitTestCase.php';
36 * Test contact import parser.
40 class CRM_Contact_Imports_Parser_ContactTest
extends CiviUnitTestCase
{
41 protected $_tablesToTruncate = array();
46 public function setUp() {
51 * Test import parser will update based on a rule match.
53 * In this case the contact has no external identifier.
57 public function testImportParserWithUpdateWithoutExternalIdentifier() {
58 list($originalValues, $result) = $this->setUpBaseContact();
59 $originalValues['nick_name'] = 'Old Bill';
60 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
61 $originalValues['id'] = $result['id'];
62 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
63 $this->callAPISuccessGetSingle('Contact', $originalValues);
67 * Test import parser will update contacts with an external identifier.
69 * This is the basic test where the identifier matches the import parameters.
73 public function testImportParserWithUpdateWithExternalIdentifier() {
74 list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
76 $this->assertEquals($result['id'], CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE));
77 $this->assertEquals('windows', $result['external_identifier']);
79 $originalValues['nick_name'] = 'Old Bill';
80 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
81 $originalValues['id'] = $result['id'];
83 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
84 $this->callAPISuccessGetSingle('Contact', $originalValues);
88 * Test that the import parser adds the external identifier where none is set.
92 public function testImportParserWithUpdateWithNoExternalIdentifier() {
93 list($originalValues, $result) = $this->setUpBaseContact();
94 $originalValues['nick_name'] = 'Old Bill';
95 $originalValues['external_identifier'] = 'windows';
96 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
97 $originalValues['id'] = $result['id'];
98 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
99 $this->callAPISuccessGetSingle('Contact', $originalValues);
103 * Run the import parser.
105 * @param array $originalValues
107 * @param int $onDuplicateAction
108 * @param int $expectedResult
110 protected function runImport($originalValues, $onDuplicateAction, $expectedResult) {
111 $fields = array_keys($originalValues);
112 $values = array_values($originalValues);
113 $parser = new CRM_Contact_Import_Parser_Contact($fields);
114 $parser->_contactType
= 'Individual';
115 $parser->_onDuplicate
= $onDuplicateAction;
117 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values));
121 * Set up the underlying contact.
123 * @param array $params
124 * Optional extra parameters to set.
129 protected function setUpBaseContact($params = array()) {
130 $originalValues = array_merge(array(
131 'first_name' => 'Bill',
132 'last_name' => 'Gates',
133 'email' => 'bill.gates@microsoft.com',
134 'nick_name' => 'Billy-boy',
136 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
137 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
138 return array($originalValues, $result);