Merge pull request #8470 from ankitjain28may/master
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTableTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_BAO_CustomValueTableTest
5 * @group headless
6 */
7 class CRM_Core_BAO_CustomValueTableTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11 }
12
13
14 /**
15 * Test store function for country.
16 */
17 public function testStoreCountry() {
18 $params = array();
19 $contactID = Contact::createIndividual();
20 $customGroup = Custom::createGroup($params, 'Individual');
21 $fields = array(
22 'groupId' => $customGroup->id,
23 'dataType' => 'Country',
24 'htmlType' => 'Select Country',
25 );
26
27 $customField = Custom::createField($params, $fields);
28
29 $params[] = array(
30 $customField->id => array(
31 'value' => 1228,
32 'type' => 'Country',
33 'custom_field_id' => $customField->id,
34 'custom_group_id' => $customGroup->id,
35 'table_name' => 'civicrm_value_test_group_' . $customGroup->id,
36 'column_name' => 'test_Country_' . $customField->id,
37 'file_id' => '',
38 ),
39 );
40
41 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
42 // $this->assertDBCompareValue('CRM_Custom_DAO_CustomValue', )
43
44 Custom::deleteField($customField);
45 Custom::deleteGroup($customGroup);
46 $this->contactDelete($contactID);
47 }
48
49 /**
50 * Test store function for file.
51 */
52 public function atestStoreFile() {
53 $params = array();
54 $contactID = Contact::createIndividual();
55 $customGroup = Custom::createGroup($params, 'Individual');
56 $fields = array(
57 'groupId' => $customGroup->id,
58 'dataType' => 'File',
59 'htmlType' => 'File',
60 );
61
62 $customField = Custom::createField($params, $fields);
63
64 $params[] = array(
65 $customField->id => array(
66 'value' => 'i/contact_house.png',
67 'type' => 'File',
68 'custom_field_id' => $customField->id,
69 'custom_group_id' => $customGroup->id,
70 'table_name' => 'civicrm_value_test_group_' . $customGroup->id,
71 'column_name' => 'test_File_' . $customField->id,
72 'file_id' => 1,
73 ),
74 );
75
76 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
77 // $this->assertDBCompareValue('CRM_Custom_DAO_CustomValue', )
78
79 Custom::deleteField($customField);
80 Custom::deleteGroup($customGroup);
81 $this->contactDelete($contactID);
82 }
83
84 /**
85 * Test store function for state province.
86 */
87 public function testStoreStateProvince() {
88 $params = array();
89 $contactID = Contact::createIndividual();
90 $customGroup = Custom::createGroup($params, 'Individual');
91 $fields = array(
92 'groupId' => $customGroup->id,
93 'dataType' => 'StateProvince',
94 'htmlType' => 'Select State/Province',
95 );
96
97 $customField = Custom::createField($params, $fields);
98
99 $params[] = array(
100 $customField->id => array(
101 'value' => 1029,
102 'type' => 'StateProvince',
103 'custom_field_id' => $customField->id,
104 'custom_group_id' => $customGroup->id,
105 'table_name' => 'civicrm_value_test_group_' . $customGroup->id,
106 'column_name' => 'test_StateProvince_' . $customField->id,
107 'file_id' => 1,
108 ),
109 );
110
111 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
112
113 Custom::deleteField($customField);
114 Custom::deleteGroup($customGroup);
115 $this->contactDelete($contactID);
116 }
117
118 /**
119 * Test store function for date.
120 */
121 public function testStoreDate() {
122 $params = array();
123 $contactID = Contact::createIndividual();
124 $customGroup = Custom::createGroup($params, 'Individual');
125 $fields = array(
126 'groupId' => $customGroup->id,
127 'dataType' => 'Date',
128 'htmlType' => 'Select Date',
129 );
130
131 $customField = Custom::createField($params, $fields);
132
133 $params[] = array(
134 $customField->id => array(
135 'value' => '20080608000000',
136 'type' => 'Date',
137 'custom_field_id' => $customField->id,
138 'custom_group_id' => $customGroup->id,
139 'table_name' => 'civicrm_value_test_group_' . $customGroup->id,
140 'column_name' => 'test_Date_' . $customField->id,
141 'file_id' => '',
142 ),
143 );
144
145 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
146 // $this->assertDBCompareValue('CRM_Custom_DAO_CustomValue', )
147
148 Custom::deleteField($customField);
149 Custom::deleteGroup($customGroup);
150 $this->contactDelete($contactID);
151 }
152
153 /**
154 * Test store function for rich text editor.
155 */
156 public function testStoreRichTextEditor() {
157 $params = array();
158 $contactID = Contact::createIndividual();
159 $customGroup = Custom::createGroup($params, 'Individual');
160 $fields = array(
161 'groupId' => $customGroup->id,
162 'htmlType' => 'RichTextEditor',
163 'dataType' => 'Memo',
164 );
165
166 $customField = Custom::createField($params, $fields);
167
168 $params[] = array(
169 $customField->id => array(
170 'value' => '<p><strong>This is a <u>test</u></p>',
171 'type' => 'Memo',
172 'custom_field_id' => $customField->id,
173 'custom_group_id' => $customGroup->id,
174 'table_name' => 'civicrm_value_test_group_' . $customGroup->id,
175 'column_name' => 'test_Memo_' . $customField->id,
176 'file_id' => '',
177 ),
178 );
179
180 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
181 // $this->assertDBCompareValue('CRM_Custom_DAO_CustomValue', )
182
183 Custom::deleteField($customField);
184 Custom::deleteGroup($customGroup);
185 $this->contactDelete($contactID);
186 }
187
188 /**
189 * Test getEntityValues function for stored value.
190 */
191 public function testgetEntityValues() {
192
193 $params = array();
194 $contactID = Contact::createIndividual();
195 $customGroup = Custom::createGroup($params, 'Individual');
196 $fields = array(
197 'groupId' => $customGroup->id,
198 'htmlType' => 'RichTextEditor',
199 'dataType' => 'Memo',
200 );
201
202 $customField = Custom::createField($params, $fields);
203
204 $params[] = array(
205 $customField->id => array(
206 'value' => '<p><strong>This is a <u>test</u></p>',
207 'type' => 'Memo',
208 'custom_field_id' => $customField->id,
209 'custom_group_id' => $customGroup->id,
210 'table_name' => 'civicrm_value_test_group_' . $customGroup->id,
211 'column_name' => 'test_Memo_' . $customField->id,
212 'file_id' => '',
213 ),
214 );
215
216 CRM_Core_BAO_CustomValueTable::store($params, 'civicrm_contact', $contactID);
217 // $this->assertDBCompareValue('CRM_Custom_DAO_CustomValue', )
218
219 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($contactID, 'Individual');
220
221 $this->assertEquals($entityValues[$customField->id], '<p><strong>This is a <u>test</u></p>',
222 'Checking same for returned value.'
223 );
224 Custom::deleteField($customField);
225 Custom::deleteGroup($customGroup);
226 $this->contactDelete($contactID);
227 }
228
229 public function testCustomGroupMultiple() {
230 $params = array();
231 $contactID = Contact::createIndividual();
232 $customGroup = Custom::createGroup($params, 'Individual');
233
234 $fields = array(
235 'groupId' => $customGroup->id,
236 'dataType' => 'String',
237 'htmlType' => 'Text',
238 );
239
240 $customField = Custom::createField($params, $fields);
241
242 $params = array(
243 'entityID' => $contactID,
244 'custom_' . $customField->id . '_-1' => 'First String',
245 );
246 $error = CRM_Core_BAO_CustomValueTable::setValues($params);
247
248 $newParams = array(
249 'entityID' => $contactID,
250 'custom_' . $customField->id => 1,
251 );
252 $result = CRM_Core_BAO_CustomValueTable::getValues($newParams);
253
254 $this->assertEquals($params['custom_' . $customField->id . '_-1'], $result['custom_' . $customField->id]);
255 $this->assertEquals($params['entityID'], $result['entityID']);
256
257 Custom::deleteField($customField);
258 Custom::deleteGroup($customGroup);
259 $this->contactDelete($contactID);
260 }
261
262 }