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