[PHPUNIT 8 ] Add void to more teardowns
[civicrm-core.git] / tests / phpunit / CRM / Utils / Migrate / ImportExportTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_Migrate_ImportExportTest
5 * @group headless
6 */
7 class CRM_Utils_Migrate_ImportExportTest extends CiviUnitTestCase {
8 protected $_apiversion;
9
10 public function setUp() {
11 $this->_apiversion = 3;
12 parent::setUp();
13 }
14
15 public function tearDown(): void {
16 $tablesToTruncate = [
17 'civicrm_custom_group',
18 'civicrm_custom_field',
19 ];
20 $this->quickCleanup($tablesToTruncate, TRUE);
21 }
22
23 /**
24 * Generate a list of basic XML test cases. Each test case creates a
25 * custom-group and custom-field then compares the output to a pre-defined
26 * XML file. Then, for each test-case, we reverse the process -- we
27 * load the XML into a clean DB and see if it creates matching custom-group
28 * and custom-field.
29 */
30 public function basicXmlTestCases() {
31 // a small library which we use to describe test cases
32 $fixtures = [];
33 $fixtures['textField'] = [
34 'name' => 'test_textfield',
35 'label' => 'Name1',
36 'html_type' => 'Text',
37 'data_type' => 'String',
38 'default_value' => 'abc',
39 'weight' => 4,
40 'is_required' => 1,
41 'is_searchable' => 0,
42 'is_active' => 1,
43 ];
44 $fixtures['selectField'] = [
45 // custom_group_id
46 'label' => 'Our select field',
47 'html_type' => 'Select',
48 'data_type' => 'String',
49 'weight' => 4,
50 'is_required' => 1,
51 'is_searchable' => 0,
52 'is_active' => 1,
53 // 'option_group_name' => 'our_select_field_20130818044104',
54 'option_values' => [
55 [
56 'weight' => 1,
57 'label' => 'Label1',
58 'value' => 1,
59 'is_active' => 1,
60 ],
61 [
62 'weight' => 2,
63 'label' => 'Label2',
64 'value' => 2,
65 'is_active' => 1,
66 ],
67 ],
68 ];
69
70 // the actual test cases
71 $cases = [];
72
73 $cases[] = [
74 // CustomGroup params
75 [
76 'extends' => 'Contact',
77 'title' => 'example',
78 ],
79 // CustomField params
80 $fixtures['textField'],
81 // expectedXmlFilePath
82 __DIR__ . '/fixtures/Contact-text.xml',
83 ];
84
85 /* @codingStandardsIgnoreStart
86 $cases[] = array(
87 // CustomGroup params
88 array(
89 'extends' => 'Contact',
90 'title' => 'example',
91 ),
92 // CustomField params
93 $fixtures['selectField'],
94 // expectedXmlFilePath
95 __DIR__ . '/fixtures/Contact-select.xml',
96 );
97 @codingStandardsIgnoreEnd */
98
99 $cases[] = [
100 // CustomGroup params
101 [
102 'extends' => 'Individual',
103 'title' => 'example',
104 ],
105 // CustomField params
106 $fixtures['textField'],
107 // expectedXmlFilePath
108 __DIR__ . '/fixtures/Individual-text.xml',
109 ];
110
111 $cases[] = [
112 // CustomGroup params
113 [
114 'extends' => 'Individual',
115 'extends_entity_column_value' => ['Student'],
116 'title' => 'example',
117 ],
118 // CustomField params
119 $fixtures['textField'],
120 // expectedXmlFilePath
121 __DIR__ . '/fixtures/IndividualStudent-text.xml',
122 ];
123
124 $cases[] = [
125 // CustomGroup params
126 [
127 'extends' => 'Activity',
128 'title' => 'example',
129 ],
130 // CustomField params
131 $fixtures['textField'],
132 // expectedXmlFilePath
133 __DIR__ . '/fixtures/Activity-text.xml',
134 ];
135
136 $cases[] = [
137 // CustomGroup params
138 [
139 'extends' => 'Activity',
140 'extends_entity_column_value' => [1],
141 'title' => 'example',
142 ],
143 // CustomField params
144 $fixtures['textField'],
145 // expectedXmlFilePath
146 __DIR__ . '/fixtures/ActivityMeeting-text.xml',
147 ];
148
149 return $cases;
150 }
151
152 /**
153 * Execute a basic XML test case. Each test case creates a custom-group and
154 * custom-field then compares the output to a pre-defined XML file.
155 *
156 * @param array $customGroupParams
157 * @param array $fieldParams
158 * @param $expectedXmlFilePath
159 * @dataProvider basicXmlTestCases
160 */
161 public function testBasicXMLExports($customGroupParams, $fieldParams, $expectedXmlFilePath) {
162 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', [
163 1 => [$customGroupParams['title'], 'String'],
164 ]);
165 $customGroup = $this->customGroupCreate($customGroupParams);
166 $fieldParams['custom_group_id'] = $customGroup['id'];
167 $customField = $this->callAPISuccess('custom_field', 'create', $fieldParams);
168
169 $exporter = new CRM_Utils_Migrate_Export();
170 $exporter->buildCustomGroups([$customGroup['id']]);
171 // print $exporter->toXML();
172 $this->assertEquals(file_get_contents($expectedXmlFilePath), $exporter->toXML());
173
174 $this->callAPISuccess('custom_field', 'delete', ['id' => $customField['id']]);
175 $this->callAPISuccess('custom_group', 'delete', ['id' => $customGroup['id']]);
176 }
177
178 /**
179 * @param $expectCustomGroup
180 * @param $expectCustomField
181 * @param $inputXmlFilePath
182 *
183 * @throws CRM_Core_Exception
184 * @dataProvider basicXmlTestCases
185 */
186 public function testBasicXMLImports($expectCustomGroup, $expectCustomField, $inputXmlFilePath) {
187 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', [
188 1 => [$expectCustomGroup['title'], 'String'],
189 ]);
190
191 $importer = new CRM_Utils_Migrate_Import();
192 $importer->run($inputXmlFilePath);
193
194 $customGroups = $this->callAPISuccess('custom_group', 'get', ['title' => $expectCustomGroup['title']]);
195 $this->assertEquals(1, $customGroups['count']);
196 $customGroup = array_shift($customGroups['values']);
197 foreach ($expectCustomGroup as $expectKey => $expectValue) {
198 $this->assertEquals($expectValue, $customGroup[$expectKey]);
199 }
200
201 $customFields = $this->callAPISuccess('custom_field', 'get', ['label' => $expectCustomField['label']]);
202 $this->assertEquals(1, $customFields['count']);
203 $customField = array_shift($customFields['values']);
204 foreach ($expectCustomField as $expectKey => $expectValue) {
205 $this->assertEquals($expectValue, $customField[$expectKey]);
206 }
207 }
208
209 }