Merge pull request #10393 from JMAConsulting/exportBatch
[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-2017 |
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-2017 |
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 * Create relationship type.
98 */
99 public function testRelationshipTypeCreate() {
100 $params = array(
101 'name_a_b' => 'Relation 1 for relationship type create',
102 'name_b_a' => 'Relation 2 for relationship type create',
103 'contact_type_a' => 'Individual',
104 'contact_type_b' => 'Organization',
105 'is_reserved' => 1,
106 'is_active' => 1,
107 'sequential' => 1,
108 );
109 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__, __FILE__);
110 $this->assertNotNull($result['values'][0]['id']);
111 unset($params['sequential']);
112 //assertDBState compares expected values in $result to actual values in the DB
113 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
114 }
115
116 /**
117 * Test using example code.
118 */
119 public function testRelationshipTypeCreateExample() {
120 require_once 'api/v3/examples/RelationshipType/Create.php';
121 $result = relationship_type_create_example();
122 $expectedResult = relationship_type_create_expectedresult();
123 $this->assertAPISuccess($result);
124 }
125
126 /**
127 * Check if required fields are not passed.
128 */
129 public function testRelationshipTypeDeleteWithoutRequired() {
130 $params = array(
131 'name_b_a' => 'Relation 2 delete without required',
132 'contact_type_b' => 'Individual',
133 'is_reserved' => 0,
134 'is_active' => 0,
135 );
136
137 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
138 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
139 }
140
141 /**
142 * Check with incorrect required fields.
143 */
144 public function testRelationshipTypeDeleteWithIncorrectData() {
145 $params = array(
146 'id' => 'abcd',
147 'name_b_a' => 'Relation 2 delete with incorrect',
148 'description' => 'Testing relationship type',
149 'contact_type_a' => 'Individual',
150 'contact_type_b' => 'Individual',
151 'is_reserved' => 0,
152 'is_active' => 0,
153 );
154 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
155 'id is not a valid integer'
156 );
157 }
158
159 /**
160 * Check relationship type delete.
161 */
162 public function testRelationshipTypeDelete() {
163 $id = $this->_relationshipTypeCreate();
164 // create sample relationship type.
165 $params = array(
166 'id' => $id,
167 );
168 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__, __FILE__);
169 $this->assertAPIDeleted('relationship_type', $id);
170 }
171
172 ///////////////// civicrm_relationship_type_update
173
174 /**
175 * Check with empty array.
176 */
177 public function testRelationshipTypeUpdateEmpty() {
178 $params = array();
179 $result = $this->callAPIFailure('relationship_type', 'create', $params);
180 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name_a_b, name_b_a');
181 }
182
183 /**
184 * Check with no contact type.
185 */
186 public function testRelationshipTypeUpdateWithoutContactType() {
187 // create sample relationship type.
188 $this->_relTypeID = $this->_relationshipTypeCreate();
189
190 $relTypeParams = array(
191 'id' => $this->_relTypeID,
192 'name_a_b' => 'Test 1',
193 'name_b_a' => 'Test 2',
194 'description' => 'Testing relationship type',
195 'is_reserved' => 1,
196 'is_active' => 0,
197 );
198
199 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
200 $this->assertNotNull($result['id']);
201 // assertDBState compares expected values in $result to actual values in the DB
202 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
203 }
204
205 /**
206 * Check with all parameters.
207 */
208 public function testRelationshipTypeUpdate() {
209 // create sample relationship type.
210 $this->_relTypeID = $this->_relationshipTypeCreate();
211
212 $params = array(
213 'id' => $this->_relTypeID,
214 'name_a_b' => 'Test 1 for update',
215 'name_b_a' => 'Test 2 for update',
216 'description' => 'SUNIL PAWAR relationship type',
217 'contact_type_a' => 'Individual',
218 'contact_type_b' => 'Individual',
219 'is_reserved' => 0,
220 'is_active' => 0,
221 );
222
223 $result = $this->callAPISuccess('relationship_type', 'create', $params);
224 $this->assertNotNull($result['id']);
225
226 // assertDBState compares expected values in $result to actual values in the DB
227 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
228 }
229
230 ///////////////// civicrm_relationship_types_get methods
231
232 /**
233 * Check with empty array.
234 */
235 public function testRelationshipTypesGetEmptyParams() {
236 $firstRelTypeParams = array(
237 'name_a_b' => 'Relation 27 for create',
238 'name_b_a' => 'Relation 28 for create',
239 'description' => 'Testing relationship type',
240 'contact_type_a' => 'Individual',
241 'contact_type_b' => 'Organization',
242 'is_reserved' => 1,
243 'is_active' => 1,
244 );
245
246 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
247
248 $secondRelTypeParams = array(
249 'name_a_b' => 'Relation 25 for create',
250 'name_b_a' => 'Relation 26 for create',
251 'description' => 'Testing relationship type second',
252 'contact_type_a' => 'Individual',
253 'contact_type_b' => 'Organization',
254 'is_reserved' => 0,
255 'is_active' => 1,
256 );
257 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
258 $results = $this->callAPISuccess('relationship_type', 'get', array());
259
260 $this->assertEquals(2, $results['count']);
261 }
262
263 /**
264 * Check with params Not Array.
265 */
266 public function testRelationshipTypesGetParamsNotArray() {
267
268 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
269 }
270
271 /**
272 * Check with valid params array.
273 */
274 public function testRelationshipTypesGet() {
275 $firstRelTypeParams = array(
276 'name_a_b' => 'Relation 30 for create',
277 'name_b_a' => 'Relation 31 for create',
278 'description' => 'Testing relationship type',
279 'contact_type_a' => 'Individual',
280 'contact_type_b' => 'Organization',
281 'is_reserved' => 1,
282 'is_active' => 1,
283 );
284
285 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
286
287 $secondRelTypeParams = array(
288 'name_a_b' => 'Relation 32 for create',
289 'name_b_a' => 'Relation 33 for create',
290 'description' => 'Testing relationship type second',
291 'contact_type_a' => 'Individual',
292 'contact_type_b' => 'Organization',
293 'is_reserved' => 0,
294 'is_active' => 1,
295 );
296 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
297
298 $params = array(
299 'name_a_b' => 'Relation 32 for create',
300 'name_b_a' => 'Relation 33 for create',
301 'description' => 'Testing relationship type second',
302 );
303 $results = $this->callAPISuccess('relationship_type', 'get', $params);
304
305 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__);
306 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__);
307 }
308
309 /**
310 * Create relationship type.
311 * @param null $params
312 * @return mixed
313 */
314 public function _relationshipTypeCreate($params = NULL) {
315 if (!is_array($params) || empty($params)) {
316 $params = array(
317 'name_a_b' => 'Relation 1 for create',
318 'name_b_a' => 'Relation 2 for create',
319 'description' => 'Testing relationship type',
320 'contact_type_a' => 'Individual',
321 'contact_type_b' => 'Organization',
322 'is_reserved' => 1,
323 'is_active' => 1,
324 );
325 }
326
327 return $this->relationshipTypeCreate($params);
328 }
329
330 }