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