2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
7 * Class CRM_Core_BAO_CustomFieldTest
9 class CRM_Core_BAO_CustomFieldTest
extends CiviUnitTestCase
{
11 public function setUp() {
15 public function testCreateCustomfield() {
16 $customGroup = Custom
::createGroup(array(), 'Individual');
19 'data_type' => 'String',
20 'html_type' => 'Text',
21 'custom_group_id' => $customGroup->id
,
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.'
28 'id' => $customFieldID,
29 'label' => 'editTestFld',
31 'data_type' => 'String',
32 'html_type' => 'Text',
33 'custom_group_id' => $customGroup->id
,
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.');
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");
45 Custom
::deleteGroup($customGroup);
48 public function testCreateCustomfieldColumnName() {
49 $customGroup = Custom
::createGroup(array(), 'Individual');
51 'label' => 'testFld 2',
52 'column_name' => 'special_colname',
53 'data_type' => 'String',
54 'html_type' => 'Text',
55 'custom_group_id' => $customGroup->id
,
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.'
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");
65 Custom
::deleteGroup($customGroup);
68 public function testCreateCustomfieldName() {
69 $customGroup = Custom
::createGroup(array(), 'Individual');
71 'label' => 'testFld 2',
72 'name' => 'special_fldlname',
73 'data_type' => 'String',
74 'html_type' => 'Text',
75 'custom_group_id' => $customGroup->id
,
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.'
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");
85 Custom
::deleteGroup($customGroup);
89 public function testGetFields() {
90 $customGroup = Custom
::createGroup(array(), 'Individual');
92 'label' => 'testFld1',
93 'data_type' => 'String',
94 'html_type' => 'Text',
96 'custom_group_id' => $customGroup->id
,
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.'
103 'label' => 'testFld2',
104 'data_type' => 'String',
105 'html_type' => 'Text',
107 'custom_group_id' => $customGroup->id
,
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.'
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' );
118 Custom
::deleteGroup($customGroup);
121 public function testGetDisplayedValues() {
122 $customGroup = Custom
::createGroup(array(), 'Individual');
124 'label' => 'testCountryFld1',
125 'data_type' => 'Country',
126 'html_type' => 'Select Country',
128 'default_value' => 1228,
129 'custom_group_id' => $customGroup->id
,
131 $customField1 = CRM_Core_BAO_CustomField
::create($fields);
132 $customFieldID1 = $this->assertDBNotNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id', 'custom_group_id',
133 'Database check for created CustomField.'
136 $options[$customFieldID1]['attributes'] = array(
137 'label' => 'testCountryFld1',
138 'data_type' => 'Country',
139 'html_type' => 'Select Country',
141 $display = CRM_Core_BAO_CustomField
::getDisplayValue($fields['default_value'], $customFieldID1, $options);
143 $this->assertEquals('United States', $display, 'Confirm Country display Name');
145 Custom
::deleteGroup($customGroup);
148 public function testDeleteCustomfield() {
149 $customGroup = Custom
::createGroup(array(), 'Individual');
151 'groupId' => $customGroup->id
,
152 'label' => 'Throwaway Field',
153 'dataType' => 'Memo',
154 'htmlType' => 'TextArea',
157 $customField = Custom
::createField(array(), $fields);
158 $this->assertNotNull($customField);
159 CRM_Core_BAO_CustomField
::deleteField($customField);
160 $this->assertDBNull('CRM_Core_DAO_CustomField', $customGroup->id
, 'id',
161 'custom_group_id', 'Database check for deleted Custom Field.'
163 Custom
::deleteGroup($customGroup);
167 * Move a custom field from $groupA to $groupB. Make sure that data records are
168 * correctly matched and created.
170 public function testMoveField() {
171 $countriesByName = array_flip(CRM_Core_PseudoConstant
::country(FALSE, FALSE));
172 $this->assertTrue($countriesByName['Andorra'] > 0);
174 'A' => Custom
::createGroup(array(
175 'title' => 'Test_Group A',
176 'name' => 'test_group_a',
177 'extends' => array('Individual'),
183 'B' => Custom
::createGroup(array(
184 'title' => 'Test_Group B',
185 'name' => 'test_group_b',
186 'extends' => array('Individual'),
194 'countryA' => Custom
::createField(array(), array(
195 'groupId' => $groups['A']->id
,
196 'label' => 'Country A',
197 'dataType' => 'Country',
198 'htmlType' => 'Select Country',
200 'countryB' => Custom
::createField(array(), array(
201 'groupId' => $groups['A']->id
,
202 'label' => 'Country B',
203 'dataType' => 'Country',
204 'htmlType' => 'Select Country',
206 'countryC' => Custom
::createField(array(), array(
207 'groupId' => $groups['B']->id
,
208 'label' => 'Country C',
209 'dataType' => 'Country',
210 'htmlType' => 'Select Country',
214 'alice' => Contact
::createIndividual(array(
215 'first_name' => 'Alice',
216 'last_name' => 'Albertson',
217 'custom_' . $fields['countryA']->id
=> $countriesByName['Andorra'],
218 'custom_' . $fields['countryB']->id
=> $countriesByName['Barbados'],
220 'bob' => Contact
::createIndividual(array(
221 'first_name' => 'Bob',
222 'last_name' => 'Roberts',
223 'custom_' . $fields['countryA']->id
=> $countriesByName['Austria'],
224 'custom_' . $fields['countryB']->id
=> $countriesByName['Bermuda'],
225 'custom_' . $fields['countryC']->id
=> $countriesByName['Chad'],
227 'carol' => Contact
::createIndividual(array(
228 'first_name' => 'Carol',
229 'last_name' => 'Carolson',
230 'custom_' . $fields['countryC']->id
=> $countriesByName['Cambodia'],
235 CRM_Core_BAO_CustomField
::moveField($fields['countryB']->id
, $groups['B']->id
);
237 // Group[A] no longer has fields[countryB]
238 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
240 $this->assertDBQuery(1, "SELECT {$fields['countryB']->column_name} FROM {$groups['A']->table_name}");
241 $this->fail('Expected exception when querying column on wrong table');
243 catch (PEAR_Exception
$e) {
247 // Alice: Group[B] has fields[countryB], but fields[countryC] did not exist before
248 $this->assertDBQuery(1,
249 "SELECT count(*) FROM {$groups['B']->table_name}
251 AND {$fields['countryB']->column_name} = %3
252 AND {$fields['countryC']->column_name} is null",
254 1 => array($contacts['alice'], 'Integer'),
255 3 => array($countriesByName['Barbados'], 'Integer'),
259 // Bob: Group[B] has merged fields[countryB] and fields[countryC] on the same record
260 $this->assertDBQuery(1,
261 "SELECT count(*) FROM {$groups['B']->table_name}
263 AND {$fields['countryB']->column_name} = %3
264 AND {$fields['countryC']->column_name} = %4",
266 1 => array($contacts['bob'], 'Integer'),
267 3 => array($countriesByName['Bermuda'], 'Integer'),
268 4 => array($countriesByName['Chad'], 'Integer'),
272 // Carol: Group[B] still has fields[countryC] but did not get fields[countryB]
273 $this->assertDBQuery(1,
274 "SELECT count(*) FROM {$groups['B']->table_name}
276 AND {$fields['countryB']->column_name} is null
277 AND {$fields['countryC']->column_name} = %4",
279 1 => array($contacts['carol'], 'Integer'),
280 4 => array($countriesByName['Cambodia'], 'Integer'),
284 Custom
::deleteGroup($groups['A']);
285 Custom
::deleteGroup($groups['B']);