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