e85e5a573c460e989a9279a2c09fcf3077b119d4
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueContactTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test APIv3 civicrm_activity_* functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
35 */
36
37 class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase {
38 protected $_contactID;
39 protected $_apiversion;
40 protected $CustomGroupIndividual;
41 protected $individualStudent;
42 public $_eNoticeCompliant = TRUE;
43
44 function get_info() {
45 return array(
46 'name' => 'Custom Data For Contact Subtype',
47 'description' => 'Test Custom Data for Contact subtype.',
48 'group' => 'CiviCRM API Tests',
49 );
50 }
51
52 function setUp() {
53
54 parent::setUp();
55 $this->_apiversion = 3;
56 // Create Group For Individual Contact Type
57 $groupIndividual = array('title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
58 'extends' => array('Individual'),
59 'style' => 'Inline',
60 'is_active' => 1,
61 'version' => $this->_apiversion,
62 );
63
64 $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
65
66 $this->IndividualField = $this->customFieldCreate($this->CustomGroupIndividual['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
67
68 // Create Group For Individual-Student Contact Sub Type
69 $groupIndiStudent = array(
70 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
71 'extends' => array('Individual', array('Student')),
72 'style' => 'Inline',
73 'is_active' => 1,
74 'version' => $this->_apiversion,
75 );
76
77 $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
78
79 $this->IndiStudentField = $this->customFieldCreate($this->CustomGroupIndiStudent['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
80
81 $params = array(
82 'first_name' => 'Mathev',
83 'last_name' => 'Adison',
84 'contact_type' => 'Individual',
85 'version' => $this->_apiversion,
86 );
87
88 $this->individual = $this->individualCreate($params);
89
90 $params = array(
91 'first_name' => 'Steve',
92 'last_name' => 'Tosun',
93 'contact_type' => 'Individual',
94 'contact_sub_type' => 'Student',
95 'version' => $this->_apiversion,
96 );
97 $this->individualStudent = $this->individualCreate($params);
98
99 $params = array(
100 'first_name' => 'Mark',
101 'last_name' => 'Dawson',
102 'contact_type' => 'Individual',
103 'contact_sub_type' => 'Parent',
104 'version' => $this->_apiversion,
105 );
106 $this->individualParent = $this->individualCreate($params);
107
108 $params = array(
109 'organization_name' => 'Wellspring',
110 'contact_type' => 'Organization',
111 'version' => $this->_apiversion,
112 );
113 $this->organization = $this->organizationCreate($params);
114
115 $params = array(
116 'organization_name' => 'SubUrban',
117 'contact_type' => 'Organization',
118 'contact_sub_type' => 'Sponsor',
119 'version' => $this->_apiversion,
120 );
121 $this->organizationSponsor = $this->organizationCreate($params);
122 //refresh php cached variables
123 CRM_Core_PseudoConstant::flush();
124 CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id'], True);
125 CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id'], True);
126 }
127
128 function tearDown() {
129 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
130 $this->quickCleanup($tablesToTruncate, TRUE);
131 }
132 /*
133 * Test that custom fields is returned for correct contact type only
134 */
135 function testGetFields() {
136 $result = civicrm_api('Contact', 'getfields', array('version' => 3));
137 $this->assertAPISuccess($result);
138 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'If This fails there is probably a cachine issue - failure in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
139 $result = civicrm_api('Contact', 'getfields', array('version' => 3, 'action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__);
140 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
141 $result = civicrm_api('Contact', 'getfields', array('version' => 3, 'action' => 'create', 'contact_type' => 'Organization'));
142 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
143 $result = civicrm_api('Relationship', 'getfields', array('version' => 3, 'action' => 'create'), 'in line' . __LINE__);
144 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
145 }
146
147 /**
148 * Add Custom data of Contact Type : Individual to a Contact type: Organization
149 */
150 function testAddIndividualCustomDataToOrganization() {
151
152 $params = array(
153 'id' => $this->organization,
154 'contact_type' => 'Organization',
155 "custom_{$this->IndividualField['id']}" => 'Test String',
156 'version' => $this->_apiversion,
157 'debug' => 1,// so that undefined_fields is returned
158 );
159
160 $contact = civicrm_api('contact', 'create', $params);
161 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
162 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__);
163 }
164
165 /**
166 * Add valid Empty params to a Contact Type : Individual
167 */
168 function testAddCustomDataEmptyToIndividual() {
169
170 $params = array(
171 'version' => 3,
172 );
173 $contact = $this->callAPIFailure('contact', 'create', $params);
174 $this->assertEquals($contact['error_message'], 'Mandatory key(s) missing from params array: contact_type');
175 }
176
177 /**
178 * Add valid custom data to a Contact Type : Individual
179 */
180 function testAddValidCustomDataToIndividual() {
181
182 $params = array(
183 'contact_id' => $this->individual,
184 'contact_type' => 'Individual',
185 "custom_{$this->IndividualField['id']}" => 'Test String',
186 'version' => $this->_apiversion,
187 );
188 $contact = civicrm_api('contact', 'create', $params);
189
190 $this->assertNotNull($contact['id'], 'In line ' . __LINE__);
191 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
192 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
193
194 // Check the Value in Database
195 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__);
196 }
197
198 /**
199 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
200 */
201 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
202
203 $params = array(
204 'contact_id' => $this->organizationSponsor,
205 'contact_type' => 'Organization',
206 "custom_{$this->IndiStudentField['id']}" => 'Test String',
207 'version' => $this->_apiversion,
208 'debug' => 1,// so that undefined_fields is returned
209 );
210
211 $contact = civicrm_api('contact', 'create', $params);
212 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
213 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
214 }
215
216 /**
217 * Add valid custom data to a Contact Type : Individual Subtype: Student
218 */
219 function testCreateValidCustomDataToIndividualStudent() {
220
221 $params = array(
222 'contact_id' => $this->individualStudent,
223 'contact_type' => 'Individual',
224 "custom_{$this->IndiStudentField['id']}" => 'Test String',
225 'version' => $this->_apiversion,
226 );
227
228 $result = civicrm_api('contact', 'create', $params);
229
230 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
231 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
232 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
233
234 // Check the Value in Database
235 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__);
236 }
237
238 /**
239 * Add custom data of Individual Student to a Contact Type : Individual - parent
240 */
241 function testAddIndividualStudentCustomDataToIndividualParent() {
242
243 $params = array(
244 'contact_id' => $this->individualParent,
245 'contact_type' => 'Individual',
246 "custom_{$this->IndiStudentField['id']}" => 'Test String',
247 'version' => $this->_apiversion,
248 'debug' => 1,// so that undefined_fields is returned
249 );
250 $contact = civicrm_api('contact', 'create', $params);
251 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
252 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
253 }
254
255
256
257 // Retrieve Methods
258
259 /**
260 * Retrieve Valid custom Data added to Individual Contact Type
261 */
262 function testRetrieveValidCustomDataToIndividual() {
263
264 $params = array(
265 'contact_id' => $this->individual,
266 'contact_type' => 'Individual',
267 "custom_" . $this->IndividualField['id'] => 'Test String',
268 'version' => $this->_apiversion,
269 );
270
271 $contact = civicrm_api('contact', 'create', $params);
272
273 $this->assertAPISuccess($contact);
274 $params = array(
275 'contact_id' => $this->individual,
276 'contact_type' => 'Individual',
277 "return.custom_{$this->IndividualField['id']}" => 1,
278 'version' => $this->_apiversion,
279 );
280
281 $getContact = civicrm_api('contact', 'get', $params);
282
283 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String', 'In line ' . __LINE__);
284 }
285
286 /**
287 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
288 */
289 function testRetrieveValidCustomDataToIndividualStudent() {
290
291 $params = array(
292 'contact_id' => $this->individualStudent,
293 'contact_type' => 'Individual',
294 'contact_sub_type' => 'Student',
295 "custom_{$this->IndiStudentField['id']}" => 'Test String',
296 'version' => $this->_apiversion,
297 );
298
299 $contact = civicrm_api('contact', 'create', $params);
300 $this->assertAPISuccess($contact);
301 $params = array(
302 'contact_id' => $this->individualStudent,
303 'contact_type' => 'Individual',
304 'contact_sub_type' => 'Student',
305 'version' => $this->_apiversion,
306 "return.custom_{$this->IndiStudentField['id']}" => 1,
307 );
308
309 $getContact = civicrm_api('contact', 'get', $params);
310
311
312 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__);
313 }
314 }
315