3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
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 * +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class api_v3_CustomValueTest
33 class api_v3_CustomValueTest
extends CiviUnitTestCase
{
34 protected $_apiversion = 3;
36 protected $optionGroup;
38 public $DBResetRequired = FALSE;
40 public function setUp() {
44 public function _populateOptionAndCustomGroup($type = NULL) {
46 'integer' => array(1, 2, 3),
47 'number' => array(10.11, 20.22, 30.33),
48 'string' => array(substr(sha1(rand()), 0, 4), substr(sha1(rand()), 0, 3), substr(sha1(rand()), 0, 2)),
49 'country' => array_rand(CRM_Core_PseudoConstant
::country(FALSE, FALSE), 3),
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();
110 foreach ($customFieldDataType as $dataType => $label) {
119 case 'StateProvince':
122 //Based on the custom field data-type choose desired SQL operators(to test with) and basic $type
123 if (in_array($dataType, array('String', 'Link'))) {
124 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'IS NOT NULL', 'IS NULL');
127 elseif ($dataType == 'Boolean') {
128 $validSQLOperators = array('=', '!=', 'IS NOT NULL', 'IS NULL');
132 if ($dataType == 'Country') {
135 elseif ($dataType == 'StateProvince') {
136 $type = 'state_province';
138 elseif ($dataType == 'ContactReference') {
141 elseif ($dataType == 'Date') {
145 $type = $dataType == 'Int' ?
'integer' : 'number';
147 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL');
150 //Create custom field of $dataType and html-type $html
151 foreach ($dataToHtmlTypes[$count] as $html) {
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,
159 if (!in_array($html, array('Text', 'TextArea')) && !in_array($dataType, array('Link', 'Date', 'ContactReference', 'Boolean'))) {
160 $params +
= array('option_group_id' => $this->optionGroup
[$type]['id']);
162 $customField = $this->customFieldCreate($params);
163 //Now test with $validSQLOperator SQL operators against its custom value(s)
164 $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperators, $type);
170 // skipping File data-type
177 public function _testCustomValue($customField, $sqlOps, $type) {
178 $isSerialized = CRM_Core_BAO_CustomField
::isSerialized($customField);
179 $customId = $customField['id'];
181 'contact_type' => 'Individual',
182 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com',
184 $result = $this->callAPISuccess('Contact', 'create', $params);
185 $contactId = $result['id'];
188 $seperator = CRM_Core_DAO
::VALUE_SEPARATOR
;
190 $selectedValue = $this->optionGroup
[$type]['values'];
191 $notselectedValue = $selectedValue[$count];
192 unset($selectedValue[$count]);
194 elseif ($customField['html_type'] == 'Link') {
195 $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
196 $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
198 elseif ($type == 'date') {
199 $selectedValue = date('Ymd');
200 $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday'));
201 $greaterSelectedValue = date('Ymd', strtotime('+ 1 day'));
203 elseif ($type == 'contact') {
204 $selectedValue = $this->optionGroup
[$type]['values'][1];
205 $notselectedValue = $this->optionGroup
[$type]['values'][0];
207 elseif ($type == 'boolean') {
209 $notselectedValue = 0;
212 $selectedValue = $this->optionGroup
[$type]['values'][0];
213 $notselectedValue = $this->optionGroup
[$type]['values'][$count];
214 if (in_array(">", $sqlOps)) {
215 $greaterSelectedValue = $selectedValue +
1;
216 $lesserSelectedValue = $selectedValue - 1;
220 $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
221 $this->callAPISuccess('CustomValue', 'create', $params);
223 foreach ($sqlOps as $op) {
224 $qillOp = CRM_Utils_Array
::value($op, CRM_Core_SelectValues
::getSearchBuilderOperators(), $op);
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']);
232 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
233 $this->assertEquals(TRUE, array_key_exists($contactId, $result['values']));
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'];
247 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue));
250 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $lesserSelectedValue)));
251 $this->assertEquals($contactId, $result['id']);
253 elseif ($op == '<') {
254 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
255 $this->assertEquals($contactId2, $result['id']);
258 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com'));
259 $contactId3 = $result['id'];
260 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue));
262 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
264 $this->assertEquals($contactId, $result['values'][$contactId]['id']);
266 $this->assertEquals($contactId3, $result['values'][$contactId3]['id']);
269 $this->assertEquals($contactId2, $result['values'][$contactId2]['id']);
271 $this->callAPISuccess('contact', 'delete', array('id' => $contactId3));
274 $this->callAPISuccess('contact', 'delete', array('id' => $contactId2));
278 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $selectedValue)));
279 $this->assertEquals($contactId, $result['id']);
283 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $notselectedValue)));
284 $this->assertEquals($contactId, $result['id']);
288 $selectedValue = is_array($selectedValue) ?
$selectedValue[0] : $selectedValue;
289 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => "%$selectedValue%")));
290 $this->assertEquals($contactId, $result['id']);
294 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
295 $this->assertEquals($contactId, $result['id']);
299 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
300 $this->assertEquals(FALSE, array_key_exists($contactId, $result['values']));
304 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
305 $this->assertEquals($contactId, $result['id']);
310 $this->callAPISuccess('Contact', 'delete', array('id' => $contactId));
314 * Ensure custom data is updated when option values are modified
316 * @link https://issues.civicrm.org/jira/browse/CRM-11856
318 * @throws \CiviCRM_API3_Exception
320 public function testAlterOptionValue() {
321 $this->_populateOptionAndCustomGroup('string');
323 $selectField = $this->customFieldCreate(array(
324 'custom_group_id' => $this->ids
['string']['custom_group_id'],
325 'label' => 'Custom Select',
326 'html_type' => 'Select',
327 'option_group_id' => $this->optionGroup
['string']['id'],
329 $selectField = civicrm_api3('customField', 'getsingle', array('id' => $selectField['id']));
330 $radioField = $this->customFieldCreate(array(
331 'custom_group_id' => $this->ids
['string']['custom_group_id'],
332 'label' => 'Custom Radio',
333 'html_type' => 'Radio',
334 'option_group_id' => $selectField['option_group_id'],
336 $multiSelectField = $this->customFieldCreate(array(
337 'custom_group_id' => $this->ids
['string']['custom_group_id'],
338 'label' => 'Custom Multi-Select',
339 'html_type' => 'Multi-Select',
340 'option_group_id' => $selectField['option_group_id'],
342 $selectName = 'custom_' . $selectField['id'];
343 $radioName = 'custom_' . $radioField['id'];
344 $multiSelectName = 'custom_' . $multiSelectField['id'];
345 $controlFieldName = 'custom_' . $this->ids
['string']['custom_field_id'];
348 'first_name' => 'abc4',
349 'last_name' => 'xyz4',
350 'contact_type' => 'Individual',
351 'email' => 'man4@yahoo.com',
352 $selectName => $this->optionGroup
['string']['values'][0],
353 $multiSelectName => $this->optionGroup
['string']['values'],
354 $radioName => $this->optionGroup
['string']['values'][1],
355 // The control group in a science experiment should be unaffected
356 $controlFieldName => $this->optionGroup
['string']['values'][2],
359 $contact = $this->callAPISuccess('Contact', 'create', $params);
361 $result = $this->callAPISuccess('Contact', 'getsingle', array(
362 'id' => $contact['id'],
363 'return' => array($selectName, $multiSelectName),
365 $this->assertEquals($params[$selectName], $result[$selectName]);
366 $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]);
368 $this->callAPISuccess('OptionValue', 'create', array(
369 'value' => 'one-modified',
370 'option_group_id' => $selectField['option_group_id'],
371 'name' => 'string 1',
373 'match-mandatory' => array('option_group_id', 'name'),
377 $result = $this->callAPISuccess('Contact', 'getsingle', array(
378 'id' => $contact['id'],
379 'return' => array($selectName, $multiSelectName, $controlFieldName, $radioName),
381 // Ensure the relevant fields have been updated
382 $this->assertEquals('one-modified', $result[$selectName]);
383 $this->assertEquals(array('one-modified', $params[$radioName], $params[$controlFieldName]), $result[$multiSelectName]);
384 // This field should not have changed because we didn't alter this option
385 $this->assertEquals($params[$radioName], $result[$radioName]);
386 // This should not have changed because this field doesn't use the affected option group
387 $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]);