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 = civicrm_api('contact', 'create', $contactParams);
154 $this->assertEquals($contact['is_error'], 1, "In line " . __LINE__
);
156 // check for Type:Organization Subtype:sub_individual
157 $contactParams = array(
158 'organization_name' => 'Compumentor',
159 'contact_type' => 'Organization',
160 'contact_sub_type' => $this->subTypeIndividual
,
161 'version' => $this->_apiversion
,
163 $contact = civicrm_api('contact', 'create', $contactParams);
164 $this->assertEquals($contact['is_error'], 1, "In line " . __LINE__
);
169 * test update with no subtype to valid subtype
172 function testContactUpdateNoSubtypeValid() {
174 // check for Type:Individual
175 $contactParams = array(
176 'first_name' => 'Anne',
177 'last_name' => 'Grant',
178 'contact_type' => 'Individual',
179 'version' => $this->_apiversion
,
181 $contact = civicrm_api('contact', 'create', $contactParams);
182 // subype:sub_individual
183 $updateParams = array(
184 'first_name' => 'John',
185 'last_name' => 'Grant',
186 'contact_id' => $contact['id'],
187 'contact_type' => 'Individual',
188 'contact_sub_type' => $this->subTypeIndividual
,
189 'version' => $this->_apiversion
,
191 $updateContact = civicrm_api('contact', 'create', $updateParams);
192 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
193 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
196 'contact_id' => $contact['id'],
197 'version' => $this->_apiversion
,
199 $getContacts = civicrm_api('contact', 'get', $params);
200 $result = $getContacts['values'][$contact['id']];
202 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
203 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
204 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
205 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
206 civicrm_api('contact', 'delete', $params);
208 // check for Type:Organization
209 $contactParams = array(
210 'organization_name' => 'Compumentor',
211 'contact_type' => 'Organization',
212 'version' => $this->_apiversion
,
214 $contact = civicrm_api('contact', 'create', $contactParams);
216 // subype:sub_organization
217 $updateParams = array(
218 'organization_name' => 'Intel Arts',
219 'contact_id' => $contact['id'],
220 'contact_type' => 'Organization',
221 'contact_sub_type' => $this->subTypeOrganization
,
222 'version' => $this->_apiversion
,
224 $updateContact = civicrm_api('contact', 'create', $updateParams);
226 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
227 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
230 'contact_id' => $contact['id'],
231 'version' => $this->_apiversion
,
233 $getContacts = civicrm_api('contact', 'get', $params);
234 $result = $getContacts['values'][$contact['id']];
236 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
237 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
238 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
239 civicrm_api('contact', 'delete', $params);
244 * test update with no subtype to invalid subtype
246 function testContactUpdateNoSubtypeInvalid() {
248 // check for Type:Individual
249 $contactParams = array(
250 'first_name' => 'Anne',
251 'last_name' => 'Grant',
252 'contact_type' => 'Individual',
253 'version' => $this->_apiversion
,
255 $contact = civicrm_api('contact', 'create', $contactParams);
257 // subype:sub_household
258 $updateParams = array(
259 'first_name' => 'John',
260 'last_name' => 'Grant',
261 'contact_id' => $contact['id'],
262 'contact_type' => 'Individual',
263 'contact_sub_type' => $this->subTypeHousehold
,
264 'version' => $this->_apiversion
,
266 $updateContact = civicrm_api('contact', 'create', $updateParams);
268 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__
);
270 'contact_id' => $contact['id'],
271 'version' => $this->_apiversion
,
273 civicrm_api('contact', 'delete', $params);
275 // check for Type:Organization
276 $contactParams = array(
277 'organization_name' => 'Compumentor',
278 'contact_type' => 'Organization',
279 'version' => $this->_apiversion
,
281 $contact = civicrm_api('contact', 'create', $contactParams);
283 $updateParams = array(
284 'organization_name' => 'Intel Arts',
285 'contact_id' => $contact['id'],
286 'contact_type' => 'Organization',
287 'contact_sub_type' => $this->subTypeIndividual
,
288 'version' => $this->_apiversion
,
290 $updateContact = civicrm_api('contact', 'create', $updateParams);
292 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__
);
294 'contact_id' => $contact['id'],
295 'version' => $this->_apiversion
,
297 civicrm_api('contact', 'delete', $params);
301 * test update with no subtype to valid subtype
304 function testContactUpdateSubtypeValid() {
307 'label' => 'sub2_individual',
308 'name' => 'sub2_individual',
313 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
314 $subtype = $params['name'];
316 // check for Type:Individual subype:sub_individual
317 $contactParams = array(
318 'first_name' => 'Anne',
319 'last_name' => 'Grant',
320 'contact_type' => 'Individual',
321 'contact_sub_type' => $this->subTypeIndividual
,
322 'version' => $this->_apiversion
,
324 $contact = civicrm_api('contact', 'create', $contactParams);
325 // subype:sub2_individual
326 $updateParams = array(
327 'id' => $contact['id'],
328 'first_name' => 'John',
329 'last_name' => 'Grant',
330 'contact_id' => $contact['id'],
331 'contact_type' => 'Individual',
332 'contact_sub_type' => $subtype,
333 'version' => $this->_apiversion
,
335 $updateContact = civicrm_api('contact', 'create', $updateParams);
337 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
338 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
341 'contact_id' => $contact['id'],
342 'version' => $this->_apiversion
,
344 $getContacts = civicrm_api('contact', 'get', $params);
345 $result = $getContacts['values'][$contact['id']];
347 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
348 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
349 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
350 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
351 civicrm_api('contact', 'delete', $params);
355 'label' => 'sub2_organization',
356 'name' => 'sub2_organization',
361 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
362 $subtype = $params['name'];
364 // check for Type:Organization subype:sub_organization
365 $contactParams = array(
366 'organization_name' => 'Compumentor',
367 'contact_type' => 'Organization',
368 'contact_sub_type' => $this->subTypeOrganization
,
369 'version' => $this->_apiversion
,
371 $contact = civicrm_api('contact', 'create', $contactParams);
373 // subype:sub2_organization
374 $updateParams = array(
375 'organization_name' => 'Intel Arts',
376 'contact_id' => $contact['id'],
377 'contact_type' => 'Organization',
378 'contact_sub_type' => $subtype,
379 'version' => $this->_apiversion
,
381 $updateContact = civicrm_api('contact', 'create', $updateParams);
383 $this->assertEquals($updateContact['is_error'], 0, "In line " . __LINE__
);
384 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
387 'contact_id' => $contact['id'],
388 'version' => $this->_apiversion
,
390 $getContacts = civicrm_api('contact', 'get', $params);
391 $result = $getContacts['values'][$contact['id']];
393 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
394 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
395 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
396 civicrm_api('contact', 'delete', $params);
400 * test update with no subtype to invalid subtype
402 function testContactUpdateSubtypeInvalid() {
404 // check for Type:Individual subtype:sub_individual
405 $contactParams = array(
406 'first_name' => 'Anne',
407 'last_name' => 'Grant',
408 'contact_type' => 'Individual',
409 'contact_sub_type' => $this->subTypeIndividual
,
410 'version' => $this->_apiversion
,
412 $contact = civicrm_api('contact', 'create', $contactParams);
414 // subype:sub_household
415 $updateParams = array(
416 'first_name' => 'John',
417 'last_name' => 'Grant',
418 'contact_id' => $contact['id'],
419 'contact_type' => 'Individual',
420 'contact_sub_type' => $this->subTypeHousehold
,
421 'version' => $this->_apiversion
,
423 $updateContact = civicrm_api('contact', 'create', $updateParams);
425 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__
);
427 'contact_id' => $contact['id'],
428 'version' => $this->_apiversion
,
430 civicrm_api('contact', 'delete', $params);
432 // check for Type:Organization subtype:
433 $contactParams = array(
434 'organization_name' => 'Compumentor',
435 'contact_type' => 'Organization',
436 'contact_sub_type' => $this->subTypeOrganization
,
437 'version' => $this->_apiversion
,
439 $contact = civicrm_api('contact', 'create', $contactParams);
441 $updateParams = array(
442 'organization_name' => 'Intel Arts',
443 'contact_id' => $contact['id'],
444 'contact_sub_type' => $this->subTypeIndividual
,
445 'version' => $this->_apiversion
,
447 $updateContact = civicrm_api('contact', 'create', $updateParams);
449 $this->assertEquals($updateContact['is_error'], 1, "In line " . __LINE__
);
451 'contact_id' => $contact['id'],
452 'version' => $this->_apiversion
,
454 civicrm_api('contact', 'delete', $params);