| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CRM_Core_BAO_CustomFieldTest |
| 5 | * @group headless |
| 6 | */ |
| 7 | class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { |
| 8 | |
| 9 | protected $customFieldID; |
| 10 | |
| 11 | public function setUp() { |
| 12 | parent::setUp(); |
| 13 | } |
| 14 | |
| 15 | public function testCreateCustomField() { |
| 16 | $customGroup = $this->createCustomField(); |
| 17 | $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', |
| 18 | 'Database check for created CustomField.' |
| 19 | ); |
| 20 | $fields = array( |
| 21 | 'id' => $customFieldID, |
| 22 | 'label' => 'editTestFld', |
| 23 | 'is_active' => 1, |
| 24 | 'data_type' => 'String', |
| 25 | 'html_type' => 'Text', |
| 26 | 'custom_group_id' => $customGroup['id'], |
| 27 | ); |
| 28 | |
| 29 | CRM_Core_BAO_CustomField::create($fields); |
| 30 | $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.'); |
| 31 | $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.'); |
| 32 | |
| 33 | $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.'); |
| 34 | $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); |
| 35 | $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName, |
| 36 | "Column name ends in ID"); |
| 37 | |
| 38 | $this->customGroupDelete($customGroup['id']); |
| 39 | } |
| 40 | |
| 41 | public function testCreateCustomFieldColumnName() { |
| 42 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); |
| 43 | $fields = array( |
| 44 | 'label' => 'testFld 2', |
| 45 | 'column_name' => 'special_colname', |
| 46 | 'data_type' => 'String', |
| 47 | 'html_type' => 'Text', |
| 48 | 'custom_group_id' => $customGroup['id'], |
| 49 | ); |
| 50 | CRM_Core_BAO_CustomField::create($fields); |
| 51 | $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', |
| 52 | 'Database check for created CustomField.' |
| 53 | ); |
| 54 | $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.'); |
| 55 | $this->assertEquals($fields['column_name'], $dbColumnName, |
| 56 | "Column name set as specified"); |
| 57 | |
| 58 | $this->customGroupDelete($customGroup['id']); |
| 59 | } |
| 60 | |
| 61 | public function testCreateCustomFieldName() { |
| 62 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); |
| 63 | $fields = array( |
| 64 | 'label' => 'testFld 2', |
| 65 | 'name' => 'special_fldlname', |
| 66 | 'data_type' => 'String', |
| 67 | 'html_type' => 'Text', |
| 68 | 'custom_group_id' => $customGroup['id'], |
| 69 | ); |
| 70 | CRM_Core_BAO_CustomField::create($fields); |
| 71 | $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', |
| 72 | 'Database check for created CustomField.' |
| 73 | ); |
| 74 | $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.'); |
| 75 | $this->assertEquals($fields['name'], $dbFieldName, |
| 76 | "Column name set as specified"); |
| 77 | |
| 78 | $this->customGroupDelete($customGroup['id']); |
| 79 | } |
| 80 | |
| 81 | public function testGetFields() { |
| 82 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); |
| 83 | $fields = array( |
| 84 | 'label' => 'testFld1', |
| 85 | 'data_type' => 'String', |
| 86 | 'html_type' => 'Text', |
| 87 | 'is_active' => 1, |
| 88 | 'custom_group_id' => $customGroup['id'], |
| 89 | ); |
| 90 | CRM_Core_BAO_CustomField::create($fields); |
| 91 | $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', |
| 92 | 'Database check for created CustomField.' |
| 93 | ); |
| 94 | $fields = array( |
| 95 | 'label' => 'testFld2', |
| 96 | 'data_type' => 'String', |
| 97 | 'html_type' => 'Text', |
| 98 | 'is_active' => 1, |
| 99 | 'custom_group_id' => $customGroup['id'], |
| 100 | ); |
| 101 | CRM_Core_BAO_CustomField::create($fields); |
| 102 | $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', 'custom_group_id', |
| 103 | 'Database check for created CustomField.' |
| 104 | ); |
| 105 | |
| 106 | $this->customGroupDelete($customGroup['id']); |
| 107 | } |
| 108 | |
| 109 | public function testGetDisplayedValues() { |
| 110 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); |
| 111 | $fieldsToCreate = array( |
| 112 | array( |
| 113 | 'data_type' => 'Country', |
| 114 | 'html_type' => 'Select Country', |
| 115 | 'tests' => array( |
| 116 | 'United States' => 1228, |
| 117 | '' => NULL, |
| 118 | ), |
| 119 | ), |
| 120 | array( |
| 121 | 'data_type' => 'StateProvince', |
| 122 | 'html_type' => 'Multi-Select State/Province', |
| 123 | 'tests' => array( |
| 124 | '' => 0, |
| 125 | 'Alabama' => 1000, |
| 126 | 'Alabama, Alaska' => array(1000, 1001), |
| 127 | ), |
| 128 | ), |
| 129 | array( |
| 130 | 'data_type' => 'String', |
| 131 | 'html_type' => 'Radio', |
| 132 | 'option_values' => array( |
| 133 | 'key' => 'KeyLabel', |
| 134 | ), |
| 135 | 'tests' => array( |
| 136 | 'KeyLabel' => 'key', |
| 137 | ), |
| 138 | ), |
| 139 | array( |
| 140 | 'data_type' => 'String', |
| 141 | 'html_type' => 'CheckBox', |
| 142 | 'option_values' => array( |
| 143 | 'key1' => 'Label1', |
| 144 | 'key2' => 'Label2', |
| 145 | 'key3' => 'Label3', |
| 146 | 'key4' => 'Label4', |
| 147 | ), |
| 148 | 'tests' => array( |
| 149 | 'Label1' => array('key1'), |
| 150 | 'Label2' => 'key2', |
| 151 | 'Label2, Label3' => array('key2', 'key3'), |
| 152 | 'Label3, Label4' => CRM_Utils_Array::implodePadded(array('key3', 'key4')), |
| 153 | 'Label1, Label4' => array('key1' => 1, 'key4' => 1), |
| 154 | ), |
| 155 | ), |
| 156 | array( |
| 157 | 'data_type' => 'Date', |
| 158 | 'html_type' => 'Select Date', |
| 159 | 'date_format' => 'd M yy', |
| 160 | 'time_format' => 1, |
| 161 | 'tests' => array( |
| 162 | '1 Jun 1999 1:30PM' => '1999-06-01 13:30', |
| 163 | '' => '', |
| 164 | ), |
| 165 | ), |
| 166 | ); |
| 167 | foreach ($fieldsToCreate as $num => $field) { |
| 168 | $params = $field + array( |
| 169 | 'label' => 'test field ' . $num, |
| 170 | 'custom_group_id' => $customGroup['id'], |
| 171 | ); |
| 172 | unset($params['tests']); |
| 173 | $createdField = $this->callAPISuccess('customField', 'create', $params); |
| 174 | foreach ($field['tests'] as $expected => $input) { |
| 175 | $this->assertEquals($expected, CRM_Core_BAO_CustomField::displayValue($input, $createdField['id'])); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | $this->customGroupDelete($customGroup['id']); |
| 180 | } |
| 181 | |
| 182 | public function testGetDisplayedValuesContactRef() { |
| 183 | $customGroup = $this->customGroupCreate(['extends' => 'Individual']); |
| 184 | $params = [ |
| 185 | 'data_type' => 'ContactReference', |
| 186 | 'html_type' => 'Autocomplete-Select', |
| 187 | 'label' => 'test ref', |
| 188 | 'custom_group_id' => $customGroup['id'], |
| 189 | ]; |
| 190 | $createdField = $this->callAPISuccess('customField', 'create', $params); |
| 191 | $contact1 = $this->individualCreate(); |
| 192 | $contact2 = $this->individualCreate(['custom_' . $createdField['id'] => $contact1['id']]); |
| 193 | |
| 194 | $this->assertEquals($contact1['display_name'], CRM_Core_BAO_CustomField::displayValue($contact2['id'], $createdField['id'])); |
| 195 | $this->assertEquals("Bob", CRM_Core_BAO_CustomField::displayValue("Bob", $createdField['id'])); |
| 196 | |
| 197 | $this->contactDelete($contact2['id']); |
| 198 | $this->contactDelete($contact1['id']); |
| 199 | $this->customGroupDelete($customGroup['id']); |
| 200 | } |
| 201 | |
| 202 | public function testDeleteCustomField() { |
| 203 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); |
| 204 | $fields = array( |
| 205 | 'custom_group_id' => $customGroup['id'], |
| 206 | 'label' => 'Throwaway Field', |
| 207 | 'dataType' => 'Memo', |
| 208 | 'htmlType' => 'TextArea', |
| 209 | ); |
| 210 | |
| 211 | $customField = $this->customFieldCreate($fields); |
| 212 | $fieldObject = new CRM_Core_BAO_CustomField(); |
| 213 | $fieldObject->id = $customField['id']; |
| 214 | $fieldObject->find(TRUE); |
| 215 | CRM_Core_BAO_CustomField::deleteField($fieldObject); |
| 216 | $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup['id'], 'id', |
| 217 | 'custom_group_id', 'Database check for deleted Custom Field.' |
| 218 | ); |
| 219 | $this->customGroupDelete($customGroup['id']); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Move a custom field from $groupA to $groupB. |
| 224 | * |
| 225 | * Make sure that data records are correctly matched and created. |
| 226 | */ |
| 227 | public function testMoveField() { |
| 228 | $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); |
| 229 | $this->assertTrue($countriesByName['Andorra'] > 0); |
| 230 | $groups = array( |
| 231 | 'A' => $this->customGroupCreate(array( |
| 232 | 'title' => 'Test_Group A', |
| 233 | 'name' => 'test_group_a', |
| 234 | 'extends' => array('Individual'), |
| 235 | 'style' => 'Inline', |
| 236 | 'is_multiple' => 0, |
| 237 | 'is_active' => 1, |
| 238 | 'version' => 3, |
| 239 | )), |
| 240 | 'B' => $this->customGroupCreate(array( |
| 241 | 'title' => 'Test_Group B', |
| 242 | 'name' => 'test_group_b', |
| 243 | 'extends' => array('Individual'), |
| 244 | 'style' => 'Inline', |
| 245 | 'is_multiple' => 0, |
| 246 | 'is_active' => 1, |
| 247 | 'version' => 3, |
| 248 | )), |
| 249 | ); |
| 250 | $groupA = $groups['A']['values'][$groups['A']['id']]; |
| 251 | $groupB = $groups['B']['values'][$groups['B']['id']]; |
| 252 | $countryA = $this->customFieldCreate(array( |
| 253 | 'custom_group_id' => $groups['A']['id'], |
| 254 | 'label' => 'Country A', |
| 255 | 'dataType' => 'Country', |
| 256 | 'htmlType' => 'Select Country', |
| 257 | 'default_value' => NULL, |
| 258 | )); |
| 259 | $countryB = $this->customFieldCreate(array( |
| 260 | 'custom_group_id' => $groups['A']['id'], |
| 261 | 'label' => 'Country B', |
| 262 | 'dataType' => 'Country', |
| 263 | 'htmlType' => 'Select Country', |
| 264 | 'default_value' => NULL, |
| 265 | )); |
| 266 | $countryC = $this->customFieldCreate(array( |
| 267 | 'custom_group_id' => $groups['B']['id'], |
| 268 | 'label' => 'Country C', |
| 269 | 'dataType' => 'Country', |
| 270 | 'htmlType' => 'Select Country', |
| 271 | 'default_value' => NULL, |
| 272 | )); |
| 273 | |
| 274 | $fields = array( |
| 275 | 'countryA' => $countryA['values'][$countryA['id']], |
| 276 | 'countryB' => $countryB['values'][$countryB['id']], |
| 277 | 'countryC' => $countryC['values'][$countryC['id']], |
| 278 | ); |
| 279 | $contacts = array( |
| 280 | 'alice' => $this->individualCreate(array( |
| 281 | 'first_name' => 'Alice', |
| 282 | 'last_name' => 'Albertson', |
| 283 | 'custom_' . $fields['countryA']['id'] => $countriesByName['Andorra'], |
| 284 | 'custom_' . $fields['countryB']['id'] => $countriesByName['Barbados'], |
| 285 | )), |
| 286 | 'bob' => $this->individualCreate(array( |
| 287 | 'first_name' => 'Bob', |
| 288 | 'last_name' => 'Roberts', |
| 289 | 'custom_' . $fields['countryA']['id'] => $countriesByName['Austria'], |
| 290 | 'custom_' . $fields['countryB']['id'] => $countriesByName['Bermuda'], |
| 291 | 'custom_' . $fields['countryC']['id'] => $countriesByName['Chad'], |
| 292 | )), |
| 293 | 'carol' => $this->individualCreate(array( |
| 294 | 'first_name' => 'Carol', |
| 295 | 'last_name' => 'Carolson', |
| 296 | 'custom_' . $fields['countryC']['id'] => $countriesByName['Cambodia'], |
| 297 | )), |
| 298 | ); |
| 299 | |
| 300 | // Move! |
| 301 | CRM_Core_BAO_CustomField::moveField($fields['countryB']['id'], $groupB['id']); |
| 302 | |
| 303 | // Group[A] no longer has fields[countryB] |
| 304 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); |
| 305 | try { |
| 306 | $this->assertDBQuery(1, "SELECT {$fields['countryB']['column_name']} FROM " . $groupA['table_name']); |
| 307 | $this->fail('Expected exception when querying column on wrong table'); |
| 308 | } |
| 309 | catch (PEAR_Exception$e) { |
| 310 | } |
| 311 | $errorScope = NULL; |
| 312 | |
| 313 | // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before |
| 314 | $this->assertDBQuery(1, |
| 315 | "SELECT count(*) FROM {$groupB['table_name']} |
| 316 | WHERE entity_id = %1 |
| 317 | AND {$fields['countryB']['column_name']} = %3 |
| 318 | AND {$fields['countryC']['column_name']} is null", |
| 319 | array( |
| 320 | 1 => array($contacts['alice'], 'Integer'), |
| 321 | 3 => array($countriesByName['Barbados'], 'Integer'), |
| 322 | ) |
| 323 | ); |
| 324 | |
| 325 | // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record |
| 326 | $this->assertDBQuery(1, |
| 327 | "SELECT count(*) FROM {$groupB['table_name']} |
| 328 | WHERE entity_id = %1 |
| 329 | AND {$fields['countryB']['column_name']} = %3 |
| 330 | AND {$fields['countryC']['column_name']} = %4", |
| 331 | array( |
| 332 | 1 => array($contacts['bob'], 'Integer'), |
| 333 | 3 => array($countriesByName['Bermuda'], 'Integer'), |
| 334 | 4 => array($countriesByName['Chad'], 'Integer'), |
| 335 | ) |
| 336 | ); |
| 337 | |
| 338 | // Carol: Group[B] still has fields[countryC] but did not get fields[countryB] |
| 339 | $this->assertDBQuery(1, |
| 340 | "SELECT count(*) FROM {$groupB['table_name']} |
| 341 | WHERE entity_id = %1 |
| 342 | AND {$fields['countryB']['column_name']} is null |
| 343 | AND {$fields['countryC']['column_name']} = %4", |
| 344 | array( |
| 345 | 1 => array($contacts['carol'], 'Integer'), |
| 346 | 4 => array($countriesByName['Cambodia'], 'Integer'), |
| 347 | ) |
| 348 | ); |
| 349 | |
| 350 | $this->customGroupDelete($groups['A']['id']); |
| 351 | $this->customGroupDelete($groupB['id']); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Test get custom field id function. |
| 356 | */ |
| 357 | public function testGetCustomFieldID() { |
| 358 | $this->createCustomField(); |
| 359 | $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld'); |
| 360 | $this->assertEquals($this->customFieldID, $fieldID); |
| 361 | |
| 362 | $fieldID = CRM_Core_BAO_CustomField::getCustomFieldID('testFld', 'new custom group'); |
| 363 | $this->assertEquals($this->customFieldID, $fieldID); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @return array |
| 368 | */ |
| 369 | protected function createCustomField() { |
| 370 | $customGroup = $this->customGroupCreate(array('extends' => 'Individual')); |
| 371 | $fields = array( |
| 372 | 'label' => 'testFld', |
| 373 | 'data_type' => 'String', |
| 374 | 'html_type' => 'Text', |
| 375 | 'custom_group_id' => $customGroup['id'], |
| 376 | ); |
| 377 | $field = CRM_Core_BAO_CustomField::create($fields); |
| 378 | $this->customFieldID = $field->id; |
| 379 | return $customGroup; |
| 380 | } |
| 381 | |
| 382 | } |