3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
32 require_once 'CiviTest/CiviUnitTestCase.php';
35 * Class api_v3_ContactTypeTest
37 class api_v3_ContactTypeTest
extends CiviUnitTestCase
{
38 protected $_apiversion;
40 public function setUp() {
42 $this->useTransaction(TRUE);
43 $this->_apiversion
= 3;
45 'label' => 'sub_individual',
46 'name' => 'sub_individual',
51 $result = CRM_Contact_BAO_ContactType
::add($params);
52 $this->subTypeIndividual
= $params['name'];
55 'label' => 'sub_organization',
56 'name' => 'sub_organization',
61 $result = CRM_Contact_BAO_ContactType
::add($params);
62 $this->subTypeOrganization
= $params['name'];
65 'label' => 'sub_household',
66 'name' => 'sub_household',
71 $result = CRM_Contact_BAO_ContactType
::add($params);
72 $this->subTypeHousehold
= $params['name'];
76 * Test add methods with valid data
79 public function testContactCreate() {
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
,
88 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
90 'contact_id' => $contact['id'],
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);
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
,
105 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
108 'contact_id' => $contact['id'],
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);
120 * Test add with invalid data
122 public function testContactAddInvalidData() {
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
,
131 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
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
,
139 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
144 * Test update with no subtype to valid subtype
147 public function testContactUpdateNoSubtypeValid() {
149 // check for Type:Individual
150 $contactParams = array(
151 'first_name' => 'Anne',
152 'last_name' => 'Grant',
153 'contact_type' => 'Individual',
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
,
164 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
165 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
168 'contact_id' => $contact['id'],
170 $getContacts = $this->callAPISuccess('contact', 'get', $params);
171 $result = $getContacts['values'][$contact['id']];
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);
179 // check for Type:Organization
180 $contactParams = array(
181 'organization_name' => 'Compumentor',
182 'contact_type' => 'Organization',
184 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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
,
193 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
194 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
197 'contact_id' => $contact['id'],
199 $getContacts = $this->callAPISuccess('contact', 'get', $params);
200 $result = $getContacts['values'][$contact['id']];
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);
210 * Test update with no subtype to invalid subtype
212 public function testContactUpdateNoSubtypeInvalid() {
214 // check for Type:Individual
215 $contactParams = array(
216 'first_name' => 'Anne',
217 'last_name' => 'Grant',
218 'contact_type' => 'Individual',
220 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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
,
230 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
232 'contact_id' => $contact['id'],
234 $this->callAPISuccess('contact', 'delete', $params);
236 // check for Type:Organization
237 $contactParams = array(
238 'organization_name' => 'Compumentor',
239 'contact_type' => 'Organization',
241 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
243 $updateParams = array(
244 'organization_name' => 'Intel Arts',
245 'contact_id' => $contact['id'],
246 'contact_type' => 'Organization',
247 'contact_sub_type' => $this->subTypeIndividual
,
249 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
251 'contact_id' => $contact['id'],
253 $this->callAPISuccess('contact', 'delete', $params);
257 * Test update with no subtype to valid subtype
260 public function testContactUpdateSubtypeValid() {
263 'label' => 'sub2_individual',
264 'name' => 'sub2_individual',
269 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
270 $subtype = $params['name'];
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
,
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,
289 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
291 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
294 'contact_id' => $contact['id'],
296 $getContacts = $this->callAPISuccess('contact', 'get', $params);
297 $result = $getContacts['values'][$contact['id']];
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);
307 'label' => 'sub2_organization',
308 'name' => 'sub2_organization',
313 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
314 $subtype = $params['name'];
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
,
322 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
331 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
332 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
335 'contact_id' => $contact['id'],
337 $getContacts = $this->callAPISuccess('contact', 'get', $params);
338 $result = $getContacts['values'][$contact['id']];
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);
347 * Test update with no subtype to invalid subtype
349 public function testContactUpdateSubtypeInvalid() {
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
,
358 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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
,
368 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
370 'contact_id' => $contact['id'],
372 $this->callAPISuccess('contact', 'delete', $params);
374 // check for Type:Organization subtype:
375 $contactParams = array(
376 'organization_name' => 'Compumentor',
377 'contact_type' => 'Organization',
378 'contact_sub_type' => $this->subTypeOrganization
,
380 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
382 $updateParams = array(
383 'organization_name' => 'Intel Arts',
384 'contact_id' => $contact['id'],
385 'contact_sub_type' => $this->subTypeIndividual
,
387 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
389 'contact_id' => $contact['id'],
391 $this->callAPISuccess('contact', 'delete', $params);