4 * Class CRM_Utils_Migrate_ImportExportTest
7 class CRM_Utils_Migrate_ImportExportTest
extends CiviUnitTestCase
{
9 public function tearDown(): void
{
11 'civicrm_custom_group',
12 'civicrm_custom_field',
14 $this->quickCleanup($tablesToTruncate, TRUE);
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
24 public function basicXmlTestCases() {
25 // a small library which we use to describe test cases
27 $fixtures['textField'] = [
28 'name' => 'test_textfield',
30 'html_type' => 'Text',
31 'data_type' => 'String',
32 'default_value' => 'abc',
38 $fixtures['selectField'] = [
40 'label' => 'Our select field',
41 'html_type' => 'Select',
42 'data_type' => 'String',
47 // 'option_group_name' => 'our_select_field_20130818044104',
64 // the actual test cases
70 'extends' => 'Contact',
74 $fixtures['textField'],
75 // expectedXmlFilePath
76 __DIR__
. '/fixtures/Contact-text.xml',
79 /* @codingStandardsIgnoreStart
83 'extends' => 'Contact',
87 $fixtures['selectField'],
88 // expectedXmlFilePath
89 __DIR__ . '/fixtures/Contact-select.xml',
91 @codingStandardsIgnoreEnd */
96 'extends' => 'Individual',
100 $fixtures['textField'],
101 // expectedXmlFilePath
102 __DIR__
. '/fixtures/Individual-text.xml',
106 // CustomGroup params
108 'extends' => 'Individual',
109 'extends_entity_column_value' => ['Student'],
110 'title' => 'example',
112 // CustomField params
113 $fixtures['textField'],
114 // expectedXmlFilePath
115 __DIR__
. '/fixtures/IndividualStudent-text.xml',
119 // CustomGroup params
121 'extends' => 'Activity',
122 'title' => 'example',
124 // CustomField params
125 $fixtures['textField'],
126 // expectedXmlFilePath
127 __DIR__
. '/fixtures/Activity-text.xml',
131 // CustomGroup params
133 'extends' => 'Activity',
134 'extends_entity_column_value' => [1],
135 'title' => 'example',
137 // CustomField params
138 $fixtures['textField'],
139 // expectedXmlFilePath
140 __DIR__
. '/fixtures/ActivityMeeting-text.xml',
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.
150 * @param array $customGroupParams
151 * @param array $fieldParams
152 * @param $expectedXmlFilePath
153 * @dataProvider basicXmlTestCases
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'],
159 $customGroup = $this->customGroupCreate($customGroupParams);
160 $fieldParams['custom_group_id'] = $customGroup['id'];
161 $customField = $this->callAPISuccess('custom_field', 'create', $fieldParams);
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());
168 $this->callAPISuccess('custom_field', 'delete', ['id' => $customField['id']]);
169 $this->callAPISuccess('custom_group', 'delete', ['id' => $customGroup['id']]);
173 * @param $expectCustomGroup
174 * @param $expectCustomField
175 * @param $inputXmlFilePath
177 * @throws CRM_Core_Exception
178 * @dataProvider basicXmlTestCases
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'],
185 $importer = new CRM_Utils_Migrate_Import();
186 $importer->run($inputXmlFilePath);
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]);
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]);