3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_activity_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
19 class api_v3_CustomValueContactTypeTest
extends CiviUnitTestCase
{
20 protected $_contactID;
21 protected $_apiversion = 3;
22 protected $CustomGroupIndividual;
23 protected $individualStudent;
25 public function setUp() {
27 // Create Group For Individual Contact Type
29 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
30 'extends' => ['Individual'],
35 $this->CustomGroupIndividual
= $this->customGroupCreate($groupIndividual);
37 $this->IndividualField
= $this->customFieldCreate(['custom_group_id' => $this->CustomGroupIndividual
['id']]);
39 // Create Group For Individual-Student Contact Sub Type
41 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
42 'extends' => ['Individual', ['Student']],
47 $this->CustomGroupIndiStudent
= $this->customGroupCreate($groupIndiStudent);
49 $this->IndiStudentField
= $this->customFieldCreate(['custom_group_id' => $this->CustomGroupIndiStudent
['id']]);
52 'first_name' => 'Mathev',
53 'last_name' => 'Adison',
54 'contact_type' => 'Individual',
57 $this->individual
= $this->individualCreate($params);
60 'first_name' => 'Steve',
61 'last_name' => 'Tosun',
62 'contact_type' => 'Individual',
63 'contact_sub_type' => 'Student',
65 $this->individualStudent
= $this->individualCreate($params);
68 'first_name' => 'Mark',
69 'last_name' => 'Dawson',
70 'contact_type' => 'Individual',
71 'contact_sub_type' => 'Parent',
73 $this->individualParent
= $this->individualCreate($params);
76 'organization_name' => 'Wellspring',
77 'contact_type' => 'Organization',
79 $this->organization
= $this->organizationCreate($params);
82 'organization_name' => 'SubUrban',
83 'contact_type' => 'Organization',
84 'contact_sub_type' => 'Sponsor',
86 $this->organizationSponsor
= $this->organizationCreate($params);
87 //refresh php cached variables
88 CRM_Core_PseudoConstant
::flush();
89 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndividualField
['id'], TRUE);
90 CRM_Core_BAO_CustomField
::getTableColumnGroup($this->IndiStudentField
['id'], TRUE);
93 public function tearDown() {
94 $tablesToTruncate = ['civicrm_contact', 'civicrm_cache'];
95 $this->quickCleanup($tablesToTruncate, TRUE);
99 * Test that custom fields is returned for correct contact type only.
101 public function testGetFields() {
102 $result = $this->callAPISuccess('Contact', 'getfields', []);
103 $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));
104 $result = $this->callAPISuccess('Contact', 'getfields', [
105 'action' => 'create',
106 'contact_type' => 'Individual',
107 ], 'in line' . __LINE__
);
108 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
109 $result = $this->callAPISuccess('Contact', 'getfields', [
110 'action' => 'create',
111 'contact_type' => 'Organization',
113 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__
. print_r(array_keys($result['values']), TRUE));
114 $result = $this->callAPISuccess('Relationship', 'getfields', ['action' => 'create'], 'in line' . __LINE__
);
115 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
119 * Add Custom data of Contact Type : Individual to a Contact type: Organization
121 public function testAddIndividualCustomDataToOrganization() {
124 'id' => $this->organization
,
125 'contact_type' => 'Organization',
126 "custom_{$this->IndividualField['id']}" => 'Test String',
127 // so that undefined_fields is returned
131 $contact = $this->callAPISuccess('contact', 'create', $params);
132 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
133 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__
);
137 * Add valid Empty params to a Contact Type : Individual
138 * note - don't copy & paste this - is of marginal value
140 public function testAddCustomDataEmptyToIndividual() {
141 $contact = $this->callAPIFailure('contact', 'create', [],
142 'Mandatory key(s) missing from params array: contact_type'
147 * Add valid custom data to a Contact Type : Individual
149 public function testAddValidCustomDataToIndividual() {
152 'contact_id' => $this->individual
,
153 'contact_type' => 'Individual',
154 "custom_{$this->IndividualField['id']}" => 'Test String',
156 $contact = $this->callAPISuccess('contact', 'create', $params);
158 $this->assertNotNull($contact['id']);
159 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individual
);
160 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
162 // Check the Value in Database
163 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String');
167 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
169 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
172 'contact_id' => $this->organizationSponsor
,
173 'contact_type' => 'Organization',
174 "custom_{$this->IndiStudentField['id']}" => 'Test String',
175 // so that undefined_fields is returned
179 $contact = $this->callAPISuccess('contact', 'create', $params);
180 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
181 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
185 * Add valid custom data to a Contact Type : Individual Subtype: Student
187 public function testCreateValidCustomDataToIndividualStudent() {
190 'contact_id' => $this->individualStudent
,
191 'contact_type' => 'Individual',
192 "custom_{$this->IndiStudentField['id']}" => 'Test String',
195 $result = $this->callAPISuccess('contact', 'create', $params);
197 $this->assertNotNull($result['id']);
198 $entityValues = CRM_Core_BAO_CustomValueTable
::getEntityValues($this->individualStudent
);
199 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
201 // Check the Value in Database
202 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String');
206 * Add custom data of Individual Student to a Contact Type : Individual - parent
208 public function testAddIndividualStudentCustomDataToIndividualParent() {
211 'contact_id' => $this->individualParent
,
212 'contact_type' => 'Individual',
213 "custom_{$this->IndiStudentField['id']}" => 'Test String',
214 // so that undefined_fields is returned
217 $contact = $this->callAPISuccess('contact', 'create', $params);
218 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__
);
219 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__
);
225 * Retrieve Valid custom Data added to Individual Contact Type.
227 public function testRetrieveValidCustomDataToIndividual() {
230 'contact_id' => $this->individual
,
231 'contact_type' => 'Individual',
232 "custom_" . $this->IndividualField
['id'] => 'Test String',
235 $contact = $this->callAPISuccess('contact', 'create', $params);
237 $this->assertAPISuccess($contact);
239 'contact_id' => $this->individual
,
240 'contact_type' => 'Individual',
241 "return.custom_{$this->IndividualField['id']}" => 1,
244 $getContact = $this->callAPISuccess('contact', 'get', $params);
246 $this->assertEquals($getContact['values'][$this->individual
]["custom_" . $this->IndividualField
['id']], 'Test String');
250 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
252 public function testRetrieveValidCustomDataToIndividualStudent() {
255 'contact_id' => $this->individualStudent
,
256 'contact_type' => 'Individual',
257 'contact_sub_type' => 'Student',
258 "custom_{$this->IndiStudentField['id']}" => 'Test String',
261 $contact = $this->callAPISuccess('contact', 'create', $params);
262 $this->assertAPISuccess($contact);
264 'contact_id' => $this->individualStudent
,
265 'contact_type' => 'Individual',
266 'contact_sub_type' => 'Student',
267 "return.custom_{$this->IndiStudentField['id']}" => 1,
270 $getContact = $this->callAPISuccess('contact', 'get', $params);
272 $this->assertEquals($getContact['values'][$this->individualStudent
]["custom_{$this->IndiStudentField['id']}"], 'Test String');