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