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