Merge pull request #17203 from artfulrobot/artfulrobot-cleanup-job-improvements
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentProcessorTest.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"
14 *
15 * @group headless
16 */
17 class api_v3_PaymentProcessorTest extends CiviUnitTestCase {
18 protected $_paymentProcessorType;
19 protected $_params;
20
21 /**
22 * Set up class.
23 *
24 * @throws \CRM_Core_Exception
25 */
26 public function setUp() {
27 parent::setUp();
28 $this->useTransaction(TRUE);
29 // Create dummy processor
30 $params = [
31 'name' => 'API_Test_PP_Type',
32 'title' => 'API Test Payment Processor Type',
33 'class_name' => 'CRM_Core_Payment_APITest',
34 'billing_mode' => 'form',
35 'is_recur' => 0,
36 'payment_instrument_id' => 2,
37 ];
38 $result = $this->callAPISuccess('payment_processor_type', 'create', $params);
39 $this->_paymentProcessorType = $result['id'];
40 $this->_params = [
41 'name' => 'API Test PP',
42 'payment_processor_type_id' => $this->_paymentProcessorType,
43 'class_name' => 'CRM_Core_Payment_APITest',
44 'is_recur' => 0,
45 'domain_id' => 1,
46 ];
47 }
48
49 /**
50 * Check create with no name specified.
51 * @dataProvider versionThreeAndFour
52 */
53 public function testPaymentProcessorCreateWithoutName($version) {
54 $this->_apiversion = $version;
55 $this->callAPIFailure('payment_processor', 'create', ['is_active' => 1]);
56 }
57
58 /**
59 * Create payment processor.
60 * @dataProvider versionThreeAndFour
61 *
62 * @throws \CRM_Core_Exception
63 */
64 public function testPaymentProcessorCreate($version) {
65 $this->_apiversion = $version;
66 $params = $this->_params;
67 $result = $this->callAPIAndDocument('payment_processor', 'create', $params, __FUNCTION__, __FILE__);
68 $this->callAPISuccessGetSingle('EntityFinancialAccount', ['entity_table' => 'civicrm_payment_processor', 'entity_id' => $result['id']]);
69
70 // Test that the option values are flushed so ths can be used straight away.
71 $this->callAPISuccess('ContributionRecur', 'create', [
72 'contact_id' => $this->individualCreate(),
73 'amount' => 5,
74 'financial_type_id' => 'Donation',
75 'payment_processor_id' => 'API Test PP',
76 'frequency_interval' => 1,
77 ]);
78 $this->getAndCheck($params, $result['id'], 'PaymentProcessor');
79 $this->assertEquals(2, $result['values'][$result['id']]['payment_instrument_id']);
80 }
81
82 /**
83 * Update payment processor.
84 * @dataProvider versionThreeAndFour
85 *
86 * @throws \CRM_Core_Exception
87 */
88 public function testPaymentProcessorUpdate($version) {
89 $this->_apiversion = $version;
90 $params = $this->_params;
91 $params['payment_instrument_id'] = 1;
92 $result = $this->callAPISuccess('payment_processor', 'create', $params);
93 $this->assertNotNull($result['id']);
94
95 $updateParams = [
96 'id' => $result['id'],
97 'name' => 'Update API Test',
98 ];
99 $this->assertDBState('CRM_Financial_DAO_PaymentProcessor', $result['id'], $params);
100 $this->callAPISuccess('payment_processor', 'create', $updateParams);
101 $result = $this->callAPISuccess('payment_processor', 'get', ['id' => $result['id']]);
102
103 $expectedResult = [
104 'id' => $result['id'],
105 'domain_id' => $params['domain_id'],
106 'name' => $updateParams['name'],
107 'payment_processor_type_id' => $params['payment_processor_type_id'],
108 'is_default' => 0,
109 'is_test' => 0,
110 'class_name' => $params['class_name'],
111 'billing_mode' => 1,
112 'is_recur' => $params['is_recur'],
113 'payment_type' => 1,
114 'payment_instrument_id' => 1,
115 'is_active' => 1,
116 ];
117 if ($version === 4) {
118 // In APIv3 If a field is default NULL it is not returned.
119 foreach ($result['values'][$result['id']] as $field => $value) {
120 if (is_null($value)) {
121 unset($result['values'][$result['id']][$field]);
122 }
123 }
124 }
125 $this->checkArrayEquals($expectedResult, $result['values'][$result['id']]);
126 }
127
128 /**
129 * Test using example code.
130 */
131 public function testPaymentProcessorCreateExample() {
132 require_once 'api/v3/examples/PaymentProcessor/Create.ex.php';
133 $result = payment_processor_create_example();
134 $expectedResult = payment_processor_create_expectedresult();
135 $this->assertAPISuccess($result);
136 }
137
138 /**
139 * Check payment processor delete.
140 * @dataProvider versionThreeAndFour
141 *
142 * @throws \CRM_Core_Exception
143 */
144 public function testPaymentProcessorDelete($version) {
145 $this->_apiversion = $version;
146 $result = $this->callAPISuccess('payment_processor', 'create', $this->_params);
147 $params = [
148 'id' => $result['id'],
149 ];
150
151 $this->callAPIAndDocument('payment_processor', 'delete', $params, __FUNCTION__, __FILE__);
152 }
153
154 /**
155 * Check with valid params array.
156 * @dataProvider versionThreeAndFour
157 *
158 * @throws \CRM_Core_Exception
159 */
160 public function testPaymentProcessorsGet($version) {
161 $this->_apiversion = $version;
162 $params = $this->_params;
163 $params['user_name'] = 'test@test.com';
164 $this->callAPISuccess('payment_processor', 'create', $params);
165
166 $params = [
167 'user_name' => 'test@test.com',
168 ];
169 $results = $this->callAPISuccess('payment_processor', 'get', $params);
170
171 $this->assertEquals(1, $results['count']);
172 $this->assertEquals('test@test.com', $results['values'][$results['id']]['user_name']);
173 }
174
175 }