Merge pull request #14760 from eileenmcnaughton/unsub
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentProcessorTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Class contains api test cases for "civicrm_payment_processor_type"
30 *
31 * @group headless
32 */
33 class api_v3_PaymentProcessorTypeTest extends CiviUnitTestCase {
34 protected $_ppTypeID;
35 protected $_apiversion;
36
37 public function setUp() {
38
39 parent::setUp();
40 $this->useTransaction(TRUE);
41 $this->_apiversion = 3;
42 }
43
44 // function tearDown() {
45 //
46 // $tablesToTruncate = array(
47 // 'civicrm_payment_processor_type',
48 // );
49 // $this->quickCleanup($tablesToTruncate);
50 // }
51
52 ///////////////// civicrm_payment_processor_type_add methods
53
54 /**
55 * Check with no name.
56 */
57 public function testPaymentProcessorTypeCreateWithoutName() {
58 $payProcParams = [
59 'is_active' => 1,
60 ];
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 = [
72 'sequential' => 1,
73 'name' => 'API_Test_PP',
74 'title' => 'API Test Payment Processor',
75 'class_name' => 'CRM_Core_Payment_APITest',
76 'billing_mode' => 'form',
77 'is_recur' => 0,
78 ];
79 $result = $this->callAPIAndDocument('payment_processor_type', 'create', $params, __FUNCTION__, __FILE__);
80 $this->assertNotNull($result['values'][0]['id']);
81
82 // mutate $params to match expected return value
83 unset($params['sequential']);
84 $params['billing_mode'] = CRM_Core_Payment::BILLING_MODE_FORM;
85 //assertDBState compares expected values in $result to actual values in the DB
86 $this->assertDBState('CRM_Financial_DAO_PaymentProcessorType', $result['id'], $params);
87 }
88
89 /**
90 * Test using example code.
91 */
92 public function testPaymentProcessorTypeCreateExample() {
93 require_once 'api/v3/examples/PaymentProcessorType/Create.php';
94 $result = payment_processor_type_create_example();
95 $expectedResult = payment_processor_type_create_expectedresult();
96 $this->assertAPISuccess($result);
97 }
98
99 ///////////////// civicrm_payment_processor_type_delete methods
100
101 /**
102 * Check with empty array.
103 */
104 public function testPaymentProcessorTypeDeleteEmpty() {
105 $params = [];
106 $result = $this->callAPIFailure('payment_processor_type', 'delete', $params);
107 }
108
109 /**
110 * Check with No array.
111 */
112 public function testPaymentProcessorTypeDeleteParamsNotArray() {
113 $result = $this->callAPIFailure('payment_processor_type', 'delete', 'string');
114 }
115
116 /**
117 * Check if required fields are not passed.
118 */
119 public function testPaymentProcessorTypeDeleteWithoutRequired() {
120 $params = [
121 'name' => 'API_Test_PP',
122 'title' => 'API Test Payment Processor',
123 'class_name' => 'CRM_Core_Payment_APITest',
124 ];
125
126 $result = $this->callAPIFailure('payment_processor_type', 'delete', $params);
127 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: id');
128 }
129
130 /**
131 * Check with incorrect required fields.
132 */
133 public function testPaymentProcessorTypeDeleteWithIncorrectData() {
134 $result = $this->callAPIFailure('payment_processor_type', 'delete', ['id' => 'abcd']);
135 }
136
137 /**
138 * Check payment processor type delete.
139 */
140 public function testPaymentProcessorTypeDelete() {
141 $payProcType = $this->paymentProcessorTypeCreate();
142 $params = [
143 'id' => $payProcType,
144 ];
145
146 $result = $this->callAPIAndDocument('payment_processor_type', 'delete', $params, __FUNCTION__, __FILE__);
147 }
148
149 ///////////////// civicrm_payment_processor_type_update
150
151 /**
152 * Check with empty array.
153 */
154 public function testPaymentProcessorTypeUpdateEmpty() {
155 $params = [];
156 $result = $this->callAPIFailure('payment_processor_type', 'create', $params);
157 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: name, title, class_name, billing_mode');
158 }
159
160 /**
161 * Check with No array.
162 */
163 public function testPaymentProcessorTypeUpdateParamsNotArray() {
164 $result = $this->callAPIFailure('payment_processor_type', 'create', 'string');
165 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
166 }
167
168 /**
169 * Check with all parameters.
170 */
171 public function testPaymentProcessorTypeUpdate() {
172 // create sample payment processor type.
173 $this->_ppTypeID = $this->paymentProcessorTypeCreate(NULL);
174
175 $params = [
176 'id' => $this->_ppTypeID,
177 // keep the same
178 'name' => 'API_Test_PP',
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', []);
198 $baselineCount = $results['count'];
199
200 $firstRelTypeParams = [
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 = [
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', []);
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 = [
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 = [
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 = [
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
257 }