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