copyright and version fixes
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTypeTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
29
30
31
32require_once 'CiviTest/CiviUnitTestCase.php';
33class api_v3_ContactTypeTest extends CiviUnitTestCase {
34 protected $_apiversion;
b7c9bc4c 35
6a488035
TO
36 function setUp() {
37 parent::setUp();
38 $this->_apiversion = 3;
39 $params = array(
40 'label' => 'sub_individual',
41 'name' => 'sub_individual',
42 // Individual
43 'parent_id' => 1,
44 'is_active' => 1,
45 );
46 $result = CRM_Contact_BAO_ContactType::add($params);
47 $this->subTypeIndividual = $params['name'];
48 $this->_subTypeIndividualId = $result->id;
49
50 $params = array(
51 'label' => 'sub_organization',
52 'name' => 'sub_organization',
53 // Organization
54 'parent_id' => 3,
55 'is_active' => 1,
56 );
57 $result = CRM_Contact_BAO_ContactType::add($params);
58 $this->subTypeOrganization = $params['name'];
59 $this->_subTypeOrganizationId = $result->id;
60
61 $params = array(
62 'label' => 'sub_household',
63 'name' => 'sub_household',
64 // Household
65 'parent_id' => 2,
66 'is_active' => 1,
67 );
68 $result = CRM_Contact_BAO_ContactType::add($params);
69 $this->subTypeHousehold = $params['name'];
70 $this->_subTypeHouseholdId = $result->id;
71 }
72
73 /**
74 * Tears down the fixture, for example, closes a network connection.
75 * This method is called after a test is executed.
76 *
77 */
78 function tearDown() {
79 $contactTypeIds = array(
80 $this->_subTypeIndividualId,
81 $this->_subTypeOrganizationId,
82 $this->_subTypeHouseholdId,
83 );
84 foreach ($contactTypeIds as $typeId) {
85 $this->contactTypeDelete($typeId);
86 }
87 }
88
89 /*
90 * test add methods with valid data
91 * success expected
92 */
93 function testContactCreate() {
94
95 // check for Type:Individual Subtype:sub_individual
96 $contactParams = array(
97 'first_name' => 'Anne',
98 'last_name' => 'Grant',
99 'contact_type' => 'Individual',
100 'contact_sub_type' => $this->subTypeIndividual,
6a488035 101 );
fc928539 102 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
103 $params = array(
104 'contact_id' => $contact['id'],
6a488035 105 );
fc928539 106 $result = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
107 $this->assertEquals($result['values'][$contact['id']]['first_name'], $contactParams['first_name'], "In line " . __LINE__);
108 $this->assertEquals($result['values'][$contact['id']]['last_name'], $contactParams['last_name'], "In line " . __LINE__);
109 $this->assertEquals($result['values'][$contact['id']]['contact_type'], $contactParams['contact_type'], "In line " . __LINE__);
110 $this->assertEquals(end($result['values'][$contact['id']]['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__);
fc928539 111 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
112
113 // check for Type:Organization Subtype:sub_organization
114 $contactParams = array(
115 'organization_name' => 'Compumentor',
116 'contact_type' => 'Organization',
117 'contact_sub_type' => $this->subTypeOrganization,
6a488035 118 );
fc928539 119 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
120
121 $params = array(
122 'contact_id' => $contact['id'],
6a488035 123 );
fc928539 124 $getContacts = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
125 $result = $getContacts['values'][$contact['id']];
126 $this->assertEquals($result['organization_name'], $contactParams['organization_name'], "In line " . __LINE__);
127 $this->assertEquals($result['contact_type'], $contactParams['contact_type'], "In line " . __LINE__);
128 $this->assertEquals(end($result['contact_sub_type']), $contactParams['contact_sub_type'], "In line " . __LINE__);
fc928539 129 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
130 }
131
132
133 /*
134 * test add with invalid data
135 */
136 function testContactAddInvalidData() {
137
138 // check for Type:Individual Subtype:sub_household
139 $contactParams = array(
140 'first_name' => 'Anne',
141 'last_name' => 'Grant',
142 'contact_type' => 'Individual',
143 'contact_sub_type' => $this->subTypeHousehold,
6a488035 144 );
d0e1eff2 145 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
6a488035
TO
146
147 // check for Type:Organization Subtype:sub_individual
148 $contactParams = array(
149 'organization_name' => 'Compumentor',
150 'contact_type' => 'Organization',
151 'contact_sub_type' => $this->subTypeIndividual,
6a488035 152 );
d0e1eff2 153 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
6a488035
TO
154 }
155
156
157 /*
158 * test update with no subtype to valid subtype
159 * success expected
160 */
161 function testContactUpdateNoSubtypeValid() {
162
163 // check for Type:Individual
164 $contactParams = array(
165 'first_name' => 'Anne',
166 'last_name' => 'Grant',
167 'contact_type' => 'Individual',
6a488035 168 );
fc928539 169 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
170 // subype:sub_individual
171 $updateParams = array(
172 'first_name' => 'John',
173 'last_name' => 'Grant',
174 'contact_id' => $contact['id'],
175 'contact_type' => 'Individual',
176 'contact_sub_type' => $this->subTypeIndividual,
6a488035 177 );
fc928539 178 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
6a488035
TO
179 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
180
181 $params = array(
182 'contact_id' => $contact['id'],
6a488035 183 );
fc928539 184 $getContacts = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
185 $result = $getContacts['values'][$contact['id']];
186
187 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__);
188 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__);
189 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
190 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
fc928539 191 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
192
193 // check for Type:Organization
194 $contactParams = array(
195 'organization_name' => 'Compumentor',
196 'contact_type' => 'Organization',
6a488035 197 );
fc928539 198 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
199
200 // subype:sub_organization
201 $updateParams = array(
202 'organization_name' => 'Intel Arts',
203 'contact_id' => $contact['id'],
204 'contact_type' => 'Organization',
205 'contact_sub_type' => $this->subTypeOrganization,
6a488035 206 );
fc928539 207 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
6a488035
TO
208 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
209
210 $params = array(
211 'contact_id' => $contact['id'],
6a488035 212 );
fc928539 213 $getContacts = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
214 $result = $getContacts['values'][$contact['id']];
215
216 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__);
217 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
218 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
fc928539 219 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
220 }
221
222
223 /*
224 * test update with no subtype to invalid subtype
225 */
226 function testContactUpdateNoSubtypeInvalid() {
227
228 // check for Type:Individual
229 $contactParams = array(
230 'first_name' => 'Anne',
231 'last_name' => 'Grant',
232 'contact_type' => 'Individual',
6a488035 233 );
fc928539 234 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
235
236 // subype:sub_household
237 $updateParams = array(
238 'first_name' => 'John',
239 'last_name' => 'Grant',
240 'contact_id' => $contact['id'],
241 'contact_type' => 'Individual',
242 'contact_sub_type' => $this->subTypeHousehold,
6a488035 243 );
d0e1eff2 244 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
6a488035
TO
245 $params = array(
246 'contact_id' => $contact['id'],
fc928539 247 );
248 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
249
250 // check for Type:Organization
251 $contactParams = array(
252 'organization_name' => 'Compumentor',
253 'contact_type' => 'Organization',
6a488035 254 );
fc928539 255 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
256
257 $updateParams = array(
258 'organization_name' => 'Intel Arts',
259 'contact_id' => $contact['id'],
260 'contact_type' => 'Organization',
261 'contact_sub_type' => $this->subTypeIndividual,
6a488035 262 );
d0e1eff2 263 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
6a488035
TO
264 $params = array(
265 'contact_id' => $contact['id'],
6a488035 266 );
fc928539 267 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
268 }
269
270 /*
271 * test update with no subtype to valid subtype
272 * success expected
273 */
274 function testContactUpdateSubtypeValid() {
275
276 $params = array(
277 'label' => 'sub2_individual',
278 'name' => 'sub2_individual',
279 // Individual
280 'parent_id' => 1,
281 'is_active' => 1,
282 );
283 $getSubtype = CRM_Contact_BAO_ContactType::add($params);
284 $subtype = $params['name'];
285
286 // check for Type:Individual subype:sub_individual
287 $contactParams = array(
288 'first_name' => 'Anne',
289 'last_name' => 'Grant',
290 'contact_type' => 'Individual',
291 'contact_sub_type' => $this->subTypeIndividual,
6a488035 292 );
fc928539 293 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
294 // subype:sub2_individual
295 $updateParams = array(
296 'id' => $contact['id'],
297 'first_name' => 'John',
298 'last_name' => 'Grant',
299 'contact_id' => $contact['id'],
300 'contact_type' => 'Individual',
301 'contact_sub_type' => $subtype,
6a488035 302 );
fc928539 303 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
6a488035 304
6a488035
TO
305 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
306
307 $params = array(
308 'contact_id' => $contact['id'],
6a488035 309 );
fc928539 310 $getContacts = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
311 $result = $getContacts['values'][$contact['id']];
312
313 $this->assertEquals($result['first_name'], $updateParams['first_name'], "In line " . __LINE__);
314 $this->assertEquals($result['last_name'], $updateParams['last_name'], "In line " . __LINE__);
315 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
316 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
fc928539 317 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
318
319
320 $params = array(
321 'label' => 'sub2_organization',
322 'name' => 'sub2_organization',
323 // Organization
324 'parent_id' => 3,
325 'is_active' => 1,
326 );
327 $getSubtype = CRM_Contact_BAO_ContactType::add($params);
328 $subtype = $params['name'];
329
330 // check for Type:Organization subype:sub_organization
331 $contactParams = array(
332 'organization_name' => 'Compumentor',
333 'contact_type' => 'Organization',
334 'contact_sub_type' => $this->subTypeOrganization,
6a488035 335 );
fc928539 336 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
337
338 // subype:sub2_organization
339 $updateParams = array(
340 'organization_name' => 'Intel Arts',
341 'contact_id' => $contact['id'],
342 'contact_type' => 'Organization',
343 'contact_sub_type' => $subtype,
6a488035 344 );
fc928539 345 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
6a488035
TO
346 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
347
348 $params = array(
349 'contact_id' => $contact['id'],
6a488035 350 );
fc928539 351 $getContacts = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
352 $result = $getContacts['values'][$contact['id']];
353
354 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__);
355 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
356 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
fc928539 357 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
358 }
359
360 /*
361 * test update with no subtype to invalid subtype
362 */
363 function testContactUpdateSubtypeInvalid() {
364
365 // check for Type:Individual subtype:sub_individual
366 $contactParams = array(
367 'first_name' => 'Anne',
368 'last_name' => 'Grant',
369 'contact_type' => 'Individual',
370 'contact_sub_type' => $this->subTypeIndividual,
6a488035 371 );
fc928539 372 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
373
374 // subype:sub_household
375 $updateParams = array(
376 'first_name' => 'John',
377 'last_name' => 'Grant',
378 'contact_id' => $contact['id'],
379 'contact_type' => 'Individual',
380 'contact_sub_type' => $this->subTypeHousehold,
6a488035 381 );
d0e1eff2 382 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
6a488035
TO
383 $params = array(
384 'contact_id' => $contact['id'],
6a488035 385 );
fc928539 386 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
387
388 // check for Type:Organization subtype:
389 $contactParams = array(
390 'organization_name' => 'Compumentor',
391 'contact_type' => 'Organization',
392 'contact_sub_type' => $this->subTypeOrganization,
6a488035 393 );
fc928539 394 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
395
396 $updateParams = array(
397 'organization_name' => 'Intel Arts',
398 'contact_id' => $contact['id'],
399 'contact_sub_type' => $this->subTypeIndividual,
6a488035 400 );
d0e1eff2 401 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
6a488035
TO
402 $params = array(
403 'contact_id' => $contact['id'],
6a488035 404 );
fc928539 405 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
406 }
407}
408