4 * Class CRM_Core_BAO_CustomValueTableSetGetTest
7 class CRM_Core_BAO_CustomValueTableSetGetTest
extends CiviUnitTestCase
{
9 public function setUp() {
14 * Test setValues() and GetValues() methods with custom Date field
16 public function testSetGetValuesDate() {
18 $contactID = $this->individualCreate();
21 $customGroup = $this->customGroupCreate(['is_multiple' => 1]);
23 //create Custom Field of data type Date
25 'custom_group_id' => $customGroup['id'],
26 'data_type' => 'Date',
27 'html_type' => 'Select Date',
28 'default_value' => '',
30 $customField = $this->customFieldCreate($fields);
32 // Retrieve the field ID for sample custom field 'test_Date'
33 $params = ['label' => 'test_Date'];
36 CRM_Core_BAO_CustomField
::retrieve($params, $field);
37 $fieldID = $customField['id'];
39 // Set test_Date to a valid date value
40 $date = '20080608000000';
42 'entityID' => $contactID,
43 'custom_' . $fieldID => $date,
45 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
46 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
48 // Check that the date value is stored
51 'entityID' => $contactID,
52 'custom_' . $fieldID => 1,
54 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
56 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
57 $this->assertEquals($values['custom_' . $fieldID . '_1'],
58 CRM_Utils_Date
::mysqlToIso($date),
59 'Verify that the date value is stored for contact ' . $contactID
62 // Now set test_Date to an invalid date value and try to reset
63 $badDate = '20080631000000';
65 'entityID' => $contactID,
66 'custom_' . $fieldID => $badDate,
69 CRM_Core_TemporaryErrorScope
::useException();
72 CRM_Core_BAO_CustomValueTable
::setValues($params);
74 catch (Exception
$e) {
75 $message = $e->getMessage();
79 // Check that an exception has been thrown
80 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
83 'entityID' => $contactID,
84 'custom_' . $fieldID => 1,
86 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
87 $this->assertEquals($values['custom_' . $fieldID . '_1'],
88 CRM_Utils_Date
::mysqlToIso($date),
89 'Verify that the date value has NOT been updated for contact ' . $contactID
92 // Test setting test_Date to null
94 'entityID' => $contactID,
95 'custom_' . $fieldID => NULL,
97 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
99 // Check that the date value is empty
101 'entityID' => $contactID,
102 'custom_' . $fieldID => 1,
104 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
105 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
108 $this->customFieldDelete($customField);
109 $this->customGroupDelete($customGroup['id']);
110 $this->contactDelete($contactID);
114 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
116 public function testSetGetValuesYesNoRadio() {
117 $contactID = $this->individualCreate();
119 $customGroup = $this->customGroupCreate(['is_multiple' => 1]);
121 //create Custom Field of type YesNo(Boolean) Radio
123 'custom_group_id' => $customGroup['id'],
124 'data_type' => 'Boolean',
125 'html_type' => 'Radio',
126 'default_value' => '',
128 $customField = $this->customFieldCreate($fields);
130 // Retrieve the field ID for sample custom field 'test_Boolean'
131 $params = ['label' => 'test_Boolean'];
135 CRM_Core_BAO_CustomField
::retrieve($params, $field);
137 $fieldID = $customField['id'];
139 // valid boolean value '1' for Boolean Radio
142 'entityID' => $contactID,
143 'custom_' . $fieldID => $yesNo,
145 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
147 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
149 // Check that the YesNo radio value is stored
151 'entityID' => $contactID,
152 'custom_' . $fieldID => 1,
154 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
156 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
157 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
158 'Verify that the boolean value is stored for contact ' . $contactID
161 // Now set YesNo radio to an invalid boolean value and try to reset
164 'entityID' => $contactID,
165 'custom_' . $fieldID => $badYesNo,
168 CRM_Core_TemporaryErrorScope
::useException();
171 CRM_Core_BAO_CustomValueTable
::setValues($params);
173 catch (Exception
$e) {
174 $message = $e->getMessage();
178 // Check that an exception has been thrown
179 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
182 'entityID' => $contactID,
183 'custom_' . $fieldID => 1,
185 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
187 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
188 'Verify that the date value has NOT been updated for contact ' . $contactID
192 $this->customFieldDelete($customField['id']);
193 $this->customGroupDelete($customGroup['id']);
194 $this->contactDelete($contactID);