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