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