Merge pull request #17706 from demeritcowboy/mysql-ssl-alt
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueContactTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_activity_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19 class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase {
20 protected $_contactID;
21 protected $_apiversion = 3;
22 protected $CustomGroupIndividual;
23 protected $individualStudent;
24
25 public function setUp() {
26 parent::setUp();
27 // Create Group For Individual Contact Type
28 $groupIndividual = [
29 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
30 'extends' => ['Individual'],
31 'style' => 'Inline',
32 'is_active' => 1,
33 ];
34
35 $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
36
37 $this->IndividualField = $this->customFieldCreate(['custom_group_id' => $this->CustomGroupIndividual['id']]);
38
39 // Create Group For Individual-Student Contact Sub Type
40 $groupIndiStudent = [
41 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
42 'extends' => ['Individual', ['Student']],
43 'style' => 'Inline',
44 'is_active' => 1,
45 ];
46
47 $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
48
49 $this->IndiStudentField = $this->customFieldCreate(['custom_group_id' => $this->CustomGroupIndiStudent['id']]);
50
51 $params = [
52 'first_name' => 'Mathev',
53 'last_name' => 'Adison',
54 'contact_type' => 'Individual',
55 ];
56
57 $this->individual = $this->individualCreate($params);
58
59 $params = [
60 'first_name' => 'Steve',
61 'last_name' => 'Tosun',
62 'contact_type' => 'Individual',
63 'contact_sub_type' => 'Student',
64 ];
65 $this->individualStudent = $this->individualCreate($params);
66
67 $params = [
68 'first_name' => 'Mark',
69 'last_name' => 'Dawson',
70 'contact_type' => 'Individual',
71 'contact_sub_type' => 'Parent',
72 ];
73 $this->individualParent = $this->individualCreate($params);
74
75 $params = [
76 'organization_name' => 'Wellspring',
77 'contact_type' => 'Organization',
78 ];
79 $this->organization = $this->organizationCreate($params);
80
81 $params = [
82 'organization_name' => 'SubUrban',
83 'contact_type' => 'Organization',
84 'contact_sub_type' => 'Sponsor',
85 ];
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);
91 }
92
93 public function tearDown() {
94 $tablesToTruncate = ['civicrm_contact', 'civicrm_cache'];
95 $this->quickCleanup($tablesToTruncate, TRUE);
96 }
97
98 /**
99 * Test that custom fields is returned for correct contact type only.
100 */
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',
112 ]);
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']);
116 }
117
118 /**
119 * Add Custom data of Contact Type : Individual to a Contact type: Organization
120 */
121 public function testAddIndividualCustomDataToOrganization() {
122
123 $params = [
124 'id' => $this->organization,
125 'contact_type' => 'Organization',
126 "custom_{$this->IndividualField['id']}" => 'Test String',
127 // so that undefined_fields is returned
128 'debug' => 1,
129 ];
130
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__);
134 }
135
136 /**
137 * Add valid Empty params to a Contact Type : Individual
138 * note - don't copy & paste this - is of marginal value
139 */
140 public function testAddCustomDataEmptyToIndividual() {
141 $contact = $this->callAPIFailure('contact', 'create', [],
142 'Mandatory key(s) missing from params array: contact_type'
143 );
144 }
145
146 /**
147 * Add valid custom data to a Contact Type : Individual
148 */
149 public function testAddValidCustomDataToIndividual() {
150
151 $params = [
152 'contact_id' => $this->individual,
153 'contact_type' => 'Individual',
154 "custom_{$this->IndividualField['id']}" => 'Test String',
155 ];
156 $contact = $this->callAPISuccess('contact', 'create', $params);
157
158 $this->assertNotNull($contact['id']);
159 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
160 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
161
162 // Check the Value in Database
163 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String');
164 }
165
166 /**
167 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
168 */
169 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
170
171 $params = [
172 'contact_id' => $this->organizationSponsor,
173 'contact_type' => 'Organization',
174 "custom_{$this->IndiStudentField['id']}" => 'Test String',
175 // so that undefined_fields is returned
176 'debug' => 1,
177 ];
178
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__);
182 }
183
184 /**
185 * Add valid custom data to a Contact Type : Individual Subtype: Student
186 */
187 public function testCreateValidCustomDataToIndividualStudent() {
188
189 $params = [
190 'contact_id' => $this->individualStudent,
191 'contact_type' => 'Individual',
192 "custom_{$this->IndiStudentField['id']}" => 'Test String',
193 ];
194
195 $result = $this->callAPISuccess('contact', 'create', $params);
196
197 $this->assertNotNull($result['id']);
198 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
199 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
200
201 // Check the Value in Database
202 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String');
203 }
204
205 /**
206 * Add custom data of Individual Student to a Contact Type : Individual - parent
207 */
208 public function testAddIndividualStudentCustomDataToIndividualParent() {
209
210 $params = [
211 'contact_id' => $this->individualParent,
212 'contact_type' => 'Individual',
213 "custom_{$this->IndiStudentField['id']}" => 'Test String',
214 // so that undefined_fields is returned
215 'debug' => 1,
216 ];
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__);
220 }
221
222 // Retrieve Methods
223
224 /**
225 * Retrieve Valid custom Data added to Individual Contact Type.
226 */
227 public function testRetrieveValidCustomDataToIndividual() {
228
229 $params = [
230 'contact_id' => $this->individual,
231 'contact_type' => 'Individual',
232 "custom_" . $this->IndividualField['id'] => 'Test String',
233 ];
234
235 $contact = $this->callAPISuccess('contact', 'create', $params);
236
237 $this->assertAPISuccess($contact);
238 $params = [
239 'contact_id' => $this->individual,
240 'contact_type' => 'Individual',
241 "return.custom_{$this->IndividualField['id']}" => 1,
242 ];
243
244 $getContact = $this->callAPISuccess('contact', 'get', $params);
245
246 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String');
247 }
248
249 /**
250 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
251 */
252 public function testRetrieveValidCustomDataToIndividualStudent() {
253
254 $params = [
255 'contact_id' => $this->individualStudent,
256 'contact_type' => 'Individual',
257 'contact_sub_type' => 'Student',
258 "custom_{$this->IndiStudentField['id']}" => 'Test String',
259 ];
260
261 $contact = $this->callAPISuccess('contact', 'create', $params);
262 $this->assertAPISuccess($contact);
263 $params = [
264 'contact_id' => $this->individualStudent,
265 'contact_type' => 'Individual',
266 'contact_sub_type' => 'Student',
267 "return.custom_{$this->IndiStudentField['id']}" => 1,
268 ];
269
270 $getContact = $this->callAPISuccess('contact', 'get', $params);
271
272 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String');
273 }
274
275 }