unit test fixes 4.7beta4
[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 'boolean' => NULL,
54 );
55
56 $dataValues = !empty($type) ? array($type => $dataValues[$type]) : $dataValues;
57
58 foreach ($dataValues as $dataType => $values) {
59 $this->optionGroup[$dataType] = array('values' => $values);
60 if (!empty($values)) {
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 }
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'];
75 }
76 }
77 $this->ids[$dataType] = $this->entityCustomGroupWithSingleFieldCreate("$dataType Custom Group", 'Contacts');
78 }
79
80 }
81
82 public function tearDown() {
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);
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 }
101 }
102
103 public function testCreateCustomValue() {
104 $this->_populateOptionAndCustomGroup();
105
106 $customFieldDataType = CRM_Core_BAO_CustomField::dataType();
107 $dataToHtmlTypes = CRM_Core_BAO_CustomField::dataToHtml();
108 $count = 0;
109
110 foreach ($customFieldDataType as $dataType => $label) {
111 switch ($dataType) {
112 case 'Country':
113 case 'StateProvince':
114 $this->foreignKeyChecksOff();
115 case 'String':
116 case 'Link':
117 case 'Int':
118 case 'Float':
119 case 'Money':
120 case 'Date':
121 case 'Boolean':
122
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');
126 $type = 'string';
127 }
128 elseif ($dataType == 'Boolean') {
129 $validSQLOperators = array('=', '!=', 'IS NOT NULL', 'IS NULL');
130 $type = 'boolean';
131 }
132 else {
133 if ($dataType == 'Country') {
134 $type = 'country';
135 }
136 elseif ($dataType == 'StateProvince') {
137 $type = 'state_province';
138 }
139 elseif ($dataType == 'ContactReference') {
140 $type = 'contact';
141 }
142 elseif ($dataType == 'Date') {
143 $type = 'date';
144 }
145 else {
146 $type = $dataType == 'Int' ? 'integer' : 'number';
147 }
148 $validSQLOperators = array('=', '!=', 'IN', 'NOT IN', '<=', '>=', '>', '<', 'IS NOT NULL', 'IS NULL');
149 }
150
151 //Create custom field of $dataType and html-type $html
152 foreach ($dataToHtmlTypes[$count] as $html) {
153 $params = array(
154 'custom_group_id' => $this->ids[$type]['custom_group_id'],
155 'label' => "$dataType - $html",
156 'data_type' => $dataType,
157 'html_type' => $html,
158 'default_value' => NULL,
159 );
160 if (!in_array($html, array('Text', 'TextArea')) && !in_array($dataType, array('Link', 'Date', 'ContactReference', 'Boolean'))) {
161 $params += array('option_group_id' => $this->optionGroup[$type]['id']);
162 }
163 $customField = $this->customFieldCreate($params);
164 //Now test with $validSQLOperator SQL operators against its custom value(s)
165 $this->_testCustomValue($customField['values'][$customField['id']], $validSQLOperators, $type);
166 }
167 $count++;
168 break;
169
170 default:
171 // skipping File data-type
172 $count++;
173 break;
174 }
175 }
176 }
177
178 public function _testCustomValue($customField, $sqlOps, $type) {
179 $isSerialized = CRM_Core_BAO_CustomField::isSerialized($customField);
180 $customId = $customField['id'];
181 $params = array(
182 'contact_type' => 'Individual',
183 'email' => substr(sha1(rand()), 0, 7) . 'man1@yahoo.com',
184 );
185 $result = $this->callAPISuccess('Contact', 'create', $params);
186 $contactId = $result['id'];
187
188 $count = rand(1, 2);
189 $seperator = CRM_Core_DAO::VALUE_SEPARATOR;
190 if ($isSerialized) {
191 $selectedValue = $this->optionGroup[$type]['values'];
192 $notselectedValue = $selectedValue[$count];
193 unset($selectedValue[$count]);
194 }
195 elseif ($customField['html_type'] == 'Link') {
196 $selectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
197 $notselectedValue = "http://" . substr(sha1(rand()), 0, 7) . ".com";
198 }
199 elseif ($type == 'date') {
200 $selectedValue = date('Ymd');
201 $notselectedValue = $lesserSelectedValue = date('Ymd', strtotime('yesterday'));
202 $greaterSelectedValue = date('Ymd', strtotime('+ 1 day'));
203 }
204 elseif ($type == 'contact') {
205 $selectedValue = $this->optionGroup[$type]['values'][1];
206 $notselectedValue = $this->optionGroup[$type]['values'][0];
207 }
208 elseif ($type == 'boolean') {
209 $selectedValue = 1;
210 $notselectedValue = 0;
211 }
212 else {
213 $selectedValue = $this->optionGroup[$type]['values'][0];
214 $notselectedValue = $this->optionGroup[$type]['values'][$count];
215 if (in_array(">", $sqlOps)) {
216 $greaterSelectedValue = $selectedValue + 1;
217 $lesserSelectedValue = $selectedValue - 1;
218 }
219 }
220
221 $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
222 $this->callAPISuccess('CustomValue', 'create', $params);
223
224 foreach ($sqlOps as $op) {
225 $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
226 switch ($op) {
227 case '=':
228 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => (is_array($selectedValue) ? implode(CRM_Core_DAO::VALUE_SEPARATOR, $selectedValue) : $selectedValue)));
229 $this->assertEquals($contactId, $result['id']);
230 break;
231
232 case '!=':
233 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
234 $this->assertEquals(TRUE, array_key_exists($contactId, $result['values']));
235 break;
236
237 case '>':
238 case '<':
239 case '>=':
240 case '<=':
241 if ($isSerialized) {
242 continue;
243 }
244 // To be precise in for these operator we can't just rely on one contact,
245 // hence creating multiple contact with custom value less/more then $selectedValue respectively
246 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man2@yahoo.com'));
247 $contactId2 = $result['id'];
248 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId2, 'custom_' . $customId => $lesserSelectedValue));
249
250 if ($op == '>') {
251 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $lesserSelectedValue)));
252 $this->assertEquals($contactId, $result['id']);
253 }
254 elseif ($op == '<') {
255 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
256 $this->assertEquals($contactId2, $result['id']);
257 }
258 else {
259 $result = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => substr(sha1(rand()), 0, 7) . 'man3@yahoo.com'));
260 $contactId3 = $result['id'];
261 $this->callAPISuccess('CustomValue', 'create', array('entity_id' => $contactId3, 'custom_' . $customId => $greaterSelectedValue));
262
263 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $selectedValue)));
264
265 $this->assertEquals($contactId, $result['values'][$contactId]['id']);
266 if ($op == '>=') {
267 $this->assertEquals($contactId3, $result['values'][$contactId3]['id']);
268 }
269 else {
270 $this->assertEquals($contactId2, $result['values'][$contactId2]['id']);
271 }
272 $this->callAPISuccess('contact', 'delete', array('id' => $contactId3));
273 }
274
275 $this->callAPISuccess('contact', 'delete', array('id' => $contactId2));
276 break;
277
278 case 'IN':
279 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $selectedValue)));
280 $this->assertEquals($contactId, $result['id']);
281 break;
282
283 case 'NOT IN':
284 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => (array) $notselectedValue)));
285 $this->assertEquals($contactId, $result['id']);
286 break;
287
288 case 'LIKE':
289 $selectedValue = is_array($selectedValue) ? $selectedValue[0] : $selectedValue;
290 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => "%$selectedValue%")));
291 $this->assertEquals($contactId, $result['id']);
292 break;
293
294 case 'NOT LIKE':
295 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => $notselectedValue)));
296 $this->assertEquals($contactId, $result['id']);
297 break;
298
299 case 'IS NULL':
300 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
301 $this->assertEquals(FALSE, array_key_exists($contactId, $result['values']));
302 break;
303
304 case 'IS NOT NULL':
305 $result = $this->callAPISuccess('Contact', 'Get', array('custom_' . $customId => array($op => 1)));
306 $this->assertEquals($contactId, $result['id']);
307 break;
308 }
309 }
310
311 $this->callAPISuccess('Contact', 'delete', array('id' => $contactId));
312 }
313
314 /**
315 * Ensure custom data is updated when option values are modified
316 *
317 * @link https://issues.civicrm.org/jira/browse/CRM-11856
318 *
319 * @throws \CiviCRM_API3_Exception
320 */
321 public function testAlterOptionValue() {
322 $this->_populateOptionAndCustomGroup('string');
323
324 $selectField = $this->customFieldCreate(array(
325 'custom_group_id' => $this->ids['string']['custom_group_id'],
326 'label' => 'Custom Select',
327 'html_type' => 'Select',
328 'option_group_id' => $this->optionGroup['string']['id'],
329 ));
330 $selectField = civicrm_api3('customField', 'getsingle', array('id' => $selectField['id']));
331 $radioField = $this->customFieldCreate(array(
332 'custom_group_id' => $this->ids['string']['custom_group_id'],
333 'label' => 'Custom Radio',
334 'html_type' => 'Radio',
335 'option_group_id' => $selectField['option_group_id'],
336 ));
337 $multiSelectField = $this->customFieldCreate(array(
338 'custom_group_id' => $this->ids['string']['custom_group_id'],
339 'label' => 'Custom Multi-Select',
340 'html_type' => 'Multi-Select',
341 'option_group_id' => $selectField['option_group_id'],
342 ));
343 $selectName = 'custom_' . $selectField['id'];
344 $radioName = 'custom_' . $radioField['id'];
345 $multiSelectName = 'custom_' . $multiSelectField['id'];
346 $controlFieldName = 'custom_' . $this->ids['string']['custom_field_id'];
347
348 $params = array(
349 'first_name' => 'abc4',
350 'last_name' => 'xyz4',
351 'contact_type' => 'Individual',
352 'email' => 'man4@yahoo.com',
353 $selectName => $this->optionGroup['string']['values'][0],
354 $multiSelectName => $this->optionGroup['string']['values'],
355 $radioName => $this->optionGroup['string']['values'][1],
356 // The control group in a science experiment should be unaffected
357 $controlFieldName => $this->optionGroup['string']['values'][2],
358 );
359
360 $contact = $this->callAPISuccess('Contact', 'create', $params);
361
362 $result = $this->callAPISuccess('Contact', 'getsingle', array(
363 'id' => $contact['id'],
364 'return' => array($selectName, $multiSelectName),
365 ));
366 $this->assertEquals($params[$selectName], $result[$selectName]);
367 $this->assertEquals($params[$multiSelectName], $result[$multiSelectName]);
368
369 $this->callAPISuccess('OptionValue', 'create', array(
370 'value' => 'one-modified',
371 'option_group_id' => $selectField['option_group_id'],
372 'name' => 'string 1',
373 'options' => array(
374 'match-mandatory' => array('option_group_id', 'name'),
375 ),
376 ));
377
378 $result = $this->callAPISuccess('Contact', 'getsingle', array(
379 'id' => $contact['id'],
380 'return' => array($selectName, $multiSelectName, $controlFieldName, $radioName),
381 ));
382 // Ensure the relevant fields have been updated
383 $this->assertEquals('one-modified', $result[$selectName]);
384 $this->assertEquals(array('one-modified', $params[$radioName], $params[$controlFieldName]), $result[$multiSelectName]);
385 // This field should not have changed because we didn't alter this option
386 $this->assertEquals($params[$radioName], $result[$radioName]);
387 // This should not have changed because this field doesn't use the affected option group
388 $this->assertEquals($params[$controlFieldName], $result[$controlFieldName]);
389 }
390
391 }