Merge pull request #8193 from colemanw/scardinius-issue-CRM-16911
[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 = array();
18 $contactID = Contact::createIndividual();
19
20 //create Custom Group
21 $customGroup = Custom::createGroup($params, 'Individual', TRUE);
22
23 //create Custom Field of data type Date
24 $fields = array(
25 'groupId' => $customGroup->id,
26 'dataType' => 'Date',
27 'htmlType' => 'Select Date',
28 );
29 $customField = Custom::createField($params, $fields);
30
31 // Retrieve the field ID for sample custom field 'test_Date'
32 $params = array('label' => 'test_Date');
33 $field = array();
34
35 CRM_Core_BAO_CustomField::retrieve($params, $field);
36 $fieldID = $field['id'];
37
38 // Set test_Date to a valid date value
39 $date = '20080608000000';
40 $params = array(
41 'entityID' => $contactID,
42 'custom_' . $fieldID => $date,
43 );
44 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
45 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
46
47 // Check that the date value is stored
48 $values = array();
49 $params = array(
50 'entityID' => $contactID,
51 'custom_' . $fieldID => 1,
52 );
53 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
54
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
59 );
60
61 // Now set test_Date to an invalid date value and try to reset
62 $badDate = '20080631000000';
63 $params = array(
64 'entityID' => $contactID,
65 'custom_' . $fieldID => $badDate,
66 );
67
68 $errorScope = CRM_Core_TemporaryErrorScope::useException();
69 $message = NULL;
70 try {
71 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
72 }
73 catch (Exception $e) {
74 $message = $e->getMessage();
75 }
76 $errorScope = NULL;
77
78 // Check that an exception has been thrown
79 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
80
81 $params = array(
82 'entityID' => $contactID,
83 'custom_' . $fieldID => 1,
84 );
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
92 $params = array(
93 'entityID' => $contactID,
94 'custom_' . $fieldID => NULL,
95 );
96 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
97
98 // Check that the date value is empty
99 $params = array(
100 'entityID' => $contactID,
101 'custom_' . $fieldID => 1,
102 );
103 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
104 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
105
106 // Cleanup
107 Custom::deleteField($customField);
108 Custom::deleteGroup($customGroup);
109 Contact::delete($contactID);
110 }
111
112 /**
113 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
114 */
115 public function testSetGetValuesYesNoRadio() {
116 $params = array();
117 $contactID = Contact::createIndividual();
118
119 //create Custom Group
120 $customGroup = Custom::createGroup($params, 'Individual', TRUE);
121
122 //create Custom Field of type YesNo(Boolean) Radio
123 $fields = array(
124 'groupId' => $customGroup->id,
125 'dataType' => 'Boolean',
126 'htmlType' => 'Radio',
127 );
128 $customField = Custom::createField($params, $fields);
129
130 // Retrieve the field ID for sample custom field 'test_Boolean'
131 $params = array('label' => 'test_Boolean');
132 $field = array();
133
134 //get field Id
135 CRM_Core_BAO_CustomField::retrieve($params, $field);
136
137 $fieldID = $field['id'];
138
139 // valid boolean value '1' for Boolean Radio
140 $yesNo = '1';
141 $params = array(
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 $values = array();
151 $params = array(
152 'entityID' => $contactID,
153 'custom_' . $fieldID => 1,
154 );
155 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
156
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
160 );
161
162 // Now set YesNo radio to an invalid boolean value and try to reset
163 $badYesNo = '20';
164 $params = array(
165 'entityID' => $contactID,
166 'custom_' . $fieldID => $badYesNo,
167 );
168
169 $errorScope = CRM_Core_TemporaryErrorScope::useException();
170 $message = NULL;
171 try {
172 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
173 }
174 catch (Exception $e) {
175 $message = $e->getMessage();
176 }
177 $errorScope = NULL;
178
179 // Check that an exception has been thrown
180 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
181
182 $params = array(
183 'entityID' => $contactID,
184 'custom_' . $fieldID => 1,
185 );
186 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
187
188 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
189 'Verify that the date value has NOT been updated for contact ' . $contactID
190 );
191
192 // Cleanup
193 Custom::deleteField($customField);
194 Custom::deleteGroup($customGroup);
195 Contact::delete($contactID);
196 }
197
198 }