2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
7 * Class CRM_Core_BAO_CustomValueTableSetGetTest
9 class CRM_Core_BAO_CustomValueTableSetGetTest
extends CiviUnitTestCase
{
11 public function setUp() {
16 * Test setValues() and GetValues() methods with custom Date field
18 public function testSetGetValuesDate() {
20 $contactID = Contact
::createIndividual();
23 $customGroup = Custom
::createGroup($params, 'Individual', TRUE);
25 //create Custom Field of data type Date
27 'groupId' => $customGroup->id
,
29 'htmlType' => 'Select Date',
31 $customField = Custom
::createField($params, $fields);
33 // Retrieve the field ID for sample custom field 'test_Date'
34 $params = array('label' => 'test_Date');
37 CRM_Core_BAO_CustomField
::retrieve($params, $field);
38 $fieldID = $field['id'];
40 // Set test_Date to a valid date value
41 $date = '20080608000000';
43 'entityID' => $contactID,
44 'custom_' . $fieldID => $date,
46 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
47 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
49 // Check that the date value is stored
52 'entityID' => $contactID,
53 'custom_' . $fieldID => 1,
55 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
57 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
58 $this->assertEquals($values['custom_' . $fieldID . '_1'],
59 CRM_Utils_Date
::mysqlToIso($date),
60 'Verify that the date value is stored for contact ' . $contactID
63 // Now set test_Date to an invalid date value and try to reset
64 $badDate = '20080631000000';
66 'entityID' => $contactID,
67 'custom_' . $fieldID => $badDate,
70 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
73 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
75 catch (Exception
$e) {
76 $message = $e->getMessage();
80 // Check that an exception has been thrown
81 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
84 'entityID' => $contactID,
85 'custom_' . $fieldID => 1,
87 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
88 $this->assertEquals($values['custom_' . $fieldID . '_1'],
89 CRM_Utils_Date
::mysqlToIso($date),
90 'Verify that the date value has NOT been updated for contact ' . $contactID
93 // Test setting test_Date to null
95 'entityID' => $contactID,
96 'custom_' . $fieldID => NULL,
98 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
100 // Check that the date value is empty
102 'entityID' => $contactID,
103 'custom_' . $fieldID => 1,
105 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
106 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
109 Custom
::deleteField($customField);
110 Custom
::deleteGroup($customGroup);
111 Contact
::delete($contactID);
115 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
117 public function testSetGetValuesYesNoRadio() {
119 $contactID = Contact
::createIndividual();
121 //create Custom Group
122 $customGroup = Custom
::createGroup($params, 'Individual', TRUE);
124 //create Custom Field of type YesNo(Boolean) Radio
126 'groupId' => $customGroup->id
,
127 'dataType' => 'Boolean',
128 'htmlType' => 'Radio',
130 $customField = Custom
::createField($params, $fields);
132 // Retrieve the field ID for sample custom field 'test_Boolean'
133 $params = array('label' => 'test_Boolean');
137 CRM_Core_BAO_CustomField
::retrieve($params, $field);
139 $fieldID = $field['id'];
141 // valid boolean value '1' for Boolean Radio
144 'entityID' => $contactID,
145 'custom_' . $fieldID => $yesNo,
147 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
149 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
151 // Check that the YesNo radio value is stored
154 'entityID' => $contactID,
155 'custom_' . $fieldID => 1,
157 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
159 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
160 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
161 'Verify that the boolean value is stored for contact ' . $contactID
164 // Now set YesNo radio to an invalid boolean value and try to reset
167 'entityID' => $contactID,
168 'custom_' . $fieldID => $badYesNo,
171 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
174 $result = CRM_Core_BAO_CustomValueTable
::setValues($params);
176 catch (Exception
$e) {
177 $message = $e->getMessage();
181 // Check that an exception has been thrown
182 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
185 'entityID' => $contactID,
186 'custom_' . $fieldID => 1,
188 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
190 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
191 'Verify that the date value has NOT been updated for contact ' . $contactID
195 Custom
::deleteField($customField);
196 Custom
::deleteGroup($customGroup);
197 Contact
::delete($contactID);