3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class api_v3_ContactTypeTest
34 class api_v3_ContactTypeTest
extends CiviUnitTestCase
{
35 protected $_apiversion;
37 public function setUp() {
39 $this->useTransaction(TRUE);
40 $this->_apiversion
= 3;
42 'label' => 'sub_individual',
43 'name' => 'sub_individual',
48 $result = CRM_Contact_BAO_ContactType
::add($params);
49 $this->subTypeIndividual
= $params['name'];
52 'label' => 'sub_organization',
53 'name' => 'sub_organization',
58 $result = CRM_Contact_BAO_ContactType
::add($params);
59 $this->subTypeOrganization
= $params['name'];
62 'label' => 'sub_household',
63 'name' => 'sub_household',
68 $result = CRM_Contact_BAO_ContactType
::add($params);
69 $this->subTypeHousehold
= $params['name'];
73 * Test add methods with valid data.
76 public function testContactCreate() {
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
,
85 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
87 'contact_id' => $contact['id'],
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);
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
,
102 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
105 'contact_id' => $contact['id'],
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);
117 * Test add with invalid data.
119 public function testContactAddInvalidData() {
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
,
128 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
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
,
136 $contact = $this->callAPIFailure('contact', 'create', $contactParams);
141 * Test update with no subtype to valid subtype.
144 public function testContactUpdateNoSubtypeValid() {
146 // check for Type:Individual
147 $contactParams = array(
148 'first_name' => 'Anne',
149 'last_name' => 'Grant',
150 'contact_type' => 'Individual',
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
,
161 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
162 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
165 'contact_id' => $contact['id'],
167 $getContacts = $this->callAPISuccess('contact', 'get', $params);
168 $result = $getContacts['values'][$contact['id']];
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);
176 // check for Type:Organization
177 $contactParams = array(
178 'organization_name' => 'Compumentor',
179 'contact_type' => 'Organization',
181 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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
,
190 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
191 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
194 'contact_id' => $contact['id'],
196 $getContacts = $this->callAPISuccess('contact', 'get', $params);
197 $result = $getContacts['values'][$contact['id']];
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);
207 * Test update with no subtype to invalid subtype.
209 public function testContactUpdateNoSubtypeInvalid() {
211 // check for Type:Individual
212 $contactParams = array(
213 'first_name' => 'Anne',
214 'last_name' => 'Grant',
215 'contact_type' => 'Individual',
217 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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
,
227 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
229 'contact_id' => $contact['id'],
231 $this->callAPISuccess('contact', 'delete', $params);
233 // check for Type:Organization
234 $contactParams = array(
235 'organization_name' => 'Compumentor',
236 'contact_type' => 'Organization',
238 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
240 $updateParams = array(
241 'organization_name' => 'Intel Arts',
242 'contact_id' => $contact['id'],
243 'contact_type' => 'Organization',
244 'contact_sub_type' => $this->subTypeIndividual
,
246 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
248 'contact_id' => $contact['id'],
250 $this->callAPISuccess('contact', 'delete', $params);
254 * Test update with no subtype to valid subtype.
257 public function testContactUpdateSubtypeValid() {
260 'label' => 'sub2_individual',
261 'name' => 'sub2_individual',
266 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
267 $subtype = $params['name'];
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
,
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,
286 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
288 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
291 'contact_id' => $contact['id'],
293 $getContacts = $this->callAPISuccess('contact', 'get', $params);
294 $result = $getContacts['values'][$contact['id']];
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);
303 'label' => 'sub2_organization',
304 'name' => 'sub2_organization',
309 $getSubtype = CRM_Contact_BAO_ContactType
::add($params);
310 $subtype = $params['name'];
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
,
318 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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,
327 $updateContact = $this->callAPISuccess('contact', 'create', $updateParams);
328 $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__
);
331 'contact_id' => $contact['id'],
333 $getContacts = $this->callAPISuccess('contact', 'get', $params);
334 $result = $getContacts['values'][$contact['id']];
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);
343 * Test update with no subtype to invalid subtype.
345 public function testContactUpdateSubtypeInvalid() {
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
,
354 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
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
,
364 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
366 'contact_id' => $contact['id'],
368 $this->callAPISuccess('contact', 'delete', $params);
370 // check for Type:Organization subtype:
371 $contactParams = array(
372 'organization_name' => 'Compumentor',
373 'contact_type' => 'Organization',
374 'contact_sub_type' => $this->subTypeOrganization
,
376 $contact = $this->callAPISuccess('contact', 'create', $contactParams);
378 $updateParams = array(
379 'organization_name' => 'Intel Arts',
380 'contact_id' => $contact['id'],
381 'contact_sub_type' => $this->subTypeIndividual
,
383 $updateContact = $this->callAPIFailure('contact', 'create', $updateParams);
385 'contact_id' => $contact['id'],
387 $this->callAPISuccess('contact', 'delete', $params);