3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 =3;
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',
54 // Create Group For Individual Contact Type
55 $groupIndividual = array(
56 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
57 'extends' => array('Individual'),
62 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
64 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
66 // Create Group For Individual-Student Contact Sub Type
67 $groupIndiStudent = array(
68 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
69 'extends' => array('Individual', array('Student')),
74 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
76 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
79 'first_name' => 'Mathev',
80 'last_name' => 'Adison',
81 'contact_type' => 'Individual',
84 $this->individual
= $this->individualCreate($params);
87 'first_name' => 'Steve',
88 'last_name' => 'Tosun',
89 'contact_type' => 'Individual',
90 'contact_sub_type' => 'Student',
92 $this->individualStudent
= $this->individualCreate($params);
95 'first_name' => 'Mark',
96 'last_name' => 'Dawson',
97 'contact_type' => 'Individual',
98 'contact_sub_type' => 'Parent',
100 $this->individualParent
= $this->individualCreate($params);
103 'organization_name' => 'Wellspring',
104 'contact_type' => 'Organization',
106 $this->organization
= $this->organizationCreate($params);
109 'organization_name' => 'SubUrban',
110 'contact_type' => 'Organization',
111 'contact_sub_type' => 'Sponsor',
113 $this->organizationSponsor
= $this->organizationCreate($params);
114 //refresh php cached variables
115 CRM_Core_PseudoConstant
::flush();
116 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], True);
117 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], True);
120 function tearDown() {
121 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
122 $this->quickCleanup($tablesToTruncate, TRUE);
125 * Test that custom fields is returned for correct contact type only
127 function testGetFields() {
128 $result = $this->callAPISuccess('Contact', 'getfields', array());
129 $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));
130 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__
);
131 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
132 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Organization'));
133 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
134 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__
);
135 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
139 * Add Custom data of Contact Type : Individual to a Contact type: Organization
141 function testAddIndividualCustomDataToOrganization() {
144 'id' => $this->organization
,
145 'contact_type' => 'Organization',
146 "custom_{$this->IndividualField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
149 $contact = $this->callAPISuccess('contact', 'create', $params);
150 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
151 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
155 * Add valid Empty params to a Contact Type : Individual
156 * note - don't copy & paste this - is of marginal value
158 function testAddCustomDataEmptyToIndividual() {
159 $contact = $this->callAPIFailure('contact', 'create', array(),
160 'Mandatory key(s) missing from params array: contact_type'
165 * Add valid custom data to a Contact Type : Individual
167 function testAddValidCustomDataToIndividual() {
170 'contact_id' => $this->individual
,
171 'contact_type' => 'Individual',
172 "custom_{$this->IndividualField['id']}" => 'Test String',
174 $contact = $this->callAPISuccess('contact', 'create', $params);
176 $this->assertNotNull($contact['id'], 'In line ' . __LINE__
);
177 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
178 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
180 // Check the Value in Database
181 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
185 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
187 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
190 'contact_id' => $this->organizationSponsor
,
191 'contact_type' => 'Organization',
192 "custom_{$this->IndiStudentField['id']}" => 'Test String',
193 'debug' => 1,// so that undefined_fields is returned
196 $contact = $this->callAPISuccess('contact', 'create', $params);
197 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
198 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
202 * Add valid custom data to a Contact Type : Individual Subtype: Student
204 function testCreateValidCustomDataToIndividualStudent() {
207 'contact_id' => $this->individualStudent
,
208 'contact_type' => 'Individual',
209 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
211 $result = $this->callAPISuccess('contact', 'create', $params);
213 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
214 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
215 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
217 // Check the Value in Database
218 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
222 * Add custom data of Individual Student to a Contact Type : Individual - parent
224 function testAddIndividualStudentCustomDataToIndividualParent() {
227 'contact_id' => $this->individualParent
,
228 'contact_type' => 'Individual',
229 "custom_{$this->IndiStudentField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
231 $contact = $this->callAPISuccess('contact', 'create', $params);
232 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
233 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
241 * Retrieve Valid custom Data added to Individual Contact Type
243 function testRetrieveValidCustomDataToIndividual() {
246 'contact_id' => $this->individual
,
247 'contact_type' => 'Individual',
248 "custom_" . $this->IndividualField
['id'] => 'Test String', );
250 $contact = $this->callAPISuccess('contact', 'create', $params);
252 $this->assertAPISuccess($contact);
254 'contact_id' => $this->individual
,
255 'contact_type' => 'Individual',
256 "return.custom_{$this->IndividualField['id']}" => 1,
259 $getContact = $this->callAPISuccess('contact', 'get', $params);
261 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
265 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
267 function testRetrieveValidCustomDataToIndividualStudent() {
270 'contact_id' => $this->individualStudent
,
271 'contact_type' => 'Individual',
272 'contact_sub_type' => 'Student',
273 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
275 $contact = $this->callAPISuccess('contact', 'create', $params);
276 $this->assertAPISuccess($contact);
278 'contact_id' => $this->individualStudent
,
279 'contact_type' => 'Individual',
280 'contact_sub_type' => 'Student', "return.custom_{$this->IndiStudentField['id']}" => 1,
283 $getContact = $this->callAPISuccess('contact', 'get', $params);
286 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);