Add in Country and StateProvince APIv4 Entities
[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 ];
37 $result = $this->callAPISuccess('payment_processor_type', 'create', $params);
38 $this->_paymentProcessorType = $result['id'];
39 $this->_params = [
40 'name' => 'API Test PP',
41 'payment_processor_type_id' => $this->_paymentProcessorType,
42 'class_name' => 'CRM_Core_Payment_APITest',
43 'is_recur' => 0,
44 'domain_id' => 1,
45 ];
46 }
47
48 /**
49 * Check create with no name specified.
50 */
51 public function testPaymentProcessorCreateWithoutName() {
52 $this->callAPIFailure('payment_processor', 'create', ['is_active' => 1]);
53 }
54
55 /**
56 * Create payment processor.
57 *
58 * @throws \CRM_Core_Exception
59 */
60 public function testPaymentProcessorCreate() {
61 $params = $this->_params;
62 $result = $this->callAPIAndDocument('payment_processor', 'create', $params, __FUNCTION__, __FILE__);
63 $this->callAPISuccessGetSingle('EntityFinancialAccount', ['entity_table' => 'civicrm_payment_processor', 'entity_id' => $result['id']]);
64
65 // Test that the option values are flushed so ths can be used straight away.
66 $this->callAPISuccess('ContributionRecur', 'create', [
67 'contact_id' => $this->individualCreate(),
68 'amount' => 5,
69 'financial_type_id' => 'Donation',
70 'payment_processor_id' => 'API Test PP',
71 'frequency_interval' => 1,
72 ]);
73 $this->getAndCheck($params, $result['id'], 'PaymentProcessor');
74 }
75
76 /**
77 * Update payment processor.
78 *
79 * @throws \CRM_Core_Exception
80 */
81 public function testPaymentProcessorUpdate() {
82 $params = $this->_params;
83 $result = $this->callAPISuccess('payment_processor', 'create', $params);
84 $this->assertNotNull($result['id']);
85
86 $updateParams = [
87 'id' => $result['id'],
88 'name' => 'Update API Test',
89 ];
90 $this->assertDBState('CRM_Financial_DAO_PaymentProcessor', $result['id'], $params);
91 $this->callAPISuccess('payment_processor', 'create', $updateParams);
92 $result = $this->callAPISuccess('payment_processor', 'get', ['id' => $result['id']]);
93
94 $expectedResult = [
95 'id' => $result['id'],
96 'domain_id' => $params['domain_id'],
97 'name' => $updateParams['name'],
98 'payment_processor_type_id' => $params['payment_processor_type_id'],
99 'is_default' => 0,
100 'is_test' => 0,
101 'class_name' => $params['class_name'],
102 'billing_mode' => 1,
103 'is_recur' => $params['is_recur'],
104 'payment_type' => 1,
105 'payment_instrument_id' => 1,
106 'is_active' => 1,
107 ];
108 $this->checkArrayEquals($expectedResult, $result['values'][$result['id']]);
109 }
110
111 /**
112 * Test using example code.
113 */
114 public function testPaymentProcessorCreateExample() {
115 require_once 'api/v3/examples/PaymentProcessor/Create.ex.php';
116 $result = payment_processor_create_example();
117 $expectedResult = payment_processor_create_expectedresult();
118 $this->assertAPISuccess($result);
119 }
120
121 /**
122 * Check payment processor delete.
123 *
124 * @throws \CRM_Core_Exception
125 */
126 public function testPaymentProcessorDelete() {
127 $result = $this->callAPISuccess('payment_processor', 'create', $this->_params);
128 $params = [
129 'id' => $result['id'],
130 ];
131
132 $this->callAPIAndDocument('payment_processor', 'delete', $params, __FUNCTION__, __FILE__);
133 }
134
135 /**
136 * Check with valid params array.
137 *
138 * @throws \CRM_Core_Exception
139 */
140 public function testPaymentProcessorsGet() {
141 $params = $this->_params;
142 $params['user_name'] = 'test@test.com';
143 $this->callAPISuccess('payment_processor', 'create', $params);
144
145 $params = [
146 'user_name' => 'test@test.com',
147 ];
148 $results = $this->callAPISuccess('payment_processor', 'get', $params);
149
150 $this->assertEquals(1, $results['count']);
151 $this->assertEquals('test@test.com', $results['values'][$results['id']]['user_name']);
152 }
153
154 }