Merge pull request #6523 from eileenmcnaughton/CRM-16955
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34require_once 'CiviTest/CiviUnitTestCase.php';
35require_once 'CiviTest/Custom.php';
4cbe18b8
EM
36
37/**
38 * Class CRM_Core_BAO_CustomValueTest
39 */
6a488035 40class CRM_Core_BAO_CustomValueTest extends CiviUnitTestCase {
00be9182 41 public function testTypeCheckWithValidInput() {
6a488035 42
6a488035
TO
43 $values = array(
44 'Memo' => 'Test1',
45 'String' => 'Test',
46 'Int' => 1,
47 'Float' => 10.00,
48 'Date' => '2008-06-24',
6c6e6187 49 'Boolean' => TRUE,
6a488035
TO
50 'StateProvince' => 'California',
51 'Country' => 'US',
52 'Link' => 'http://civicrm.org',
53 );
54 foreach ($values as $type => $value) {
55 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
56 if ($type == 'Date') {
57 $this->assertEquals($valid, '2008-06-24', 'Checking type ' . $type . ' for returned CustomField Type.');
58 }
59 else {
6c6e6187 60 $this->assertEquals($valid, TRUE, 'Checking type ' . $type . ' for returned CustomField Type.');
6a488035
TO
61 }
62 }
63 }
64
00be9182 65 public function testTypeCheckWithInvalidInput() {
6a488035
TO
66 $values = array('check1' => 'chk');
67 foreach ($values as $type => $value) {
68 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
69 $this->assertEquals($valid, NULL, 'Checking invalid type for returned CustomField Type.');
70 }
71 }
72
00be9182 73 public function testTypeCheckWithWrongInput() {
6a488035
TO
74 $values = array(
75 'String' => 1,
76 'Boolean' => 'US',
77 );
78 foreach ($values as $type => $value) {
79 $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value);
80 $this->assertEquals($valid, NULL, 'Checking type ' . $type . ' for returned CustomField Type.');
81 }
82 }
83
00be9182 84 public function testTypeToFieldWithValidInput() {
6a488035
TO
85 $values = array(
86 'String' => 'char_data',
87 'File' => 'char_data',
88 'Boolean' => 'int_data',
89 'Int' => 'int_data',
90 'StateProvince' => 'int_data',
91 'Country' => 'int_data',
92 'Float' => 'float_data',
93 'Memo' => 'memo_data',
94 'Money' => 'decimal_data',
95 'Date' => 'date_data',
96 'Link' => 'char_data',
97 );
98
99 foreach ($values as $type => $value) {
100 $valid = CRM_Core_BAO_CustomValue::typeToField($type);
101 $this->assertEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.');
102 }
103 }
104
00be9182 105 public function testTypeToFieldWithWrongInput() {
6a488035
TO
106 $values = array(
107 'String' => 'memo_data',
108 'File' => 'date_data',
109 'Boolean' => 'char_data',
110 );
111 foreach ($values as $type => $value) {
112 $valid = CRM_Core_BAO_CustomValue::typeToField($type);
113 $this->assertNotEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.');
114 }
115 }
116
7991e3b2 117 public function fixCustomFieldValue() {
6a488035
TO
118 $customGroup = Custom::createGroup(array(), 'Individual');
119
120 $fields = array(
121 'groupId' => $customGroup->id,
122 'dataType' => 'Memo',
123 'htmlType' => 'TextArea',
124 );
125
126 $customField = Custom::createField(array(), $fields);
127
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
137 Custom::deleteField($customField);
138 Custom::deleteGroup($customGroup);
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}