[NFC] code tidy ups
[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 map field.
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 * @throws \CRM_Core_Exception
53 * @throws \CiviCRM_API3_Exception
54 */
55 public function testSubmit($params, $mapper, $expecteds = []) {
56 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');
57 $form = $this->getFormObject('CRM_Contact_Import_Form_MapField');
58 /* @var CRM_Contact_Import_Form_MapField $form */
59 $form->set('contactType', CRM_Import_Parser::CONTACT_INDIVIDUAL);
60 $form->_columnNames = ['nada', 'first_name', 'last_name', 'address'];
61 $form->set('importTableName', 'civicrm_import_job_xxx');
62 $form->preProcess();
63 $form->submit($params, $mapper);
64
65 CRM_Core_DAO::executeQuery("DROP TABLE civicrm_import_job_xxx");
66 if (!empty($expecteds)) {
67 foreach ($expecteds as $expected) {
68 $result = $this->callAPISuccess($expected['entity'], 'get', array_merge($expected['values'], ['sequential' => 1]));
69 $this->assertEquals($expected['count'], $result['count']);
70 if (isset($expected['result'])) {
71 foreach ($expected['result'] as $key => $expectedValues) {
72 foreach ($expectedValues as $valueKey => $value) {
73 $this->assertEquals($value, $result['values'][$key][$valueKey]);
74 }
75 }
76 }
77 }
78 }
79 $this->quickCleanup(['civicrm_mapping', 'civicrm_mapping_field']);
80 }
81
82 /**
83 * Get data to pass through submit function.
84 *
85 * @return array
86 */
87 public function getSubmitData() {
88 return [
89 'basic_data' => [
90 [
91 'saveMappingName' => '',
92 'saveMappingDesc' => '',
93 ],
94 [
95 0 => [0 => 'do_not_import'],
96 1 => [0 => 'first_name'],
97 2 => [0 => 'last_name'],
98 3 => [0 => 'street_address', 1 => 2],
99 ],
100 ],
101 'save_mapping' => [
102 [
103 'saveMappingName' => 'new mapping',
104 'saveMappingDesc' => 'save it',
105 'saveMapping' => 1,
106 ],
107 [
108 0 => [0 => 'do_not_import'],
109 1 => [0 => 'first_name'],
110 2 => [0 => 'last_name'],
111 3 => [0 => 'street_address', 1 => 2],
112 ],
113 [
114 ['entity' => 'mapping', 'count' => 1, 'values' => ['name' => 'new mapping']],
115 [
116 'entity' =>
117 'mapping_field',
118 'count' => 4,
119 'values' => [],
120 'result' => [
121 0 => ['name' => '- do not import -'],
122 1 => ['name' => 'First Name'],
123 2 => ['name' => 'Last Name'],
124 3 => ['name' => 'Street Address', 'location_type_id' => 2],
125 ],
126 ],
127 ],
128 ],
129 ];
130 }
131
132 /**
133 * Instantiate form object
134 *
135 * @param string $class
136 * @param array $formValues
137 * @param string $pageName
138 * @return \CRM_Core_Form
139 * @throws \CRM_Core_Exception
140 */
141 public function getFormObject($class, $formValues = [], $pageName = '') {
142 $form = parent::getFormObject($class);
143 $contactFields = CRM_Contact_BAO_Contact::importableFields();
144 $fields = [];
145 foreach ($contactFields as $name => $field) {
146 $fields[$name] = $field['title'];
147 }
148 $form->set('fields', $fields);
149 return $form;
150 }
151
152 }