Merge pull request #14639 from colemanw/i18nCleanup
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomValueTableSetGetTest.php
CommitLineData
6a488035 1<?php
aba1cd8b
EM
2
3/**
4 * Class CRM_Core_BAO_CustomValueTableSetGetTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Core_BAO_CustomValueTableSetGetTest extends CiviUnitTestCase {
6a488035 8
00be9182 9 public function setUp() {
6a488035
TO
10 parent::setUp();
11 }
12
c490a46a
CW
13 /**
14 * Test setValues() and GetValues() methods with custom Date field
15 */
00be9182 16 public function testSetGetValuesDate() {
6a488035 17 $params = array();
f2040bc6 18 $contactID = $this->individualCreate();
6a488035
TO
19
20 //create Custom Group
8d63d44a 21 $customGroup = $this->customGroupCreate(array('is_multiple' => 1));
6a488035
TO
22
23 //create Custom Field of data type Date
24 $fields = array(
8d63d44a 25 'custom_group_id' => $customGroup['id'],
26 'data_type' => 'Date',
27 'html_type' => 'Select Date',
28 'default_value' => '',
6a488035 29 );
8d63d44a 30 $customField = $this->customFieldCreate($fields);
6a488035
TO
31
32 // Retrieve the field ID for sample custom field 'test_Date'
33 $params = array('label' => 'test_Date');
34 $field = array();
35
36 CRM_Core_BAO_CustomField::retrieve($params, $field);
8d63d44a 37 $fieldID = $customField['id'];
6a488035
TO
38
39 // Set test_Date to a valid date value
40 $date = '20080608000000';
41 $params = array(
42 'entityID' => $contactID,
43 'custom_' . $fieldID => $date,
44 );
45 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
46 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
47
48 // Check that the date value is stored
49 $values = array();
50 $params = array(
51 'entityID' => $contactID,
52 'custom_' . $fieldID => 1,
53 );
54 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
55
56 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
57 $this->assertEquals($values['custom_' . $fieldID . '_1'],
58 CRM_Utils_Date::mysqlToIso($date),
59 'Verify that the date value is stored for contact ' . $contactID
60 );
61
62 // Now set test_Date to an invalid date value and try to reset
63 $badDate = '20080631000000';
64 $params = array(
65 'entityID' => $contactID,
66 'custom_' . $fieldID => $badDate,
67 );
68
8d63d44a 69 CRM_Core_TemporaryErrorScope::useException();
6c6e6187 70 $message = NULL;
6a488035 71 try {
8d63d44a 72 CRM_Core_BAO_CustomValueTable::setValues($params);
0db6c3e1 73 }
481a74f4 74 catch (Exception $e) {
6a488035
TO
75 $message = $e->getMessage();
76 }
77 $errorScope = NULL;
78
79 // Check that an exception has been thrown
481a74f4 80 $this->assertNotNull($message, 'Verify than an exception is thrown when bad date is passed');
6a488035
TO
81
82 $params = array(
83 'entityID' => $contactID,
84 'custom_' . $fieldID => 1,
85 );
86 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
87 $this->assertEquals($values['custom_' . $fieldID . '_1'],
88 CRM_Utils_Date::mysqlToIso($date),
89 'Verify that the date value has NOT been updated for contact ' . $contactID
90 );
91
92 // Test setting test_Date to null
93 $params = array(
94 'entityID' => $contactID,
95 'custom_' . $fieldID => NULL,
96 );
97 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
98
99 // Check that the date value is empty
100 $params = array(
101 'entityID' => $contactID,
102 'custom_' . $fieldID => 1,
103 );
104 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
105 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
106
107 // Cleanup
8d63d44a 108 $this->customFieldDelete($customField);
109 $this->customGroupDelete($customGroup['id']);
93ac19cd 110 $this->contactDelete($contactID);
6a488035
TO
111 }
112
c490a46a
CW
113 /**
114 * Test setValues() and getValues() methods with custom field YesNo(Boolean) Radio
c490a46a 115 */
00be9182 116 public function testSetGetValuesYesNoRadio() {
f2040bc6 117 $contactID = $this->individualCreate();
6a488035 118
8d63d44a 119 $customGroup = $this->customGroupCreate(array('is_multiple' => 1));
6a488035
TO
120
121 //create Custom Field of type YesNo(Boolean) Radio
122 $fields = array(
8d63d44a 123 'custom_group_id' => $customGroup['id'],
124 'data_type' => 'Boolean',
125 'html_type' => 'Radio',
126 'default_value' => '',
6a488035 127 );
8d63d44a 128 $customField = $this->customFieldCreate($fields);
6a488035
TO
129
130 // Retrieve the field ID for sample custom field 'test_Boolean'
131 $params = array('label' => 'test_Boolean');
132 $field = array();
133
134 //get field Id
135 CRM_Core_BAO_CustomField::retrieve($params, $field);
136
8d63d44a 137 $fieldID = $customField['id'];
6a488035
TO
138
139 // valid boolean value '1' for Boolean Radio
140 $yesNo = '1';
141 $params = array(
142 'entityID' => $contactID,
143 'custom_' . $fieldID => $yesNo,
144 );
145 $result = CRM_Core_BAO_CustomValueTable::setValues($params);
146
147 $this->assertEquals($result['is_error'], 0, 'Verify that is_error = 0 (success).');
148
149 // Check that the YesNo radio value is stored
6a488035
TO
150 $params = array(
151 'entityID' => $contactID,
152 'custom_' . $fieldID => 1,
153 );
154 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
155
156 $this->assertEquals($values['is_error'], 0, 'Verify that is_error = 0 (success).');
157 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
158 'Verify that the boolean value is stored for contact ' . $contactID
159 );
160
6a488035
TO
161 // Now set YesNo radio to an invalid boolean value and try to reset
162 $badYesNo = '20';
163 $params = array(
164 'entityID' => $contactID,
165 'custom_' . $fieldID => $badYesNo,
166 );
167
8d63d44a 168 CRM_Core_TemporaryErrorScope::useException();
6c6e6187 169 $message = NULL;
6a488035 170 try {
8d63d44a 171 CRM_Core_BAO_CustomValueTable::setValues($params);
0db6c3e1
TO
172 }
173 catch (Exception $e) {
6a488035
TO
174 $message = $e->getMessage();
175 }
176 $errorScope = NULL;
177
178 // Check that an exception has been thrown
481a74f4 179 $this->assertNotNull($message, 'Verify than an exception is thrown when bad boolean is passed');
6a488035
TO
180
181 $params = array(
182 'entityID' => $contactID,
183 'custom_' . $fieldID => 1,
184 );
185 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
186
187 $this->assertEquals($values["custom_{$fieldID}_1"], $yesNo,
188 'Verify that the date value has NOT been updated for contact ' . $contactID
189 );
190
191 // Cleanup
8d63d44a 192 $this->customFieldDelete($customField['id']);
193 $this->customGroupDelete($customGroup['id']);
93ac19cd 194 $this->contactDelete($contactID);
6a488035 195 }
96025800 196
6a488035 197}