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