4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
33 require_once 'CiviTest/CiviUnitTestCase.php';
34 class api_v3_ContactTypeTest
extends CiviUnitTestCase
{
35 protected $_apiversion;
36 public $_eNoticeCompliant = TRUE;
39 $this->_apiversion
= 3;
41 'label' => 'sub_individual',
42 'name' => 'sub_individual',
47 $result = CRM_Contact_BAO_ContactType
::add($params);
48 $this->subTypeIndividual
= $params['name'];
49 $this->_subTypeIndividualId
= $result->id
;
52 'label' => 'sub_organization',
53 'name' => 'sub_organization',
58 $result = CRM_Contact_BAO_ContactType
::add($params);
59 $this->subTypeOrganization
= $params['name'];
60 $this->_subTypeOrganizationId
= $result->id
;
63 'label' => 'sub_household',
64 'name' => 'sub_household',
69 $result = CRM_Contact_BAO_ContactType
::add($params);
70 $this->subTypeHousehold
= $params['name'];
71 $this->_subTypeHouseholdId
= $result->id
;
75 * Tears down the fixture, for example, closes a network connection.
76 * This method is called after a test is executed.
80 $contactTypeIds = array(
81 $this->_subTypeIndividualId
,
82 $this->_subTypeOrganizationId
,
83 $this->_subTypeHouseholdId
,
85 foreach ($contactTypeIds as $typeId) {
86 $this->contactTypeDelete($typeId);
91 * test add methods with valid data
94 function testContactCreate() {
96 // check for Type:Individual Subtype:sub_individual
97 $contactParams = array(
98 'first_name' => 'Anne',
99 'last_name' => 'Grant',
100 'contact_type' => 'Individual',
101 'contact_sub_type' => $this->subTypeIndividual
,
102 'version' => $this->_apiversion
,
104 $contact = civicrm_api('contact', 'create', $contactParams);
105 $this->assertEquals($contact['is_error'], 0, "In line " . __LINE__
);
107 'contact_id' => $contact['id'],
108 'version' => $this->_apiversion
,
110 $result = civicrm_api('contact', 'get', $params);
111 $this->assertEquals($result['values'][$contact['id']]['first_name'], $contactParams['first_name'], "In line " . __LINE__
);
112 $this->assertEquals($result['values'][$contact['id']]['last_name'], $contactParams['last_name'], "In line " . __LINE__
);
113 $this->assertEquals($result['values'][$contact['id']]['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
114 $this->assertEquals(end($result['values'][$contact['id']]['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
115 civicrm_api('contact', 'delete', $params);
117 // check for Type:Organization Subtype:sub_organization
118 $contactParams = array(
119 'organization_name' => 'Compumentor',
120 'contact_type' => 'Organization',
121 'contact_sub_type' => $this->subTypeOrganization
,
122 'version' => $this->_apiversion
,
124 $contact = civicrm_api('contact', 'create', $contactParams);
125 $this->assertEquals($contact['is_error'], 0, "In line " . __LINE__
);
128 'contact_id' => $contact['id'],
129 'version' => $this->_apiversion
,
131 $getContacts = civicrm_api('contact', 'get', $params);
132 $result = $getContacts['values'][$contact['id']];
133 $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__
);
134 $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
135 $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
136 civicrm_api('contact', 'delete', $params);
141 * test add with invalid data
143 function testContactAddInvalidData() {
145 // check for Type:Individual Subtype:sub_household
146 $contactParams = array(
147 'first_name' => 'Anne',
148 'last_name' => 'Grant',
149 'contact_type' => 'Individual',
150 'contact_sub_type' => $this->subTypeHousehold
,
151 'version' => $this->_apiversion
,
153 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
155 // check for Type:Organization Subtype:sub_individual
156 $contactParams = array(
157 'organization_name' => 'Compumentor',
158 'contact_type' => 'Organization',
159 'contact_sub_type' => $this->subTypeIndividual
,
160 'version' => $this->_apiversion
,
162 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
167 * test update with no subtype to valid subtype
170 function testContactUpdateNoSubtypeValid() {
172 // check for Type:Individual
173 $contactParams = array(
174 'first_name' => 'Anne',
175 'last_name' => 'Grant',
176 'contact_type' => 'Individual',
177 'version' => $this->_apiversion
,
179 $contact = civicrm_api('contact', 'create', $contactParams);
180 // subype:sub_individual
181 $updateParams = array(
182 'first_name' => 'John',
183 'last_name' => 'Grant',
184 'contact_id' => $contact['id'],
185 'contact_type' => 'Individual',
186 'contact_sub_type' => $this->subTypeIndividual
,
187 'version' => $this->_apiversion
,
189 $updateContact = civicrm_api('contact', 'create', $updateParams);
190 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
191 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
194 'contact_id' => $contact['id'],
195 'version' => $this->_apiversion
,
197 $getContacts = civicrm_api('contact', 'get', $params);
198 $result = $getContacts['values'][$contact['id']];
200 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
201 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
202 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
203 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
204 civicrm_api('contact', 'delete', $params);
206 // check for Type:Organization
207 $contactParams = array(
208 'organization_name' => 'Compumentor',
209 'contact_type' => 'Organization',
210 'version' => $this->_apiversion
,
212 $contact = civicrm_api('contact', 'create', $contactParams);
214 // subype:sub_organization
215 $updateParams = array(
216 'organization_name' => 'Intel Arts',
217 'contact_id' => $contact['id'],
218 'contact_type' => 'Organization',
219 'contact_sub_type' => $this->subTypeOrganization
,
220 'version' => $this->_apiversion
,
222 $updateContact = civicrm_api('contact', 'create', $updateParams);
224 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
225 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
228 'contact_id' => $contact['id'],
229 'version' => $this->_apiversion
,
231 $getContacts = civicrm_api('contact', 'get', $params);
232 $result = $getContacts['values'][$contact['id']];
234 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
235 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
236 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
237 civicrm_api('contact', 'delete', $params);
242 * test update with no subtype to invalid subtype
244 function testContactUpdateNoSubtypeInvalid() {
246 // check for Type:Individual
247 $contactParams = array(
248 'first_name' => 'Anne',
249 'last_name' => 'Grant',
250 'contact_type' => 'Individual',
251 'version' => $this->_apiversion
,
253 $contact = civicrm_api('contact', 'create', $contactParams);
255 // subype:sub_household
256 $updateParams = array(
257 'first_name' => 'John',
258 'last_name' => 'Grant',
259 'contact_id' => $contact['id'],
260 'contact_type' => 'Individual',
261 'contact_sub_type' => $this->subTypeHousehold
,
262 'version' => $this->_apiversion
,
264 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
266 'contact_id' => $contact['id'],
267 'version' => $this->_apiversion
,
269 civicrm_api('contact', 'delete', $params);
271 // check for Type:Organization
272 $contactParams = array(
273 'organization_name' => 'Compumentor',
274 'contact_type' => 'Organization',
275 'version' => $this->_apiversion
,
277 $contact = civicrm_api('contact', 'create', $contactParams);
279 $updateParams = array(
280 'organization_name' => 'Intel Arts',
281 'contact_id' => $contact['id'],
282 'contact_type' => 'Organization',
283 'contact_sub_type' => $this->subTypeIndividual
,
284 'version' => $this->_apiversion
,
286 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
288 'contact_id' => $contact['id'],
289 'version' => $this->_apiversion
,
291 civicrm_api('contact', 'delete', $params);
295 * test update with no subtype to valid subtype
298 function testContactUpdateSubtypeValid() {
301 'label' => 'sub2_individual',
302 'name' => 'sub2_individual',
307 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
308 $subtype = $params['name'];
310 // check for Type:Individual subype:sub_individual
311 $contactParams = array(
312 'first_name' => 'Anne',
313 'last_name' => 'Grant',
314 'contact_type' => 'Individual',
315 'contact_sub_type' => $this->subTypeIndividual
,
316 'version' => $this->_apiversion
,
318 $contact = civicrm_api('contact', 'create', $contactParams);
319 // subype:sub2_individual
320 $updateParams = array(
321 'id' => $contact['id'],
322 'first_name' => 'John',
323 'last_name' => 'Grant',
324 'contact_id' => $contact['id'],
325 'contact_type' => 'Individual',
326 'contact_sub_type' => $subtype,
327 'version' => $this->_apiversion
,
329 $updateContact = civicrm_api('contact', 'create', $updateParams);
331 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
332 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
335 'contact_id' => $contact['id'],
336 'version' => $this->_apiversion
,
338 $getContacts = civicrm_api('contact', 'get', $params);
339 $result = $getContacts['values'][$contact['id']];
341 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
342 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
343 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
344 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
345 civicrm_api('contact', 'delete', $params);
349 'label' => 'sub2_organization',
350 'name' => 'sub2_organization',
355 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
356 $subtype = $params['name'];
358 // check for Type:Organization subype:sub_organization
359 $contactParams = array(
360 'organization_name' => 'Compumentor',
361 'contact_type' => 'Organization',
362 'contact_sub_type' => $this->subTypeOrganization
,
363 'version' => $this->_apiversion
,
365 $contact = civicrm_api('contact', 'create', $contactParams);
367 // subype:sub2_organization
368 $updateParams = array(
369 'organization_name' => 'Intel Arts',
370 'contact_id' => $contact['id'],
371 'contact_type' => 'Organization',
372 'contact_sub_type' => $subtype,
373 'version' => $this->_apiversion
,
375 $updateContact = civicrm_api('contact', 'create', $updateParams);
377 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
378 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
381 'contact_id' => $contact['id'],
382 'version' => $this->_apiversion
,
384 $getContacts = civicrm_api('contact', 'get', $params);
385 $result = $getContacts['values'][$contact['id']];
387 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
388 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
389 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
390 civicrm_api('contact', 'delete', $params);
394 * test update with no subtype to invalid subtype
396 function testContactUpdateSubtypeInvalid() {
398 // check for Type:Individual subtype:sub_individual
399 $contactParams = array(
400 'first_name' => 'Anne',
401 'last_name' => 'Grant',
402 'contact_type' => 'Individual',
403 'contact_sub_type' => $this->subTypeIndividual
,
404 'version' => $this->_apiversion
,
406 $contact = civicrm_api('contact', 'create', $contactParams);
408 // subype:sub_household
409 $updateParams = array(
410 'first_name' => 'John',
411 'last_name' => 'Grant',
412 'contact_id' => $contact['id'],
413 'contact_type' => 'Individual',
414 'contact_sub_type' => $this->subTypeHousehold
,
415 'version' => $this->_apiversion
,
417 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
419 'contact_id' => $contact['id'],
420 'version' => $this->_apiversion
,
422 civicrm_api('contact', 'delete', $params);
424 // check for Type:Organization subtype:
425 $contactParams = array(
426 'organization_name' => 'Compumentor',
427 'contact_type' => 'Organization',
428 'contact_sub_type' => $this->subTypeOrganization
,
429 'version' => $this->_apiversion
,
431 $contact = civicrm_api('contact', 'create', $contactParams);
433 $updateParams = array(
434 'organization_name' => 'Intel Arts',
435 'contact_id' => $contact['id'],
436 'contact_sub_type' => $this->subTypeIndividual
,
437 'version' => $this->_apiversion
,
439 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
441 'contact_id' => $contact['id'],
442 'version' => $this->_apiversion
,
444 civicrm_api('contact', 'delete', $params);