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('customGroup');
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 = 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');
179 * Add valid custom data to a Contact Type : Individual
181 function testAddValidCustomDataToIndividual() {
184 'contact_id' => $this->individual
,
185 'contact_type' => 'Individual',
186 "custom_{$this->IndividualField['id']}" => 'Test String',
187 'version' => $this->_apiversion
,
189 $contact = civicrm_api('contact', 'create', $params);
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']}"];
195 // Check the Value in Database
196 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
200 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
202 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
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
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__
);
218 * Add valid custom data to a Contact Type : Individual Subtype: Student
220 function testCreateValidCustomDataToIndividualStudent() {
223 'contact_id' => $this->individualStudent
,
224 'contact_type' => 'Individual',
225 "custom_{$this->IndiStudentField['id']}" => 'Test String',
226 'version' => $this->_apiversion
,
229 $result = civicrm_api('contact', 'create', $params);
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']}"];
235 // Check the Value in Database
236 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
240 * Add custom data of Individual Student to a Contact Type : Individual - parent
242 function testAddIndividualStudentCustomDataToIndividualParent() {
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
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__
);
261 * Retrieve Valid custom Data added to Individual Contact Type
263 function testRetrieveValidCustomDataToIndividual() {
266 'contact_id' => $this->individual
,
267 'contact_type' => 'Individual',
268 "custom_" . $this->IndividualField
['id'] => 'Test String',
269 'version' => $this->_apiversion
,
272 $contact = civicrm_api('contact', 'create', $params);
274 $this->assertAPISuccess($contact);
276 'contact_id' => $this->individual
,
277 'contact_type' => 'Individual',
278 "return.custom_{$this->IndividualField['id']}" => 1,
279 'version' => $this->_apiversion
,
282 $getContact = civicrm_api('contact', 'get', $params);
284 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
288 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
290 function testRetrieveValidCustomDataToIndividualStudent() {
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
,
300 $contact = civicrm_api('contact', 'create', $params);
301 $this->assertAPISuccess($contact);
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,
310 $getContact = civicrm_api('contact', 'get', $params);
313 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);