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