Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-09-05-18-12-52
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
1 <?php
2 /**
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
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 * +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_CustomValueTest
32 */
33 class api_v3_CustomValueTest extends CiviUnitTestCase {
34 protected $_apiversion = 3;
35 protected $ids;
36 protected $optionGroup;
37
38 public $DBResetRequired = FALSE;
39
40 public function setUp() {
41 parent::setUp();
42 }
43
44 public function _populateOptionAndCustomGroup($type = NULL) {
45 $dataValues = array(
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),
51 'date' => NULL,
52 'contact' => NULL,
53 );
54
55 $dataValues = !empty($type) ? array($type => $dataValues[$type]) : $dataValues;
56
57 foreach ($dataValues as $dataType => $values) {
58 $this->optionGroup[$dataType] = array('values' => $values);
59 if (!empty($values)) {
60 $result = $this->callAPISuccess('OptionGroup', 'create',
61 array(
62 'name' => "{$dataType}_group",
63 'api.option_value.create' => array('label' => "$dataType 1", 'value' => $values[0]),
64 'api.option_value.create.1' => array('label' => "$dataType 2", 'value' => $values[1]),
65 'api.option_value.create.2' => array('label' => "$dataType 3", 'value' => $values[2]),
66 )
67 );
68 $this->optionGroup[$dataType]['id'] = $result['id'];
69 }
70 elseif ($dataType == 'contact') {
71 for ($i = 0; $i < 3; $i++) {
72 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . '@yahoo.com'));
73 $this->optionGroup[$dataType]['values'][$i] = $result['id'];
74 }
75 }
76 $this->ids[$dataType] = $this->entityCustomGroupWithSingleFieldCreate("$dataType Custom Group", 'Contacts');
77 }
78
79 }
80
81 public function tearDown() {
82 $tablesToTruncate = array(
83 'civicrm_email',
84 'civicrm_custom_field',
85 'civicrm_custom_group',
86 'civicrm_contact',
87 );
88
89 // true tells quickCleanup to drop any tables that might have been created in the test
90 $this->quickCleanup($tablesToTruncate, TRUE);
91
92 // cleanup created option group for each custom-set before running next test
93 if (!empty($this->optionGroup)) {
94 foreach ($this->optionGroup as $type => $value) {
95 if (!empty($value['id'])) {
96 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $value['id']));
97 }
98 }
99 }
100 }
101
102 public function testCreateCustomValue() {
103 $this->_populateOptionAndCustomGroup();
104
105 $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
106 $dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml();
107 $count = 0;
108
109 foreach ($customFieldDataType as $dataType => $label) {
110 switch ($dataType) {
111 case 'Date':
112 case 'StateProvince';
113 case 'String':
114 case 'Link':
115 case 'Int':
116 case 'Float':
117 case 'Money':
118 if (in_array($dataType, array('String', 'Link'))) {
119 $validSQLOperator = array('=', '!=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'IS NOT NULL', 'IS NULL');
120 $type = 'string';
121 }
122 else {
123 if ($dataType == 'StateProvince') {
124 $type = 'state_province';
125 }
126 elseif ($dataType == 'ContactReference') {
127 $type = 'contact';
128 }
129 elseif ($dataType == 'Date') {
130 $type = 'date';
131 }
132 else {
133 $type = $dataType == 'Int' ? 'integer' : 'number';
134 }
135 $validSQLOperator = array('=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL');
136 }
137
138 foreach ($dataToHtmlTypes[$count] as $html) {
139 $params = array(
140 'custom_group_id' => $this->ids[$type]['custom_group_id'],
141 'label' => "$dataType - $html",
142 'data_type' => $dataType,
143 'html_type' => $html,
144 'default_value' => NULL,
145 );
146 if (!in_array($html, array('Text', 'TextArea')) && !in_array($dataType, array('Link', 'Date', 'ContactReference'))) {
147 $params += array('option_group_id' => $this->optionGroup[$type]['id']);
148 }
149 $customField = $this->customFieldCreate($params);
150 $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperator, $type);
151 }
152 $count++;
153 break;
154
155 default:
156 //TODO: Test case of Country fields remain as it throws foreign key contraint ONLY in test environment
157 $count++;
158 break;
159 }
160 }
161 }
162
163 public function _testCustomValue($customField, $sqlOps, $type) {
164 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($customField);
165 $customId = $customField['id'];
166 $params = array(
167 'contact_type' => 'Individual',
168 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com',
169 );
170 $result = $this->callAPISuccess('Contact', 'create', $params);
171 $contactId = $result['id'];
172
173 $count = rand(1, 2);
174 $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
175 if ($isSerialized) {
176 $selectedValue = $this->optionGroup[$type]['values'];
177 $notselectedValue = $selectedValue[$count];
178 unset($selectedValue[$count]);
179 }
180 elseif ($customField['html_type'] == 'Link') {
181 $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
182 $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
183 }
184 elseif ($type == 'date') {
185 $selectedValue = date('Ymd');
186 $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday'));
187 $greaterSelectedValue = date('Ymd', strtotime('+ 1 day'));
188 }
189 elseif ($type == 'contact') {
190 $selectedValue = $this->optionGroup[$type]['values'][1];
191 $notselectedValue = $this->optionGroup[$type]['values'][0];
192 }
193 else {
194 $selectedValue = $this->optionGroup[$type]['values'][0];
195 $notselectedValue = $this->optionGroup[$type]['values'][$count];
196 if (in_array(">", $sqlOps)) {
197 $greaterSelectedValue = $selectedValue + 1;
198 $lesserSelectedValue = $selectedValue - 1;
199 }
200 }
201
202 $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
203 $this->callAPISuccess('CustomValue', 'create', $params);
204
205 foreach ($sqlOps as $op) {
206 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
207 $description = "\nFind Contact where '$customField[label]' $qillOp ";
208 switch ($op) {
209 case '=':
210 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => (is_array($selectedValue) ? implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValue) : $selectedValue)));
211 $this->assertEquals($contactId, $result['id']);
212 echo $description . implode("[separator]", (array) $selectedValue);
213 break;
214
215 case '!=':
216 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
217 $this->assertEquals(TRUE, array_key_exists($contactId, $result['values']));
218 echo $description . $notselectedValue;
219 break;
220
221 case '>':
222 case '<':
223 case '>=':
224 case '<=':
225 if ($isSerialized) {
226 continue;
227 }
228 // To be precise in for these operator we can't just rely on one contact,
229 // hence creating multiple contact with custom value less/more then $selectedValue respectively
230 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man2@yahoo.com'));
231 $contactId2 = $result['id'];
232 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue));
233
234 if ($op == '>') {
235 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $lesserSelectedValue)));
236 $this->assertEquals($contactId, $result['id']);
237 echo $description . $lesserSelectedValue;
238 }
239 elseif ($op == '<') {
240 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
241 $this->assertEquals($contactId2, $result['id']);
242 echo $description . $selectedValue;
243 }
244 else {
245 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com'));
246 $contactId3 = $result['id'];
247 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue));
248
249 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
250 echo $description . $selectedValue;
251
252 $this->assertEquals($contactId, $result['values'][$contactId]['id']);
253 if ($op == '>=') {
254 $this->assertEquals($contactId3, $result['values'][$contactId3]['id']);
255 }
256 else {
257 $this->assertEquals($contactId2, $result['values'][$contactId2]['id']);
258 }
259 $this->callAPISuccess('contact', 'delete', array('id' => $contactId3));
260 }
261
262 $this->callAPISuccess('contact', 'delete', array('id' => $contactId2));
263 break;
264
265 case 'IN':
266 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $selectedValue)));
267 $this->assertEquals($contactId, $result['id']);
268 echo $description . implode(",", (array) $selectedValue);
269 break;
270
271 case 'NOT IN':
272 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $notselectedValue)));
273 $this->assertEquals($contactId, $result['id']);
274 echo $description . implode(",", (array) $notselectedValue);
275 break;
276
277 case 'LIKE':
278 $selectedValue = is_array($selectedValue) ? $selectedValue[0] : $selectedValue;
279 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => "%$selectedValue%")));
280 $this->assertEquals($contactId, $result['id']);
281 echo $description . "%$selectedValue%";
282 break;
283
284 case 'NOT LIKE':
285 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
286 $this->assertEquals($contactId, $result['id']);
287 echo $description . "'$notselectedValue'";
288 break;
289
290 case 'IS NULL':
291 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
292 $this->assertEquals(FALSE, array_key_exists($contactId, $result['values']));
293 echo $description;
294 break;
295
296 case 'IS NOT NULL':
297 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
298 $this->assertEquals($contactId, $result['id']);
299 echo $description;
300 break;
301 }
302 }
303
304 $this->callAPISuccess('Contact', 'delete', array('id' => $contactId));
305 }
306
307 /**
308 * Ensure custom data is updated when option values are modified
309 *
310 * @link https://issues.civicrm.org/jira/browse/CRM-11856
311 *
312 * @throws \CiviCRM_API3_Exception
313 */
314 public function testAlterOptionValue() {
315 $this->_populateOptionAndCustomGroup('string');
316
317 $selectField = $this->customFieldCreate(array(
318 'custom_group_id' => $this->ids['string']['custom_group_id'],
319 'label' => 'Custom Select',
320 'html_type' => 'Select',
321 'option_group_id' => $this->optionGroup['string']['id'],
322 ));
323 $selectField = civicrm_api3('customField', 'getsingle', array('id' => $selectField['id']));
324 $radioField = $this->customFieldCreate(array(
325 'custom_group_id' => $this->ids['string']['custom_group_id'],
326 'label' => 'Custom Radio',
327 'html_type' => 'Radio',
328 'option_group_id' => $selectField['option_group_id'],
329 ));
330 $multiSelectField = $this->customFieldCreate(array(
331 'custom_group_id' => $this->ids['string']['custom_group_id'],
332 'label' => 'Custom Multi-Select',
333 'html_type' => 'Multi-Select',
334 'option_group_id' => $selectField['option_group_id'],
335 ));
336 $selectName = 'custom_' . $selectField['id'];
337 $radioName = 'custom_' . $radioField['id'];
338 $multiSelectName = 'custom_' . $multiSelectField['id'];
339 $controlFieldName = 'custom_' . $this->ids['string']['custom_field_id'];
340
341 $params = array(
342 'first_name' => 'abc4',
343 'last_name' => 'xyz4',
344 'contact_type' => 'Individual',
345 'email' => 'man4@yahoo.com',
346 $selectName => $this->optionGroup['string']['values'][0],
347 $multiSelectName => $this->optionGroup['string']['values'],
348 $radioName => $this->optionGroup['string']['values'][1],
349 // The control group in a science experiment should be unaffected
350 $controlFieldName => $this->optionGroup['string']['values'][2],
351 );
352
353 $contact = $this->callAPISuccess('Contact', 'create', $params);
354
355 $result = $this->callAPISuccess('Contact', 'getsingle', array(
356 'id' => $contact['id'],
357 'return' => array($selectName, $multiSelectName),
358 ));
359 $this->assertEquals($params[$selectName], $result[$selectName]);
360 $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]);
361
362 $this->callAPISuccess('OptionValue', 'create', array(
363 'value' => 'one-modified',
364 'option_group_id' => $selectField['option_group_id'],
365 'name' => 'string 1',
366 'options' => array(
367 'match-mandatory' => array('option_group_id', 'name'),
368 ),
369 ));
370
371 $result = $this->callAPISuccess('Contact', 'getsingle', array(
372 'id' => $contact['id'],
373 'return' => array($selectName, $multiSelectName, $controlFieldName, $radioName),
374 ));
375 // Ensure the relevant fields have been updated
376 $this->assertEquals('one-modified', $result[$selectName]);
377 $this->assertEquals(array('one-modified', $params[$radioName], $params[$controlFieldName]), $result[$multiSelectName]);
378 // This field should not have changed because we didn't alter this option
379 $this->assertEquals($params[$radioName], $result[$radioName]);
380 // This should not have changed because this field doesn't use the affected option group
381 $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]);
382 }
383
384 }