4 * Class CRM_Utils_Migrate_ImportExportTest
6 class CRM_Utils_Migrate_ImportExportTest
extends CiviUnitTestCase
{
7 protected $_apiversion;
9 public function setUp() {
10 $this->_apiversion
= 3;
14 public function tearDown() {
15 $tablesToTruncate = array(
16 'civicrm_custom_group',
17 'civicrm_custom_field',
19 $this->quickCleanup($tablesToTruncate, TRUE);
23 * Generate a list of basic XML test cases. Each test case creates a
24 * custom-group and custom-field then compares the output to a pre-defined
25 * XML file. Then, for each test-case, we reverse the process -- we
26 * load the XML into a clean DB and see if it creates matching custom-group
29 public function basicXmlTestCases() {
30 // a small library which we use to describe test cases
32 $fixtures['textField'] = array(
33 'name' => 'test_textfield',
35 'html_type' => 'Text',
36 'data_type' => 'String',
37 'default_value' => 'abc',
43 $fixtures['selectField'] = array(
45 'label' => 'Our select field',
46 'html_type' => 'Select',
47 'data_type' => 'String',
52 // 'option_group_name' => 'our_select_field_20130818044104',
53 'option_values' => array(
69 // the actual test cases
75 'extends' => 'Contact',
79 $fixtures['textField'],
80 // expectedXmlFilePath
81 __DIR__
. '/fixtures/Contact-text.xml',
84 /* @codingStandardsIgnoreStart
88 'extends' => 'Contact',
92 $fixtures['selectField'],
93 // expectedXmlFilePath
94 __DIR__ . '/fixtures/Contact-select.xml',
96 @codingStandardsIgnoreEnd */
101 'extends' => 'Individual',
102 'title' => 'example',
104 // CustomField params
105 $fixtures['textField'],
106 // expectedXmlFilePath
107 __DIR__
. '/fixtures/Individual-text.xml',
111 // CustomGroup params
113 'extends' => 'Individual',
114 'extends_entity_column_value' => array('Student'),
115 'title' => 'example',
117 // CustomField params
118 $fixtures['textField'],
119 // expectedXmlFilePath
120 __DIR__
. '/fixtures/IndividualStudent-text.xml',
124 // CustomGroup params
126 'extends' => 'Activity',
127 'title' => 'example',
129 // CustomField params
130 $fixtures['textField'],
131 // expectedXmlFilePath
132 __DIR__
. '/fixtures/Activity-text.xml',
136 // CustomGroup params
138 'extends' => 'Activity',
139 'extends_entity_column_value' => array(array_search('Meeting', CRM_Core_PseudoConstant
::activityType())),
140 'title' => 'example',
142 // CustomField params
143 $fixtures['textField'],
144 // expectedXmlFilePath
145 __DIR__
. '/fixtures/ActivityMeeting-text.xml',
152 * Execute a basic XML test case. Each test case creates a custom-group and
153 * custom-field then compares the output to a pre-defined XML file.
155 * @param array $customGroupParams
156 * @param array $fieldParams
157 * @param $expectedXmlFilePath
158 * @dataProvider basicXmlTestCases
160 public function testBasicXMLExports($customGroupParams, $fieldParams, $expectedXmlFilePath) {
161 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', array(
162 1 => array($customGroupParams['title'], 'String'),
164 $customGroup = $this->customGroupCreate($customGroupParams);
165 $fieldParams['custom_group_id'] = $customGroup['id'];
166 $customField = $this->callAPISuccess('custom_field', 'create', $fieldParams);
168 $exporter = new CRM_Utils_Migrate_Export();
169 $exporter->buildCustomGroups(array($customGroup['id']));
170 // print $exporter->toXML();
171 $this->assertEquals(file_get_contents($expectedXmlFilePath), $exporter->toXML());
173 $this->callAPISuccess('custom_field', 'delete', array('id' => $customField['id']));
174 $this->callAPISuccess('custom_group', 'delete', array('id' => $customGroup['id']));
178 * @param $expectCustomGroup
179 * @param $expectCustomField
180 * @param $inputXmlFilePath
182 * @throws CRM_Core_Exception
183 * @dataProvider basicXmlTestCases
185 public function testBasicXMLImports($expectCustomGroup, $expectCustomField, $inputXmlFilePath) {
186 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', array(
187 1 => array($expectCustomGroup['title'], 'String'),
190 $importer = new CRM_Utils_Migrate_Import();
191 $importer->run($inputXmlFilePath);
193 $customGroups = $this->callAPISuccess('custom_group', 'get', array('title' => $expectCustomGroup['title']));
194 $this->assertEquals(1, $customGroups['count']);
195 $customGroup = array_shift($customGroups['values']);
196 foreach ($expectCustomGroup as $expectKey => $expectValue) {
197 $this->assertEquals($expectValue, $customGroup[$expectKey]);
200 $customFields = $this->callAPISuccess('custom_field', 'get', array('label' => $expectCustomField['label']));
201 $this->assertEquals(1, $customFields['count']);
202 $customField = array_shift($customFields['values']);
203 foreach ($expectCustomField as $expectKey => $expectValue) {
204 $this->assertEquals($expectValue, $customField[$expectKey]);