tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / CRM / Utils / Migrate / ImportExportTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_Migrate_ImportExportTest
5 */
6 class CRM_Utils_Migrate_ImportExportTest extends CiviUnitTestCase {
7 protected $_apiversion;
8
9 public function setUp() {
10 $this->_apiversion = 3;
11 parent::setUp();
12 }
13
14 public function tearDown() {
15 $tablesToTruncate = array(
16 'civicrm_custom_group',
17 'civicrm_custom_field',
18 );
19 $this->quickCleanup($tablesToTruncate, TRUE);
20 }
21
22 /**
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
27 * and custom-field.
28 */
29 public function basicXmlTestCases() {
30 // a small library which we use to describe test cases
31 $fixtures = array();
32 $fixtures['textField'] = array(
33 'name' => 'test_textfield',
34 'label' => 'Name1',
35 'html_type' => 'Text',
36 'data_type' => 'String',
37 'default_value' => 'abc',
38 'weight' => 4,
39 'is_required' => 1,
40 'is_searchable' => 0,
41 'is_active' => 1,
42 );
43 $fixtures['selectField'] = array(
44 // custom_group_id
45 'label' => 'Our select field',
46 'html_type' => 'Select',
47 'data_type' => 'String',
48 'weight' => 4,
49 'is_required' => 1,
50 'is_searchable' => 0,
51 'is_active' => 1,
52 // 'option_group_name' => 'our_select_field_20130818044104',
53 'option_values' => array(
54 array(
55 'weight' => 1,
56 'label' => 'Label1',
57 'value' => 1,
58 'is_active' => 1,
59 ),
60 array(
61 'weight' => 2,
62 'label' => 'Label2',
63 'value' => 2,
64 'is_active' => 1,
65 ),
66 ),
67 );
68
69 // the actual test cases
70 $cases = array();
71
72 $cases[] = array(
73 // CustomGroup params
74 array(
75 'extends' => 'Contact',
76 'title' => 'example',
77 ),
78 // CustomField params
79 $fixtures['textField'],
80 // expectedXmlFilePath
81 __DIR__ . '/fixtures/Contact-text.xml',
82 );
83
84 /* @codingStandardsIgnoreStart
85 $cases[] = array(
86 // CustomGroup params
87 array(
88 'extends' => 'Contact',
89 'title' => 'example',
90 ),
91 // CustomField params
92 $fixtures['selectField'],
93 // expectedXmlFilePath
94 __DIR__ . '/fixtures/Contact-select.xml',
95 );
96 @codingStandardsIgnoreEnd */
97
98 $cases[] = array(
99 // CustomGroup params
100 array(
101 'extends' => 'Individual',
102 'title' => 'example',
103 ),
104 // CustomField params
105 $fixtures['textField'],
106 // expectedXmlFilePath
107 __DIR__ . '/fixtures/Individual-text.xml',
108 );
109
110 $cases[] = array(
111 // CustomGroup params
112 array(
113 'extends' => 'Individual',
114 'extends_entity_column_value' => array('Student'),
115 'title' => 'example',
116 ),
117 // CustomField params
118 $fixtures['textField'],
119 // expectedXmlFilePath
120 __DIR__ . '/fixtures/IndividualStudent-text.xml',
121 );
122
123 $cases[] = array(
124 // CustomGroup params
125 array(
126 'extends' => 'Activity',
127 'title' => 'example',
128 ),
129 // CustomField params
130 $fixtures['textField'],
131 // expectedXmlFilePath
132 __DIR__ . '/fixtures/Activity-text.xml',
133 );
134
135 $cases[] = array(
136 // CustomGroup params
137 array(
138 'extends' => 'Activity',
139 'extends_entity_column_value' => array(array_search('Meeting', CRM_Core_PseudoConstant::activityType())),
140 'title' => 'example',
141 ),
142 // CustomField params
143 $fixtures['textField'],
144 // expectedXmlFilePath
145 __DIR__ . '/fixtures/ActivityMeeting-text.xml',
146 );
147
148 return $cases;
149 }
150
151 /**
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.
154 *
155 * @param array $customGroupParams
156 * @param array $fieldParams
157 * @param $expectedXmlFilePath
158 * @dataProvider basicXmlTestCases
159 */
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'),
163 ));
164 $customGroup = $this->customGroupCreate($customGroupParams);
165 $fieldParams['custom_group_id'] = $customGroup['id'];
166 $customField = $this->callAPISuccess('custom_field', 'create', $fieldParams);
167
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());
172
173 $this->callAPISuccess('custom_field', 'delete', array('id' => $customField['id']));
174 $this->callAPISuccess('custom_group', 'delete', array('id' => $customGroup['id']));
175 }
176
177 /**
178 * @param $expectCustomGroup
179 * @param $expectCustomField
180 * @param $inputXmlFilePath
181 *
182 * @throws CRM_Core_Exception
183 * @dataProvider basicXmlTestCases
184 */
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'),
188 ));
189
190 $importer = new CRM_Utils_Migrate_Import();
191 $importer->run($inputXmlFilePath);
192
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]);
198 }
199
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]);
205 }
206 }
207
208 }