3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class contains api test cases for "civicrm_relationship_type"
17 class api_v3_RelationshipTypeTest
extends CiviUnitTestCase
{
20 protected $_relTypeID;
21 protected $_apiversion = 3;
23 public function setUp() {
26 $this->_cId_a
= $this->individualCreate();
27 $this->_cId_b
= $this->organizationCreate();
30 public function tearDown() {
34 'civicrm_relationship_type',
36 $this->quickCleanup($tablesToTruncate);
39 ///////////////// civicrm_relationship_type_add methods
44 * @dataProvider versionThreeAndFour
46 public function testRelationshipTypeCreateWithoutName($version) {
47 $this->_apiversion
= $version;
49 'contact_type_a' => 'Individual',
50 'contact_type_b' => 'Organization',
52 $result = $this->callAPIFailure('relationship_type', 'create', $relTypeParams);
56 * Create relationship type.
58 * @dataProvider versionThreeAndFour
60 public function testRelationshipTypeCreate($version) {
61 $this->_apiversion
= $version;
63 'name_a_b' => 'Relation 1 for relationship type create',
64 'name_b_a' => 'Relation 2 for relationship type create',
65 'contact_type_a' => 'Individual',
66 'contact_type_b' => 'Organization',
71 $result = $this->callAPIAndDocument('relationship_type', 'create', $params, __FUNCTION__
, __FILE__
);
72 $this->assertNotNull($result['values'][0]['id']);
73 unset($params['sequential']);
74 //assertDBState compares expected values in $result to actual values in the DB
75 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
79 * Test using example code.
81 * @dataProvider versionThreeAndFour
83 public function testRelationshipTypeCreateExample($version) {
84 $this->_apiversion
= $version;
85 require_once 'api/v3/examples/RelationshipType/Create.ex.php';
86 $result = relationship_type_create_example();
87 $expectedResult = relationship_type_create_expectedresult();
88 $this->assertAPISuccess($result);
92 * Check if required fields are not passed.
94 * @dataProvider versionThreeAndFour
96 public function testRelationshipTypeDeleteWithoutRequired($version) {
97 $this->_apiversion
= $version;
99 'name_b_a' => 'Relation 2 delete without required',
100 'contact_type_b' => 'Individual',
105 $result = $this->callAPIFailure('relationship_type', 'delete', $params);
107 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
110 $this->assertEquals($result['error_message'], 'Parameter "where" is required.');
115 * Check with incorrect required fields.
117 public function testRelationshipTypeDeleteWithIncorrectData() {
120 'name_b_a' => 'Relation 2 delete with incorrect',
121 'description' => 'Testing relationship type',
122 'contact_type_a' => 'Individual',
123 'contact_type_b' => 'Individual',
127 $result = $this->callAPIFailure('relationship_type', 'delete', $params,
128 'id is not a valid integer'
133 * Check relationship type delete.
134 * @param int $version
135 * @dataProvider versionThreeAndFour
137 public function testRelationshipTypeDelete($version) {
138 $this->_apiversion
= $version;
139 $id = $this->_relationshipTypeCreate();
140 // create sample relationship type.
144 $result = $this->callAPIAndDocument('relationship_type', 'delete', $params, __FUNCTION__
, __FILE__
);
145 $this->assertAPIDeleted('relationship_type', $id);
148 ///////////////// civicrm_relationship_type_update
151 * Check with empty array.
152 * @param int $version
153 * @dataProvider versionThreeAndFour
155 public function testRelationshipTypeUpdateEmpty($version) {
156 $this->_apiversion
= $version;
158 $result = $this->callAPIFailure('relationship_type', 'create', $params);
159 $this->assertContains('name_a_b', $result['error_message']);
160 $this->assertContains('name_b_a', $result['error_message']);
164 * Check with no contact type.
165 * @param int $version
166 * @dataProvider versionThreeAndFour
168 public function testRelationshipTypeUpdateWithoutContactType($version) {
169 $this->_apiversion
= $version;
170 // create sample relationship type.
171 $this->_relTypeID
= $this->_relationshipTypeCreate();
174 'id' => $this->_relTypeID
,
175 'name_a_b' => 'Test 1',
176 'name_b_a' => 'Test 2',
177 'description' => 'Testing relationship type',
182 $result = $this->callAPISuccess('relationship_type', 'create', $relTypeParams);
183 $this->assertNotNull($result['id']);
184 // assertDBState compares expected values in $result to actual values in the DB
185 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $relTypeParams);
189 * Check with all parameters.
190 * @param int $version
191 * @dataProvider versionThreeAndFour
193 public function testRelationshipTypeUpdate($version) {
194 $this->_apiversion
= $version;
195 // create sample relationship type.
196 $this->_relTypeID
= $this->_relationshipTypeCreate();
199 'id' => $this->_relTypeID
,
200 'name_a_b' => 'Test 1 for update',
201 'name_b_a' => 'Test 2 for update',
202 'description' => 'SUNIL PAWAR relationship type',
203 'contact_type_a' => 'Individual',
204 'contact_type_b' => 'Individual',
209 $result = $this->callAPISuccess('relationship_type', 'create', $params);
210 $this->assertNotNull($result['id']);
212 // assertDBState compares expected values in $result to actual values in the DB
213 $this->assertDBState('CRM_Contact_DAO_RelationshipType', $result['id'], $params);
216 ///////////////// civicrm_relationship_types_get methods
219 * Check with empty array.
220 * @param int $version
221 * @dataProvider versionThreeAndFour
223 public function testRelationshipTypesGetEmptyParams($version) {
224 $this->_apiversion
= $version;
225 $firstRelTypeParams = [
226 'name_a_b' => 'Relation 27 for create',
227 'name_b_a' => 'Relation 28 for create',
228 'description' => 'Testing relationship type',
229 'contact_type_a' => 'Individual',
230 'contact_type_b' => 'Organization',
235 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
237 $secondRelTypeParams = [
238 'name_a_b' => 'Relation 25 for create',
239 'name_b_a' => 'Relation 26 for create',
240 'description' => 'Testing relationship type second',
241 'contact_type_a' => 'Individual',
242 'contact_type_b' => 'Organization',
246 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
247 $results = $this->callAPISuccess('relationship_type', 'get', []);
249 $this->assertEquals(2, $results['count']);
253 * Check with valid params array.
254 * @param int $version
255 * @dataProvider versionThreeAndFour
257 public function testRelationshipTypesGet($version) {
258 $this->_apiversion
= $version;
259 $firstRelTypeParams = [
260 'name_a_b' => 'Relation 30 for create',
261 'name_b_a' => 'Relation 31 for create',
262 'description' => 'Testing relationship type',
263 'contact_type_a' => 'Individual',
264 'contact_type_b' => 'Organization',
269 $first = $this->callAPISuccess('RelationshipType', 'Create', $firstRelTypeParams);
271 $secondRelTypeParams = [
272 'name_a_b' => 'Relation 32 for create',
273 'name_b_a' => 'Relation 33 for create',
274 'description' => 'Testing relationship type second',
275 'contact_type_a' => 'Individual',
276 'contact_type_b' => 'Organization',
280 $second = $this->callAPISuccess('RelationshipType', 'Create', $secondRelTypeParams);
283 'name_a_b' => 'Relation 32 for create',
284 'name_b_a' => 'Relation 33 for create',
285 'description' => 'Testing relationship type second',
287 $results = $this->callAPISuccess('relationship_type', 'get', $params);
289 $this->assertEquals(1, $results['count'], ' in line ' . __LINE__
);
290 $this->assertEquals(1, $results['values'][$results['id']]['is_active'], ' in line ' . __LINE__
);
294 * Create relationship type.
295 * @param null $params
298 public function _relationshipTypeCreate($params = NULL) {
299 if (!is_array($params) ||
empty($params)) {
301 'name_a_b' => 'Relation 1 for create',
302 'name_b_a' => 'Relation 2 for create',
303 'description' => 'Testing relationship type',
304 'contact_type_a' => 'Individual',
305 'contact_type_b' => 'Organization',
311 return $this->relationshipTypeCreate($params);