2 require_once 'CiviTest/CiviUnitTestCase.php';
3 class CRM_Utils_Migrate_ImportExportTest
extends CiviUnitTestCase
{
4 protected $_apiversion;
7 $this->_apiversion
= 3;
12 $tablesToTruncate = array(
13 'civicrm_custom_group',
14 'civicrm_custom_field',
16 $this->quickCleanup($tablesToTruncate, TRUE);
20 * Generate a list of basic XML test cases. Each test case creates a
21 * custom-group and custom-field then compares the output to a pre-defined
22 * XML file. Then, for each test-case, we reverse the process -- we
23 * load the XML into a clean DB and see if it creates matching custom-group
26 function basicXmlTestCases() {
27 // a small library which we use to describe test cases
29 $fixtures['textField'] = array(
30 'name' => 'test_textfield',
32 'html_type' => 'Text',
33 'data_type' => 'String',
34 'default_value' => 'abc',
40 $fixtures['selectField'] = array(
42 'label' => 'Our select field',
43 'html_type' => 'Select',
44 'data_type' => 'String',
49 // 'option_group_name' => 'our_select_field_20130818044104',
50 'option_values' => array(
66 // the actual test cases
72 'extends' => 'Contact',
76 $fixtures['textField'],
77 // expectedXmlFilePath
78 __DIR__
. '/fixtures/Contact-text.xml',
85 'extends' => 'Contact',
89 $fixtures['selectField'],
90 // expectedXmlFilePath
91 __DIR__ . '/fixtures/Contact-select.xml',
98 'extends' => 'Individual',
101 // CustomField params
102 $fixtures['textField'],
103 // expectedXmlFilePath
104 __DIR__
. '/fixtures/Individual-text.xml',
108 // CustomGroup params
110 'extends' => 'Individual',
111 'extends_entity_column_value' => array('Student'),
112 'title' => 'example',
114 // CustomField params
115 $fixtures['textField'],
116 // expectedXmlFilePath
117 __DIR__
. '/fixtures/IndividualStudent-text.xml',
121 // CustomGroup params
123 'extends' => 'Activity',
124 'title' => 'example',
126 // CustomField params
127 $fixtures['textField'],
128 // expectedXmlFilePath
129 __DIR__
. '/fixtures/Activity-text.xml',
133 // CustomGroup params
135 'extends' => 'Activity',
136 'extends_entity_column_value' => array(array_search('Meeting', CRM_Core_PseudoConstant
::activityType())),
137 'title' => 'example',
139 // CustomField params
140 $fixtures['textField'],
141 // expectedXmlFilePath
142 __DIR__
. '/fixtures/ActivityMeeting-text.xml',
149 * Execute a basic XML test case. Each test case creates a custom-group and
150 * custom-field then compares the output to a pre-defined XML file.
152 * @param $customGroupParams
153 * @param $fieldParams
154 * @param $expectedXmlFilePath
155 * @dataProvider basicXmlTestCases
157 function testBasicXMLExports($customGroupParams, $fieldParams, $expectedXmlFilePath) {
158 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', array(
159 1 => array($customGroupParams['title'], 'String')
161 $customGroup = $this->customGroupCreate($customGroupParams);
162 $fieldParams['custom_group_id'] = $customGroup['id'];
163 $customField = $this->callAPISuccess('custom_field', 'create', $fieldParams);
165 $exporter = new CRM_Utils_Migrate_Export();
166 $exporter->buildCustomGroups(array($customGroup['id']));
167 // print $exporter->toXML();
168 $this->assertEquals(file_get_contents($expectedXmlFilePath), $exporter->toXML());
170 $this->callAPISuccess('custom_field', 'delete', array('id' => $customField['id']));
171 $this->callAPISuccess('custom_group', 'delete', array('id' => $customGroup['id']));
175 * @param $customGroupParams
176 * @param $fieldParams
177 * @param $expectedXmlFilePath
178 * @dataProvider basicXmlTestCases
180 function testBasicXMLImports($expectCustomGroup, $expectCustomField, $inputXmlFilePath) {
181 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', array(
182 1 => array($expectCustomGroup['title'], 'String')
185 $importer = new CRM_Utils_Migrate_Import();
186 $importer->run($inputXmlFilePath);
188 $customGroups = $this->callAPISuccess('custom_group', 'get', array('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', array('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]);