3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_activity_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
34 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
35 protected $_contactID;
36 protected $_apiversion = 3;
37 protected $CustomGroupIndividual;
38 protected $individualStudent;
40 public function setUp() {
42 // Create Group For Individual Contact Type
43 $groupIndividual = array(
44 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
45 'extends' => array('Individual'),
50 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
52 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
54 // Create Group For Individual-Student Contact Sub Type
55 $groupIndiStudent = array(
56 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
57 'extends' => array('Individual', array('Student')),
62 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
64 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
67 'first_name' => 'Mathev',
68 'last_name' => 'Adison',
69 'contact_type' => 'Individual',
72 $this->individual
= $this->individualCreate($params);
75 'first_name' => 'Steve',
76 'last_name' => 'Tosun',
77 'contact_type' => 'Individual',
78 'contact_sub_type' => 'Student',
80 $this->individualStudent
= $this->individualCreate($params);
83 'first_name' => 'Mark',
84 'last_name' => 'Dawson',
85 'contact_type' => 'Individual',
86 'contact_sub_type' => 'Parent',
88 $this->individualParent
= $this->individualCreate($params);
91 'organization_name' => 'Wellspring',
92 'contact_type' => 'Organization',
94 $this->organization
= $this->organizationCreate($params);
97 'organization_name' => 'SubUrban',
98 'contact_type' => 'Organization',
99 'contact_sub_type' => 'Sponsor',
101 $this->organizationSponsor
= $this->organizationCreate($params);
102 //refresh php cached variables
103 CRM_Core_PseudoConstant
::flush();
104 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], TRUE);
105 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], TRUE);
108 public function tearDown() {
109 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
110 $this->quickCleanup($tablesToTruncate, TRUE);
114 * Test that custom fields is returned for correct contact type only.
116 public function testGetFields() {
117 $result = $this->callAPISuccess('Contact', 'getfields', array());
118 $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));
119 $result = $this->callAPISuccess('Contact', 'getfields', array(
120 'action' => 'create',
121 'contact_type' => 'Individual',
122 ), 'in line' . __LINE__
);
123 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
124 $result = $this->callAPISuccess('Contact', 'getfields', array(
125 'action' => 'create',
126 'contact_type' => 'Organization',
128 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
129 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__
);
130 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
134 * Add Custom data of Contact Type : Individual to a Contact type: Organization
136 public function testAddIndividualCustomDataToOrganization() {
139 'id' => $this->organization
,
140 'contact_type' => 'Organization',
141 "custom_{$this->IndividualField['id']}" => 'Test String',
142 'debug' => 1, // so that undefined_fields is returned
145 $contact = $this->callAPISuccess('contact', 'create', $params);
146 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
147 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
151 * Add valid Empty params to a Contact Type : Individual
152 * note - don't copy & paste this - is of marginal value
154 public function testAddCustomDataEmptyToIndividual() {
155 $contact = $this->callAPIFailure('contact', 'create', array(),
156 'Mandatory key(s) missing from params array: contact_type'
161 * Add valid custom data to a Contact Type : Individual
163 public function testAddValidCustomDataToIndividual() {
166 'contact_id' => $this->individual
,
167 'contact_type' => 'Individual',
168 "custom_{$this->IndividualField['id']}" => 'Test String',
170 $contact = $this->callAPISuccess('contact', 'create', $params);
172 $this->assertNotNull($contact['id']);
173 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
174 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
176 // Check the Value in Database
177 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String');
181 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
183 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
186 'contact_id' => $this->organizationSponsor
,
187 'contact_type' => 'Organization',
188 "custom_{$this->IndiStudentField['id']}" => 'Test String',
189 'debug' => 1, // so that undefined_fields is returned
192 $contact = $this->callAPISuccess('contact', 'create', $params);
193 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
194 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
198 * Add valid custom data to a Contact Type : Individual Subtype: Student
200 public function testCreateValidCustomDataToIndividualStudent() {
203 'contact_id' => $this->individualStudent
,
204 'contact_type' => 'Individual',
205 "custom_{$this->IndiStudentField['id']}" => 'Test String',
208 $result = $this->callAPISuccess('contact', 'create', $params);
210 $this->assertNotNull($result['id']);
211 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
212 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
214 // Check the Value in Database
215 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String');
219 * Add custom data of Individual Student to a Contact Type : Individual - parent
221 public function testAddIndividualStudentCustomDataToIndividualParent() {
224 'contact_id' => $this->individualParent
,
225 'contact_type' => 'Individual',
226 "custom_{$this->IndiStudentField['id']}" => 'Test String',
227 'debug' => 1, // so that undefined_fields is returned
229 $contact = $this->callAPISuccess('contact', 'create', $params);
230 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
231 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
237 * Retrieve Valid custom Data added to Individual Contact Type.
239 public function testRetrieveValidCustomDataToIndividual() {
242 'contact_id' => $this->individual
,
243 'contact_type' => 'Individual',
244 "custom_" . $this->IndividualField
['id'] => 'Test String',
247 $contact = $this->callAPISuccess('contact', 'create', $params);
249 $this->assertAPISuccess($contact);
251 'contact_id' => $this->individual
,
252 'contact_type' => 'Individual',
253 "return.custom_{$this->IndividualField['id']}" => 1,
256 $getContact = $this->callAPISuccess('contact', 'get', $params);
258 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String');
262 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
264 public function testRetrieveValidCustomDataToIndividualStudent() {
267 'contact_id' => $this->individualStudent
,
268 'contact_type' => 'Individual',
269 'contact_sub_type' => 'Student',
270 "custom_{$this->IndiStudentField['id']}" => 'Test String',
273 $contact = $this->callAPISuccess('contact', 'create', $params);
274 $this->assertAPISuccess($contact);
276 'contact_id' => $this->individualStudent
,
277 'contact_type' => 'Individual',
278 'contact_sub_type' => 'Student',
279 "return.custom_{$this->IndiStudentField['id']}" => 1,
282 $getContact = $this->callAPISuccess('contact', 'get', $params);
284 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String');