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