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