Commit | Line | Data |
---|---|---|
eac4fc12 | 1 | <?php |
aba1cd8b EM |
2 | |
3 | /** | |
4 | * Class CRM_Utils_Migrate_ImportExportTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
bc24cdad | 7 | class CRM_Utils_Migrate_ImportExportTest extends CiviUnitTestCase { |
eac4fc12 | 8 | protected $_apiversion; |
eac4fc12 | 9 | |
00be9182 | 10 | public function setUp() { |
eac4fc12 TO |
11 | $this->_apiversion = 3; |
12 | parent::setUp(); | |
13 | } | |
14 | ||
00be9182 | 15 | public function tearDown() { |
9099cab3 | 16 | $tablesToTruncate = [ |
eac4fc12 TO |
17 | 'civicrm_custom_group', |
18 | 'civicrm_custom_field', | |
9099cab3 | 19 | ]; |
eac4fc12 TO |
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 | |
0f882f9b TO |
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. | |
eac4fc12 | 29 | */ |
00be9182 | 30 | public function basicXmlTestCases() { |
eac4fc12 | 31 | // a small library which we use to describe test cases |
9099cab3 CW |
32 | $fixtures = []; |
33 | $fixtures['textField'] = [ | |
eac4fc12 TO |
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, | |
9099cab3 CW |
43 | ]; |
44 | $fixtures['selectField'] = [ | |
eac4fc12 TO |
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', | |
9099cab3 CW |
54 | 'option_values' => [ |
55 | [ | |
eac4fc12 TO |
56 | 'weight' => 1, |
57 | 'label' => 'Label1', | |
58 | 'value' => 1, | |
59 | 'is_active' => 1, | |
9099cab3 CW |
60 | ], |
61 | [ | |
eac4fc12 TO |
62 | 'weight' => 2, |
63 | 'label' => 'Label2', | |
64 | 'value' => 2, | |
65 | 'is_active' => 1, | |
9099cab3 CW |
66 | ], |
67 | ], | |
68 | ]; | |
eac4fc12 TO |
69 | |
70 | // the actual test cases | |
9099cab3 | 71 | $cases = []; |
eac4fc12 | 72 | |
9099cab3 | 73 | $cases[] = [ |
eac4fc12 | 74 | // CustomGroup params |
9099cab3 | 75 | [ |
eac4fc12 | 76 | 'extends' => 'Contact', |
0f882f9b | 77 | 'title' => 'example', |
9099cab3 | 78 | ], |
eac4fc12 TO |
79 | // CustomField params |
80 | $fixtures['textField'], | |
81 | // expectedXmlFilePath | |
82 | __DIR__ . '/fixtures/Contact-text.xml', | |
9099cab3 | 83 | ]; |
eac4fc12 | 84 | |
e70a7fc0 | 85 | /* @codingStandardsIgnoreStart |
eac4fc12 TO |
86 | $cases[] = array( |
87 | // CustomGroup params | |
88 | array( | |
89 | 'extends' => 'Contact', | |
0f882f9b | 90 | 'title' => 'example', |
eac4fc12 TO |
91 | ), |
92 | // CustomField params | |
93 | $fixtures['selectField'], | |
94 | // expectedXmlFilePath | |
95 | __DIR__ . '/fixtures/Contact-select.xml', | |
96 | ); | |
e70a7fc0 | 97 | @codingStandardsIgnoreEnd */ |
eac4fc12 | 98 | |
9099cab3 | 99 | $cases[] = [ |
eac4fc12 | 100 | // CustomGroup params |
9099cab3 | 101 | [ |
eac4fc12 | 102 | 'extends' => 'Individual', |
0f882f9b | 103 | 'title' => 'example', |
9099cab3 | 104 | ], |
eac4fc12 TO |
105 | // CustomField params |
106 | $fixtures['textField'], | |
107 | // expectedXmlFilePath | |
108 | __DIR__ . '/fixtures/Individual-text.xml', | |
9099cab3 | 109 | ]; |
eac4fc12 | 110 | |
9099cab3 | 111 | $cases[] = [ |
eac4fc12 | 112 | // CustomGroup params |
9099cab3 | 113 | [ |
eac4fc12 | 114 | 'extends' => 'Individual', |
9099cab3 | 115 | 'extends_entity_column_value' => ['Student'], |
0f882f9b | 116 | 'title' => 'example', |
9099cab3 | 117 | ], |
eac4fc12 TO |
118 | // CustomField params |
119 | $fixtures['textField'], | |
120 | // expectedXmlFilePath | |
121 | __DIR__ . '/fixtures/IndividualStudent-text.xml', | |
9099cab3 | 122 | ]; |
eac4fc12 | 123 | |
9099cab3 | 124 | $cases[] = [ |
eac4fc12 | 125 | // CustomGroup params |
9099cab3 | 126 | [ |
eac4fc12 | 127 | 'extends' => 'Activity', |
0f882f9b | 128 | 'title' => 'example', |
9099cab3 | 129 | ], |
eac4fc12 TO |
130 | // CustomField params |
131 | $fixtures['textField'], | |
132 | // expectedXmlFilePath | |
133 | __DIR__ . '/fixtures/Activity-text.xml', | |
9099cab3 | 134 | ]; |
eac4fc12 | 135 | |
9099cab3 | 136 | $cases[] = [ |
eac4fc12 | 137 | // CustomGroup params |
9099cab3 | 138 | [ |
eac4fc12 | 139 | 'extends' => 'Activity', |
ead0c08f | 140 | 'extends_entity_column_value' => [1], |
0f882f9b | 141 | 'title' => 'example', |
9099cab3 | 142 | ], |
eac4fc12 TO |
143 | // CustomField params |
144 | $fixtures['textField'], | |
145 | // expectedXmlFilePath | |
146 | __DIR__ . '/fixtures/ActivityMeeting-text.xml', | |
9099cab3 | 147 | ]; |
eac4fc12 TO |
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 | * | |
100fef9d CW |
156 | * @param array $customGroupParams |
157 | * @param array $fieldParams | |
eac4fc12 TO |
158 | * @param $expectedXmlFilePath |
159 | * @dataProvider basicXmlTestCases | |
160 | */ | |
00be9182 | 161 | public function testBasicXMLExports($customGroupParams, $fieldParams, $expectedXmlFilePath) { |
9099cab3 CW |
162 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', [ |
163 | 1 => [$customGroupParams['title'], 'String'], | |
164 | ]); | |
eac4fc12 TO |
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(); | |
9099cab3 | 170 | $exporter->buildCustomGroups([$customGroup['id']]); |
eac4fc12 TO |
171 | // print $exporter->toXML(); |
172 | $this->assertEquals(file_get_contents($expectedXmlFilePath), $exporter->toXML()); | |
173 | ||
9099cab3 CW |
174 | $this->callAPISuccess('custom_field', 'delete', ['id' => $customField['id']]); |
175 | $this->callAPISuccess('custom_group', 'delete', ['id' => $customGroup['id']]); | |
eac4fc12 | 176 | } |
0f882f9b TO |
177 | |
178 | /** | |
2a6da8d7 EM |
179 | * @param $expectCustomGroup |
180 | * @param $expectCustomField | |
181 | * @param $inputXmlFilePath | |
182 | * | |
183 | * @throws CRM_Core_Exception | |
0f882f9b TO |
184 | * @dataProvider basicXmlTestCases |
185 | */ | |
00be9182 | 186 | public function testBasicXMLImports($expectCustomGroup, $expectCustomField, $inputXmlFilePath) { |
9099cab3 CW |
187 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', [ |
188 | 1 => [$expectCustomGroup['title'], 'String'], | |
189 | ]); | |
0f882f9b TO |
190 | |
191 | $importer = new CRM_Utils_Migrate_Import(); | |
192 | $importer->run($inputXmlFilePath); | |
193 | ||
9099cab3 | 194 | $customGroups = $this->callAPISuccess('custom_group', 'get', ['title' => $expectCustomGroup['title']]); |
0f882f9b TO |
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 | ||
9099cab3 | 201 | $customFields = $this->callAPISuccess('custom_field', 'get', ['label' => $expectCustomField['label']]); |
0f882f9b TO |
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 | } | |
96025800 | 208 | |
2a6da8d7 | 209 | } |