Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
b6708aeb | 2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
b6708aeb | 6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
6a488035 TO |
28 | |
29 | /* | |
30 | +--------------------------------------------------------------------+ | |
232624b1 | 31 | | CiviCRM version 4.4 | |
6a488035 TO |
32 | +--------------------------------------------------------------------+ |
33 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
34 | +--------------------------------------------------------------------+ | |
35 | | This file is a part of CiviCRM. | | |
36 | | | | |
37 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
38 | | under the terms of the GNU Affero General Public License | | |
39 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
40 | | | | |
41 | | CiviCRM is distributed in the hope that it will be useful, but | | |
42 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
43 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
44 | | See the GNU Affero General Public License for more details. | | |
45 | | | | |
46 | | You should have received a copy of the GNU Affero General Public | | |
47 | | License and the CiviCRM Licensing Exception along | | |
48 | | with this program; if not, contact CiviCRM LLC | | |
49 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
50 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
51 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
52 | +--------------------------------------------------------------------+ | |
53 | */ | |
54 | ||
55 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
56 | ||
57 | /** | |
58 | * Class contains api test cases for "civicrm_relationship_type" | |
59 | * | |
60 | */ | |
61 | class api_v3_RelationshipTypeTest extends CiviUnitTestCase { | |
62 | protected $_cId_a; | |
63 | protected $_cId_b; | |
64 | protected $_relTypeID; | |
b2402735 | 65 | protected $_apiversion = 3; |
6a488035 TO |
66 | public $_eNoticeCompliant = TRUE; |
67 | function get_info() { | |
68 | return array( | |
69 | 'name' => 'RelationshipType Create', | |
70 | 'description' => 'Test all RelationshipType Create API methods.', | |
71 | 'group' => 'CiviCRM API Tests', | |
72 | ); | |
73 | } | |
74 | ||
75 | function setUp() { | |
76 | ||
77 | parent::setUp(); | |
e4d5f1e2 | 78 | $this->_cId_a = $this->individualCreate(); |
79 | $this->_cId_b = $this->organizationCreate(); | |
6a488035 TO |
80 | } |
81 | ||
82 | function tearDown() { | |
83 | ||
84 | $tablesToTruncate = array( | |
85 | 'civicrm_contact', | |
86 | 'civicrm_relationship_type', | |
87 | ); | |
88 | $this->quickCleanup($tablesToTruncate); | |
89 | } | |
90 | ||
91 | ///////////////// civicrm_relationship_type_add methods | |
92 | ||
93 | /** | |
94 | * check with no name | |
95 | */ | |
96 | function testRelationshipTypeCreateWithoutName() { | |
97 | $relTypeParams = array( | |
98 | 'contact_type_a' => 'Individual', | |
99 | 'contact_type_b' => 'Organization', | |
6a488035 | 100 | ); |
7fbb4198 | 101 | $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams, |
6a488035 TO |
102 | 'Mandatory key(s) missing from params array: name_a_b, name_b_a' |
103 | ); | |
104 | } | |
105 | ||
106 | /** | |
107 | * check with no contact type | |
108 | */ | |
109 | function testRelationshipTypeCreateWithoutContactType() { | |
110 | $relTypeParams = array( | |
111 | 'name_a_b' => 'Relation 1 without contact type', | |
112 | 'name_b_a' => 'Relation 2 without contact type', | |
6a488035 | 113 | ); |
7fbb4198 | 114 | $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams, |
6a488035 TO |
115 | 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b' |
116 | ); | |
117 | } | |
118 | ||
119 | /** | |
120 | * create relationship type | |
121 | */ | |
122 | function testRelationshipTypeCreate() { | |
123 | $params = array( | |
124 | 'name_a_b' => 'Relation 1 for relationship type create', | |
125 | 'name_b_a' => 'Relation 2 for relationship type create', | |
126 | 'contact_type_a' => 'Individual', | |
127 | 'contact_type_b' => 'Organization', | |
128 | 'is_reserved' => 1, | |
129 | 'is_active' => 1, | |
6a488035 TO |
130 | 'sequential' => 1, |
131 | ); | |
7fbb4198 | 132 | $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 133 | $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__); |
6a488035 TO |
134 | unset($params['sequential']); |
135 | //assertDBState compares expected values in $result to actual values in the DB | |
136 | $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Test using example code | |
141 | */ | |
142 | function testRelationshipTypeCreateExample() { | |
143 | require_once 'api/v3/examples/RelationshipTypeCreate.php'; | |
144 | $result = relationship_type_create_example(); | |
145 | $expectedResult = relationship_type_create_expectedresult(); | |
791c263c | 146 | $this->assertAPISuccess($result); |
6a488035 TO |
147 | } |
148 | ||
6a488035 TO |
149 | /** |
150 | * check if required fields are not passed | |
151 | */ | |
152 | function testRelationshipTypeDeleteWithoutRequired() { | |
153 | $params = array( | |
154 | 'name_b_a' => 'Relation 2 delete without required', | |
155 | 'contact_type_b' => 'Individual', | |
156 | 'is_reserved' => 0, | |
157 | 'is_active' => 0, | |
158 | ); | |
159 | ||
d0e1eff2 CW |
160 | $result = $this->callAPIFailure('relationship_type', 'delete', $params); |
161 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id'); | |
6a488035 TO |
162 | } |
163 | ||
164 | /** | |
165 | * check with incorrect required fields | |
166 | */ | |
167 | function testRelationshipTypeDeleteWithIncorrectData() { | |
168 | $params = array( | |
169 | 'id' => 'abcd', | |
170 | 'name_b_a' => 'Relation 2 delete with incorrect', | |
171 | 'description' => 'Testing relationship type', | |
172 | 'contact_type_a' => 'Individual', | |
173 | 'contact_type_b' => 'Individual', | |
174 | 'is_reserved' => 0, | |
175 | 'is_active' => 0, | |
6a488035 | 176 | ); |
b2402735 | 177 | $result = $this->callAPIFailure('relationship_type', 'delete', $params, |
178 | 'Invalid value for relationship type ID' | |
179 | ); | |
6a488035 TO |
180 | } |
181 | ||
182 | /** | |
183 | * check relationship type delete | |
184 | */ | |
185 | function testRelationshipTypeDelete() { | |
b2402735 | 186 | $id = $this->_relationshipTypeCreate(); |
6a488035 TO |
187 | // create sample relationship type. |
188 | $params = array( | |
b2402735 | 189 | 'id' => $id, |
6a488035 | 190 | ); |
b2402735 | 191 | $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__); |
192 | $this->assertAPIDeleted('relationship_type', $id); | |
6a488035 TO |
193 | } |
194 | ||
195 | ///////////////// civicrm_relationship_type_update | |
196 | ||
197 | /** | |
198 | * check with empty array | |
199 | */ | |
200 | function testRelationshipTypeUpdateEmpty() { | |
201 | $params = array(); | |
d0e1eff2 CW |
202 | $result = $this->callAPIFailure('relationship_type', 'create', $params); |
203 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name_a_b, name_b_a, contact_type_a, contact_type_b'); | |
6a488035 TO |
204 | } |
205 | ||
6a488035 TO |
206 | /** |
207 | * check with no contact type | |
208 | */ | |
209 | function testRelationshipTypeUpdateWithoutContactType() { | |
210 | // create sample relationship type. | |
75638074 | 211 | $this->_relTypeID = $this->_relationshipTypeCreate(); |
6a488035 TO |
212 | |
213 | $relTypeParams = array( | |
214 | 'id' => $this->_relTypeID, | |
215 | 'name_a_b' => 'Test 1', | |
216 | 'name_b_a' => 'Test 2', | |
217 | 'description' => 'Testing relationship type', | |
218 | 'is_reserved' => 1, | |
219 | 'is_active' => 0, | |
6a488035 TO |
220 | ); |
221 | ||
b2402735 | 222 | $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams); |
6a488035 | 223 | $this->assertNotNull($result['id']); |
6a488035 TO |
224 | // assertDBState compares expected values in $result to actual values in the DB |
225 | $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams); | |
226 | } | |
227 | ||
228 | /** | |
229 | * check with all parameters | |
230 | */ | |
231 | function testRelationshipTypeUpdate() { | |
232 | // create sample relationship type. | |
75638074 | 233 | $this->_relTypeID = $this->_relationshipTypeCreate(); |
6a488035 TO |
234 | |
235 | $params = array( | |
236 | 'id' => $this->_relTypeID, | |
237 | 'name_a_b' => 'Test 1 for update', | |
238 | 'name_b_a' => 'Test 2 for update', | |
239 | 'description' => 'SUNIL PAWAR relationship type', | |
240 | 'contact_type_a' => 'Individual', | |
241 | 'contact_type_b' => 'Individual', | |
242 | 'is_reserved' => 0, | |
243 | 'is_active' => 0, | |
6a488035 TO |
244 | ); |
245 | ||
7fbb4198 | 246 | $result = $this->callAPISuccess('relationship_type', 'create', $params); |
6a488035 | 247 | $this->assertNotNull($result['id']); |
7fbb4198 | 248 | |
6a488035 TO |
249 | // assertDBState compares expected values in $result to actual values in the DB |
250 | $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params); | |
251 | } | |
252 | ||
253 | ///////////////// civicrm_relationship_types_get methods | |
254 | ||
255 | /** | |
256 | * check with empty array | |
257 | */ | |
258 | function testRelationshipTypesGetEmptyParams() { | |
259 | $firstRelTypeParams = array( | |
260 | 'name_a_b' => 'Relation 27 for create', | |
261 | 'name_b_a' => 'Relation 28 for create', | |
262 | 'description' => 'Testing relationship type', | |
263 | 'contact_type_a' => 'Individual', | |
264 | 'contact_type_b' => 'Organization', | |
265 | 'is_reserved' => 1, | |
266 | 'is_active' => 1, | |
6a488035 TO |
267 | ); |
268 | ||
7fbb4198 | 269 | $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams); |
6a488035 TO |
270 | |
271 | $secondRelTypeParams = array( | |
272 | 'name_a_b' => 'Relation 25 for create', | |
273 | 'name_b_a' => 'Relation 26 for create', | |
274 | 'description' => 'Testing relationship type second', | |
275 | 'contact_type_a' => 'Individual', | |
276 | 'contact_type_b' => 'Organization', | |
277 | 'is_reserved' => 0, | |
278 | 'is_active' => 1, | |
6a488035 | 279 | ); |
7fbb4198 | 280 | $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams); |
281 | $results = $this->callAPISuccess('relationship_type', 'get', array()); | |
6a488035 TO |
282 | |
283 | $this->assertEquals(2, $results['count']); | |
6a488035 TO |
284 | } |
285 | ||
286 | /** | |
287 | * check with params Not Array. | |
288 | */ | |
289 | function testRelationshipTypesGetParamsNotArray() { | |
290 | ||
d0e1eff2 | 291 | $results = $this->callAPIFailure('relationship_type', 'get', 'string'); |
6a488035 TO |
292 | } |
293 | ||
294 | /** | |
295 | * check with valid params array. | |
296 | */ | |
297 | function testRelationshipTypesGet() { | |
298 | $firstRelTypeParams = array( | |
299 | 'name_a_b' => 'Relation 30 for create', | |
300 | 'name_b_a' => 'Relation 31 for create', | |
301 | 'description' => 'Testing relationship type', | |
302 | 'contact_type_a' => 'Individual', | |
303 | 'contact_type_b' => 'Organization', | |
304 | 'is_reserved' => 1, | |
305 | 'is_active' => 1, | |
6a488035 TO |
306 | ); |
307 | ||
7fbb4198 | 308 | $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams); |
6a488035 TO |
309 | |
310 | $secondRelTypeParams = array( | |
311 | 'name_a_b' => 'Relation 32 for create', | |
312 | 'name_b_a' => 'Relation 33 for create', | |
313 | 'description' => 'Testing relationship type second', | |
314 | 'contact_type_a' => 'Individual', | |
315 | 'contact_type_b' => 'Organization', | |
316 | 'is_reserved' => 0, | |
317 | 'is_active' => 1, | |
6a488035 | 318 | ); |
7fbb4198 | 319 | $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams); |
6a488035 TO |
320 | |
321 | $params = array( | |
322 | 'name_a_b' => 'Relation 32 for create', | |
323 | 'name_b_a' => 'Relation 33 for create', | |
324 | 'description' => 'Testing relationship type second', | |
6a488035 | 325 | ); |
7fbb4198 | 326 | $results = $this->callAPISuccess('relationship_type', 'get', $params); |
6a488035 | 327 | |
6a488035 TO |
328 | $this->assertEquals(1, $results['count'], ' in line ' . __LINE__); |
329 | $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__); | |
330 | } | |
331 | ||
332 | /** | |
333 | * create relationship type. | |
334 | */ | |
335 | function _relationshipTypeCreate($params = NULL) { | |
336 | if (!is_array($params) || empty($params)) { | |
337 | $params = array( | |
338 | 'name_a_b' => 'Relation 1 for create', | |
339 | 'name_b_a' => 'Relation 2 for create', | |
340 | 'description' => 'Testing relationship type', | |
341 | 'contact_type_a' => 'Individual', | |
342 | 'contact_type_b' => 'Organization', | |
343 | 'is_reserved' => 1, | |
344 | 'is_active' => 1, | |
6a488035 TO |
345 | ); |
346 | } | |
347 | ||
348 | return $this->relationshipTypeCreate($params); | |
349 | } | |
350 | } | |
351 |