3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 require_once 'CiviTest/CiviUnitTestCase.php';
35 * Class api_v3_ContactTypeTest
37 class api_v3_ContactTypeTest
extends CiviUnitTestCase
{
38 protected $_apiversion;
42 $this->_apiversion
= 3;
44 'label' => 'sub_individual',
45 'name' => 'sub_individual',
50 $result = CRM_Contact_BAO_ContactType
::add($params);
51 $this->subTypeIndividual
= $params['name'];
52 $this->_subTypeIndividualId
= $result->id
;
55 'label' => 'sub_organization',
56 'name' => 'sub_organization',
61 $result = CRM_Contact_BAO_ContactType
::add($params);
62 $this->subTypeOrganization
= $params['name'];
63 $this->_subTypeOrganizationId
= $result->id
;
66 'label' => 'sub_household',
67 'name' => 'sub_household',
72 $result = CRM_Contact_BAO_ContactType
::add($params);
73 $this->subTypeHousehold
= $params['name'];
74 $this->_subTypeHouseholdId
= $result->id
;
78 * Tears down the fixture, for example, closes a network connection.
79 * This method is called after a test is executed.
83 $contactTypeIds = array(
84 $this->_subTypeIndividualId
,
85 $this->_subTypeOrganizationId
,
86 $this->_subTypeHouseholdId
,
88 foreach ($contactTypeIds as $typeId) {
89 $this->contactTypeDelete($typeId);
94 * test add methods with valid data
97 function testContactCreate() {
99 // check for Type:Individual Subtype:sub_individual
100 $contactParams = array(
101 'first_name' => 'Anne',
102 'last_name' => 'Grant',
103 'contact_type' => 'Individual',
104 'contact_sub_type' => $this->subTypeIndividual
,
106 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
108 'contact_id' => $contact['id'],
110 $result = $this->callAPISuccess('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 $this->callAPISuccess('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
,
123 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
126 'contact_id' => $contact['id'],
128 $getContacts = $this->callAPISuccess('contact', 'get', $params);
129 $result = $getContacts['values'][$contact['id']];
130 $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__
);
131 $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__
);
132 $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__
);
133 $this->callAPISuccess('contact', 'delete', $params);
138 * test add with invalid data
140 function testContactAddInvalidData() {
142 // check for Type:Individual Subtype:sub_household
143 $contactParams = array(
144 'first_name' => 'Anne',
145 'last_name' => 'Grant',
146 'contact_type' => 'Individual',
147 'contact_sub_type' => $this->subTypeHousehold
,
149 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
151 // check for Type:Organization Subtype:sub_individual
152 $contactParams = array(
153 'organization_name' => 'Compumentor',
154 'contact_type' => 'Organization',
155 'contact_sub_type' => $this->subTypeIndividual
,
157 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
162 * test update with no subtype to valid subtype
165 function testContactUpdateNoSubtypeValid() {
167 // check for Type:Individual
168 $contactParams = array(
169 'first_name' => 'Anne',
170 'last_name' => 'Grant',
171 'contact_type' => 'Individual',
173 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
174 // subype:sub_individual
175 $updateParams = array(
176 'first_name' => 'John',
177 'last_name' => 'Grant',
178 'contact_id' => $contact['id'],
179 'contact_type' => 'Individual',
180 'contact_sub_type' => $this->subTypeIndividual
,
182 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
183 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
186 'contact_id' => $contact['id'],
188 $getContacts = $this->callAPISuccess('contact', 'get', $params);
189 $result = $getContacts['values'][$contact['id']];
191 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
192 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
193 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
194 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
195 $this->callAPISuccess('contact', 'delete', $params);
197 // check for Type:Organization
198 $contactParams = array(
199 'organization_name' => 'Compumentor',
200 'contact_type' => 'Organization',
202 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
204 // subype:sub_organization
205 $updateParams = array(
206 'organization_name' => 'Intel Arts',
207 'contact_id' => $contact['id'],
208 'contact_type' => 'Organization',
209 'contact_sub_type' => $this->subTypeOrganization
,
211 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
212 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
215 'contact_id' => $contact['id'],
217 $getContacts = $this->callAPISuccess('contact', 'get', $params);
218 $result = $getContacts['values'][$contact['id']];
220 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
221 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
222 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
223 $this->callAPISuccess('contact', 'delete', $params);
228 * test update with no subtype to invalid subtype
230 function testContactUpdateNoSubtypeInvalid() {
232 // check for Type:Individual
233 $contactParams = array(
234 'first_name' => 'Anne',
235 'last_name' => 'Grant',
236 'contact_type' => 'Individual',
238 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
240 // subype:sub_household
241 $updateParams = array(
242 'first_name' => 'John',
243 'last_name' => 'Grant',
244 'contact_id' => $contact['id'],
245 'contact_type' => 'Individual',
246 'contact_sub_type' => $this->subTypeHousehold
,
248 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
250 'contact_id' => $contact['id'],
252 $this->callAPISuccess('contact', 'delete', $params);
254 // check for Type:Organization
255 $contactParams = array(
256 'organization_name' => 'Compumentor',
257 'contact_type' => 'Organization',
259 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
261 $updateParams = array(
262 'organization_name' => 'Intel Arts',
263 'contact_id' => $contact['id'],
264 'contact_type' => 'Organization',
265 'contact_sub_type' => $this->subTypeIndividual
,
267 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
269 'contact_id' => $contact['id'],
271 $this->callAPISuccess('contact', 'delete', $params);
275 * test update with no subtype to valid subtype
278 function testContactUpdateSubtypeValid() {
281 'label' => 'sub2_individual',
282 'name' => 'sub2_individual',
287 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
288 $subtype = $params['name'];
290 // check for Type:Individual subype:sub_individual
291 $contactParams = array(
292 'first_name' => 'Anne',
293 'last_name' => 'Grant',
294 'contact_type' => 'Individual',
295 'contact_sub_type' => $this->subTypeIndividual
,
297 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
298 // subype:sub2_individual
299 $updateParams = array(
300 'id' => $contact['id'],
301 'first_name' => 'John',
302 'last_name' => 'Grant',
303 'contact_id' => $contact['id'],
304 'contact_type' => 'Individual',
305 'contact_sub_type' => $subtype,
307 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
309 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
312 'contact_id' => $contact['id'],
314 $getContacts = $this->callAPISuccess('contact', 'get', $params);
315 $result = $getContacts['values'][$contact['id']];
317 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__
);
318 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__
);
319 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
320 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
321 $this->callAPISuccess('contact', 'delete', $params);
325 'label' => 'sub2_organization',
326 'name' => 'sub2_organization',
331 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
332 $subtype = $params['name'];
334 // check for Type:Organization subype:sub_organization
335 $contactParams = array(
336 'organization_name' => 'Compumentor',
337 'contact_type' => 'Organization',
338 'contact_sub_type' => $this->subTypeOrganization
,
340 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
342 // subype:sub2_organization
343 $updateParams = array(
344 'organization_name' => 'Intel Arts',
345 'contact_id' => $contact['id'],
346 'contact_type' => 'Organization',
347 'contact_sub_type' => $subtype,
349 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
350 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
353 'contact_id' => $contact['id'],
355 $getContacts = $this->callAPISuccess('contact', 'get', $params);
356 $result = $getContacts['values'][$contact['id']];
358 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__
);
359 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__
);
360 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__
);
361 $this->callAPISuccess('contact', 'delete', $params);
365 * test update with no subtype to invalid subtype
367 function testContactUpdateSubtypeInvalid() {
369 // check for Type:Individual subtype:sub_individual
370 $contactParams = array(
371 'first_name' => 'Anne',
372 'last_name' => 'Grant',
373 'contact_type' => 'Individual',
374 'contact_sub_type' => $this->subTypeIndividual
,
376 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
378 // subype:sub_household
379 $updateParams = array(
380 'first_name' => 'John',
381 'last_name' => 'Grant',
382 'contact_id' => $contact['id'],
383 'contact_type' => 'Individual',
384 'contact_sub_type' => $this->subTypeHousehold
,
386 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
388 'contact_id' => $contact['id'],
390 $this->callAPISuccess('contact', 'delete', $params);
392 // check for Type:Organization subtype:
393 $contactParams = array(
394 'organization_name' => 'Compumentor',
395 'contact_type' => 'Organization',
396 'contact_sub_type' => $this->subTypeOrganization
,
398 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
400 $updateParams = array(
401 'organization_name' => 'Intel Arts',
402 'contact_id' => $contact['id'],
403 'contact_sub_type' => $this->subTypeIndividual
,
405 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
407 'contact_id' => $contact['id'],
409 $this->callAPISuccess('contact', 'delete', $params);