3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test APIv3 civicrm_activity_* functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
37 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
38 protected $_contactID;
39 protected $_apiversion;
40 protected $CustomGroupIndividual;
41 protected $individualStudent;
42 public $_eNoticeCompliant = TRUE;
46 'name' => 'Custom Data For Contact Subtype',
47 'description' => 'Test Custom Data for Contact subtype.',
48 'group' => 'CiviCRM API Tests',
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'),
61 'version' => $this->_apiversion
,
64 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
66 $this->IndividualField
= $this->customFieldCreate($this->CustomGroupIndividual
['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
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')),
74 'version' => $this->_apiversion
,
77 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
79 $this->IndiStudentField
= $this->customFieldCreate($this->CustomGroupIndiStudent
['id'], "Custom Field" . substr(sha1(rand()), 0, 7));
82 'first_name' => 'Mathev',
83 'last_name' => 'Adison',
84 'contact_type' => 'Individual',
85 'version' => $this->_apiversion
,
88 $this->individual
= $this->individualCreate($params);
91 'first_name' => 'Steve',
92 'last_name' => 'Tosun',
93 'contact_type' => 'Individual',
94 'contact_sub_type' => 'Student',
95 'version' => $this->_apiversion
,
97 $this->individualStudent
= $this->individualCreate($params);
100 'first_name' => 'Mark',
101 'last_name' => 'Dawson',
102 'contact_type' => 'Individual',
103 'contact_sub_type' => 'Parent',
104 'version' => $this->_apiversion
,
106 $this->individualParent
= $this->individualCreate($params);
109 'organization_name' => 'Wellspring',
110 'contact_type' => 'Organization',
111 'version' => $this->_apiversion
,
113 $this->organization
= $this->organizationCreate($params);
116 'organization_name' => 'SubUrban',
117 'contact_type' => 'Organization',
118 'contact_sub_type' => 'Sponsor',
119 'version' => $this->_apiversion
,
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);
128 function tearDown() {
129 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
130 $this->quickCleanup($tablesToTruncate, TRUE);
133 * Test that custom fields is returned for correct contact type only
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']);
148 * Add Custom data of Contact Type : Individual to a Contact type: Organization
150 function testAddIndividualCustomDataToOrganization() {
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
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__
);
166 * Add valid Empty params to a Contact Type : Individual
168 function testAddCustomDataEmptyToIndividual() {
173 $contact = $this->callAPIFailure('contact', 'create', $params);
174 $this->assertEquals($contact['error_message'], 'Mandatory key(s) missing from params array: contact_type');
178 * Add valid custom data to a Contact Type : Individual
180 function testAddValidCustomDataToIndividual() {
183 'contact_id' => $this->individual
,
184 'contact_type' => 'Individual',
185 "custom_{$this->IndividualField['id']}" => 'Test String',
186 'version' => $this->_apiversion
,
188 $contact = civicrm_api('contact', 'create', $params);
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']}"];
194 // Check the Value in Database
195 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
199 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
201 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
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
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__
);
217 * Add valid custom data to a Contact Type : Individual Subtype: Student
219 function testCreateValidCustomDataToIndividualStudent() {
222 'contact_id' => $this->individualStudent
,
223 'contact_type' => 'Individual',
224 "custom_{$this->IndiStudentField['id']}" => 'Test String',
225 'version' => $this->_apiversion
,
228 $result = civicrm_api('contact', 'create', $params);
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']}"];
234 // Check the Value in Database
235 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
239 * Add custom data of Individual Student to a Contact Type : Individual - parent
241 function testAddIndividualStudentCustomDataToIndividualParent() {
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
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__
);
260 * Retrieve Valid custom Data added to Individual Contact Type
262 function testRetrieveValidCustomDataToIndividual() {
265 'contact_id' => $this->individual
,
266 'contact_type' => 'Individual',
267 "custom_" . $this->IndividualField
['id'] => 'Test String',
268 'version' => $this->_apiversion
,
271 $contact = civicrm_api('contact', 'create', $params);
273 $this->assertAPISuccess($contact);
275 'contact_id' => $this->individual
,
276 'contact_type' => 'Individual',
277 "return.custom_{$this->IndividualField['id']}" => 1,
278 'version' => $this->_apiversion
,
281 $getContact = civicrm_api('contact', 'get', $params);
283 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
287 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
289 function testRetrieveValidCustomDataToIndividualStudent() {
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
,
299 $contact = civicrm_api('contact', 'create', $params);
300 $this->assertAPISuccess($contact);
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,
309 $getContact = civicrm_api('contact', 'get', $params);
312 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);