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