Merge pull request #17294 from agh1/sr-rel-perms
[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
20 public function setUp() {
21
22 parent::setUp();
23 $this->useTransaction(TRUE);
24 }
25
26 // function tearDown() {
27 //
28 // $tablesToTruncate = array(
29 // 'civicrm_payment_processor_type',
30 // );
31 // $this->quickCleanup($tablesToTruncate);
32 // }
33
34 ///////////////// civicrm_payment_processor_type_add methods
35
36 /**
37 * Check with no name.
38 * @dataProvider versionThreeAndFour
39 */
40 public function testPaymentProcessorTypeCreateWithoutName($version) {
41 $this->_apiversion = $version;
42 $payProcParams = [
43 'is_active' => 1,
44 ];
45 $result = $this->callAPIFailure('payment_processor_type', 'create', $payProcParams);
46 $this->assertContains('name, title, class_name, billing_mode', $result['error_message']);
47 }
48
49 /**
50 * Create payment processor type.
51 * @dataProvider versionThreeAndFour
52 */
53 public function testPaymentProcessorTypeCreate($version) {
54 $this->_apiversion = $version;
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 * @dataProvider versionThreeAndFour
88 */
89 public function testPaymentProcessorTypeDeleteEmpty($version) {
90 $this->_apiversion = $version;
91 $params = [];
92 $result = $this->callAPIFailure('payment_processor_type', 'delete', $params);
93 }
94
95 /**
96 * Check if required fields are not passed.
97 * @dataProvider versionThreeAndFour
98 */
99 public function testPaymentProcessorTypeDeleteWithoutRequired($version) {
100 $this->_apiversion = $version;
101 $params = [
102 'name' => 'API_Test_PP',
103 'title' => 'API Test Payment Processor',
104 'class_name' => 'CRM_Core_Payment_APITest',
105 ];
106
107 $result = $this->callAPIFailure('payment_processor_type', 'delete', $params);
108 $this->assertEquals(($version === 4 ? 'Parameter "where" is required.' : 'Mandatory key(s) missing from params array: id'), $result['error_message']);
109 }
110
111 /**
112 * Check with incorrect required fields.
113 * @dataProvider versionThreeAndFour
114 */
115 public function testPaymentProcessorTypeDeleteWithIncorrectData($version) {
116 $this->_apiversion = $version;
117 $result = $this->callAPIFailure('payment_processor_type', 'delete', ['id' => 'abcd']);
118 }
119
120 /**
121 * Check payment processor type delete.
122 * @dataProvider versionThreeAndFour
123 */
124 public function testPaymentProcessorTypeDelete($version) {
125 $this->_apiversion = $version;
126 $payProcType = $this->paymentProcessorTypeCreate();
127 $params = [
128 'id' => $payProcType,
129 ];
130
131 $result = $this->callAPIAndDocument('payment_processor_type', 'delete', $params, __FUNCTION__, __FILE__);
132 }
133
134 ///////////////// civicrm_payment_processor_type_update
135
136 /**
137 * Check with empty array.
138 * @dataProvider versionThreeAndFour
139 */
140 public function testPaymentProcessorTypeUpdateEmpty($version) {
141 $this->_apiversion = $version;
142 $params = [];
143 $result = $this->callAPIFailure('payment_processor_type', 'create', $params);
144 $this->assertContains('name, title, class_name, billing_mode', $result['error_message']);
145 }
146
147 /**
148 * Check with all parameters.
149 * @dataProvider versionThreeAndFour
150 */
151 public function testPaymentProcessorTypeUpdate($version) {
152 $this->_apiversion = $version;
153 // create sample payment processor type.
154 $this->_ppTypeID = $this->paymentProcessorTypeCreate(NULL);
155
156 $params = [
157 'id' => $this->_ppTypeID,
158 // keep the same
159 'name' => 'API_Test_PP',
160 'title' => 'API Test Payment Processor 2',
161 'class_name' => 'CRM_Core_Payment_APITest 2',
162 'billing_mode' => 2,
163 'is_recur' => 0,
164 ];
165
166 $result = $this->callAPISuccess('payment_processor_type', 'create', $params);
167 $this->assertNotNull($result['id']);
168 // assertDBState compares expected values in $result to actual values in the DB
169 $this->assertDBState('CRM_Financial_DAO_PaymentProcessorType', $this->_ppTypeID, $params);
170 }
171
172 ///////////////// civicrm_payment_processor_types_get methods
173
174 /**
175 * Check with empty array.
176 * @dataProvider versionThreeAndFour
177 */
178 public function testPaymentProcessorTypesGetEmptyParams($version) {
179 $this->_apiversion = $version;
180 $results = $this->callAPISuccess('payment_processor_type', 'get', []);
181 $baselineCount = $results['count'];
182
183 $firstRelTypeParams = [
184 'name' => 'API_Test_PP',
185 'title' => 'API Test Payment Processor',
186 'class_name' => 'CRM_Core_Payment_APITest',
187 'billing_mode' => 1,
188 'is_recur' => 0,
189 ];
190
191 $first = $this->callAPISuccess('PaymentProcessorType', 'Create', $firstRelTypeParams);
192
193 $secondRelTypeParams = [
194 'name' => 'API_Test_PP2',
195 'title' => 'API Test Payment Processor 2',
196 'class_name' => 'CRM_Core_Payment_APITest 2',
197 'billing_mode' => 2,
198 'is_recur' => 0,
199 ];
200 $second = $this->callAPISuccess('PaymentProcessorType', 'Create', $secondRelTypeParams);
201 $result = $this->callAPISuccess('payment_processor_type', 'get', []);
202
203 $this->assertEquals($baselineCount + 2, $result['count']);
204 $this->assertAPISuccess($result);
205 }
206
207 /**
208 * Check with valid params array.
209 * @dataProvider versionThreeAndFour
210 */
211 public function testPaymentProcessorTypesGet($version) {
212 $this->_apiversion = $version;
213 $firstRelTypeParams = [
214 'name' => 'API_Test_PP_11',
215 'title' => 'API Test Payment Processor 11',
216 'class_name' => 'CRM_Core_Payment_APITest_11',
217 'billing_mode' => 1,
218 'is_recur' => 0,
219 ];
220
221 $first = $this->callAPISuccess('PaymentProcessorType', 'Create', $firstRelTypeParams);
222
223 $secondRelTypeParams = [
224 'name' => 'API_Test_PP_12',
225 'title' => 'API Test Payment Processor 12',
226 'class_name' => 'CRM_Core_Payment_APITest_12',
227 'billing_mode' => 2,
228 'is_recur' => 0,
229 ];
230 $second = $this->callAPISuccess('PaymentProcessorType', 'Create', $secondRelTypeParams);
231
232 $params = [
233 'name' => 'API_Test_PP_12',
234 ];
235 $result = $this->callAPISuccess('payment_processor_type', 'get', $params);
236
237 $this->assertAPISuccess($result);
238 $this->assertEquals(1, $result['count'], ' in line ' . __LINE__);
239 $this->assertEquals('CRM_Core_Payment_APITest_12', $result['values'][$result['id']]['class_name'], ' in line ' . __LINE__);
240 }
241
242 }