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