Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
4cbe18b8 EM |
18 | /** |
19 | * Class CRM_Core_BAO_CustomValueTest | |
acb109b7 | 20 | * @group headless |
4cbe18b8 | 21 | */ |
6a488035 | 22 | class CRM_Core_BAO_CustomValueTest extends CiviUnitTestCase { |
39b959db | 23 | |
00be9182 | 24 | public function testTypeCheckWithValidInput() { |
6a488035 | 25 | |
9099cab3 | 26 | $values = [ |
6a488035 TO |
27 | 'Memo' => 'Test1', |
28 | 'String' => 'Test', | |
29 | 'Int' => 1, | |
30 | 'Float' => 10.00, | |
31 | 'Date' => '2008-06-24', | |
6c6e6187 | 32 | 'Boolean' => TRUE, |
6a488035 TO |
33 | 'StateProvince' => 'California', |
34 | 'Country' => 'US', | |
35 | 'Link' => 'http://civicrm.org', | |
9099cab3 | 36 | ]; |
6a488035 TO |
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 { | |
6c6e6187 | 43 | $this->assertEquals($valid, TRUE, 'Checking type ' . $type . ' for returned CustomField Type.'); |
6a488035 TO |
44 | } |
45 | } | |
46 | } | |
47 | ||
00be9182 | 48 | public function testTypeCheckWithInvalidInput() { |
9099cab3 | 49 | $values = ['check1' => 'chk']; |
6a488035 TO |
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 | ||
00be9182 | 56 | public function testTypeCheckWithWrongInput() { |
9099cab3 | 57 | $values = [ |
6a488035 TO |
58 | 'String' => 1, |
59 | 'Boolean' => 'US', | |
9099cab3 | 60 | ]; |
6a488035 TO |
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 | ||
00be9182 | 67 | public function testTypeToFieldWithValidInput() { |
9099cab3 | 68 | $values = [ |
6a488035 TO |
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', | |
9099cab3 | 80 | ]; |
6a488035 TO |
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 | ||
00be9182 | 88 | public function testTypeToFieldWithWrongInput() { |
9099cab3 | 89 | $values = [ |
6a488035 TO |
90 | 'String' => 'memo_data', |
91 | 'File' => 'date_data', | |
92 | 'Boolean' => 'char_data', | |
9099cab3 | 93 | ]; |
6a488035 TO |
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 | ||
7991e3b2 | 100 | public function fixCustomFieldValue() { |
9099cab3 | 101 | $customGroup = $this->customGroupCreate(['extends' => 'Individual']); |
6a488035 | 102 | |
9099cab3 | 103 | $fields = [ |
23ead872 | 104 | 'custom_group_id' => $customGroup['id'], |
105 | 'data_type' => 'Memo', | |
106 | 'html_type' => 'TextArea', | |
107 | 'default_value' => '', | |
9099cab3 | 108 | ]; |
6a488035 | 109 | |
23ead872 | 110 | $customField = $this->customFieldCreate($fields); |
6a488035 | 111 | |
23ead872 | 112 | $custom = 'custom_' . $customField['id']; |
9099cab3 | 113 | $params = [ |
6a488035 TO |
114 | 'email' => 'abc@webaccess.co.in', |
115 | $custom => 'note', | |
9099cab3 | 116 | ]; |
6a488035 | 117 | |
7991e3b2 | 118 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($params); |
6a488035 TO |
119 | $this->assertEquals($params[$custom], '%note%', 'Checking the returned value of type Memo.'); |
120 | ||
23ead872 | 121 | $this->customFieldDelete($customField['id']); |
122 | $this->customGroupDelete($customGroup['id']); | |
6a488035 TO |
123 | } |
124 | ||
7991e3b2 | 125 | public function testFixCustomFieldValueWithEmptyParams() { |
9099cab3 | 126 | $params = []; |
7991e3b2 | 127 | $result = CRM_Core_BAO_CustomValue::fixCustomFieldValue($params); |
6a488035 TO |
128 | $this->assertEquals($result, NULL, 'Checking the returned value of type Memo.'); |
129 | } | |
96025800 | 130 | |
6a488035 | 131 | } |