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