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 //@todo make BAO enotice compliant & remove the line below
7 // WARNING - NEVER COPY & PASTE $_eNoticeCompliant = FALSE
8 // new test classes should be compliant.
9 public $_eNoticeCompliant = FALSE;
12 'name' => 'Custom Field BAOs',
13 'description' => 'Test all Core_BAO_CustomField methods.',
14 'group' => 'CiviCRM BAO Tests',
22 function testCreateCustomfield() {
23 $customGroup = Custom
::createGroup(array(), 'Individual');
26 'data_type' => 'String',
27 'html_type' => 'Text',
28 'custom_group_id' => $customGroup->id
,
30 $customField = CRM_Core_BAO_CustomField
::create($fields);
31 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id', 'custom_group_id',
32 'Database check for created CustomField.'
35 'id' => $customFieldID,
36 'label' => 'editTestFld',
38 'data_type' => 'String',
39 'html_type' => 'Text',
40 'custom_group_id' => $customGroup->id
,
43 $customField = CRM_Core_BAO_CustomField
::create($fields);
44 $this->assertDBNotNull('CRM_Core_DAO_CustomField', 1, 'id', 'is_active', 'Database check for edited CustomField.');
45 $this->assertDBNotNull('CRM_Core_DAO_CustomField', $fields['label'], 'id', 'label', 'Database check for edited CustomField.');
47 $dbFieldName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'name', 'id', 'Database check for edited CustomField.');
48 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
49 $this->assertEquals(strtolower("{$dbFieldName}_{$customFieldID}"), $dbColumnName,
50 "Column name ends in ID");
52 Custom
::deleteGroup($customGroup);
55 function testCreateCustomfieldColumnName() {
56 $customGroup = Custom
::createGroup(array(), 'Individual');
58 'label' => 'testFld 2',
59 'column_name' => 'special_colname',
60 'data_type' => 'String',
61 'html_type' => 'Text',
62 'custom_group_id' => $customGroup->id
,
64 $customField = CRM_Core_BAO_CustomField
::create($fields);
65 $customFieldID = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id', 'custom_group_id',
66 'Database check for created CustomField.'
68 $dbColumnName = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customFieldID, 'column_name', 'id', 'Database check for edited CustomField.');
69 $this->assertEquals($fields['column_name'], $dbColumnName,
70 "Column name set as specified");
72 Custom
::deleteGroup($customGroup);
75 function testGetFields() {
76 $customGroup = Custom
::createGroup(array(), 'Individual');
78 'label' => 'testFld1',
79 'data_type' => 'String',
80 'html_type' => 'Text',
82 'custom_group_id' => $customGroup->id
,
84 $customField1 = CRM_Core_BAO_CustomField
::create($fields);
85 $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id', 'custom_group_id',
86 'Database check for created CustomField.'
89 'label' => 'testFld2',
90 'data_type' => 'String',
91 'html_type' => 'Text',
93 'custom_group_id' => $customGroup->id
,
95 $customField2 = CRM_Core_BAO_CustomField
::create($fields);
96 $customFieldID2 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id', 'custom_group_id',
97 'Database check for created CustomField.'
99 $getCustomFields = array();
100 $getCustomFields = CRM_Core_BAO_CustomField
::getFields('Individual', TRUE, TRUE);
101 //CRM_Core_Error::debug('fdf',$getCustomFields);
102 //$this->assertEquals( 'testFld1', $getCustomFields[$customFieldID1][0], 'Confirm First Custom field label' );
103 //$this->assertEquals( 'testFld2', $getCustomFields[$customFieldID2][0], 'Confirm Second Custom field label' );
106 Custom
::deleteGroup($customGroup);
109 function testGetDisplayedValues() {
110 $customGroup = Custom
::createGroup(array(), 'Individual');
112 'label' => 'testCountryFld1',
113 'data_type' => 'Country',
114 'html_type' => 'Select Country',
116 'default_value' => 1228,
117 'custom_group_id' => $customGroup->id
,
119 $customField1 = CRM_Core_BAO_CustomField
::create($fields);
120 $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id', 'custom_group_id',
121 'Database check for created CustomField.'
124 $options[$customFieldID1]['attributes'] = array(
125 'label' => 'testCountryFld1',
126 'data_type' => 'Country',
127 'html_type' => 'Select Country',
129 $display = CRM_Core_BAO_CustomField
::getDisplayValue($fields['default_value'], $customFieldID1, $options);
131 $this->assertEquals('United States', $display, 'Confirm Country display Name');
133 Custom
::deleteGroup($customGroup);
136 function testDeleteCustomfield() {
137 $customGroup = Custom
::createGroup(array(), 'Individual');
139 'groupId' => $customGroup->id
,
140 'label' => 'Throwaway Field',
141 'data_type' => 'Memo',
142 'html_type' => 'TextArea',
145 $customField = Custom
::createField(array(), $fields);
146 $this->assertNotNull($customField);
147 CRM_Core_BAO_CustomField
::deleteField($customField);
148 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id',
149 'custom_group_id', 'Database check for deleted Custom Field.'
151 Custom
::deleteGroup($customGroup);
155 * Move a custom field from $groupA to $groupB. Make sure that data records are
156 * correctly matched and created.
158 function testMoveField() {
159 $countriesByName = array_flip(CRM_Core_PseudoConstant
::country(FALSE, FALSE));
160 $this->assertTrue($countriesByName['Andorra'] > 0);
162 'A' => Custom
::createGroup(array(
163 'title' => 'Test_Group A',
164 'name' => 'test_group_a',
165 'extends' => array('Individual'),
171 'B' => Custom
::createGroup(array(
172 'title' => 'Test_Group B',
173 'name' => 'test_group_b',
174 'extends' => array('Individual'),
182 'countryA' => Custom
::createField(array(
184 'groupId' => $groups['A']->id
,
185 'label' => 'Country A',
186 'dataType' => 'Country',
187 'htmlType' => 'Select Country',
189 'countryB' => Custom
::createField(array(
191 'groupId' => $groups['A']->id
,
192 'label' => 'Country B',
193 'dataType' => 'Country',
194 'htmlType' => 'Select Country',
196 'countryC' => Custom
::createField(array(
198 'groupId' => $groups['B']->id
,
199 'label' => 'Country C',
200 'dataType' => 'Country',
201 'htmlType' => 'Select Country',
205 'alice' => Contact
::createIndividual(array(
206 'first_name' => 'Alice',
207 'last_name' => 'Albertson',
208 'custom_' . $fields['countryA']->id
=> $countriesByName['Andorra'],
209 'custom_' . $fields['countryB']->id
=> $countriesByName['Barbados'],
211 'bob' => Contact
::createIndividual(array(
212 'first_name' => 'Bob',
213 'last_name' => 'Roberts',
214 'custom_' . $fields['countryA']->id
=> $countriesByName['Austria'],
215 'custom_' . $fields['countryB']->id
=> $countriesByName['Bermuda'],
216 'custom_' . $fields['countryC']->id
=> $countriesByName['Chad'],
218 'carol' => Contact
::createIndividual(array(
219 'first_name' => 'Carol',
220 'last_name' => 'Carolson',
221 'custom_' . $fields['countryC']->id
=> $countriesByName['Cambodia'],
226 CRM_Core_BAO_CustomField
::moveField($fields['countryB']->id
, $groups['B']->id
);
228 // Group[A] no longer has fields[countryB]
229 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
231 $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}");
232 $this->fail('Expected exception when querying column on wrong table');
234 catch(PEAR_Exception
$e) {}
237 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
238 $this->assertDBQuery(1,
239 "SELECT count(*) FROM {$groups['B']->table_name}
241 AND {$fields['countryB']->column_name} = %3
242 AND {$fields['countryC']->column_name} is null",
244 1 => array($contacts['alice'], 'Integer'),
245 3 => array($countriesByName['Barbados'], 'Integer'),
249 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
250 $this->assertDBQuery(1,
251 "SELECT count(*) FROM {$groups['B']->table_name}
253 AND {$fields['countryB']->column_name} = %3
254 AND {$fields['countryC']->column_name} = %4",
256 1 => array($contacts['bob'], 'Integer'),
257 3 => array($countriesByName['Bermuda'], 'Integer'),
258 4 => array($countriesByName['Chad'], 'Integer'),
262 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
263 $this->assertDBQuery(1,
264 "SELECT count(*) FROM {$groups['B']->table_name}
266 AND {$fields['countryB']->column_name} is null
267 AND {$fields['countryC']->column_name} = %4",
269 1 => array($contacts['carol'], 'Integer'),
270 4 => array($countriesByName['Cambodia'], 'Integer'),
274 Custom
::deleteGroup($groups['A']);
275 Custom
::deleteGroup($groups['B']);