INFRA-132 - @param type fixes
[civicrm-core.git] / tests / phpunit / api / v3 / RelationshipTypeTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
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 */
28
29 /*
30 +--------------------------------------------------------------------+
31 | CiviCRM version 4.6 |
32 +--------------------------------------------------------------------+
33 | Copyright CiviCRM LLC (c) 2004-2014 |
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;
65 protected $_apiversion = 3;
66
67 public function setUp() {
68
69 parent::setUp();
70 $this->_cId_a = $this->individualCreate();
71 $this->_cId_b = $this->organizationCreate();
72 }
73
74 public function tearDown() {
75
76 $tablesToTruncate = array(
77 'civicrm_contact',
78 'civicrm_relationship_type',
79 );
80 $this->quickCleanup($tablesToTruncate);
81 }
82
83 ///////////////// civicrm_relationship_type_add methods
84
85 /**
86 * Check with no name
87 */
88 public function testRelationshipTypeCreateWithoutName() {
89 $relTypeParams = array(
90 'contact_type_a' => 'Individual',
91 'contact_type_b' => 'Organization',
92 );
93 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
94 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
95 );
96 }
97
98 /**
99 * Check with no contact type
100 */
101 public function testRelationshipTypeCreateWithoutContactType() {
102 $relTypeParams = array(
103 'name_a_b' => 'Relation 1 without contact type',
104 'name_b_a' => 'Relation 2 without contact type',
105 );
106 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
107 'Mandatory key(s) missing from params array: contact_type_a, contact_type_b'
108 );
109 }
110
111 /**
112 * Create relationship type
113 */
114 public function testRelationshipTypeCreate() {
115 $params = array(
116 'name_a_b' => 'Relation 1 for relationship type create',
117 'name_b_a' => 'Relation 2 for relationship type create',
118 'contact_type_a' => 'Individual',
119 'contact_type_b' => 'Organization',
120 'is_reserved' => 1,
121 'is_active' => 1,
122 'sequential' => 1,
123 );
124 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
125 $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__);
126 unset($params['sequential']);
127 //assertDBState compares expected values in $result to actual values in the DB
128 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
129 }
130
131 /**
132 * Test using example code
133 */
134 public function testRelationshipTypeCreateExample() {
135 require_once 'api/v3/examples/RelationshipType/Create.php';
136 $result = relationship_type_create_example();
137 $expectedResult = relationship_type_create_expectedresult();
138 $this->assertAPISuccess($result);
139 }
140
141 /**
142 * Check if required fields are not passed
143 */
144 public function testRelationshipTypeDeleteWithoutRequired() {
145 $params = array(
146 'name_b_a' => 'Relation 2 delete without required',
147 'contact_type_b' => 'Individual',
148 'is_reserved' => 0,
149 'is_active' => 0,
150 );
151
152 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
153 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
154 }
155
156 /**
157 * Check with incorrect required fields
158 */
159 public function testRelationshipTypeDeleteWithIncorrectData() {
160 $params = array(
161 'id' => 'abcd',
162 'name_b_a' => 'Relation 2 delete with incorrect',
163 'description' => 'Testing relationship type',
164 'contact_type_a' => 'Individual',
165 'contact_type_b' => 'Individual',
166 'is_reserved' => 0,
167 'is_active' => 0,
168 );
169 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
170 'Invalid value for relationship type ID'
171 );
172 }
173
174 /**
175 * Check relationship type delete
176 */
177 public function testRelationshipTypeDelete() {
178 $id = $this->_relationshipTypeCreate();
179 // create sample relationship type.
180 $params = array(
181 'id' => $id,
182 );
183 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
184 $this->assertAPIDeleted('relationship_type', $id);
185 }
186
187 ///////////////// civicrm_relationship_type_update
188
189 /**
190 * Check with empty array
191 */
192 public function testRelationshipTypeUpdateEmpty() {
193 $params = array();
194 $result = $this->callAPIFailure('relationship_type', 'create', $params);
195 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name_a_b, name_b_a, contact_type_a, contact_type_b');
196 }
197
198 /**
199 * Check with no contact type
200 */
201 public function testRelationshipTypeUpdateWithoutContactType() {
202 // create sample relationship type.
203 $this->_relTypeID = $this->_relationshipTypeCreate();
204
205 $relTypeParams = array(
206 'id' => $this->_relTypeID,
207 'name_a_b' => 'Test 1',
208 'name_b_a' => 'Test 2',
209 'description' => 'Testing relationship type',
210 'is_reserved' => 1,
211 'is_active' => 0,
212 );
213
214 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
215 $this->assertNotNull($result['id']);
216 // assertDBState compares expected values in $result to actual values in the DB
217 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
218 }
219
220 /**
221 * Check with all parameters
222 */
223 public function testRelationshipTypeUpdate() {
224 // create sample relationship type.
225 $this->_relTypeID = $this->_relationshipTypeCreate();
226
227 $params = array(
228 'id' => $this->_relTypeID,
229 'name_a_b' => 'Test 1 for update',
230 'name_b_a' => 'Test 2 for update',
231 'description' => 'SUNIL PAWAR relationship type',
232 'contact_type_a' => 'Individual',
233 'contact_type_b' => 'Individual',
234 'is_reserved' => 0,
235 'is_active' => 0,
236 );
237
238 $result = $this->callAPISuccess('relationship_type', 'create', $params);
239 $this->assertNotNull($result['id']);
240
241 // assertDBState compares expected values in $result to actual values in the DB
242 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
243 }
244
245 ///////////////// civicrm_relationship_types_get methods
246
247 /**
248 * Check with empty array
249 */
250 public function testRelationshipTypesGetEmptyParams() {
251 $firstRelTypeParams = array(
252 'name_a_b' => 'Relation 27 for create',
253 'name_b_a' => 'Relation 28 for create',
254 'description' => 'Testing relationship type',
255 'contact_type_a' => 'Individual',
256 'contact_type_b' => 'Organization',
257 'is_reserved' => 1,
258 'is_active' => 1,
259 );
260
261 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
262
263 $secondRelTypeParams = array(
264 'name_a_b' => 'Relation 25 for create',
265 'name_b_a' => 'Relation 26 for create',
266 'description' => 'Testing relationship type second',
267 'contact_type_a' => 'Individual',
268 'contact_type_b' => 'Organization',
269 'is_reserved' => 0,
270 'is_active' => 1,
271 );
272 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
273 $results = $this->callAPISuccess('relationship_type', 'get', array());
274
275 $this->assertEquals(2, $results['count']);
276 }
277
278 /**
279 * Check with params Not Array.
280 */
281 public function testRelationshipTypesGetParamsNotArray() {
282
283 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
284 }
285
286 /**
287 * Check with valid params array.
288 */
289 public function testRelationshipTypesGet() {
290 $firstRelTypeParams = array(
291 'name_a_b' => 'Relation 30 for create',
292 'name_b_a' => 'Relation 31 for create',
293 'description' => 'Testing relationship type',
294 'contact_type_a' => 'Individual',
295 'contact_type_b' => 'Organization',
296 'is_reserved' => 1,
297 'is_active' => 1,
298 );
299
300 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
301
302 $secondRelTypeParams = array(
303 'name_a_b' => 'Relation 32 for create',
304 'name_b_a' => 'Relation 33 for create',
305 'description' => 'Testing relationship type second',
306 'contact_type_a' => 'Individual',
307 'contact_type_b' => 'Organization',
308 'is_reserved' => 0,
309 'is_active' => 1,
310 );
311 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
312
313 $params = array(
314 'name_a_b' => 'Relation 32 for create',
315 'name_b_a' => 'Relation 33 for create',
316 'description' => 'Testing relationship type second',
317 );
318 $results = $this->callAPISuccess('relationship_type', 'get', $params);
319
320 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
321 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
322 }
323
324 /**
325 * Create relationship type.
326 */
327 public function _relationshipTypeCreate($params = NULL) {
328 if (!is_array($params) || empty($params)) {
329 $params = array(
330 'name_a_b' => 'Relation 1 for create',
331 'name_b_a' => 'Relation 2 for create',
332 'description' => 'Testing relationship type',
333 'contact_type_a' => 'Individual',
334 'contact_type_b' => 'Organization',
335 'is_reserved' => 1,
336 'is_active' => 1,
337 );
338 }
339
340 return $this->relationshipTypeCreate($params);
341 }
342 }