3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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;
49 'name' => 'Custom Data For Contact Subtype',
50 'description' => 'Test Custom Data for Contact Subtype.',
51 'group' => 'CiviCRM API Tests',
57 // Create Group For Individual Contact Type
58 $groupIndividual = array(
59 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
60 'extends' => array('Individual'),
65 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
67 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
69 // Create Group For Individual-Student Contact Sub Type
70 $groupIndiStudent = array(
71 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
72 'extends' => array('Individual', array('Student')),
77 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
79 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
82 'first_name' => 'Mathev',
83 'last_name' => 'Adison',
84 'contact_type' => 'Individual',
87 $this->individual
= $this->individualCreate($params);
90 'first_name' => 'Steve',
91 'last_name' => 'Tosun',
92 'contact_type' => 'Individual',
93 'contact_sub_type' => 'Student',
95 $this->individualStudent
= $this->individualCreate($params);
98 'first_name' => 'Mark',
99 'last_name' => 'Dawson',
100 'contact_type' => 'Individual',
101 'contact_sub_type' => 'Parent',
103 $this->individualParent
= $this->individualCreate($params);
106 'organization_name' => 'Wellspring',
107 'contact_type' => 'Organization',
109 $this->organization
= $this->organizationCreate($params);
112 'organization_name' => 'SubUrban',
113 'contact_type' => 'Organization',
114 'contact_sub_type' => 'Sponsor',
116 $this->organizationSponsor
= $this->organizationCreate($params);
117 //refresh php cached variables
118 CRM_Core_PseudoConstant
::flush();
119 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], True);
120 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], True);
123 function tearDown() {
124 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
125 $this->quickCleanup($tablesToTruncate, TRUE);
128 * Test that custom fields is returned for correct contact type only
130 function testGetFields() {
131 $result = $this->callAPISuccess('Contact', 'getfields', array());
132 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'If This fails there is probably a caching issue - failure in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
133 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__
);
134 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
135 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Organization'));
136 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
137 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__
);
138 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
142 * Add Custom data of Contact Type : Individual to a Contact type: Organization
144 function testAddIndividualCustomDataToOrganization() {
147 'id' => $this->organization
,
148 'contact_type' => 'Organization',
149 "custom_{$this->IndividualField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
152 $contact = $this->callAPISuccess('contact', 'create', $params);
153 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
154 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
158 * Add valid Empty params to a Contact Type : Individual
159 * note - don't copy & paste this - is of marginal value
161 function testAddCustomDataEmptyToIndividual() {
162 $contact = $this->callAPIFailure('contact', 'create', array(),
163 'Mandatory key(s) missing from params array: contact_type'
168 * Add valid custom data to a Contact Type : Individual
170 function testAddValidCustomDataToIndividual() {
173 'contact_id' => $this->individual
,
174 'contact_type' => 'Individual',
175 "custom_{$this->IndividualField['id']}" => 'Test String',
177 $contact = $this->callAPISuccess('contact', 'create', $params);
179 $this->assertNotNull($contact['id'], 'In line ' . __LINE__
);
180 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
181 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
183 // Check the Value in Database
184 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__
);
188 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
190 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
193 'contact_id' => $this->organizationSponsor
,
194 'contact_type' => 'Organization',
195 "custom_{$this->IndiStudentField['id']}" => 'Test String',
196 'debug' => 1,// so that undefined_fields is returned
199 $contact = $this->callAPISuccess('contact', 'create', $params);
200 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
201 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
205 * Add valid custom data to a Contact Type : Individual Subtype: Student
207 function testCreateValidCustomDataToIndividualStudent() {
210 'contact_id' => $this->individualStudent
,
211 'contact_type' => 'Individual',
212 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
214 $result = $this->callAPISuccess('contact', 'create', $params);
216 $this->assertNotNull($result['id'], 'In line ' . __LINE__
);
217 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
218 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
220 // Check the Value in Database
221 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__
);
225 * Add custom data of Individual Student to a Contact Type : Individual - parent
227 function testAddIndividualStudentCustomDataToIndividualParent() {
230 'contact_id' => $this->individualParent
,
231 'contact_type' => 'Individual',
232 "custom_{$this->IndiStudentField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
234 $contact = $this->callAPISuccess('contact', 'create', $params);
235 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
236 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
244 * Retrieve Valid custom Data added to Individual Contact Type
246 function testRetrieveValidCustomDataToIndividual() {
249 'contact_id' => $this->individual
,
250 'contact_type' => 'Individual',
251 "custom_" . $this->IndividualField
['id'] => 'Test String', );
253 $contact = $this->callAPISuccess('contact', 'create', $params);
255 $this->assertAPISuccess($contact);
257 'contact_id' => $this->individual
,
258 'contact_type' => 'Individual',
259 "return.custom_{$this->IndividualField['id']}" => 1,
262 $getContact = $this->callAPISuccess('contact', 'get', $params);
264 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String', 'In line ' . __LINE__
);
268 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
270 function testRetrieveValidCustomDataToIndividualStudent() {
273 'contact_id' => $this->individualStudent
,
274 'contact_type' => 'Individual',
275 'contact_sub_type' => 'Student',
276 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
278 $contact = $this->callAPISuccess('contact', 'create', $params);
279 $this->assertAPISuccess($contact);
281 'contact_id' => $this->individualStudent
,
282 'contact_type' => 'Individual',
283 'contact_sub_type' => 'Student', "return.custom_{$this->IndiStudentField['id']}" => 1,
286 $getContact = $this->callAPISuccess('contact', 'get', $params);
289 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__
);