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