3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 +--------------------------------------------------------------------+
31 +--------------------------------------------------------------------+
32 | Copyright CiviCRM LLC (c) 2004-2019 |
33 +--------------------------------------------------------------------+
34 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
55 * Class contains api test cases for "civicrm_relationship_type"
59 class api_v3_RelationshipTypeTest
extends CiviUnitTestCase
{
62 protected $_relTypeID;
63 protected $_apiversion = 3;
65 public function setUp() {
68 $this->_cId_a
= $this->individualCreate();
69 $this->_cId_b
= $this->organizationCreate();
72 public function tearDown() {
74 $tablesToTruncate = array(
76 'civicrm_relationship_type',
78 $this->quickCleanup($tablesToTruncate);
81 ///////////////// civicrm_relationship_type_add methods
86 public function testRelationshipTypeCreateWithoutName() {
87 $relTypeParams = array(
88 'contact_type_a' => 'Individual',
89 'contact_type_b' => 'Organization',
91 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams,
92 'Mandatory key(s) missing from params array: name_a_b, name_b_a'
97 * Create relationship type.
99 public function testRelationshipTypeCreate() {
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',
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);
117 * Test using example code.
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);
127 * Check if required fields are not passed.
129 public function testRelationshipTypeDeleteWithoutRequired() {
131 'name_b_a' => 'Relation 2 delete without required',
132 'contact_type_b' => 'Individual',
137 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
138 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
142 * Check with incorrect required fields.
144 public function testRelationshipTypeDeleteWithIncorrectData() {
147 'name_b_a' => 'Relation 2 delete with incorrect',
148 'description' => 'Testing relationship type',
149 'contact_type_a' => 'Individual',
150 'contact_type_b' => 'Individual',
154 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
155 'id is not a valid integer'
160 * Check relationship type delete.
162 public function testRelationshipTypeDelete() {
163 $id = $this->_relationshipTypeCreate();
164 // create sample relationship type.
168 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__
, __FILE__
);
169 $this->assertAPIDeleted('relationship_type', $id);
172 ///////////////// civicrm_relationship_type_update
175 * Check with empty array.
177 public function testRelationshipTypeUpdateEmpty() {
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');
184 * Check with no contact type.
186 public function testRelationshipTypeUpdateWithoutContactType() {
187 // create sample relationship type.
188 $this->_relTypeID
= $this->_relationshipTypeCreate();
190 $relTypeParams = array(
191 'id' => $this->_relTypeID
,
192 'name_a_b' => 'Test 1',
193 'name_b_a' => 'Test 2',
194 'description' => 'Testing relationship type',
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);
206 * Check with all parameters.
208 public function testRelationshipTypeUpdate() {
209 // create sample relationship type.
210 $this->_relTypeID
= $this->_relationshipTypeCreate();
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',
223 $result = $this->callAPISuccess('relationship_type', 'create', $params);
224 $this->assertNotNull($result['id']);
226 // assertDBState compares expected values in $result to actual values in the DB
227 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
230 ///////////////// civicrm_relationship_types_get methods
233 * Check with empty array.
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',
246 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
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',
257 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
258 $results = $this->callAPISuccess('relationship_type', 'get', array());
260 $this->assertEquals(2, $results['count']);
264 * Check with params Not Array.
266 public function testRelationshipTypesGetParamsNotArray() {
268 $results = $this->callAPIFailure('relationship_type', 'get', 'string');
272 * Check with valid params array.
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',
285 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
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',
296 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
299 'name_a_b' => 'Relation 32 for create',
300 'name_b_a' => 'Relation 33 for create',
301 'description' => 'Testing relationship type second',
303 $results = $this->callAPISuccess('relationship_type', 'get', $params);
305 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__
);
306 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__
);
310 * Create relationship type.
311 * @param null $params
314 public function _relationshipTypeCreate($params = NULL) {
315 if (!is_array($params) ||
empty($params)) {
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',
327 return $this->relationshipTypeCreate($params);