Merge branch '4.5' into 4.6
[civicrm-core.git] / tests / phpunit / CRM / Utils / Migrate / ImportExportTest.php
CommitLineData
eac4fc12
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
aba1cd8b
EM
3
4/**
5 * Class CRM_Utils_Migrate_ImportExportTest
6 */
bc24cdad 7class 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() {
eac4fc12
TO
16 $tablesToTruncate = array(
17 'civicrm_custom_group',
18 'civicrm_custom_field',
19 );
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
TO
31 // a small library which we use to describe test cases
32 $fixtures = array();
33 $fixtures['textField'] = array(
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,
43 );
44 $fixtures['selectField'] = array(
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',
54 'option_values' => array(
55 array(
56 'weight' => 1,
57 'label' => 'Label1',
58 'value' => 1,
59 'is_active' => 1,
60 ),
61 array(
62 'weight' => 2,
63 'label' => 'Label2',
64 'value' => 2,
65 'is_active' => 1,
66 ),
67 ),
68 );
69
70 // the actual test cases
71 $cases = array();
72
73 $cases[] = array(
74 // CustomGroup params
75 array(
76 'extends' => 'Contact',
0f882f9b 77 'title' => 'example',
eac4fc12
TO
78 ),
79 // CustomField params
80 $fixtures['textField'],
81 // expectedXmlFilePath
82 __DIR__ . '/fixtures/Contact-text.xml',
83 );
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
TO
98
99 $cases[] = array(
100 // CustomGroup params
101 array(
102 'extends' => 'Individual',
0f882f9b 103 'title' => 'example',
eac4fc12
TO
104 ),
105 // CustomField params
106 $fixtures['textField'],
107 // expectedXmlFilePath
108 __DIR__ . '/fixtures/Individual-text.xml',
109 );
110
111 $cases[] = array(
112 // CustomGroup params
113 array(
114 'extends' => 'Individual',
0f882f9b
TO
115 'extends_entity_column_value' => array('Student'),
116 'title' => 'example',
eac4fc12
TO
117 ),
118 // CustomField params
119 $fixtures['textField'],
120 // expectedXmlFilePath
121 __DIR__ . '/fixtures/IndividualStudent-text.xml',
122 );
123
124 $cases[] = array(
125 // CustomGroup params
126 array(
127 'extends' => 'Activity',
0f882f9b 128 'title' => 'example',
eac4fc12
TO
129 ),
130 // CustomField params
131 $fixtures['textField'],
132 // expectedXmlFilePath
133 __DIR__ . '/fixtures/Activity-text.xml',
134 );
135
136 $cases[] = array(
137 // CustomGroup params
138 array(
139 'extends' => 'Activity',
0f882f9b
TO
140 'extends_entity_column_value' => array(array_search('Meeting', CRM_Core_PseudoConstant::activityType())),
141 'title' => 'example',
eac4fc12
TO
142 ),
143 // CustomField params
144 $fixtures['textField'],
145 // expectedXmlFilePath
146 __DIR__ . '/fixtures/ActivityMeeting-text.xml',
147 );
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) {
0f882f9b 162 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', array(
21dfd5f5 163 1 => array($customGroupParams['title'], 'String'),
0f882f9b 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();
170 $exporter->buildCustomGroups(array($customGroup['id']));
171 // print $exporter->toXML();
172 $this->assertEquals(file_get_contents($expectedXmlFilePath), $exporter->toXML());
173
174 $this->callAPISuccess('custom_field', 'delete', array('id' => $customField['id']));
175 $this->callAPISuccess('custom_group', 'delete', array('id' => $customGroup['id']));
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) {
0f882f9b 187 $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE title = %1', array(
21dfd5f5 188 1 => array($expectCustomGroup['title'], 'String'),
0f882f9b
TO
189 ));
190
191 $importer = new CRM_Utils_Migrate_Import();
192 $importer->run($inputXmlFilePath);
193
194 $customGroups = $this->callAPISuccess('custom_group', 'get', array('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]);
199 }
200
201 $customFields = $this->callAPISuccess('custom_field', 'get', array('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]);
206 }
207 }
96025800 208
2a6da8d7 209}