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 = Custom
::createGroup($params, 'Individual', TRUE);
23 //create Custom Field of data type Date
25 'groupId' => $customGroup->id
,
27 'htmlType' => 'Select Date',
29 $customField = Custom
::createField($params, $fields);
31 // Retrieve the field ID for sample custom field 'test_Date'
32 $params = array('label' => 'test_Date');
35 CRM_Core_BAO_CustomField
::retrieve($params, $field);
36 $fieldID = $field['id'];
38 // Set test_Date to a valid date value
39 $date = '20080608000000';
41 'entityID' => $contactID,
42 'custom_' . $fieldID => $date,
44 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
45 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
47 // Check that the date value is stored
50 'entityID' => $contactID,
51 'custom_' . $fieldID => 1,
53 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
55 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
56 $this->assertEquals($values['custom_' . $fieldID . '_1'],
57 CRM_Utils_Date
::mysqlToIso($date),
58 'Verify that the date value is stored for contact ' . $contactID
61 // Now set test_Date to an invalid date value and try to reset
62 $badDate = '20080631000000';
64 'entityID' => $contactID,
65 'custom_' . $fieldID => $badDate,
68 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
71 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
73 catch (Exception
$e) {
74 $message = $e->getMessage();
78 // Check that an exception has been thrown
79 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
82 'entityID' => $contactID,
83 'custom_' . $fieldID => 1,
85 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
86 $this->assertEquals($values['custom_' . $fieldID . '_1'],
87 CRM_Utils_Date
::mysqlToIso($date),
88 'Verify that the date value has NOT been updated for contact ' . $contactID
91 // Test setting test_Date to null
93 'entityID' => $contactID,
94 'custom_' . $fieldID => NULL,
96 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
98 // Check that the date value is empty
100 'entityID' => $contactID,
101 'custom_' . $fieldID => 1,
103 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
104 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
107 Custom
::deleteField($customField);
108 Custom
::deleteGroup($customGroup);
109 $this->contactDelete($contactID);
113 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
115 public function testSetGetValuesYesNoRadio() {
117 $contactID = $this->individualCreate();
119 //create Custom Group
120 $customGroup = Custom
::createGroup($params, 'Individual', TRUE);
122 //create Custom Field of type YesNo(Boolean) Radio
124 'groupId' => $customGroup->id
,
125 'dataType' => 'Boolean',
126 'htmlType' => 'Radio',
128 $customField = Custom
::createField($params, $fields);
130 // Retrieve the field ID for sample custom field 'test_Boolean'
131 $params = array('label' => 'test_Boolean');
135 CRM_Core_BAO_CustomField
::retrieve($params, $field);
137 $fieldID = $field['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
152 'entityID' => $contactID,
153 'custom_' . $fieldID => 1,
155 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
157 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
158 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
159 'Verify that the boolean value is stored for contact ' . $contactID
162 // Now set YesNo radio to an invalid boolean value and try to reset
165 'entityID' => $contactID,
166 'custom_' . $fieldID => $badYesNo,
169 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
172 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
174 catch (Exception
$e) {
175 $message = $e->getMessage();
179 // Check that an exception has been thrown
180 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
183 'entityID' => $contactID,
184 'custom_' . $fieldID => 1,
186 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
188 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
189 'Verify that the date value has NOT been updated for contact ' . $contactID
193 Custom
::deleteField($customField);
194 Custom
::deleteGroup($customGroup);
195 $this->contactDelete($contactID);