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