Merge pull request #4863 from totten/master-phpcbf4
[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',
138 'debug' => 1, // so that undefined_fields is returned
139 );
140
141 $contact = $this->callAPISuccess('contact', 'create', $params);
142 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
143 $this->assertTrue(in_array("custom_{$this->IndividualField['id']}", $contact['undefined_fields']), __LINE__);
144 }
145
146 /**
147 * Add valid Empty params to a Contact Type : Individual
148 * note - don't copy & paste this - is of marginal value
149 */
150 public function testAddCustomDataEmptyToIndividual() {
151 $contact = $this->callAPIFailure('contact', 'create', array(),
152 'Mandatory key(s) missing from params array: contact_type'
153 );
154 }
155
156 /**
157 * Add valid custom data to a Contact Type : Individual
158 */
159 public function testAddValidCustomDataToIndividual() {
160
161 $params = array(
162 'contact_id' => $this->individual,
163 'contact_type' => 'Individual',
164 "custom_{$this->IndividualField['id']}" => 'Test String',
165 );
166 $contact = $this->callAPISuccess('contact', 'create', $params);
167
168 $this->assertNotNull($contact['id'], 'In line ' . __LINE__);
169 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individual);
170 $elements["custom_{$this->IndividualField['id']}"] = $entityValues["{$this->IndividualField['id']}"];
171
172 // Check the Value in Database
173 $this->assertEquals($elements["custom_{$this->IndividualField['id']}"], 'Test String', 'In line ' . __LINE__);
174 }
175
176 /**
177 * Add Custom data of Contact Type : Individual , SubType : Student to a Contact type: Organization Subtype: Sponsor
178 */
179 public function testAddIndividualStudentCustomDataToOrganizationSponsor() {
180
181 $params = array(
182 'contact_id' => $this->organizationSponsor,
183 'contact_type' => 'Organization',
184 "custom_{$this->IndiStudentField['id']}" => 'Test String',
185 'debug' => 1, // so that undefined_fields is returned
186 );
187
188 $contact = $this->callAPISuccess('contact', 'create', $params);
189 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
190 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
191 }
192
193 /**
194 * Add valid custom data to a Contact Type : Individual Subtype: Student
195 */
196 public function testCreateValidCustomDataToIndividualStudent() {
197
198 $params = array(
199 'contact_id' => $this->individualStudent,
200 'contact_type' => 'Individual',
201 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
202
203 $result = $this->callAPISuccess('contact', 'create', $params);
204
205 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
206 $entityValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->individualStudent);
207 $elements["custom_{$this->IndiStudentField['id']}"] = $entityValues["{$this->IndiStudentField['id']}"];
208
209 // Check the Value in Database
210 $this->assertEquals($elements["custom_{$this->IndiStudentField['id']}"], 'Test String', 'in line ' . __LINE__);
211 }
212
213 /**
214 * Add custom data of Individual Student to a Contact Type : Individual - parent
215 */
216 public function testAddIndividualStudentCustomDataToIndividualParent() {
217
218 $params = array(
219 'contact_id' => $this->individualParent,
220 'contact_type' => 'Individual',
221 "custom_{$this->IndiStudentField['id']}" => 'Test String',
222 'debug' => 1, // so that undefined_fields is returned
223 );
224 $contact = $this->callAPISuccess('contact', 'create', $params);
225 $this->assertTrue(is_array($contact['undefined_fields']), __LINE__);
226 $this->assertTrue(in_array("custom_{$this->IndiStudentField['id']}", $contact['undefined_fields']), __LINE__);
227 }
228
229
230
231 // Retrieve Methods
232
233 /**
234 * Retrieve Valid custom Data added to Individual Contact Type
235 */
236 public function testRetrieveValidCustomDataToIndividual() {
237
238 $params = array(
239 'contact_id' => $this->individual,
240 'contact_type' => 'Individual',
241 "custom_" . $this->IndividualField['id'] => 'Test String', );
242
243 $contact = $this->callAPISuccess('contact', 'create', $params);
244
245 $this->assertAPISuccess($contact);
246 $params = array(
247 'contact_id' => $this->individual,
248 'contact_type' => 'Individual',
249 "return.custom_{$this->IndividualField['id']}" => 1,
250 );
251
252 $getContact = $this->callAPISuccess('contact', 'get', $params);
253
254 $this->assertEquals($getContact['values'][$this->individual]["custom_" . $this->IndividualField['id']], 'Test String', 'In line ' . __LINE__);
255 }
256
257 /**
258 * Retrieve Valid custom Data added to Individual Contact Type , Subtype : Student.
259 */
260 public function testRetrieveValidCustomDataToIndividualStudent() {
261
262 $params = array(
263 'contact_id' => $this->individualStudent,
264 'contact_type' => 'Individual',
265 'contact_sub_type' => 'Student',
266 "custom_{$this->IndiStudentField['id']}" => 'Test String', );
267
268 $contact = $this->callAPISuccess('contact', 'create', $params);
269 $this->assertAPISuccess($contact);
270 $params = array(
271 'contact_id' => $this->individualStudent,
272 'contact_type' => 'Individual',
273 'contact_sub_type' => 'Student',
274 "return.custom_{$this->IndiStudentField['id']}" => 1,
275 );
276
277 $getContact = $this->callAPISuccess('contact', 'get', $params);
278
279 $this->assertEquals($getContact['values'][$this->individualStudent]["custom_{$this->IndiStudentField['id']}"], 'Test String', 'In line ' . __LINE__);
280 }
281 }