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