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