3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 that the import parser will do an update when external identifier is set.
55 public function testImportParserWithUpdateWithoutExternalIdentifier() {
56 $originalValues = array(
57 'first_name' => 'Bill',
58 'last_name' => 'Gates',
59 'email' => 'bill.gates@microsoft.com',
60 'nick_name' => 'Billy-boy',
62 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
63 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
64 $originalValues['nick_name'] = 'Old Bill';
65 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
66 $originalValues['id'] = $result['id'];
67 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
68 $this->callAPISuccessGetSingle('Contact', $originalValues);
72 * Test that the import parser will do an update when external identifier is set.
76 public function testImportParserWithUpdateWithExternalIdentifier() {
77 $originalValues = array(
78 'first_name' => 'Bill',
79 'last_name' => 'Gates',
80 'email' => 'bill.gates@microsoft.com',
81 'external_identifier' => 'windows',
82 'nick_name' => 'Billy-boy',
84 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
85 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
86 $this->assertEquals($result['id'], CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', 'windows', 'id', 'external_identifier', TRUE));
87 $this->assertEquals('windows', $result['external_identifier']);
88 $originalValues['nick_name'] = 'Old Bill';
89 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
90 $originalValues['id'] = $result['id'];
91 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
92 $this->callAPISuccessGetSingle('Contact', $originalValues);
96 * Test that the import parser updates when a new external identifier is set.
100 public function testImportParserWithUpdateWithNewExternalIdentifier() {
101 $originalValues = array(
102 'first_name' => 'Bill',
103 'last_name' => 'Gates',
104 'email' => 'bill.gates@microsoft.com',
105 'nick_name' => 'Billy-boy',
107 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
108 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
109 $originalValues['nick_name'] = 'Old Bill';
110 $originalValues['external_identifier'] = 'windows';
111 $this->runImport($originalValues, CRM_Import_Parser
::DUPLICATE_UPDATE
, CRM_Import_Parser
::VALID
);
112 $originalValues['id'] = $result['id'];
113 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
114 $this->callAPISuccessGetSingle('Contact', $originalValues);
118 * Run the import parser.
120 * @param array $originalValues
122 * @param int $onDuplicateAction
123 * @param int $expectedResult
125 protected function runImport($originalValues, $onDuplicateAction, $expectedResult) {
126 $fields = array_keys($originalValues);
127 $values = array_values($originalValues);
128 $parser = new CRM_Contact_Import_Parser_Contact($fields);
129 $parser->_contactType
= 'Individual';
130 $parser->_onDuplicate
= $onDuplicateAction;
132 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values));