Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 TO |
27 | |
28 | ||
6a488035 | 29 | require_once 'CiviTest/CiviUnitTestCase.php'; |
4cbe18b8 EM |
30 | |
31 | /** | |
32 | * Class api_v3_ContactTypeTest | |
33 | */ | |
6a488035 TO |
34 | class api_v3_ContactTypeTest extends CiviUnitTestCase { |
35 | protected $_apiversion; | |
b7c9bc4c | 36 | |
00be9182 | 37 | public function setUp() { |
6a488035 | 38 | parent::setUp(); |
77a09a77 | 39 | $this->useTransaction(TRUE); |
6a488035 TO |
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']; | |
6a488035 TO |
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']; | |
6a488035 TO |
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']; | |
6a488035 TO |
70 | } |
71 | ||
c490a46a | 72 | /** |
eceb18cc | 73 | * Test add methods with valid data. |
c490a46a CW |
74 | * success expected |
75 | */ | |
00be9182 | 76 | public function testContactCreate() { |
6a488035 TO |
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, | |
6a488035 | 84 | ); |
fc928539 | 85 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
86 | $params = array( |
87 | 'contact_id' => $contact['id'], | |
6a488035 | 88 | ); |
fc928539 | 89 | $result = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 94 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
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, | |
6a488035 | 101 | ); |
fc928539 | 102 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
103 | |
104 | $params = array( | |
105 | 'contact_id' => $contact['id'], | |
6a488035 | 106 | ); |
fc928539 | 107 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 112 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
113 | } |
114 | ||
115 | ||
c490a46a | 116 | /** |
eceb18cc | 117 | * Test add with invalid data. |
c490a46a | 118 | */ |
00be9182 | 119 | public function testContactAddInvalidData() { |
6a488035 TO |
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, | |
6a488035 | 127 | ); |
d0e1eff2 | 128 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 135 | ); |
d0e1eff2 | 136 | $contact = $this->callAPIFailure('contact', 'create', $contactParams); |
6a488035 TO |
137 | } |
138 | ||
139 | ||
c490a46a | 140 | /** |
eceb18cc | 141 | * Test update with no subtype to valid subtype. |
c490a46a CW |
142 | * success expected |
143 | */ | |
00be9182 | 144 | public function testContactUpdateNoSubtypeValid() { |
6a488035 TO |
145 | |
146 | // check for Type:Individual | |
147 | $contactParams = array( | |
148 | 'first_name' => 'Anne', | |
149 | 'last_name' => 'Grant', | |
150 | 'contact_type' => 'Individual', | |
6a488035 | 151 | ); |
fc928539 | 152 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 160 | ); |
fc928539 | 161 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
162 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
163 | ||
164 | $params = array( | |
165 | 'contact_id' => $contact['id'], | |
6a488035 | 166 | ); |
fc928539 | 167 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 174 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
175 | |
176 | // check for Type:Organization | |
177 | $contactParams = array( | |
178 | 'organization_name' => 'Compumentor', | |
179 | 'contact_type' => 'Organization', | |
6a488035 | 180 | ); |
fc928539 | 181 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 189 | ); |
fc928539 | 190 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
191 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
192 | ||
193 | $params = array( | |
194 | 'contact_id' => $contact['id'], | |
6a488035 | 195 | ); |
fc928539 | 196 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 202 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
203 | } |
204 | ||
205 | ||
c490a46a | 206 | /** |
eceb18cc | 207 | * Test update with no subtype to invalid subtype. |
c490a46a | 208 | */ |
00be9182 | 209 | public function testContactUpdateNoSubtypeInvalid() { |
6a488035 TO |
210 | |
211 | // check for Type:Individual | |
212 | $contactParams = array( | |
213 | 'first_name' => 'Anne', | |
214 | 'last_name' => 'Grant', | |
215 | 'contact_type' => 'Individual', | |
6a488035 | 216 | ); |
fc928539 | 217 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 226 | ); |
d0e1eff2 | 227 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
228 | $params = array( |
229 | 'contact_id' => $contact['id'], | |
92915c55 | 230 | ); |
fc928539 | 231 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
232 | |
233 | // check for Type:Organization | |
234 | $contactParams = array( | |
235 | 'organization_name' => 'Compumentor', | |
236 | 'contact_type' => 'Organization', | |
6a488035 | 237 | ); |
fc928539 | 238 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
239 | |
240 | $updateParams = array( | |
241 | 'organization_name' => 'Intel Arts', | |
242 | 'contact_id' => $contact['id'], | |
243 | 'contact_type' => 'Organization', | |
244 | 'contact_sub_type' => $this->subTypeIndividual, | |
6a488035 | 245 | ); |
d0e1eff2 | 246 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
247 | $params = array( |
248 | 'contact_id' => $contact['id'], | |
6a488035 | 249 | ); |
fc928539 | 250 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
251 | } |
252 | ||
c490a46a | 253 | /** |
eceb18cc | 254 | * Test update with no subtype to valid subtype. |
c490a46a CW |
255 | * success expected |
256 | */ | |
00be9182 | 257 | public function testContactUpdateSubtypeValid() { |
6a488035 TO |
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, | |
6a488035 | 275 | ); |
fc928539 | 276 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 285 | ); |
fc928539 | 286 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 | 287 | |
6a488035 TO |
288 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
289 | ||
290 | $params = array( | |
291 | 'contact_id' => $contact['id'], | |
6a488035 | 292 | ); |
fc928539 | 293 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 300 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 | 301 | |
6a488035 TO |
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, | |
6a488035 | 317 | ); |
fc928539 | 318 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 326 | ); |
fc928539 | 327 | $updateContact = $this->callAPISuccess('contact', 'create', $updateParams); |
6a488035 TO |
328 | $this->assertEquals($updateContact['id'], $contact['id'], "In line " . __LINE__); |
329 | ||
330 | $params = array( | |
331 | 'contact_id' => $contact['id'], | |
6a488035 | 332 | ); |
fc928539 | 333 | $getContacts = $this->callAPISuccess('contact', 'get', $params); |
6a488035 TO |
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__); | |
fc928539 | 339 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
340 | } |
341 | ||
c490a46a | 342 | /** |
eceb18cc | 343 | * Test update with no subtype to invalid subtype. |
c490a46a | 344 | */ |
00be9182 | 345 | public function testContactUpdateSubtypeInvalid() { |
6a488035 TO |
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, | |
6a488035 | 353 | ); |
fc928539 | 354 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
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, | |
6a488035 | 363 | ); |
d0e1eff2 | 364 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
365 | $params = array( |
366 | 'contact_id' => $contact['id'], | |
6a488035 | 367 | ); |
fc928539 | 368 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 TO |
369 | |
370 | // check for Type:Organization subtype: | |
371 | $contactParams = array( | |
372 | 'organization_name' => 'Compumentor', | |
373 | 'contact_type' => 'Organization', | |
374 | 'contact_sub_type' => $this->subTypeOrganization, | |
6a488035 | 375 | ); |
fc928539 | 376 | $contact = $this->callAPISuccess('contact', 'create', $contactParams); |
6a488035 TO |
377 | |
378 | $updateParams = array( | |
379 | 'organization_name' => 'Intel Arts', | |
380 | 'contact_id' => $contact['id'], | |
381 | 'contact_sub_type' => $this->subTypeIndividual, | |
6a488035 | 382 | ); |
d0e1eff2 | 383 | $updateContact = $this->callAPIFailure('contact', 'create', $updateParams); |
6a488035 TO |
384 | $params = array( |
385 | 'contact_id' => $contact['id'], | |
6a488035 | 386 | ); |
fc928539 | 387 | $this->callAPISuccess('contact', 'delete', $params); |
6a488035 | 388 | } |
96025800 | 389 | |
6a488035 | 390 | } |