Merge pull request #4809 from totten/master-cs2
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
34 /**
35 * Class api_v3_ContactTypeTest
36 */
37 class api_v3_ContactTypeTest extends CiviUnitTestCase {
38 protected $_apiversion;
39
40 public function setUp() {
41 parent::setUp();
42 $this->useTransaction(TRUE);
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'];
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'];
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'];
73 }
74
75 /**
76 * Test add methods with valid data
77 * success expected
78 */
79 public function testContactCreate() {
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,
87 );
88 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
89 $params = array(
90 'contact_id' => $contact['id'],
91 );
92 $result = $this->callAPISuccess('contact', 'get', $params);
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__);
97 $this->callAPISuccess('contact', 'delete', $params);
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,
104 );
105 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
106
107 $params = array(
108 'contact_id' => $contact['id'],
109 );
110 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
115 $this->callAPISuccess('contact', 'delete', $params);
116 }
117
118
119 /**
120 * Test add with invalid data
121 */
122 public function testContactAddInvalidData() {
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,
130 );
131 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
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,
138 );
139 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
140 }
141
142
143 /**
144 * Test update with no subtype to valid subtype
145 * success expected
146 */
147 public function testContactUpdateNoSubtypeValid() {
148
149 // check for Type:Individual
150 $contactParams = array(
151 'first_name' => 'Anne',
152 'last_name' => 'Grant',
153 'contact_type' => 'Individual',
154 );
155 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
163 );
164 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
165 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
166
167 $params = array(
168 'contact_id' => $contact['id'],
169 );
170 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
177 $this->callAPISuccess('contact', 'delete', $params);
178
179 // check for Type:Organization
180 $contactParams = array(
181 'organization_name' => 'Compumentor',
182 'contact_type' => 'Organization',
183 );
184 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
192 );
193 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
194 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
195
196 $params = array(
197 'contact_id' => $contact['id'],
198 );
199 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
205 $this->callAPISuccess('contact', 'delete', $params);
206 }
207
208
209 /**
210 * Test update with no subtype to invalid subtype
211 */
212 public function testContactUpdateNoSubtypeInvalid() {
213
214 // check for Type:Individual
215 $contactParams = array(
216 'first_name' => 'Anne',
217 'last_name' => 'Grant',
218 'contact_type' => 'Individual',
219 );
220 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
229 );
230 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
231 $params = array(
232 'contact_id' => $contact['id'],
233 );
234 $this->callAPISuccess('contact', 'delete', $params);
235
236 // check for Type:Organization
237 $contactParams = array(
238 'organization_name' => 'Compumentor',
239 'contact_type' => 'Organization',
240 );
241 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
242
243 $updateParams = array(
244 'organization_name' => 'Intel Arts',
245 'contact_id' => $contact['id'],
246 'contact_type' => 'Organization',
247 'contact_sub_type' => $this->subTypeIndividual,
248 );
249 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
250 $params = array(
251 'contact_id' => $contact['id'],
252 );
253 $this->callAPISuccess('contact', 'delete', $params);
254 }
255
256 /**
257 * Test update with no subtype to valid subtype
258 * success expected
259 */
260 public function testContactUpdateSubtypeValid() {
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,
278 );
279 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
288 );
289 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
290
291 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
292
293 $params = array(
294 'contact_id' => $contact['id'],
295 );
296 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
303 $this->callAPISuccess('contact', 'delete', $params);
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,
321 );
322 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
330 );
331 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
332 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__);
333
334 $params = array(
335 'contact_id' => $contact['id'],
336 );
337 $getContacts = $this->callAPISuccess('contact', 'get', $params);
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__);
343 $this->callAPISuccess('contact', 'delete', $params);
344 }
345
346 /**
347 * Test update with no subtype to invalid subtype
348 */
349 public function testContactUpdateSubtypeInvalid() {
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,
357 );
358 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
367 );
368 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
369 $params = array(
370 'contact_id' => $contact['id'],
371 );
372 $this->callAPISuccess('contact', 'delete', $params);
373
374 // check for Type:Organization subtype:
375 $contactParams = array(
376 'organization_name' => 'Compumentor',
377 'contact_type' => 'Organization',
378 'contact_sub_type' => $this->subTypeOrganization,
379 );
380 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
381
382 $updateParams = array(
383 'organization_name' => 'Intel Arts',
384 'contact_id' => $contact['id'],
385 'contact_sub_type' => $this->subTypeIndividual,
386 );
387 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
388 $params = array(
389 'contact_id' => $contact['id'],
390 );
391 $this->callAPISuccess('contact', 'delete', $params);
392 }
393 }