3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2016 |
7 * +--------------------------------------------------------------------+
8 * | This file is a part of CiviCRM. |
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. |
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. |
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 * +--------------------------------------------------------------------+
29 * Class api_v3_CustomValueTest
32 class api_v3_CustomValueTest
extends CiviUnitTestCase
{
33 protected $_apiversion = 3;
35 protected $optionGroup;
37 public $DBResetRequired = FALSE;
39 public function setUp() {
43 public function _populateOptionAndCustomGroup($type = NULL) {
45 'integer' => array(1, 2, 3),
46 'number' => array(10.11, 20.22, 30.33),
47 'string' => array(substr(sha1(rand()), 0, 4), substr(sha1(rand()), 0, 3), substr(sha1(rand()), 0, 2)),
48 // 'country' => array_rand(CRM_Core_PseudoConstant::country(FALSE, FALSE), 3),
49 // This does not work in the test at the moment due to caching issues.
50 //'state_province' => array_rand(CRM_Core_PseudoConstant::stateProvince(FALSE, FALSE), 3),
56 $dataValues = !empty($type) ?
array($type => $dataValues[$type]) : $dataValues;
58 foreach ($dataValues as $dataType => $values) {
59 $this->optionGroup
[$dataType] = array('values' => $values);
60 if (!empty($values)) {
61 $result = $this->callAPISuccess('OptionGroup', 'create',
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]),
69 $this->optionGroup
[$dataType]['id'] = $result['id'];
71 elseif ($dataType == 'contact') {
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'));
74 $this->optionGroup
[$dataType]['values'][$i] = $result['id'];
77 $this->ids
[$dataType] = $this->entityCustomGroupWithSingleFieldCreate("$dataType Custom Group", 'Contacts');
82 public function tearDown() {
83 $tablesToTruncate = array(
85 'civicrm_custom_field',
86 'civicrm_custom_group',
90 // true tells quickCleanup to drop any tables that might have been created in the test
91 $this->quickCleanup($tablesToTruncate, TRUE);
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']));
103 public function testCreateCustomValue() {
104 $this->_populateOptionAndCustomGroup();
106 $customFieldDataType = CRM_Core_BAO_CustomField
::dataType();
107 $dataToHtmlTypes = CRM_Core_BAO_CustomField
::dataToHtml();
109 $optionSupportingHTMLTypes = array('Select', 'Radio', 'CheckBox', 'AdvMulti-Select', 'Autocomplete-Select', 'Multi-Select');
111 foreach ($customFieldDataType as $dataType => $label) {
114 // case 'StateProvince':
123 //Based on the custom field data-type choose desired SQL operators(to test with) and basic $type
124 if (in_array($dataType, array('String', 'Link'))) {
125 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'IS NOT NULL', 'IS NULL');
128 elseif ($dataType == 'Boolean') {
129 $validSQLOperators = array('=', '!=', 'IS NOT NULL', 'IS NULL');
133 if ($dataType == 'Country') {
136 elseif ($dataType == 'StateProvince') {
137 $type = 'state_province';
139 elseif ($dataType == 'ContactReference') {
142 elseif ($dataType == 'Date') {
146 $type = $dataType == 'Int' ?
'integer' : 'number';
148 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL');
151 //Create custom field of $dataType and html-type $html
152 foreach ($dataToHtmlTypes[$count] as $html) {
153 // per CRM-18568 the like operator does not currently work for fields with options.
154 // the LIKE operator could potentially bypass ACLs (as could IS NOT NULL) and some thought needs to be given
156 if (in_array($html, $optionSupportingHTMLTypes)) {
157 $validSQLOperators = array_diff($validSQLOperators, array('LIKE', 'NOT LIKE'));
160 'custom_group_id' => $this->ids
[$type]['custom_group_id'],
161 'label' => "$dataType - $html",
162 'data_type' => $dataType,
163 'html_type' => $html,
164 'default_value' => NULL,
166 if (!in_array($html, array('Text', 'TextArea')) && !in_array($dataType, array('Link', 'Date', 'ContactReference', 'Boolean'))) {
167 $params +
= array('option_group_id' => $this->optionGroup
[$type]['id']);
169 $customField = $this->customFieldCreate($params);
170 //Now test with $validSQLOperator SQL operators against its custom value(s)
171 $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperators, $type);
177 // skipping File data-type & state province due to caching issues
184 public function _testCustomValue($customField, $sqlOps, $type) {
185 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($customField);
186 $customId = $customField['id'];
188 'contact_type' => 'Individual',
189 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com',
191 $result = $this->callAPISuccess('Contact', 'create', $params);
192 $contactId = $result['id'];
197 $selectedValue = $this->optionGroup
[$type]['values'];
198 $notselectedValue = $selectedValue[$count];
199 unset($selectedValue[$count]);
201 elseif ($customField['html_type'] == 'Link') {
202 $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
203 $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
205 elseif ($type == 'date') {
206 $selectedValue = date('Ymd');
207 $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday'));
208 $greaterSelectedValue = date('Ymd', strtotime('+ 1 day'));
210 elseif ($type == 'contact') {
211 $selectedValue = $this->optionGroup
[$type]['values'][1];
212 $notselectedValue = $this->optionGroup
[$type]['values'][0];
214 elseif ($type == 'boolean') {
216 $notselectedValue = 0;
219 $selectedValue = $this->optionGroup
[$type]['values'][0];
220 $notselectedValue = $this->optionGroup
[$type]['values'][$count];
221 if (in_array(">", $sqlOps)) {
222 $greaterSelectedValue = $selectedValue +
1;
223 $lesserSelectedValue = $selectedValue - 1;
227 $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
228 $this->callAPISuccess('CustomValue', 'create', $params);
230 foreach ($sqlOps as $op) {
231 $qillOp = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
234 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => (is_array($selectedValue) ?
implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $selectedValue) : $selectedValue)));
235 $this->assertEquals($contactId, $result['id']);
239 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
240 $this->assertEquals(TRUE, array_key_exists($contactId, $result['values']));
250 // To be precise in for these operator we can't just rely on one contact,
251 // hence creating multiple contact with custom value less/more then $selectedValue respectively
252 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man2@yahoo.com'));
253 $contactId2 = $result['id'];
254 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue));
257 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $lesserSelectedValue)));
258 $this->assertEquals($contactId, $result['id']);
260 elseif ($op == '<') {
261 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
262 $this->assertEquals($contactId2, $result['id']);
265 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com'));
266 $contactId3 = $result['id'];
267 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue));
269 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
271 $this->assertEquals($contactId, $result['values'][$contactId]['id']);
273 $this->assertEquals($contactId3, $result['values'][$contactId3]['id']);
276 $this->assertEquals($contactId2, $result['values'][$contactId2]['id']);
278 $this->callAPISuccess('contact', 'delete', array('id' => $contactId3));
281 $this->callAPISuccess('contact', 'delete', array('id' => $contactId2));
285 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $selectedValue)));
286 $this->assertEquals($contactId, $result['id']);
290 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $notselectedValue)));
291 $this->assertEquals($contactId, $result['id']);
295 $selectedValue = is_array($selectedValue) ?
$selectedValue[0] : $selectedValue;
296 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => "%$selectedValue%")));
297 $this->assertEquals($contactId, $result['id']);
301 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
302 $this->assertEquals($contactId, $result['id']);
306 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
307 $this->assertEquals(FALSE, array_key_exists($contactId, $result['values']));
311 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
312 $this->assertEquals($contactId, $result['id']);
317 $this->callAPISuccess('Contact', 'delete', array('id' => $contactId));
321 * Ensure custom data is updated when option values are modified
323 * @link https://issues.civicrm.org/jira/browse/CRM-11856
325 * @throws \CiviCRM_API3_Exception
327 public function testAlterOptionValue() {
328 $this->_populateOptionAndCustomGroup('string');
330 $selectField = $this->customFieldCreate(array(
331 'custom_group_id' => $this->ids
['string']['custom_group_id'],
332 'label' => 'Custom Select',
333 'html_type' => 'Select',
334 'option_group_id' => $this->optionGroup
['string']['id'],
336 $selectField = civicrm_api3('customField', 'getsingle', array('id' => $selectField['id']));
337 $radioField = $this->customFieldCreate(array(
338 'custom_group_id' => $this->ids
['string']['custom_group_id'],
339 'label' => 'Custom Radio',
340 'html_type' => 'Radio',
341 'option_group_id' => $selectField['option_group_id'],
343 $multiSelectField = $this->customFieldCreate(array(
344 'custom_group_id' => $this->ids
['string']['custom_group_id'],
345 'label' => 'Custom Multi-Select',
346 'html_type' => 'Multi-Select',
347 'option_group_id' => $selectField['option_group_id'],
349 $selectName = 'custom_' . $selectField['id'];
350 $radioName = 'custom_' . $radioField['id'];
351 $multiSelectName = 'custom_' . $multiSelectField['id'];
352 $controlFieldName = 'custom_' . $this->ids
['string']['custom_field_id'];
355 'first_name' => 'abc4',
356 'last_name' => 'xyz4',
357 'contact_type' => 'Individual',
358 'email' => 'man4@yahoo.com',
359 $selectName => $this->optionGroup
['string']['values'][0],
360 $multiSelectName => $this->optionGroup
['string']['values'],
361 $radioName => $this->optionGroup
['string']['values'][1],
362 // The control group in a science experiment should be unaffected
363 $controlFieldName => $this->optionGroup
['string']['values'][2],
366 $contact = $this->callAPISuccess('Contact', 'create', $params);
368 $result = $this->callAPISuccess('Contact', 'getsingle', array(
369 'id' => $contact['id'],
370 'return' => array($selectName, $multiSelectName),
372 $this->assertEquals($params[$selectName], $result[$selectName]);
373 $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]);
375 $this->callAPISuccess('OptionValue', 'create', array(
376 'value' => 'one-modified',
377 'option_group_id' => $selectField['option_group_id'],
378 'name' => 'string 1',
380 'match-mandatory' => array('option_group_id', 'name'),
384 $result = $this->callAPISuccess('Contact', 'getsingle', array(
385 'id' => $contact['id'],
386 'return' => array($selectName, $multiSelectName, $controlFieldName, $radioName),
388 // Ensure the relevant fields have been updated
389 $this->assertEquals('one-modified', $result[$selectName]);
390 $this->assertEquals(array('one-modified', $params[$radioName], $params[$controlFieldName]), $result[$multiSelectName]);
391 // This field should not have changed because we didn't alter this option
392 $this->assertEquals($params[$radioName], $result[$radioName]);
393 // This should not have changed because this field doesn't use the affected option group
394 $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]);