phpcs - Fix error, "CONST keyword must be lowercase; expected const but found CONST"
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTableSetGetTest.php
CommitLineData
6a488035
TO
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3require_once 'CiviTest/Contact.php';
4require_once 'CiviTest/Custom.php';
aba1cd8b
EM
5
6/**
7 * Class CRM_Core_BAO_CustomValueTableSetGetTest
8 */
6a488035 9class CRM_Core_BAO_CustomValueTableSetGetTest extends CiviUnitTestCase {
6a488035
TO
10
11 function setUp() {
12 parent::setUp();
13 }
14
c490a46a
CW
15 /**
16 * Test setValues() and GetValues() methods with custom Date field
17 */
6a488035
TO
18 function testSetGetValuesDate() {
19 $params = array();
20 $contactID = Contact::createIndividual();
21
22 //create Custom Group
23 $customGroup = Custom::createGroup($params, 'Individual', TRUE);
24
25 //create Custom Field of data type Date
26 $fields = array(
27 'groupId' => $customGroup->id,
28 'dataType' => 'Date',
29 'htmlType' => 'Select Date',
30 );
31 $customField = Custom::createField($params, $fields);
32
33 // Retrieve the field ID for sample custom field 'test_Date'
34 $params = array('label' => 'test_Date');
35 $field = array();
36
37 CRM_Core_BAO_CustomField::retrieve($params, $field);
38 $fieldID = $field['id'];
39
40 // Set test_Date to a valid date value
41 $date = '20080608000000';
42 $params = array(
43 'entityID' => $contactID,
44 'custom_' . $fieldID => $date,
45 );
46 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
47 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
48
49 // Check that the date value is stored
50 $values = array();
51 $params = array(
52 'entityID' => $contactID,
53 'custom_' . $fieldID => 1,
54 );
55 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
56
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
61 );
62
63 // Now set test_Date to an invalid date value and try to reset
64 $badDate = '20080631000000';
65 $params = array(
66 'entityID' => $contactID,
67 'custom_' . $fieldID => $badDate,
68 );
69
70 $errorScope = CRM_Core_TemporaryErrorScope::useException();
71 $message = null;
72 try {
73 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
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 = array(
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 = array(
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 = array(
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 Custom::deleteField($customField);
109 Custom::deleteGroup($customGroup);
110 Contact::delete($contactID);
111 }
112
c490a46a
CW
113 /**
114 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
115 *
116 */
6a488035
TO
117 function testSetGetValuesYesNoRadio() {
118 $params = array();
119 $contactID = Contact::createIndividual();
120
121 //create Custom Group
122 $customGroup = Custom::createGroup($params, 'Individual', TRUE);
123
124 //create Custom Field of type YesNo(Boolean) Radio
125 $fields = array(
126 'groupId' => $customGroup->id,
127 'dataType' => 'Boolean',
128 'htmlType' => 'Radio',
129 );
130 $customField = Custom::createField($params, $fields);
131
132 // Retrieve the field ID for sample custom field 'test_Boolean'
133 $params = array('label' => 'test_Boolean');
134 $field = array();
135
136 //get field Id
137 CRM_Core_BAO_CustomField::retrieve($params, $field);
138
139 $fieldID = $field['id'];
140
141 // valid boolean value '1' for Boolean Radio
142 $yesNo = '1';
143 $params = array(
144 'entityID' => $contactID,
145 'custom_' . $fieldID => $yesNo,
146 );
147 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
148
149 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
150
151 // Check that the YesNo radio value is stored
152 $values = array();
153 $params = array(
154 'entityID' => $contactID,
155 'custom_' . $fieldID => 1,
156 );
157 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
158
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
162 );
163
164
165 // Now set YesNo radio to an invalid boolean value and try to reset
166 $badYesNo = '20';
167 $params = array(
168 'entityID' => $contactID,
169 'custom_' . $fieldID => $badYesNo,
170 );
171
172 $errorScope = CRM_Core_TemporaryErrorScope::useException();
173 $message = null;
174 try {
175 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
176 } catch (Exception $e) {
177 $message = $e->getMessage();
178 }
179 $errorScope = NULL;
180
181 // Check that an exception has been thrown
182 $this->assertNotNull( $message, 'Verify than an exception is thrown when bad boolean is passed' );
183
184 $params = array(
185 'entityID' => $contactID,
186 'custom_' . $fieldID => 1,
187 );
188 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
189
190 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
191 'Verify that the date value has NOT been updated for contact ' . $contactID
192 );
193
194 // Cleanup
195 Custom::deleteField($customField);
196 Custom::deleteGroup($customGroup);
197 Contact::delete($contactID);
198 }
199}
200