Merge pull request #19911 from eileenmcnaughton/new
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Core_BAO_CustomValueTest
20 * @group headless
21 */
22 class CRM_Core_BAO_CustomValueTest extends CiviUnitTestCase {
23
24 public function testTypeCheckWithValidInput() {
25
26 $values = [
27 'Memo' => 'Test1',
28 'String' => 'Test',
29 'Int' => 1,
30 'Float' => 10.00,
31 'Date' => '2008-06-24',
32 'Boolean' => TRUE,
33 'StateProvince' => 'California',
34 'Country' => 'US',
35 'Link' => 'http://civicrm.org',
36 ];
37 foreach ($values as $type => $value) {
38 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
39 if ($type == 'Date') {
40 $this->assertEquals($valid, '2008-06-24', 'Checking type ' . $type . ' for returned CustomField Type.');
41 }
42 else {
43 $this->assertEquals($valid, TRUE, 'Checking type ' . $type . ' for returned CustomField Type.');
44 }
45 }
46 }
47
48 public function testTypeCheckWithInvalidInput() {
49 $values = ['check1' => 'chk'];
50 foreach ($values as $type => $value) {
51 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
52 $this->assertEquals($valid, NULL, 'Checking invalid type for returned CustomField Type.');
53 }
54 }
55
56 public function testTypeCheckWithWrongInput() {
57 $values = [
58 'String' => 1,
59 'Boolean' => 'US',
60 ];
61 foreach ($values as $type => $value) {
62 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
63 $this->assertEquals($valid, NULL, 'Checking type ' . $type . ' for returned CustomField Type.');
64 }
65 }
66
67 public function testTypeToFieldWithValidInput() {
68 $values = [
69 'String' => 'char_data',
70 'File' => 'char_data',
71 'Boolean' => 'int_data',
72 'Int' => 'int_data',
73 'StateProvince' => 'int_data',
74 'Country' => 'int_data',
75 'Float' => 'float_data',
76 'Memo' => 'memo_data',
77 'Money' => 'decimal_data',
78 'Date' => 'date_data',
79 'Link' => 'char_data',
80 ];
81
82 foreach ($values as $type => $value) {
83 $valid = CRM_Core_BAO_CustomValue::typeToField($type);
84 $this->assertEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.');
85 }
86 }
87
88 public function testTypeToFieldWithWrongInput() {
89 $values = [
90 'String' => 'memo_data',
91 'File' => 'date_data',
92 'Boolean' => 'char_data',
93 ];
94 foreach ($values as $type => $value) {
95 $valid = CRM_Core_BAO_CustomValue::typeToField($type);
96 $this->assertNotEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.');
97 }
98 }
99
100 public function testFixCustomFieldValue() {
101 $customGroup = $this->customGroupCreate(['extends' => 'Individual']);
102 $params = [
103 'email' => 'abc@example.com',
104 ];
105
106 foreach ([
107 [
108 'custom_group_id' => $customGroup['id'],
109 'data_type' => 'Memo',
110 'html_type' => 'TextArea',
111 'default_value' => '',
112 'search_value' => '%note%',
113 'expected_value' => ['LIKE' => '%note%'],
114 ],
115 [
116 'custom_group_id' => $customGroup['id'],
117 'data_type' => 'String',
118 'html_type' => 'Autocomplete-Select',
119 'default_value' => '',
120 'search_value' => 'R,Y',
121 'expected_value' => ['IN' => ['R', 'Y']],
122 'option_values' => [
123 [
124 'label' => 'Red',
125 'value' => 'R',
126 'weight' => 1,
127 'is_active' => 1,
128 ],
129 [
130 'label' => 'Yellow',
131 'value' => 'Y',
132 'weight' => 2,
133 'is_active' => 1,
134 ],
135 [
136 'label' => 'Green',
137 'value' => 'G',
138 'weight' => 3,
139 'is_active' => 1,
140 ],
141 ],
142 ],
143 ] as $field) {
144 $id = $this->customFieldCreate($field)['id'];
145 $customKey = 'custom_' . $id;
146 $params[$customKey] = $field['search_value'];
147 CRM_Core_BAO_CustomValue::fixCustomFieldValue($params);
148 $this->assertEquals($params[$customKey], $field['expected_value'], 'Checking the returned value of type ' . $field['data_type']);
149
150 // delete created custom field
151 $this->customFieldDelete($id);
152 }
153
154 $this->customGroupDelete($customGroup['id']);
155 }
156
157 public function testFixCustomFieldValueWithEmptyParams() {
158 $params = [];
159 $result = CRM_Core_BAO_CustomValue::fixCustomFieldValue($params);
160 $this->assertEquals($result, NULL, 'Checking the returned value of type Memo.');
161 }
162
163 }