Merge pull request #2243 from eileenmcnaughton/CRM-14021
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTypeTest.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
29
30
31
32 require_once 'CiviTest/CiviUnitTestCase.php';
33 class api_v3_ContactTypeTest extends CiviUnitTestCase {
34 protected $_apiversion;
35
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,
101 );
102 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
103 $params = array(
104 'contact_id' => $contact['id'],
105 );
106 $result = $this->callAPISuccess('contact', 'get', $params);
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__);
111 $this->callAPISuccess('contact', 'delete', $params);
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,
118 );
119 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
120
121 $params = array(
122 'contact_id' => $contact['id'],
123 );
124 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
129 $this->callAPISuccess('contact', 'delete', $params);
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,
144 );
145 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
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,
152 );
153 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
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',
168 );
169 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
177 );
178 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
179 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
180
181 $params = array(
182 'contact_id' => $contact['id'],
183 );
184 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
191 $this->callAPISuccess('contact', 'delete', $params);
192
193 // check for Type:Organization
194 $contactParams = array(
195 'organization_name' => 'Compumentor',
196 'contact_type' => 'Organization',
197 );
198 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
206 );
207 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
208 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
209
210 $params = array(
211 'contact_id' => $contact['id'],
212 );
213 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
219 $this->callAPISuccess('contact', 'delete', $params);
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',
233 );
234 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
243 );
244 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
245 $params = array(
246 'contact_id' => $contact['id'],
247 );
248 $this->callAPISuccess('contact', 'delete', $params);
249
250 // check for Type:Organization
251 $contactParams = array(
252 'organization_name' => 'Compumentor',
253 'contact_type' => 'Organization',
254 );
255 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
256
257 $updateParams = array(
258 'organization_name' => 'Intel Arts',
259 'contact_id' => $contact['id'],
260 'contact_type' => 'Organization',
261 'contact_sub_type' => $this->subTypeIndividual,
262 );
263 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
264 $params = array(
265 'contact_id' => $contact['id'],
266 );
267 $this->callAPISuccess('contact', 'delete', $params);
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,
292 );
293 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
302 );
303 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
304
305 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
306
307 $params = array(
308 'contact_id' => $contact['id'],
309 );
310 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
317 $this->callAPISuccess('contact', 'delete', $params);
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,
335 );
336 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
344 );
345 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
346 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
347
348 $params = array(
349 'contact_id' => $contact['id'],
350 );
351 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
357 $this->callAPISuccess('contact', 'delete', $params);
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,
371 );
372 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
381 );
382 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
383 $params = array(
384 'contact_id' => $contact['id'],
385 );
386 $this->callAPISuccess('contact', 'delete', $params);
387
388 // check for Type:Organization subtype:
389 $contactParams = array(
390 'organization_name' => 'Compumentor',
391 'contact_type' => 'Organization',
392 'contact_sub_type' => $this->subTypeOrganization,
393 );
394 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
395
396 $updateParams = array(
397 'organization_name' => 'Intel Arts',
398 'contact_id' => $contact['id'],
399 'contact_sub_type' => $this->subTypeIndividual,
400 );
401 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
402 $params = array(
403 'contact_id' => $contact['id'],
404 );
405 $this->callAPISuccess('contact', 'delete', $params);
406 }
407 }
408