Merge pull request #14934 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Contact / Import / Form / MapFieldTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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_Import_Form_MapFieldTest class.
31 */
32
33 /**
34 * Test contact import mapfield.
35 *
36 * @package CiviCRM
37 * @group headless
38 */
39 class CRM_Contact_Import_Form_MapFieldTest extends CiviUnitTestCase {
40
41 /**
42 * Test the form loads without error / notice and mappings are assigned.
43 *
44 * (Added in conjunction with fixed noting on mapping assignment).
45 *
46 * @dataProvider getSubmitData
47 *
48 * @param array $params
49 * @param array $mapper
50 * @param array $expecteds
51 */
52 public function testSubmit($params, $mapper, $expecteds = []) {
53 CRM_Core_DAO::executeQuery("CREATE TABLE IF NOT EXISTS civicrm_import_job_xxx (`nada` text, `first_name` text, `last_name` text, `address` text) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci");
54 $form = $this->getFormObject('CRM_Contact_Import_Form_MapField');
55 $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
56 $form->_columnNames = ['nada', 'first_name', 'last_name', 'address'];
57 $form->set('importTableName', 'civicrm_import_job_xxx');
58 $form->preProcess();
59 $form->submit($params, $mapper);
60
61 CRM_Core_DAO::executeQuery("DROP TABLE civicrm_import_job_xxx");
62 if (!empty($expecteds)) {
63 foreach ($expecteds as $expected) {
64 $result = $this->callAPISuccess($expected['entity'], 'get', array_merge($expected['values'], ['sequential' => 1]));
65 $this->assertEquals($expected['count'], $result['count']);
66 if (isset($expected['result'])) {
67 foreach ($expected['result'] as $key => $expectedValues) {
68 foreach ($expectedValues as $valueKey => $value) {
69 $this->assertEquals($value, $result['values'][$key][$valueKey]);
70 }
71 }
72 }
73 }
74 }
75 $this->quickCleanup(['civicrm_mapping', 'civicrm_mapping_field']);
76 }
77
78 /**
79 * Get data to pass through submit function.
80 *
81 * @return array
82 */
83 public function getSubmitData() {
84 return [
85 'basic_data' => [
86 [
87 'saveMappingName' => '',
88 'saveMappingDesc' => '',
89 ],
90 [
91 0 => [0 => 'do_not_import'],
92 1 => [0 => 'first_name'],
93 2 => [0 => 'last_name'],
94 3 => [0 => 'street_address', 1 => 2],
95 ],
96 ],
97 'save_mapping' => [
98 [
99 'saveMappingName' => 'new mapping',
100 'saveMappingDesc' => 'save it',
101 'saveMapping' => 1,
102 ],
103 [
104 0 => [0 => 'do_not_import'],
105 1 => [0 => 'first_name'],
106 2 => [0 => 'last_name'],
107 3 => [0 => 'street_address', 1 => 2],
108 ],
109 [
110 ['entity' => 'mapping', 'count' => 1, 'values' => ['name' => 'new mapping']],
111 [
112 'entity' =>
113 'mapping_field',
114 'count' => 4,
115 'values' => [],
116 'result' => [
117 0 => ['name' => '- do not import -'],
118 1 => ['name' => 'First Name'],
119 2 => ['name' => 'Last Name'],
120 3 => ['name' => 'Street Address', 'location_type_id' => 2],
121 ],
122 ],
123 ],
124 ],
125 ];
126 }
127
128 /**
129 * Instantiate form object
130 *
131 * @param string $class
132 * @param array $formValues
133 * @param string $pageName
134 * @return \CRM_Core_Form
135 * @throws \CRM_Core_Exception
136 */
137 public function getFormObject($class, $formValues = [], $pageName = '') {
138 $form = parent::getFormObject($class);
139 $contactFields = CRM_Contact_BAO_Contact::importableFields();
140 $fields = [];
141 foreach ($contactFields as $name => $field) {
142 $fields[$name] = $field['title'];
143 }
144 $form->set('fields', $fields);
145 return $form;
146 }
147
148 }