Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTableSetGetTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_BAO_CustomValueTableSetGetTest
5 * @group headless
6 */
7 class CRM_Core_BAO_CustomValueTableSetGetTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11 }
12
13 /**
14 * Test setValues() and GetValues() methods with custom Date field
15 */
16 public function testSetGetValuesDate() {
17 $params = [];
18 $contactID = $this->individualCreate();
19
20 //create Custom Group
21 $customGroup = $this->customGroupCreate(['is_multiple' => 1]);
22
23 //create Custom Field of data type Date
24 $fields = [
25 'custom_group_id' => $customGroup['id'],
26 'data_type' => 'Date',
27 'html_type' => 'Select Date',
28 'default_value' => '',
29 ];
30 $customField = $this->customFieldCreate($fields);
31
32 // Retrieve the field ID for sample custom field 'test_Date'
33 $params = ['label' => 'test_Date'];
34 $field = [];
35
36 CRM_Core_BAO_CustomField::retrieve($params, $field);
37 $fieldID = $customField['id'];
38
39 // Set test_Date to a valid date value
40 $date = '20080608000000';
41 $params = [
42 'entityID' => $contactID,
43 'custom_' . $fieldID => $date,
44 ];
45 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
46 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
47
48 // Check that the date value is stored
49 $values = [];
50 $params = [
51 'entityID' => $contactID,
52 'custom_' . $fieldID => 1,
53 ];
54 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
55
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
60 );
61
62 // Now set test_Date to an invalid date value and try to reset
63 $badDate = '20080631000000';
64 $params = [
65 'entityID' => $contactID,
66 'custom_' . $fieldID => $badDate,
67 ];
68
69 CRM_Core_TemporaryErrorScope::useException();
70 $message = NULL;
71 try {
72 CRM_Core_BAO_CustomValueTable::setValues($params);
73 }
74 catch (Exception $e) {
75 $message = $e->getMessage();
76 }
77 $errorScope = NULL;
78
79 // Check that an exception has been thrown
80 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
81
82 $params = [
83 'entityID' => $contactID,
84 'custom_' . $fieldID => 1,
85 ];
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
90 );
91
92 // Test setting test_Date to null
93 $params = [
94 'entityID' => $contactID,
95 'custom_' . $fieldID => NULL,
96 ];
97 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
98
99 // Check that the date value is empty
100 $params = [
101 'entityID' => $contactID,
102 'custom_' . $fieldID => 1,
103 ];
104 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
105 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
106
107 // Cleanup
108 $this->customFieldDelete($customField);
109 $this->customGroupDelete($customGroup['id']);
110 $this->contactDelete($contactID);
111 }
112
113 /**
114 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
115 */
116 public function testSetGetValuesYesNoRadio() {
117 $contactID = $this->individualCreate();
118
119 $customGroup = $this->customGroupCreate(['is_multiple' => 1]);
120
121 //create Custom Field of type YesNo(Boolean) Radio
122 $fields = [
123 'custom_group_id' => $customGroup['id'],
124 'data_type' => 'Boolean',
125 'html_type' => 'Radio',
126 'default_value' => '',
127 ];
128 $customField = $this->customFieldCreate($fields);
129
130 // Retrieve the field ID for sample custom field 'test_Boolean'
131 $params = ['label' => 'test_Boolean'];
132 $field = [];
133
134 //get field Id
135 CRM_Core_BAO_CustomField::retrieve($params, $field);
136
137 $fieldID = $customField['id'];
138
139 // valid boolean value '1' for Boolean Radio
140 $yesNo = '1';
141 $params = [
142 'entityID' => $contactID,
143 'custom_' . $fieldID => $yesNo,
144 ];
145 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
146
147 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
148
149 // Check that the YesNo radio value is stored
150 $params = [
151 'entityID' => $contactID,
152 'custom_' . $fieldID => 1,
153 ];
154 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
155
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
159 );
160
161 // Now set YesNo radio to an invalid boolean value and try to reset
162 $badYesNo = '20';
163 $params = [
164 'entityID' => $contactID,
165 'custom_' . $fieldID => $badYesNo,
166 ];
167
168 CRM_Core_TemporaryErrorScope::useException();
169 $message = NULL;
170 try {
171 CRM_Core_BAO_CustomValueTable::setValues($params);
172 }
173 catch (Exception $e) {
174 $message = $e->getMessage();
175 }
176 $errorScope = NULL;
177
178 // Check that an exception has been thrown
179 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
180
181 $params = [
182 'entityID' => $contactID,
183 'custom_' . $fieldID => 1,
184 ];
185 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
186
187 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
188 'Verify that the date value has NOT been updated for contact ' . $contactID
189 );
190
191 // Cleanup
192 $this->customFieldDelete($customField['id']);
193 $this->customGroupDelete($customGroup['id']);
194 $this->contactDelete($contactID);
195 }
196
197 }