CRM-17275 preliminary tidy-ups, remove unused vars & fix comments & re-use test code
[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-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 import parser will update based on a rule match.
52 *
53 * In this case the contact has no external identifier.
54 *
55 * @throws \Exception
56 */
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);
64 }
65
66 /**
67 * Test import parser will update contacts with an external identifier.
68 *
69 * This is the basic test where the identifier matches the import parameters.
70 *
71 * @throws \Exception
72 */
73 public function testImportParserWithUpdateWithExternalIdentifier() {
74 list($originalValues, $result) = $this->setUpBaseContact(array('external_identifier' => 'windows'));
75
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']);
78
79 $originalValues['nick_name'] = 'Old Bill';
80 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
81 $originalValues['id'] = $result['id'];
82
83 $this->assertEquals('Old Bill', $this->callAPISuccessGetValue('Contact', array('id' => $result['id'], 'return' => 'nick_name')));
84 $this->callAPISuccessGetSingle('Contact', $originalValues);
85 }
86
87 /**
88 * Test that the import parser adds the external identifier where none is set.
89 *
90 * @throws \Exception
91 */
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);
100 }
101
102 /**
103 * Run the import parser.
104 *
105 * @param array $originalValues
106 *
107 * @param int $onDuplicateAction
108 * @param int $expectedResult
109 */
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;
116 $parser->init();
117 $this->assertEquals($expectedResult, $parser->import($onDuplicateAction, $values));
118 }
119
120 /**
121 * Set up the underlying contact.
122 *
123 * @param array $params
124 * Optional extra parameters to set.
125 *
126 * @return array
127 * @throws \Exception
128 */
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',
135 ), $params);
136 $this->runImport($originalValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID);
137 $result = $this->callAPISuccessGetSingle('Contact', $originalValues);
138 return array($originalValues, $result);
139 }
140
141 }