phpcs - Fix error, "Visibility must be declared on method"
[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
TO
304
305
306 $params = array(
307 'label' => 'sub2_organization',
308 'name' => 'sub2_organization',
309 // Organization
310 'parent_id' => 3,
311 'is_active' => 1,
312 );
313 $getSubtype = CRM_Contact_BAO_ContactType::add($params);
314 $subtype = $params['name'];
315
316 // check for Type:Organization subype:sub_organization
317 $contactParams = array(
318 'organization_name' => 'Compumentor',
319 'contact_type' => 'Organization',
320 'contact_sub_type' => $this->subTypeOrganization,
6a488035 321 );
fc928539 322 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
323
324 // subype:sub2_organization
325 $updateParams = array(
326 'organization_name' => 'Intel Arts',
327 'contact_id' => $contact['id'],
328 'contact_type' => 'Organization',
329 'contact_sub_type' => $subtype,
6a488035 330 );
fc928539 331 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
6a488035
TO
332 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
333
334 $params = array(
335 'contact_id' => $contact['id'],
6a488035 336 );
fc928539 337 $getContacts = $this->callAPISuccess('contact', 'get', $params);
6a488035
TO
338 $result = $getContacts['values'][$contact['id']];
339
340 $this->assertEquals($result['organization_name'], $updateParams['organization_name'], "In line " . __LINE__);
341 $this->assertEquals($result['contact_type'], $updateParams['contact_type'], "In line " . __LINE__);
342 $this->assertEquals(end($result['contact_sub_type']), $updateParams['contact_sub_type'], "In line " . __LINE__);
fc928539 343 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
344 }
345
c490a46a 346 /**
100fef9d 347 * Test update with no subtype to invalid subtype
c490a46a 348 */
00be9182 349 public function testContactUpdateSubtypeInvalid() {
6a488035
TO
350
351 // check for Type:Individual subtype:sub_individual
352 $contactParams = array(
353 'first_name' => 'Anne',
354 'last_name' => 'Grant',
355 'contact_type' => 'Individual',
356 'contact_sub_type' => $this->subTypeIndividual,
6a488035 357 );
fc928539 358 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
359
360 // subype:sub_household
361 $updateParams = array(
362 'first_name' => 'John',
363 'last_name' => 'Grant',
364 'contact_id' => $contact['id'],
365 'contact_type' => 'Individual',
366 'contact_sub_type' => $this->subTypeHousehold,
6a488035 367 );
d0e1eff2 368 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
6a488035
TO
369 $params = array(
370 'contact_id' => $contact['id'],
6a488035 371 );
fc928539 372 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
373
374 // check for Type:Organization subtype:
375 $contactParams = array(
376 'organization_name' => 'Compumentor',
377 'contact_type' => 'Organization',
378 'contact_sub_type' => $this->subTypeOrganization,
6a488035 379 );
fc928539 380 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
6a488035
TO
381
382 $updateParams = array(
383 'organization_name' => 'Intel Arts',
384 'contact_id' => $contact['id'],
385 'contact_sub_type' => $this->subTypeIndividual,
6a488035 386 );
d0e1eff2 387 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
6a488035
TO
388 $params = array(
389 'contact_id' => $contact['id'],
6a488035 390 );
fc928539 391 $this->callAPISuccess('contact', 'delete', $params);
6a488035
TO
392 }
393}
394