Merge branch '5.11' of https://github.com/civicrm/civicrm-core
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueContactTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_activity_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
33 * @group headless
34 */
35 class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase {
36 protected $_contactID;
37 protected $_apiversion = 3;
38 protected $CustomGroupIndividual;
39 protected $individualStudent;
40
41 public function setUp() {
42 parent::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'),
47 'style' => 'Inline',
48 'is_active' => 1,
49 );
50
51 $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
52
53 $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
54
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')),
59 'style' => 'Inline',
60 'is_active' => 1,
61 );
62
63 $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
64
65 $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
66
67 $params = array(
68 'first_name' => 'Mathev',
69 'last_name' => 'Adison',
70 'contact_type' => 'Individual',
71 );
72
73 $this->individual = $this->individualCreate($params);
74
75 $params = array(
76 'first_name' => 'Steve',
77 'last_name' => 'Tosun',
78 'contact_type' => 'Individual',
79 'contact_sub_type' => 'Student',
80 );
81 $this->individualStudent = $this->individualCreate($params);
82
83 $params = array(
84 'first_name' => 'Mark',
85 'last_name' => 'Dawson',
86 'contact_type' => 'Individual',
87 'contact_sub_type' => 'Parent',
88 );
89 $this->individualParent = $this->individualCreate($params);
90
91 $params = array(
92 'organization_name' => 'Wellspring',
93 'contact_type' => 'Organization',
94 );
95 $this->organization = $this->organizationCreate($params);
96
97 $params = array(
98 'organization_name' => 'SubUrban',
99 'contact_type' => 'Organization',
100 'contact_sub_type' => 'Sponsor',
101 );
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);
107 }
108
109 public function tearDown() {
110 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
111 $this->quickCleanup($tablesToTruncate, TRUE);
112 }
113
114 /**
115 * Test that custom fields is returned for correct contact type only.
116 */
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',
128 ));
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']);
132 }
133
134 /**
135 * Add Custom data of Contact Type : Individual to a Contact type: Organization
136 */
137 public function testAddIndividualCustomDataToOrganization() {
138
139 $params = array(
140 'id' => $this->organization,
141 'contact_type' => 'Organization',
142 "custom_{$this->IndividualField['id']}" => 'Test String',
143 'debug' => 1, // so that undefined_fields is returned
144 );
145
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__);
149 }
150
151 /**
152 * Add valid Empty params to a Contact Type : Individual
153 * note - don't copy & paste this - is of marginal value
154 */
155 public function testAddCustomDataEmptyToIndividual() {
156 $contact = $this->callAPIFailure('contact', 'create', array(),
157 'Mandatory key(s) missing from params array: contact_type'
158 );
159 }
160
161 /**
162 * Add valid custom data to a Contact Type : Individual
163 */
164 public function testAddValidCustomDataToIndividual() {
165
166 $params = array(
167 'contact_id' => $this->individual,
168 'contact_type' => 'Individual',
169 "custom_{$this->IndividualField['id']}" => 'Test String',
170 );
171 $contact = $this->callAPISuccess('contact', 'create', $params);
172
173 $this->assertNotNull($contact['id']);
174 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
175 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
176
177 // Check the Value in Database
178 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String');
179 }
180
181 /**
182 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
183 */
184 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
185
186 $params = array(
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
191 );
192
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__);
196 }
197
198 /**
199 * Add valid custom data to a Contact Type : Individual Subtype: Student
200 */
201 public function testCreateValidCustomDataToIndividualStudent() {
202
203 $params = array(
204 'contact_id' => $this->individualStudent,
205 'contact_type' => 'Individual',
206 "custom_{$this->IndiStudentField['id']}" => 'Test String',
207 );
208
209 $result = $this->callAPISuccess('contact', 'create', $params);
210
211 $this->assertNotNull($result['id']);
212 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
213 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
214
215 // Check the Value in Database
216 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String');
217 }
218
219 /**
220 * Add custom data of Individual Student to a Contact Type : Individual - parent
221 */
222 public function testAddIndividualStudentCustomDataToIndividualParent() {
223
224 $params = array(
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
229 );
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__);
233 }
234
235 // Retrieve Methods
236
237 /**
238 * Retrieve Valid custom Data added to Individual Contact Type.
239 */
240 public function testRetrieveValidCustomDataToIndividual() {
241
242 $params = array(
243 'contact_id' => $this->individual,
244 'contact_type' => 'Individual',
245 "custom_" . $this->IndividualField['id'] => 'Test String',
246 );
247
248 $contact = $this->callAPISuccess('contact', 'create', $params);
249
250 $this->assertAPISuccess($contact);
251 $params = array(
252 'contact_id' => $this->individual,
253 'contact_type' => 'Individual',
254 "return.custom_{$this->IndividualField['id']}" => 1,
255 );
256
257 $getContact = $this->callAPISuccess('contact', 'get', $params);
258
259 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String');
260 }
261
262 /**
263 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
264 */
265 public function testRetrieveValidCustomDataToIndividualStudent() {
266
267 $params = array(
268 'contact_id' => $this->individualStudent,
269 'contact_type' => 'Individual',
270 'contact_sub_type' => 'Student',
271 "custom_{$this->IndiStudentField['id']}" => 'Test String',
272 );
273
274 $contact = $this->callAPISuccess('contact', 'create', $params);
275 $this->assertAPISuccess($contact);
276 $params = array(
277 'contact_id' => $this->individualStudent,
278 'contact_type' => 'Individual',
279 'contact_sub_type' => 'Student',
280 "return.custom_{$this->IndiStudentField['id']}" => 1,
281 );
282
283 $getContact = $this->callAPISuccess('contact', 'get', $params);
284
285 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String');
286 }
287
288 }