Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-22-22-01-44
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueContactTypeTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Test APIv3 civicrm_activity_* functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
35 */
36
37class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase {
38 protected $_contactID;
fc928539 39 protected $_apiversion =3;
6a488035
TO
40 protected $CustomGroupIndividual;
41 protected $individualStudent;
b7c9bc4c 42
6a488035 43
4cbe18b8
EM
44 /**
45 * @return array
46 */
6a488035
TO
47 function get_info() {
48 return array(
49 'name' => 'Custom Data For Contact Subtype',
b8f96eb8 50 'description' => 'Test Custom Data for Contact Subtype.',
6a488035
TO
51 'group' => 'CiviCRM API Tests',
52 );
53 }
54
55 function setUp() {
6a488035 56 parent::setUp();
6a488035 57 // Create Group For Individual Contact Type
fc928539 58 $groupIndividual = array(
59 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
6a488035
TO
60 'extends' => array('Individual'),
61 'style' => 'Inline',
62 'is_active' => 1,
6a488035
TO
63 );
64
65 $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
66
b422b715 67 $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
6a488035
TO
68
69 // Create Group For Individual-Student Contact Sub Type
70 $groupIndiStudent = array(
71 'title' => 'Student Test' . substr(sha1(rand()), 0, 5),
72 'extends' => array('Individual', array('Student')),
73 'style' => 'Inline',
74 'is_active' => 1,
6a488035
TO
75 );
76
77 $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
78
b422b715 79 $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
6a488035
TO
80
81 $params = array(
82 'first_name' => 'Mathev',
83 'last_name' => 'Adison',
84 'contact_type' => 'Individual',
6a488035
TO
85 );
86
87 $this->individual = $this->individualCreate($params);
88
89 $params = array(
90 'first_name' => 'Steve',
91 'last_name' => 'Tosun',
92 'contact_type' => 'Individual',
93 'contact_sub_type' => 'Student',
6a488035
TO
94 );
95 $this->individualStudent = $this->individualCreate($params);
96
97 $params = array(
98 'first_name' => 'Mark',
99 'last_name' => 'Dawson',
100 'contact_type' => 'Individual',
101 'contact_sub_type' => 'Parent',
6a488035
TO
102 );
103 $this->individualParent = $this->individualCreate($params);
104
105 $params = array(
106 'organization_name' => 'Wellspring',
107 'contact_type' => 'Organization',
6a488035
TO
108 );
109 $this->organization = $this->organizationCreate($params);
110
111 $params = array(
112 'organization_name' => 'SubUrban',
113 'contact_type' => 'Organization',
114 'contact_sub_type' => 'Sponsor',
6a488035
TO
115 );
116 $this->organizationSponsor = $this->organizationCreate($params);
117 //refresh php cached variables
80085473 118 CRM_Core_PseudoConstant::flush();
6a488035
TO
119 CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndividualField['id'], True);
120 CRM_Core_BAO_CustomField::getTableColumnGroup($this->IndiStudentField['id'], True);
121 }
122
123 function tearDown() {
124 $tablesToTruncate = array('civicrm_contact', 'civicrm_cache');
125 $this->quickCleanup($tablesToTruncate, TRUE);
126 }
127 /*
128 * Test that custom fields is returned for correct contact type only
129 */
130 function testGetFields() {
fc928539 131 $result = $this->callAPISuccess('Contact', 'getfields', array());
c206647d 132 $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));
fc928539 133 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__);
6a488035 134 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
fc928539 135 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Organization'));
6a488035 136 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
fc928539 137 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__);
6a488035
TO
138 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values']);
139 }
140
141 /**
142 * Add Custom data of Contact Type : Individual to a Contact type: Organization
143 */
144 function testAddIndividualCustomDataToOrganization() {
145
146 $params = array(
147 'id' => $this->organization,
148 'contact_type' => 'Organization',
fc928539 149 "custom_{$this->IndividualField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
6a488035
TO
150 );
151
fc928539 152 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
153 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
154 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__);
155 }
156
157 /**
158 * Add valid Empty params to a Contact Type : Individual
fc928539 159 * note - don't copy & paste this - is of marginal value
6a488035
TO
160 */
161 function testAddCustomDataEmptyToIndividual() {
fc928539 162 $contact = $this->callAPIFailure('contact', 'create', array(),
163 'Mandatory key(s) missing from params array: contact_type'
164 );
6a488035
TO
165 }
166
167 /**
168 * Add valid custom data to a Contact Type : Individual
169 */
170 function testAddValidCustomDataToIndividual() {
171
172 $params = array(
173 'contact_id' => $this->individual,
174 'contact_type' => 'Individual',
175 "custom_{$this->IndividualField['id']}" => 'Test String',
6a488035 176 );
fc928539 177 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
178
179 $this->assertNotNull($contact['id'], 'In line ' . __LINE__);
180 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
181 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
182
183 // Check the Value in Database
184 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__);
185 }
186
187 /**
188 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
189 */
190 function testAddIndividualStudentCustomDataToOrganizationSponsor() {
191
192 $params = array(
193 'contact_id' => $this->organizationSponsor,
194 'contact_type' => 'Organization',
195 "custom_{$this->IndiStudentField['id']}" => 'Test String',
d7920890 196 'debug' => 1,// so that undefined_fields is returned
6a488035
TO
197 );
198
fc928539 199 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
200 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
201 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
202 }
203
204 /**
205 * Add valid custom data to a Contact Type : Individual Subtype: Student
206 */
207 function testCreateValidCustomDataToIndividualStudent() {
208
209 $params = array(
210 'contact_id' => $this->individualStudent,
211 'contact_type' => 'Individual',
fc928539 212 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
6a488035 213
fc928539 214 $result = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
215
216 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
217 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
218 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
219
220 // Check the Value in Database
221 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__);
222 }
223
224 /**
225 * Add custom data of Individual Student to a Contact Type : Individual - parent
226 */
227 function testAddIndividualStudentCustomDataToIndividualParent() {
228
229 $params = array(
230 'contact_id' => $this->individualParent,
231 'contact_type' => 'Individual',
fc928539 232 "custom_{$this->IndiStudentField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
6a488035 233 );
fc928539 234 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
235 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
236 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
237 }
238
239
240
241 // Retrieve Methods
242
243 /**
244 * Retrieve Valid custom Data added to Individual Contact Type
245 */
246 function testRetrieveValidCustomDataToIndividual() {
247
248 $params = array(
249 'contact_id' => $this->individual,
250 'contact_type' => 'Individual',
fc928539 251 "custom_" . $this->IndividualField['id'] => 'Test String', );
6a488035 252
fc928539 253 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
254
255 $this->assertAPISuccess($contact);
256 $params = array(
257 'contact_id' => $this->individual,
258 'contact_type' => 'Individual',
259 "return.custom_{$this->IndividualField['id']}" => 1,
6a488035
TO
260 );
261
fc928539 262 $getContact = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
263
264 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String', 'In line ' . __LINE__);
265 }
266
267 /**
268 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
269 */
270 function testRetrieveValidCustomDataToIndividualStudent() {
271
272 $params = array(
273 'contact_id' => $this->individualStudent,
274 'contact_type' => 'Individual',
275 'contact_sub_type' => 'Student',
fc928539 276 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
6a488035 277
fc928539 278 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
279 $this->assertAPISuccess($contact);
280 $params = array(
281 'contact_id' => $this->individualStudent,
282 'contact_type' => 'Individual',
fc928539 283 'contact_sub_type' => 'Student', "return.custom_{$this->IndiStudentField['id']}" => 1,
6a488035
TO
284 );
285
fc928539 286 $getContact = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
287
288
289 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__);
290 }
291}
292