| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 29 | |
| 30 | /** |
| 31 | * Class contains api test cases for "civicrm_payment_processor_type" |
| 32 | * |
| 33 | */ |
| 34 | class api_v3_PaymentProcessorTypeTest extends CiviUnitTestCase { |
| 35 | protected $_ppTypeID; |
| 36 | protected $_apiversion; |
| 37 | |
| 38 | public function setUp() { |
| 39 | |
| 40 | parent::setUp(); |
| 41 | $this->useTransaction(TRUE); |
| 42 | $this->_apiversion = 3; |
| 43 | } |
| 44 | |
| 45 | // function tearDown() { |
| 46 | // |
| 47 | // $tablesToTruncate = array( |
| 48 | // 'civicrm_payment_processor_type', |
| 49 | // ); |
| 50 | // $this->quickCleanup($tablesToTruncate); |
| 51 | // } |
| 52 | |
| 53 | ///////////////// civicrm_payment_processor_type_add methods |
| 54 | |
| 55 | /** |
| 56 | * Check with no name |
| 57 | */ |
| 58 | public function testPaymentProcessorTypeCreateWithoutName() { |
| 59 | $payProcParams = array( |
| 60 | 'is_active' => 1, |
| 61 | ); |
| 62 | $result = $this->callAPIFailure('payment_processor_type', 'create', $payProcParams); |
| 63 | $this->assertEquals($result['error_message'], |
| 64 | 'Mandatory key(s) missing from params array: name, title, class_name, billing_mode' |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create payment processor type |
| 70 | */ |
| 71 | public function testPaymentProcessorTypeCreate() { |
| 72 | $params = array( |
| 73 | 'sequential' => 1, |
| 74 | 'name' => 'API_Test_PP', |
| 75 | 'title' => 'API Test Payment Processor', |
| 76 | 'class_name' => 'CRM_Core_Payment_APITest', |
| 77 | 'billing_mode' => 'form', |
| 78 | 'is_recur' => 0, |
| 79 | ); |
| 80 | $result = $this->callAPIAndDocument('payment_processor_type', 'create', $params, __FUNCTION__, __FILE__); |
| 81 | $this->assertNotNull($result['values'][0]['id'], 'in line ' . __LINE__); |
| 82 | |
| 83 | // mutate $params to match expected return value |
| 84 | unset($params['sequential']); |
| 85 | $params['billing_mode'] = CRM_Core_Payment::BILLING_MODE_FORM; |
| 86 | //assertDBState compares expected values in $result to actual values in the DB |
| 87 | $this->assertDBState('CRM_Financial_DAO_PaymentProcessorType', $result['id'], $params); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Test using example code |
| 92 | */ |
| 93 | public function testPaymentProcessorTypeCreateExample() { |
| 94 | require_once 'api/v3/examples/PaymentProcessorType/Create.php'; |
| 95 | $result = payment_processor_type_create_example(); |
| 96 | $expectedResult = payment_processor_type_create_expectedresult(); |
| 97 | $this->assertAPISuccess($result); |
| 98 | } |
| 99 | |
| 100 | ///////////////// civicrm_payment_processor_type_delete methods |
| 101 | |
| 102 | /** |
| 103 | * Check with empty array |
| 104 | */ |
| 105 | public function testPaymentProcessorTypeDeleteEmpty() { |
| 106 | $params = array(); |
| 107 | $result = $this->callAPIFailure('payment_processor_type', 'delete', $params); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Check with No array |
| 112 | */ |
| 113 | public function testPaymentProcessorTypeDeleteParamsNotArray() { |
| 114 | $result = $this->callAPIFailure('payment_processor_type', 'delete', 'string'); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check if required fields are not passed |
| 119 | */ |
| 120 | public function testPaymentProcessorTypeDeleteWithoutRequired() { |
| 121 | $params = array( |
| 122 | 'name' => 'API_Test_PP', |
| 123 | 'title' => 'API Test Payment Processor', |
| 124 | 'class_name' => 'CRM_Core_Payment_APITest', |
| 125 | ); |
| 126 | |
| 127 | $result = $this->callAPIFailure('payment_processor_type', 'delete', $params); |
| 128 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id'); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Check with incorrect required fields |
| 133 | */ |
| 134 | public function testPaymentProcessorTypeDeleteWithIncorrectData() { |
| 135 | $result = $this->callAPIFailure('payment_processor_type', 'delete', array('id' => 'abcd')); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Check payment processor type delete |
| 140 | */ |
| 141 | public function testPaymentProcessorTypeDelete() { |
| 142 | $payProcType = $this->paymentProcessorTypeCreate(); |
| 143 | $params = array( |
| 144 | 'id' => $payProcType, |
| 145 | ); |
| 146 | |
| 147 | $result = $this->callAPIAndDocument('payment_processor_type', 'delete', $params, __FUNCTION__, __FILE__); |
| 148 | } |
| 149 | |
| 150 | ///////////////// civicrm_payment_processor_type_update |
| 151 | |
| 152 | /** |
| 153 | * Check with empty array |
| 154 | */ |
| 155 | public function testPaymentProcessorTypeUpdateEmpty() { |
| 156 | $params = array(); |
| 157 | $result = $this->callAPIFailure('payment_processor_type', 'create', $params); |
| 158 | $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name, title, class_name, billing_mode'); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Check with No array |
| 163 | */ |
| 164 | public function testPaymentProcessorTypeUpdateParamsNotArray() { |
| 165 | $result = $this->callAPIFailure('payment_processor_type', 'create', 'string'); |
| 166 | $this->assertEquals($result['error_message'], 'Input variable `params` is not an array'); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Check with all parameters |
| 171 | */ |
| 172 | public function testPaymentProcessorTypeUpdate() { |
| 173 | // create sample payment processor type. |
| 174 | $this->_ppTypeID = $this->paymentProcessorTypeCreate(NULL); |
| 175 | |
| 176 | $params = array( |
| 177 | 'id' => $this->_ppTypeID, |
| 178 | 'name' => 'API_Test_PP', // keep the same |
| 179 | 'title' => 'API Test Payment Processor 2', |
| 180 | 'class_name' => 'CRM_Core_Payment_APITest 2', |
| 181 | 'billing_mode' => 2, |
| 182 | 'is_recur' => 0, |
| 183 | ); |
| 184 | |
| 185 | $result = $this->callAPISuccess('payment_processor_type', 'create', $params); |
| 186 | $this->assertNotNull($result['id']); |
| 187 | // assertDBState compares expected values in $result to actual values in the DB |
| 188 | $this->assertDBState('CRM_Financial_DAO_PaymentProcessorType', $this->_ppTypeID, $params); |
| 189 | } |
| 190 | |
| 191 | ///////////////// civicrm_payment_processor_types_get methods |
| 192 | |
| 193 | /** |
| 194 | * Check with empty array |
| 195 | */ |
| 196 | public function testPaymentProcessorTypesGetEmptyParams() { |
| 197 | $results = $this->callAPISuccess('payment_processor_type', 'get', array()); |
| 198 | $baselineCount = $results['count']; |
| 199 | |
| 200 | $firstRelTypeParams = array( |
| 201 | 'name' => 'API_Test_PP', |
| 202 | 'title' => 'API Test Payment Processor', |
| 203 | 'class_name' => 'CRM_Core_Payment_APITest', |
| 204 | 'billing_mode' => 1, |
| 205 | 'is_recur' => 0, |
| 206 | ); |
| 207 | |
| 208 | $first = $this->callAPISuccess('PaymentProcessorType', 'Create', $firstRelTypeParams); |
| 209 | |
| 210 | $secondRelTypeParams = array( |
| 211 | 'name' => 'API_Test_PP2', |
| 212 | 'title' => 'API Test Payment Processor 2', |
| 213 | 'class_name' => 'CRM_Core_Payment_APITest 2', |
| 214 | 'billing_mode' => 2, |
| 215 | 'is_recur' => 0, |
| 216 | ); |
| 217 | $second = $this->callAPISuccess('PaymentProcessorType', 'Create', $secondRelTypeParams); |
| 218 | $result = $this->callAPISuccess('payment_processor_type', 'get', array()); |
| 219 | |
| 220 | $this->assertEquals($baselineCount + 2, $result['count']); |
| 221 | $this->assertAPISuccess($result); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Check with valid params array. |
| 226 | */ |
| 227 | public function testPaymentProcessorTypesGet() { |
| 228 | $firstRelTypeParams = array( |
| 229 | 'name' => 'API_Test_PP_11', |
| 230 | 'title' => 'API Test Payment Processor 11', |
| 231 | 'class_name' => 'CRM_Core_Payment_APITest_11', |
| 232 | 'billing_mode' => 1, |
| 233 | 'is_recur' => 0, |
| 234 | ); |
| 235 | |
| 236 | $first = $this->callAPISuccess('PaymentProcessorType', 'Create', $firstRelTypeParams); |
| 237 | |
| 238 | $secondRelTypeParams = array( |
| 239 | 'name' => 'API_Test_PP_12', |
| 240 | 'title' => 'API Test Payment Processor 12', |
| 241 | 'class_name' => 'CRM_Core_Payment_APITest_12', |
| 242 | 'billing_mode' => 2, |
| 243 | 'is_recur' => 0, |
| 244 | ); |
| 245 | $second = $this->callAPISuccess('PaymentProcessorType', 'Create', $secondRelTypeParams); |
| 246 | |
| 247 | $params = array( |
| 248 | 'name' => 'API_Test_PP_12', |
| 249 | ); |
| 250 | $result = $this->callAPISuccess('payment_processor_type', 'get', $params); |
| 251 | |
| 252 | $this->assertAPISuccess($result); |
| 253 | $this->assertEquals(1, $result['count'], ' in line ' . __LINE__); |
| 254 | $this->assertEquals('CRM_Core_Payment_APITest_12', $result['values'][$result['id']]['class_name'], ' in line ' . __LINE__); |
| 255 | } |
| 256 | } |