Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
fc928539 | 2 | /** |
92915c55 | 3 | * +--------------------------------------------------------------------+ |
81621fee | 4 | * | CiviCRM version 4.7 | |
92915c55 | 5 | * +--------------------------------------------------------------------+ |
e7112fa7 | 6 | * | Copyright CiviCRM LLC (c) 2004-2015 | |
92915c55 TO |
7 | * +--------------------------------------------------------------------+ |
8 | * | This file is a part of CiviCRM. | | |
9 | * | | | |
10 | * | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | * | under the terms of the GNU Affero General Public License | | |
12 | * | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | * | | | |
14 | * | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | * | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | * | See the GNU Affero General Public License for more details. | | |
18 | * | | | |
19 | * | You should have received a copy of the GNU Affero General Public | | |
20 | * | License and the CiviCRM Licensing Exception along | | |
21 | * | with this program; if not, contact CiviCRM LLC | | |
22 | * | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | * | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | * | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | * +--------------------------------------------------------------------+ | |
6c6e6187 | 26 | */ |
6a488035 TO |
27 | |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
e9479dcf EM |
29 | |
30 | /** | |
31 | * Class api_v3_CustomValueTest | |
32 | */ | |
6a488035 | 33 | class api_v3_CustomValueTest extends CiviUnitTestCase { |
6c6e6187 | 34 | protected $_apiversion = 3; |
6a488035 | 35 | protected $ids; |
ec28b24d | 36 | protected $optionGroup; |
b7c9bc4c | 37 | |
6a488035 TO |
38 | public $DBResetRequired = FALSE; |
39 | ||
00be9182 | 40 | public function setUp() { |
6a488035 | 41 | parent::setUp(); |
ec28b24d | 42 | } |
43 | ||
6fca1a34 | 44 | public function _populateOptionAndCustomGroup($type = NULL) { |
ec28b24d | 45 | $dataValues = array( |
8007b543 | 46 | 'integer' => array(1, 2, 3), |
47 | 'number' => array(10.11, 20.22, 30.33), | |
ec28b24d | 48 | 'string' => array(substr(sha1(rand()), 0, 4), substr(sha1(rand()), 0, 3), substr(sha1(rand()), 0, 2)), |
872a35c1 | 49 | 'country' => array_rand(CRM_Core_PseudoConstant::country(FALSE, FALSE), 3), |
ec28b24d | 50 | 'state_province' => array_rand(CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE), 3), |
51 | 'date' => NULL, | |
a6d226f4 | 52 | 'contact' => NULL, |
872a35c1 | 53 | 'boolean' => NULL, |
6a488035 | 54 | ); |
ec28b24d | 55 | |
6fca1a34 | 56 | $dataValues = !empty($type) ? array($type => $dataValues[$type]) : $dataValues; |
57 | ||
ec28b24d | 58 | foreach ($dataValues as $dataType => $values) { |
a6d226f4 | 59 | $this->optionGroup[$dataType] = array('values' => $values); |
ec28b24d | 60 | if (!empty($values)) { |
ec28b24d | 61 | $result = $this->callAPISuccess('OptionGroup', 'create', |
62 | array( | |
63 | 'name' => "{$dataType}_group", | |
64 | 'api.option_value.create' => array('label' => "$dataType 1", 'value' => $values[0]), | |
65 | 'api.option_value.create.1' => array('label' => "$dataType 2", 'value' => $values[1]), | |
66 | 'api.option_value.create.2' => array('label' => "$dataType 3", 'value' => $values[2]), | |
67 | ) | |
68 | ); | |
69 | $this->optionGroup[$dataType]['id'] = $result['id']; | |
70 | } | |
a6d226f4 | 71 | elseif ($dataType == 'contact') { |
f819588f | 72 | for ($i = 0; $i < 3; $i++) { |
73 | $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com')); | |
a6d226f4 | 74 | $this->optionGroup[$dataType]['values'][$i] = $result['id']; |
75 | } | |
76 | } | |
ec28b24d | 77 | $this->ids[$dataType] = $this->entityCustomGroupWithSingleFieldCreate("$dataType Custom Group", 'Contacts'); |
78 | } | |
79 | ||
6a488035 TO |
80 | } |
81 | ||
00be9182 | 82 | public function tearDown() { |
6a488035 TO |
83 | $tablesToTruncate = array( |
84 | 'civicrm_email', | |
85 | 'civicrm_custom_field', | |
86 | 'civicrm_custom_group', | |
87 | 'civicrm_contact', | |
88 | ); | |
89 | ||
90 | // true tells quickCleanup to drop any tables that might have been created in the test | |
91 | $this->quickCleanup($tablesToTruncate, TRUE); | |
6fca1a34 | 92 | |
93 | // cleanup created option group for each custom-set before running next test | |
94 | if (!empty($this->optionGroup)) { | |
95 | foreach ($this->optionGroup as $type => $value) { | |
96 | if (!empty($value['id'])) { | |
97 | $this->callAPISuccess('OptionGroup', 'delete', array('id' => $value['id'])); | |
98 | } | |
99 | } | |
100 | } | |
6a488035 TO |
101 | } |
102 | ||
103 | public function testCreateCustomValue() { | |
6fca1a34 | 104 | $this->_populateOptionAndCustomGroup(); |
105 | ||
ec28b24d | 106 | $customFieldDataType = CRM_Core_BAO_CustomField::dataType(); |
107 | $dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml(); | |
108 | $count = 0; | |
6a488035 | 109 | |
ec28b24d | 110 | foreach ($customFieldDataType as $dataType => $label) { |
ec28b24d | 111 | switch ($dataType) { |
ec28b24d | 112 | case 'String': |
113 | case 'Link': | |
114 | case 'Int': | |
115 | case 'Float': | |
116 | case 'Money': | |
872a35c1 | 117 | case 'Date': |
118 | case 'Country': | |
119 | case 'StateProvince': | |
120 | case 'Boolean': | |
121 | ||
122 | //Based on the custom field data-type choose desired SQL operators(to test with) and basic $type | |
ec28b24d | 123 | if (in_array($dataType, array('String', 'Link'))) { |
872a35c1 | 124 | $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'IS NOT NULL', 'IS NULL'); |
ec28b24d | 125 | $type = 'string'; |
126 | } | |
872a35c1 | 127 | elseif ($dataType == 'Boolean') { |
128 | $validSQLOperators = array('=', '!=', 'IS NOT NULL', 'IS NULL'); | |
129 | $type = 'boolean'; | |
130 | } | |
ec28b24d | 131 | else { |
872a35c1 | 132 | if ($dataType == 'Country') { |
133 | $type = 'country'; | |
134 | } | |
135 | elseif ($dataType == 'StateProvince') { | |
ec28b24d | 136 | $type = 'state_province'; |
137 | } | |
a6d226f4 | 138 | elseif ($dataType == 'ContactReference') { |
139 | $type = 'contact'; | |
140 | } | |
141 | elseif ($dataType == 'Date') { | |
142 | $type = 'date'; | |
143 | } | |
ec28b24d | 144 | else { |
a6d226f4 | 145 | $type = $dataType == 'Int' ? 'integer' : 'number'; |
ec28b24d | 146 | } |
872a35c1 | 147 | $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL'); |
ec28b24d | 148 | } |
6a488035 | 149 | |
872a35c1 | 150 | //Create custom field of $dataType and html-type $html |
ec28b24d | 151 | foreach ($dataToHtmlTypes[$count] as $html) { |
152 | $params = array( | |
153 | 'custom_group_id' => $this->ids[$type]['custom_group_id'], | |
154 | 'label' => "$dataType - $html", | |
155 | 'data_type' => $dataType, | |
156 | 'html_type' => $html, | |
157 | 'default_value' => NULL, | |
158 | ); | |
872a35c1 | 159 | if (!in_array($html, array('Text', 'TextArea')) && !in_array($dataType, array('Link', 'Date', 'ContactReference', 'Boolean'))) { |
ec28b24d | 160 | $params += array('option_group_id' => $this->optionGroup[$type]['id']); |
161 | } | |
162 | $customField = $this->customFieldCreate($params); | |
872a35c1 | 163 | //Now test with $validSQLOperator SQL operators against its custom value(s) |
164 | $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperators, $type); | |
ec28b24d | 165 | } |
166 | $count++; | |
167 | break; | |
6a488035 | 168 | |
ec28b24d | 169 | default: |
872a35c1 | 170 | // skipping File data-type |
ec28b24d | 171 | $count++; |
172 | break; | |
173 | } | |
174 | } | |
175 | } | |
6a488035 | 176 | |
ec28b24d | 177 | public function _testCustomValue($customField, $sqlOps, $type) { |
178 | $isSerialized = CRM_Core_BAO_CustomField::isSerialized($customField); | |
179 | $customId = $customField['id']; | |
6a488035 | 180 | $params = array( |
6a488035 | 181 | 'contact_type' => 'Individual', |
ec28b24d | 182 | 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com', |
6a488035 | 183 | ); |
fc928539 | 184 | $result = $this->callAPISuccess('Contact', 'create', $params); |
ec28b24d | 185 | $contactId = $result['id']; |
6a488035 | 186 | |
8007b543 | 187 | $count = rand(1, 2); |
ec28b24d | 188 | $seperator = CRM_Core_DAO::VALUE_SEPARATOR; |
189 | if ($isSerialized) { | |
190 | $selectedValue = $this->optionGroup[$type]['values']; | |
191 | $notselectedValue = $selectedValue[$count]; | |
192 | unset($selectedValue[$count]); | |
193 | } | |
194 | elseif ($customField['html_type'] == 'Link') { | |
195 | $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com"; | |
196 | $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com"; | |
197 | } | |
198 | elseif ($type == 'date') { | |
199 | $selectedValue = date('Ymd'); | |
a6d226f4 | 200 | $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday')); |
201 | $greaterSelectedValue = date('Ymd', strtotime('+ 1 day')); | |
202 | } | |
203 | elseif ($type == 'contact') { | |
204 | $selectedValue = $this->optionGroup[$type]['values'][1]; | |
205 | $notselectedValue = $this->optionGroup[$type]['values'][0]; | |
ec28b24d | 206 | } |
872a35c1 | 207 | elseif ($type == 'boolean') { |
208 | $selectedValue = 1; | |
209 | $notselectedValue = 0; | |
210 | } | |
ec28b24d | 211 | else { |
212 | $selectedValue = $this->optionGroup[$type]['values'][0]; | |
213 | $notselectedValue = $this->optionGroup[$type]['values'][$count]; | |
a6d226f4 | 214 | if (in_array(">", $sqlOps)) { |
215 | $greaterSelectedValue = $selectedValue + 1; | |
216 | $lesserSelectedValue = $selectedValue - 1; | |
217 | } | |
ec28b24d | 218 | } |
6a488035 | 219 | |
8007b543 | 220 | $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue); |
ec28b24d | 221 | $this->callAPISuccess('CustomValue', 'create', $params); |
6c6e6187 | 222 | |
ec28b24d | 223 | foreach ($sqlOps as $op) { |
a6d226f4 | 224 | $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op); |
ec28b24d | 225 | switch ($op) { |
a6d226f4 | 226 | case '=': |
227 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => (is_array($selectedValue) ? implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValue) : $selectedValue))); | |
228 | $this->assertEquals($contactId, $result['id']); | |
a6d226f4 | 229 | break; |
842ee194 | 230 | |
ec28b24d | 231 | case '!=': |
a6d226f4 | 232 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue))); |
a4a68fa4 | 233 | $this->assertEquals(TRUE, array_key_exists($contactId, $result['values'])); |
ec28b24d | 234 | break; |
6c6e6187 | 235 | |
ec28b24d | 236 | case '>': |
237 | case '<': | |
238 | case '>=': | |
239 | case '<=': | |
a6d226f4 | 240 | if ($isSerialized) { |
241 | continue; | |
242 | } | |
ec28b24d | 243 | // To be precise in for these operator we can't just rely on one contact, |
244 | // hence creating multiple contact with custom value less/more then $selectedValue respectively | |
245 | $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man2@yahoo.com')); | |
246 | $contactId2 = $result['id']; | |
8007b543 | 247 | $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue)); |
842ee194 | 248 | |
ec28b24d | 249 | if ($op == '>') { |
a6d226f4 | 250 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $lesserSelectedValue))); |
ec28b24d | 251 | $this->assertEquals($contactId, $result['id']); |
252 | } | |
253 | elseif ($op == '<') { | |
a6d226f4 | 254 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue))); |
ec28b24d | 255 | $this->assertEquals($contactId2, $result['id']); |
256 | } | |
257 | else { | |
258 | $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com')); | |
259 | $contactId3 = $result['id']; | |
8007b543 | 260 | $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue)); |
842ee194 | 261 | |
a6d226f4 | 262 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue))); |
ec28b24d | 263 | |
264 | $this->assertEquals($contactId, $result['values'][$contactId]['id']); | |
265 | if ($op == '>=') { | |
266 | $this->assertEquals($contactId3, $result['values'][$contactId3]['id']); | |
267 | } | |
268 | else { | |
269 | $this->assertEquals($contactId2, $result['values'][$contactId2]['id']); | |
270 | } | |
271 | $this->callAPISuccess('contact', 'delete', array('id' => $contactId3)); | |
272 | } | |
273 | ||
274 | $this->callAPISuccess('contact', 'delete', array('id' => $contactId2)); | |
275 | break; | |
276 | ||
277 | case 'IN': | |
a6d226f4 | 278 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $selectedValue))); |
ec28b24d | 279 | $this->assertEquals($contactId, $result['id']); |
280 | break; | |
281 | ||
282 | case 'NOT IN': | |
a6d226f4 | 283 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $notselectedValue))); |
ec28b24d | 284 | $this->assertEquals($contactId, $result['id']); |
285 | break; | |
286 | ||
287 | case 'LIKE': | |
a6d226f4 | 288 | $selectedValue = is_array($selectedValue) ? $selectedValue[0] : $selectedValue; |
289 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => "%$selectedValue%"))); | |
ec28b24d | 290 | $this->assertEquals($contactId, $result['id']); |
291 | break; | |
6a488035 | 292 | |
ec28b24d | 293 | case 'NOT LIKE': |
a6d226f4 | 294 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue))); |
ec28b24d | 295 | $this->assertEquals($contactId, $result['id']); |
296 | break; | |
6c6e6187 | 297 | |
ec28b24d | 298 | case 'IS NULL': |
a6d226f4 | 299 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1))); |
ec28b24d | 300 | $this->assertEquals(FALSE, array_key_exists($contactId, $result['values'])); |
301 | break; | |
842ee194 | 302 | |
ec28b24d | 303 | case 'IS NOT NULL': |
a6d226f4 | 304 | $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1))); |
ec28b24d | 305 | $this->assertEquals($contactId, $result['id']); |
306 | break; | |
307 | } | |
308 | } | |
6c6e6187 | 309 | |
ec28b24d | 310 | $this->callAPISuccess('Contact', 'delete', array('id' => $contactId)); |
842ee194 | 311 | } |
96025800 | 312 | |
58eaa092 CW |
313 | /** |
314 | * Ensure custom data is updated when option values are modified | |
315 | * | |
316 | * @link https://issues.civicrm.org/jira/browse/CRM-11856 | |
317 | * | |
318 | * @throws \CiviCRM_API3_Exception | |
319 | */ | |
320 | public function testAlterOptionValue() { | |
6fca1a34 | 321 | $this->_populateOptionAndCustomGroup('string'); |
322 | ||
58eaa092 | 323 | $selectField = $this->customFieldCreate(array( |
6fca1a34 | 324 | 'custom_group_id' => $this->ids['string']['custom_group_id'], |
58eaa092 CW |
325 | 'label' => 'Custom Select', |
326 | 'html_type' => 'Select', | |
6fca1a34 | 327 | 'option_group_id' => $this->optionGroup['string']['id'], |
58eaa092 CW |
328 | )); |
329 | $selectField = civicrm_api3('customField', 'getsingle', array('id' => $selectField['id'])); | |
330 | $radioField = $this->customFieldCreate(array( | |
6fca1a34 | 331 | 'custom_group_id' => $this->ids['string']['custom_group_id'], |
58eaa092 CW |
332 | 'label' => 'Custom Radio', |
333 | 'html_type' => 'Radio', | |
334 | 'option_group_id' => $selectField['option_group_id'], | |
335 | )); | |
336 | $multiSelectField = $this->customFieldCreate(array( | |
6fca1a34 | 337 | 'custom_group_id' => $this->ids['string']['custom_group_id'], |
58eaa092 CW |
338 | 'label' => 'Custom Multi-Select', |
339 | 'html_type' => 'Multi-Select', | |
340 | 'option_group_id' => $selectField['option_group_id'], | |
341 | )); | |
342 | $selectName = 'custom_' . $selectField['id']; | |
343 | $radioName = 'custom_' . $radioField['id']; | |
344 | $multiSelectName = 'custom_' . $multiSelectField['id']; | |
6fca1a34 | 345 | $controlFieldName = 'custom_' . $this->ids['string']['custom_field_id']; |
58eaa092 CW |
346 | |
347 | $params = array( | |
348 | 'first_name' => 'abc4', | |
349 | 'last_name' => 'xyz4', | |
350 | 'contact_type' => 'Individual', | |
351 | 'email' => 'man4@yahoo.com', | |
6fca1a34 | 352 | $selectName => $this->optionGroup['string']['values'][0], |
353 | $multiSelectName => $this->optionGroup['string']['values'], | |
354 | $radioName => $this->optionGroup['string']['values'][1], | |
58eaa092 | 355 | // The control group in a science experiment should be unaffected |
6fca1a34 | 356 | $controlFieldName => $this->optionGroup['string']['values'][2], |
58eaa092 CW |
357 | ); |
358 | ||
359 | $contact = $this->callAPISuccess('Contact', 'create', $params); | |
360 | ||
361 | $result = $this->callAPISuccess('Contact', 'getsingle', array( | |
362 | 'id' => $contact['id'], | |
363 | 'return' => array($selectName, $multiSelectName), | |
364 | )); | |
6fca1a34 | 365 | $this->assertEquals($params[$selectName], $result[$selectName]); |
366 | $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]); | |
58eaa092 CW |
367 | |
368 | $this->callAPISuccess('OptionValue', 'create', array( | |
369 | 'value' => 'one-modified', | |
370 | 'option_group_id' => $selectField['option_group_id'], | |
6fca1a34 | 371 | 'name' => 'string 1', |
58eaa092 CW |
372 | 'options' => array( |
373 | 'match-mandatory' => array('option_group_id', 'name'), | |
374 | ), | |
375 | )); | |
376 | ||
377 | $result = $this->callAPISuccess('Contact', 'getsingle', array( | |
378 | 'id' => $contact['id'], | |
379 | 'return' => array($selectName, $multiSelectName, $controlFieldName, $radioName), | |
380 | )); | |
381 | // Ensure the relevant fields have been updated | |
382 | $this->assertEquals('one-modified', $result[$selectName]); | |
6fca1a34 | 383 | $this->assertEquals(array('one-modified', $params[$radioName], $params[$controlFieldName]), $result[$multiSelectName]); |
58eaa092 | 384 | // This field should not have changed because we didn't alter this option |
6fca1a34 | 385 | $this->assertEquals($params[$radioName], $result[$radioName]); |
58eaa092 | 386 | // This should not have changed because this field doesn't use the affected option group |
6fca1a34 | 387 | $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]); |
58eaa092 CW |
388 | } |
389 | ||
ef10e0b5 | 390 | } |