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