Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-05-24-02-32-04
[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('customGroup');
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 = civicrm_api('contact', 'create', $params);
174 $this->assertEquals($contact['is_error'], 1);
175 $this->assertEquals($contact['error_message'], 'Mandatory key(s) missing from params array: contact_type');
176 }
177
178 /**
179 * Add valid custom data to a Contact Type : Individual
180 */
181 function testAddValidCustomDataToIndividual() {
182
183 $params = array(
184 'contact_id' => $this->individual,
185 'contact_type' => 'Individual',
186 "custom_{$this->IndividualField['id']}" => 'Test String',
187 'version' => $this->_apiversion,
188 );
189 $contact = civicrm_api('contact', 'create', $params);
190
191 $this->assertNotNull($contact['id'], 'In line ' . __LINE__);
192 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
193 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
194
195 // Check the Value in Database
196 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__);
197 }
198
199 /**
200 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
201 */
202 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
203
204 $params = array(
205 'contact_id' => $this->organizationSponsor,
206 'contact_type' => 'Organization',
207 "custom_{$this->IndiStudentField['id']}" => 'Test String',
208 'version' => $this->_apiversion,
209 'debug' => 1,// so that undefined_fields is returned
210 );
211
212 $contact = civicrm_api('contact', 'create', $params);
213 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
214 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
215 }
216
217 /**
218 * Add valid custom data to a Contact Type : Individual Subtype: Student
219 */
220 function testCreateValidCustomDataToIndividualStudent() {
221
222 $params = array(
223 'contact_id' => $this->individualStudent,
224 'contact_type' => 'Individual',
225 "custom_{$this->IndiStudentField['id']}" => 'Test String',
226 'version' => $this->_apiversion,
227 );
228
229 $result = civicrm_api('contact', 'create', $params);
230
231 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
232 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
233 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
234
235 // Check the Value in Database
236 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__);
237 }
238
239 /**
240 * Add custom data of Individual Student to a Contact Type : Individual - parent
241 */
242 function testAddIndividualStudentCustomDataToIndividualParent() {
243
244 $params = array(
245 'contact_id' => $this->individualParent,
246 'contact_type' => 'Individual',
247 "custom_{$this->IndiStudentField['id']}" => 'Test String',
248 'version' => $this->_apiversion,
249 'debug' => 1,// so that undefined_fields is returned
250 );
251 $contact = civicrm_api('contact', 'create', $params);
252 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
253 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
254 }
255
256
257
258 // Retrieve Methods
259
260 /**
261 * Retrieve Valid custom Data added to Individual Contact Type
262 */
263 function testRetrieveValidCustomDataToIndividual() {
264
265 $params = array(
266 'contact_id' => $this->individual,
267 'contact_type' => 'Individual',
268 "custom_" . $this->IndividualField['id'] => 'Test String',
269 'version' => $this->_apiversion,
270 );
271
272 $contact = civicrm_api('contact', 'create', $params);
273
274 $this->assertAPISuccess($contact);
275 $params = array(
276 'contact_id' => $this->individual,
277 'contact_type' => 'Individual',
278 "return.custom_{$this->IndividualField['id']}" => 1,
279 'version' => $this->_apiversion,
280 );
281
282 $getContact = civicrm_api('contact', 'get', $params);
283
284 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String', 'In line ' . __LINE__);
285 }
286
287 /**
288 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
289 */
290 function testRetrieveValidCustomDataToIndividualStudent() {
291
292 $params = array(
293 'contact_id' => $this->individualStudent,
294 'contact_type' => 'Individual',
295 'contact_sub_type' => 'Student',
296 "custom_{$this->IndiStudentField['id']}" => 'Test String',
297 'version' => $this->_apiversion,
298 );
299
300 $contact = civicrm_api('contact', 'create', $params);
301 $this->assertAPISuccess($contact);
302 $params = array(
303 'contact_id' => $this->individualStudent,
304 'contact_type' => 'Individual',
305 'contact_sub_type' => 'Student',
306 'version' => $this->_apiversion,
307 "return.custom_{$this->IndiStudentField['id']}" => 1,
308 );
309
310 $getContact = civicrm_api('contact', 'get', $params);
311
312
313 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__);
314 }
315 }
316