Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
3 | require_once 'CiviTest/Contact.php'; | |
4 | require_once 'CiviTest/Custom.php'; | |
aba1cd8b EM |
5 | |
6 | /** | |
7 | * Class CRM_Core_BAO_CustomValueTableSetGetTest | |
8 | */ | |
6a488035 | 9 | class CRM_Core_BAO_CustomValueTableSetGetTest extends CiviUnitTestCase { |
aba1cd8b EM |
10 | /** |
11 | * @return array | |
12 | */ | |
6a488035 TO |
13 | function get_info() { |
14 | return array( | |
15 | 'name' => 'Custom Value Table BAO setValues and getValues', | |
16 | 'description' => 'Test setValues and getValues Core_BAO_CustomValueTable methods.', | |
17 | 'group' => 'CiviCRM BAO Tests', | |
18 | ); | |
19 | } | |
20 | ||
21 | function setUp() { | |
22 | parent::setUp(); | |
23 | } | |
24 | ||
25 | /* | |
26 | * Test setValues() and GetValues() methods with custom Date field | |
27 | * | |
28 | */ | |
29 | function testSetGetValuesDate() { | |
30 | $params = array(); | |
31 | $contactID = Contact::createIndividual(); | |
32 | ||
33 | //create Custom Group | |
34 | $customGroup = Custom::createGroup($params, 'Individual', TRUE); | |
35 | ||
36 | //create Custom Field of data type Date | |
37 | $fields = array( | |
38 | 'groupId' => $customGroup->id, | |
39 | 'dataType' => 'Date', | |
40 | 'htmlType' => 'Select Date', | |
41 | ); | |
42 | $customField = Custom::createField($params, $fields); | |
43 | ||
44 | // Retrieve the field ID for sample custom field 'test_Date' | |
45 | $params = array('label' => 'test_Date'); | |
46 | $field = array(); | |
47 | ||
48 | CRM_Core_BAO_CustomField::retrieve($params, $field); | |
49 | $fieldID = $field['id']; | |
50 | ||
51 | // Set test_Date to a valid date value | |
52 | $date = '20080608000000'; | |
53 | $params = array( | |
54 | 'entityID' => $contactID, | |
55 | 'custom_' . $fieldID => $date, | |
56 | ); | |
57 | $result = CRM_Core_BAO_CustomValueTable::setValues($params); | |
58 | $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).'); | |
59 | ||
60 | // Check that the date value is stored | |
61 | $values = array(); | |
62 | $params = array( | |
63 | 'entityID' => $contactID, | |
64 | 'custom_' . $fieldID => 1, | |
65 | ); | |
66 | $values = CRM_Core_BAO_CustomValueTable::getValues($params); | |
67 | ||
68 | $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).'); | |
69 | $this->assertEquals($values['custom_' . $fieldID . '_1'], | |
70 | CRM_Utils_Date::mysqlToIso($date), | |
71 | 'Verify that the date value is stored for contact ' . $contactID | |
72 | ); | |
73 | ||
74 | // Now set test_Date to an invalid date value and try to reset | |
75 | $badDate = '20080631000000'; | |
76 | $params = array( | |
77 | 'entityID' => $contactID, | |
78 | 'custom_' . $fieldID => $badDate, | |
79 | ); | |
80 | ||
81 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
82 | $message = null; | |
83 | try { | |
84 | $result = CRM_Core_BAO_CustomValueTable::setValues($params); | |
85 | } catch ( Exception $e ) { | |
86 | $message = $e->getMessage(); | |
87 | } | |
88 | $errorScope = NULL; | |
89 | ||
90 | // Check that an exception has been thrown | |
91 | $this->assertNotNull( $message, 'Verify than an exception is thrown when bad date is passed' ); | |
92 | ||
93 | $params = array( | |
94 | 'entityID' => $contactID, | |
95 | 'custom_' . $fieldID => 1, | |
96 | ); | |
97 | $values = CRM_Core_BAO_CustomValueTable::getValues($params); | |
98 | $this->assertEquals($values['custom_' . $fieldID . '_1'], | |
99 | CRM_Utils_Date::mysqlToIso($date), | |
100 | 'Verify that the date value has NOT been updated for contact ' . $contactID | |
101 | ); | |
102 | ||
103 | // Test setting test_Date to null | |
104 | $params = array( | |
105 | 'entityID' => $contactID, | |
106 | 'custom_' . $fieldID => NULL, | |
107 | ); | |
108 | $result = CRM_Core_BAO_CustomValueTable::setValues($params); | |
109 | ||
110 | // Check that the date value is empty | |
111 | $params = array( | |
112 | 'entityID' => $contactID, | |
113 | 'custom_' . $fieldID => 1, | |
114 | ); | |
115 | $values = CRM_Core_BAO_CustomValueTable::getValues($params); | |
116 | $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).'); | |
117 | ||
118 | // Cleanup | |
119 | Custom::deleteField($customField); | |
120 | Custom::deleteGroup($customGroup); | |
121 | Contact::delete($contactID); | |
122 | } | |
123 | ||
124 | /* | |
125 | * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio | |
126 | * | |
127 | */ | |
128 | function testSetGetValuesYesNoRadio() { | |
129 | $params = array(); | |
130 | $contactID = Contact::createIndividual(); | |
131 | ||
132 | //create Custom Group | |
133 | $customGroup = Custom::createGroup($params, 'Individual', TRUE); | |
134 | ||
135 | //create Custom Field of type YesNo(Boolean) Radio | |
136 | $fields = array( | |
137 | 'groupId' => $customGroup->id, | |
138 | 'dataType' => 'Boolean', | |
139 | 'htmlType' => 'Radio', | |
140 | ); | |
141 | $customField = Custom::createField($params, $fields); | |
142 | ||
143 | // Retrieve the field ID for sample custom field 'test_Boolean' | |
144 | $params = array('label' => 'test_Boolean'); | |
145 | $field = array(); | |
146 | ||
147 | //get field Id | |
148 | CRM_Core_BAO_CustomField::retrieve($params, $field); | |
149 | ||
150 | $fieldID = $field['id']; | |
151 | ||
152 | // valid boolean value '1' for Boolean Radio | |
153 | $yesNo = '1'; | |
154 | $params = array( | |
155 | 'entityID' => $contactID, | |
156 | 'custom_' . $fieldID => $yesNo, | |
157 | ); | |
158 | $result = CRM_Core_BAO_CustomValueTable::setValues($params); | |
159 | ||
160 | $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).'); | |
161 | ||
162 | // Check that the YesNo radio value is stored | |
163 | $values = array(); | |
164 | $params = array( | |
165 | 'entityID' => $contactID, | |
166 | 'custom_' . $fieldID => 1, | |
167 | ); | |
168 | $values = CRM_Core_BAO_CustomValueTable::getValues($params); | |
169 | ||
170 | $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).'); | |
171 | $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo, | |
172 | 'Verify that the boolean value is stored for contact ' . $contactID | |
173 | ); | |
174 | ||
175 | ||
176 | // Now set YesNo radio to an invalid boolean value and try to reset | |
177 | $badYesNo = '20'; | |
178 | $params = array( | |
179 | 'entityID' => $contactID, | |
180 | 'custom_' . $fieldID => $badYesNo, | |
181 | ); | |
182 | ||
183 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
184 | $message = null; | |
185 | try { | |
186 | $result = CRM_Core_BAO_CustomValueTable::setValues($params); | |
187 | } catch (Exception $e) { | |
188 | $message = $e->getMessage(); | |
189 | } | |
190 | $errorScope = NULL; | |
191 | ||
192 | // Check that an exception has been thrown | |
193 | $this->assertNotNull( $message, 'Verify than an exception is thrown when bad boolean is passed' ); | |
194 | ||
195 | $params = array( | |
196 | 'entityID' => $contactID, | |
197 | 'custom_' . $fieldID => 1, | |
198 | ); | |
199 | $values = CRM_Core_BAO_CustomValueTable::getValues($params); | |
200 | ||
201 | $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo, | |
202 | 'Verify that the date value has NOT been updated for contact ' . $contactID | |
203 | ); | |
204 | ||
205 | // Cleanup | |
206 | Custom::deleteField($customField); | |
207 | Custom::deleteGroup($customGroup); | |
208 | Contact::delete($contactID); | |
209 | } | |
210 | } | |
211 |