REF Update CiviCRM default PEAR Error handling to be exception rather than just PEAR_...
[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
6c6e6187 69 $message = NULL;
6a488035 70 try {
8d63d44a 71 CRM_Core_BAO_CustomValueTable::setValues($params);
0db6c3e1 72 }
481a74f4 73 catch (Exception $e) {
6a488035
TO
74 $message = $e->getMessage();
75 }
76 $errorScope = NULL;
77
78 // Check that an exception has been thrown
481a74f4 79 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
6a488035 80
9099cab3 81 $params = [
6a488035
TO
82 'entityID' => $contactID,
83 'custom_' . $fieldID => 1,
9099cab3 84 ];
6a488035
TO
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
89 );
90
91 // Test setting test_Date to null
9099cab3 92 $params = [
6a488035
TO
93 'entityID' => $contactID,
94 'custom_' . $fieldID => NULL,
9099cab3 95 ];
6a488035
TO
96 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
97
98 // Check that the date value is empty
9099cab3 99 $params = [
6a488035
TO
100 'entityID' => $contactID,
101 'custom_' . $fieldID => 1,
9099cab3 102 ];
6a488035
TO
103 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
104 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
105
106 // Cleanup
8d63d44a 107 $this->customFieldDelete($customField);
108 $this->customGroupDelete($customGroup['id']);
93ac19cd 109 $this->contactDelete($contactID);
6a488035
TO
110 }
111
c490a46a
CW
112 /**
113 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
c490a46a 114 */
00be9182 115 public function testSetGetValuesYesNoRadio() {
f2040bc6 116 $contactID = $this->individualCreate();
6a488035 117
9099cab3 118 $customGroup = $this->customGroupCreate(['is_multiple' => 1]);
6a488035
TO
119
120 //create Custom Field of type YesNo(Boolean) Radio
9099cab3 121 $fields = [
8d63d44a 122 'custom_group_id' => $customGroup['id'],
123 'data_type' => 'Boolean',
124 'html_type' => 'Radio',
125 'default_value' => '',
9099cab3 126 ];
8d63d44a 127 $customField = $this->customFieldCreate($fields);
6a488035
TO
128
129 // Retrieve the field ID for sample custom field 'test_Boolean'
9099cab3
CW
130 $params = ['label' => 'test_Boolean'];
131 $field = [];
6a488035
TO
132
133 //get field Id
134 CRM_Core_BAO_CustomField::retrieve($params, $field);
135
8d63d44a 136 $fieldID = $customField['id'];
6a488035
TO
137
138 // valid boolean value '1' for Boolean Radio
139 $yesNo = '1';
9099cab3 140 $params = [
6a488035
TO
141 'entityID' => $contactID,
142 'custom_' . $fieldID => $yesNo,
9099cab3 143 ];
6a488035
TO
144 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
145
146 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
147
148 // Check that the YesNo radio value is stored
9099cab3 149 $params = [
6a488035
TO
150 'entityID' => $contactID,
151 'custom_' . $fieldID => 1,
9099cab3 152 ];
6a488035
TO
153 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
154
155 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
156 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
157 'Verify that the boolean value is stored for contact ' . $contactID
158 );
159
6a488035
TO
160 // Now set YesNo radio to an invalid boolean value and try to reset
161 $badYesNo = '20';
9099cab3 162 $params = [
6a488035
TO
163 'entityID' => $contactID,
164 'custom_' . $fieldID => $badYesNo,
9099cab3 165 ];
6a488035 166
6c6e6187 167 $message = NULL;
6a488035 168 try {
8d63d44a 169 CRM_Core_BAO_CustomValueTable::setValues($params);
0db6c3e1
TO
170 }
171 catch (Exception $e) {
6a488035
TO
172 $message = $e->getMessage();
173 }
174 $errorScope = NULL;
175
176 // Check that an exception has been thrown
481a74f4 177 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
6a488035 178
9099cab3 179 $params = [
6a488035
TO
180 'entityID' => $contactID,
181 'custom_' . $fieldID => 1,
9099cab3 182 ];
6a488035
TO
183 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
184
185 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
186 'Verify that the date value has NOT been updated for contact ' . $contactID
187 );
188
189 // Cleanup
8d63d44a 190 $this->customFieldDelete($customField['id']);
191 $this->customGroupDelete($customGroup['id']);
93ac19cd 192 $this->contactDelete($contactID);
6a488035 193 }
96025800 194
6a488035 195}