3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
35 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
36 protected $_contactID;
37 protected $_apiversion = 3;
38 protected $CustomGroupIndividual;
39 protected $individualStudent;
41 public function setUp() {
43 // Create Group For Individual Contact Type
44 $groupIndividual = array(
45 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
46 'extends' => array('Individual'),
51 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
53 $this->IndividualField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual
['id']));
55 // Create Group For Individual-Student Contact Sub Type
56 $groupIndiStudent = array(
57 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
58 'extends' => array('Individual', array('Student')),
63 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
65 $this->IndiStudentField
= $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent
['id']));
68 'first_name' => 'Mathev',
69 'last_name' => 'Adison',
70 'contact_type' => 'Individual',
73 $this->individual
= $this->individualCreate($params);
76 'first_name' => 'Steve',
77 'last_name' => 'Tosun',
78 'contact_type' => 'Individual',
79 'contact_sub_type' => 'Student',
81 $this->individualStudent
= $this->individualCreate($params);
84 'first_name' => 'Mark',
85 'last_name' => 'Dawson',
86 'contact_type' => 'Individual',
87 'contact_sub_type' => 'Parent',
89 $this->individualParent
= $this->individualCreate($params);
92 'organization_name' => 'Wellspring',
93 'contact_type' => 'Organization',
95 $this->organization
= $this->organizationCreate($params);
98 'organization_name' => 'SubUrban',
99 'contact_type' => 'Organization',
100 'contact_sub_type' => 'Sponsor',
102 $this->organizationSponsor
= $this->organizationCreate($params);
103 //refresh php cached variables
104 CRM_Core_PseudoConstant
::flush();
105 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], TRUE);
106 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], TRUE);
109 public function tearDown() {
110 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
111 $this->quickCleanup($tablesToTruncate, TRUE);
115 * Test that custom fields is returned for correct contact type only.
117 public function testGetFields() {
118 $result = $this->callAPISuccess('Contact', 'getfields', array());
119 $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));
120 $result = $this->callAPISuccess('Contact', 'getfields', array(
121 'action' => 'create',
122 'contact_type' => 'Individual',
123 ), 'in line' . __LINE__
);
124 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
125 $result = $this->callAPISuccess('Contact', 'getfields', array(
126 'action' => 'create',
127 'contact_type' => 'Organization',
129 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
130 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__
);
131 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
135 * Add Custom data of Contact Type : Individual to a Contact type: Organization
137 public function testAddIndividualCustomDataToOrganization() {
140 'id' => $this->organization
,
141 'contact_type' => 'Organization',
142 "custom_{$this->IndividualField['id']}" => 'Test String',
143 'debug' => 1, // so that undefined_fields is returned
146 $contact = $this->callAPISuccess('contact', 'create', $params);
147 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
148 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
152 * Add valid Empty params to a Contact Type : Individual
153 * note - don't copy & paste this - is of marginal value
155 public function testAddCustomDataEmptyToIndividual() {
156 $contact = $this->callAPIFailure('contact', 'create', array(),
157 'Mandatory key(s) missing from params array: contact_type'
162 * Add valid custom data to a Contact Type : Individual
164 public function testAddValidCustomDataToIndividual() {
167 'contact_id' => $this->individual
,
168 'contact_type' => 'Individual',
169 "custom_{$this->IndividualField['id']}" => 'Test String',
171 $contact = $this->callAPISuccess('contact', 'create', $params);
173 $this->assertNotNull($contact['id']);
174 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
175 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
177 // Check the Value in Database
178 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String');
182 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
184 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
187 'contact_id' => $this->organizationSponsor
,
188 'contact_type' => 'Organization',
189 "custom_{$this->IndiStudentField['id']}" => 'Test String',
190 'debug' => 1, // so that undefined_fields is returned
193 $contact = $this->callAPISuccess('contact', 'create', $params);
194 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
195 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
199 * Add valid custom data to a Contact Type : Individual Subtype: Student
201 public function testCreateValidCustomDataToIndividualStudent() {
204 'contact_id' => $this->individualStudent
,
205 'contact_type' => 'Individual',
206 "custom_{$this->IndiStudentField['id']}" => 'Test String',
209 $result = $this->callAPISuccess('contact', 'create', $params);
211 $this->assertNotNull($result['id']);
212 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
213 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
215 // Check the Value in Database
216 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String');
220 * Add custom data of Individual Student to a Contact Type : Individual - parent
222 public function testAddIndividualStudentCustomDataToIndividualParent() {
225 'contact_id' => $this->individualParent
,
226 'contact_type' => 'Individual',
227 "custom_{$this->IndiStudentField['id']}" => 'Test String',
228 'debug' => 1, // so that undefined_fields is returned
230 $contact = $this->callAPISuccess('contact', 'create', $params);
231 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
232 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
238 * Retrieve Valid custom Data added to Individual Contact Type.
240 public function testRetrieveValidCustomDataToIndividual() {
243 'contact_id' => $this->individual
,
244 'contact_type' => 'Individual',
245 "custom_" . $this->IndividualField
['id'] => 'Test String',
248 $contact = $this->callAPISuccess('contact', 'create', $params);
250 $this->assertAPISuccess($contact);
252 'contact_id' => $this->individual
,
253 'contact_type' => 'Individual',
254 "return.custom_{$this->IndividualField['id']}" => 1,
257 $getContact = $this->callAPISuccess('contact', 'get', $params);
259 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String');
263 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
265 public function testRetrieveValidCustomDataToIndividualStudent() {
268 'contact_id' => $this->individualStudent
,
269 'contact_type' => 'Individual',
270 'contact_sub_type' => 'Student',
271 "custom_{$this->IndiStudentField['id']}" => 'Test String',
274 $contact = $this->callAPISuccess('contact', 'create', $params);
275 $this->assertAPISuccess($contact);
277 'contact_id' => $this->individualStudent
,
278 'contact_type' => 'Individual',
279 'contact_sub_type' => 'Student',
280 "return.custom_{$this->IndiStudentField['id']}" => 1,
283 $getContact = $this->callAPISuccess('contact', 'get', $params);
285 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String');