4 * Class CRM_Utils_Migrate_ImportExportTest
7 class CRM_Utils_Migrate_ImportExportTest
extends CiviUnitTestCase
{
8 protected $_apiversion;
10 public function setUp() {
11 $this->_apiversion
= 3;
15 public function tearDown() {
17 'civicrm_custom_group',
18 'civicrm_custom_field',
20 $this->quickCleanup($tablesToTruncate, TRUE);
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
30 public function basicXmlTestCases() {
31 // a small library which we use to describe test cases
33 $fixtures['textField'] = [
34 'name' => 'test_textfield',
36 'html_type' => 'Text',
37 'data_type' => 'String',
38 'default_value' => 'abc',
44 $fixtures['selectField'] = [
46 'label' => 'Our select field',
47 'html_type' => 'Select',
48 'data_type' => 'String',
53 // 'option_group_name' => 'our_select_field_20130818044104',
70 // the actual test cases
76 'extends' => 'Contact',
80 $fixtures['textField'],
81 // expectedXmlFilePath
82 __DIR__
. '/fixtures/Contact-text.xml',
85 /* @codingStandardsIgnoreStart
89 'extends' => 'Contact',
93 $fixtures['selectField'],
94 // expectedXmlFilePath
95 __DIR__ . '/fixtures/Contact-select.xml',
97 @codingStandardsIgnoreEnd */
100 // CustomGroup params
102 'extends' => 'Individual',
103 'title' => 'example',
105 // CustomField params
106 $fixtures['textField'],
107 // expectedXmlFilePath
108 __DIR__
. '/fixtures/Individual-text.xml',
112 // CustomGroup params
114 'extends' => 'Individual',
115 'extends_entity_column_value' => ['Student'],
116 'title' => 'example',
118 // CustomField params
119 $fixtures['textField'],
120 // expectedXmlFilePath
121 __DIR__
. '/fixtures/IndividualStudent-text.xml',
125 // CustomGroup params
127 'extends' => 'Activity',
128 'title' => 'example',
130 // CustomField params
131 $fixtures['textField'],
132 // expectedXmlFilePath
133 __DIR__
. '/fixtures/Activity-text.xml',
137 // CustomGroup params
139 'extends' => 'Activity',
140 'extends_entity_column_value' => [1],
141 'title' => 'example',
143 // CustomField params
144 $fixtures['textField'],
145 // expectedXmlFilePath
146 __DIR__
. '/fixtures/ActivityMeeting-text.xml',
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.
156 * @param array $customGroupParams
157 * @param array $fieldParams
158 * @param $expectedXmlFilePath
159 * @dataProvider basicXmlTestCases
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'],
165 $customGroup = $this->customGroupCreate($customGroupParams);
166 $fieldParams['custom_group_id'] = $customGroup['id'];
167 $customField = $this->callAPISuccess('custom_field', 'create', $fieldParams);
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());
174 $this->callAPISuccess('custom_field', 'delete', ['id' => $customField['id']]);
175 $this->callAPISuccess('custom_group', 'delete', ['id' => $customGroup['id']]);
179 * @param $expectCustomGroup
180 * @param $expectCustomField
181 * @param $inputXmlFilePath
183 * @throws CRM_Core_Exception
184 * @dataProvider basicXmlTestCases
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'],
191 $importer = new CRM_Utils_Migrate_Import();
192 $importer->run($inputXmlFilePath);
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]);
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]);