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