Merge pull request #9779 from WeMoveEU/CRM-19963
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * Class CRM_Core_BAO_CustomValueTest
36 * @group headless
37 */
38 class CRM_Core_BAO_CustomValueTest extends CiviUnitTestCase {
39 public function testTypeCheckWithValidInput() {
40
41 $values = array(
42 'Memo' => 'Test1',
43 'String' => 'Test',
44 'Int' => 1,
45 'Float' => 10.00,
46 'Date' => '2008-06-24',
47 'Boolean' => TRUE,
48 'StateProvince' => 'California',
49 'Country' => 'US',
50 'Link' => 'http://civicrm.org',
51 );
52 foreach ($values as $type => $value) {
53 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
54 if ($type == 'Date') {
55 $this->assertEquals($valid, '2008-06-24', 'Checking type ' . $type . ' for returned CustomField Type.');
56 }
57 else {
58 $this->assertEquals($valid, TRUE, 'Checking type ' . $type . ' for returned CustomField Type.');
59 }
60 }
61 }
62
63 public function testTypeCheckWithInvalidInput() {
64 $values = array('check1' => 'chk');
65 foreach ($values as $type => $value) {
66 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
67 $this->assertEquals($valid, NULL, 'Checking invalid type for returned CustomField Type.');
68 }
69 }
70
71 public function testTypeCheckWithWrongInput() {
72 $values = array(
73 'String' => 1,
74 'Boolean' => 'US',
75 );
76 foreach ($values as $type => $value) {
77 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
78 $this->assertEquals($valid, NULL, 'Checking type ' . $type . ' for returned CustomField Type.');
79 }
80 }
81
82 public function testTypeToFieldWithValidInput() {
83 $values = array(
84 'String' => 'char_data',
85 'File' => 'char_data',
86 'Boolean' => 'int_data',
87 'Int' => 'int_data',
88 'StateProvince' => 'int_data',
89 'Country' => 'int_data',
90 'Float' => 'float_data',
91 'Memo' => 'memo_data',
92 'Money' => 'decimal_data',
93 'Date' => 'date_data',
94 'Link' => 'char_data',
95 );
96
97 foreach ($values as $type => $value) {
98 $valid = CRM_Core_BAO_CustomValue::typeToField($type);
99 $this->assertEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.');
100 }
101 }
102
103 public function testTypeToFieldWithWrongInput() {
104 $values = array(
105 'String' => 'memo_data',
106 'File' => 'date_data',
107 'Boolean' => 'char_data',
108 );
109 foreach ($values as $type => $value) {
110 $valid = CRM_Core_BAO_CustomValue::typeToField($type);
111 $this->assertNotEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.');
112 }
113 }
114
115 public function fixCustomFieldValue() {
116 $customGroup = $this->customGroupCreate(array('extends' => 'Individual'));
117
118 $fields = array(
119 'custom_group_id' => $customGroup['id'],
120 'data_type' => 'Memo',
121 'html_type' => 'TextArea',
122 'default_value' => '',
123 );
124
125 $customField = $this->customFieldCreate($fields);
126
127 $custom = 'custom_' . $customField['id'];
128 $params = array(
129 'email' => 'abc@webaccess.co.in',
130 $custom => 'note',
131 );
132
133 CRM_Core_BAO_CustomValue::fixCustomFieldValue($params);
134 $this->assertEquals($params[$custom], '%note%', 'Checking the returned value of type Memo.');
135
136 $this->customFieldDelete($customField['id']);
137 $this->customGroupDelete($customGroup['id']);
138 }
139
140 public function testFixCustomFieldValueWithEmptyParams() {
141 $params = array();
142 $result = CRM_Core_BAO_CustomValue::fixCustomFieldValue($params);
143 $this->assertEquals($result, NULL, 'Checking the returned value of type Memo.');
144 }
145
146 }