Merge branch 'JohnFF-patch-1'
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueContactTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test APIv3 civicrm_activity_* functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Contact
35 */
36
37 class api_v3_CustomValueContactTypeTest extends CiviUnitTestCase {
38 protected $_contactID;
39 protected $_apiversion =3;
40 protected $CustomGroupIndividual;
41 protected $individualStudent;
42
43
44 /**
45 * @return array
46 */
47 function get_info() {
48 return array(
49 'name' => 'Custom Data For Contact Subtype',
50 'description' => 'Test Custom Data for Contact subtype.',
51 'group' => 'CiviCRM API Tests',
52 );
53 }
54
55 function setUp() {
56 parent::setUp();
57 // Create Group For Individual Contact Type
58 $groupIndividual = array(
59 'title' => 'TestGroup For Indivi' . substr(sha1(rand()), 0, 5),
60 'extends' => array('Individual'),
61 'style' => 'Inline',
62 'is_active' => 1,
63 );
64
65 $this->CustomGroupIndividual = $this->customGroupCreate($groupIndividual);
66
67 $this->IndividualField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndividual['id']));
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,
75 );
76
77 $this->CustomGroupIndiStudent = $this->customGroupCreate($groupIndiStudent);
78
79 $this->IndiStudentField = $this->customFieldCreate(array('custom_group_id' => $this->CustomGroupIndiStudent['id']));
80
81 $params = array(
82 'first_name' => 'Mathev',
83 'last_name' => 'Adison',
84 'contact_type' => 'Individual',
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',
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',
102 );
103 $this->individualParent = $this->individualCreate($params);
104
105 $params = array(
106 'organization_name' => 'Wellspring',
107 'contact_type' => 'Organization',
108 );
109 $this->organization = $this->organizationCreate($params);
110
111 $params = array(
112 'organization_name' => 'SubUrban',
113 'contact_type' => 'Organization',
114 'contact_sub_type' => 'Sponsor',
115 );
116 $this->organizationSponsor = $this->organizationCreate($params);
117 //refresh php cached variables
118 CRM_Core_PseudoConstant::flush();
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() {
131 $result = $this->callAPISuccess('Contact', 'getfields', array());
132 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'If This fails there is probably a cachine issue - failure in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
133 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Individual'), 'in line' . __LINE__);
134 $this->assertArrayHasKey("custom_{$this->IndividualField['id']}", $result['values']);
135 $result = $this->callAPISuccess('Contact', 'getfields', array('action' => 'create', 'contact_type' => 'Organization'));
136 $this->assertArrayNotHasKey("custom_{$this->IndividualField['id']}", $result['values'], 'in line' . __LINE__ . print_r(array_keys($result['values']), TRUE));
137 $result = $this->callAPISuccess('Relationship', 'getfields', array('action' => 'create'), 'in line' . __LINE__);
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',
149 "custom_{$this->IndividualField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
150 );
151
152 $contact = $this->callAPISuccess('contact', 'create', $params);
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
159 * note - don't copy & paste this - is of marginal value
160 */
161 function testAddCustomDataEmptyToIndividual() {
162 $contact = $this->callAPIFailure('contact', 'create', array(),
163 'Mandatory key(s) missing from params array: contact_type'
164 );
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',
176 );
177 $contact = $this->callAPISuccess('contact', 'create', $params);
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',
196 'debug' => 1,// so that undefined_fields is returned
197 );
198
199 $contact = $this->callAPISuccess('contact', 'create', $params);
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',
212 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
213
214 $result = $this->callAPISuccess('contact', 'create', $params);
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',
232 "custom_{$this->IndiStudentField['id']}" => 'Test String', 'debug' => 1,// so that undefined_fields is returned
233 );
234 $contact = $this->callAPISuccess('contact', 'create', $params);
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',
251 "custom_" . $this->IndividualField['id'] => 'Test String', );
252
253 $contact = $this->callAPISuccess('contact', 'create', $params);
254
255 $this->assertAPISuccess($contact);
256 $params = array(
257 'contact_id' => $this->individual,
258 'contact_type' => 'Individual',
259 "return.custom_{$this->IndividualField['id']}" => 1,
260 );
261
262 $getContact = $this->callAPISuccess('contact', 'get', $params);
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',
276 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
277
278 $contact = $this->callAPISuccess('contact', 'create', $params);
279 $this->assertAPISuccess($contact);
280 $params = array(
281 'contact_id' => $this->individualStudent,
282 'contact_type' => 'Individual',
283 'contact_sub_type' => 'Student', "return.custom_{$this->IndiStudentField['id']}" => 1,
284 );
285
286 $getContact = $this->callAPISuccess('contact', 'get', $params);
287
288
289 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__);
290 }
291 }
292