Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.6 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
34 | ||
35 | /** | |
36 | * Test contact import parser. | |
37 | * | |
38 | * @package CiviCRM | |
39 | */ | |
40 | class CRM_Contact_Imports_Parser_ContactTest extends CiviUnitTestCase { | |
41 | protected $_tablesToTruncate = array(); | |
42 | ||
43 | /** | |
44 | * Setup function. | |
45 | */ | |
46 | public function setUp() { | |
47 | parent::setUp(); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Test that the import parser will do an update when external identifier is set. | |
52 | * | |
53 | * @throws \Exception | |
54 | */ | |
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', | |
61 | ); | |
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); | |
69 | } | |
70 | ||
71 | /** | |
72 | * Test that the import parser will do an update when external identifier is set. | |
73 | * | |
74 | * @throws \Exception | |
75 | */ | |
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', | |
83 | ); | |
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); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Test that the import parser updates when a new external identifier is set. | |
97 | * | |
98 | * @throws \Exception | |
99 | */ | |
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', | |
106 | ); | |
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); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Run the import parser. | |
119 | * | |
120 | * @param array $originalValues | |
121 | * | |
122 | * @param int $onDuplicateAction | |
123 | * @param int $expectedResult | |
124 | */ | |
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; | |
131 | $parser->init(); | |
132 | $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values)); | |
133 | } | |
134 | ||
135 | } |