Commit | Line | Data |
---|---|---|
21150aae | 1 | <?php |
21150aae CW |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
21150aae | 5 | | | |
7d61e75f TO |
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 | | |
21150aae | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
21150aae | 11 | |
21150aae CW |
12 | /** |
13 | * Class contains api test cases for "civicrm_payment_processor" | |
14 | * | |
acb109b7 | 15 | * @group headless |
21150aae CW |
16 | */ |
17 | class api_v3_PaymentProcessorTest extends CiviUnitTestCase { | |
18 | protected $_paymentProcessorType; | |
21150aae | 19 | protected $_params; |
b7c9bc4c | 20 | |
3209a807 | 21 | /** |
22 | * Set up class. | |
23 | * | |
24 | * @throws \CRM_Core_Exception | |
25 | */ | |
00be9182 | 26 | public function setUp() { |
21150aae | 27 | parent::setUp(); |
208f71f0 | 28 | $this->useTransaction(TRUE); |
21150aae | 29 | // Create dummy processor |
9099cab3 | 30 | $params = [ |
21150aae CW |
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, | |
5de5c50e | 36 | 'payment_instrument_id' => 2, |
9099cab3 | 37 | ]; |
fa7b9efb | 38 | $result = $this->callAPISuccess('payment_processor_type', 'create', $params); |
21150aae | 39 | $this->_paymentProcessorType = $result['id']; |
9099cab3 | 40 | $this->_params = [ |
21150aae CW |
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, | |
9099cab3 | 46 | ]; |
21150aae CW |
47 | } |
48 | ||
21150aae | 49 | /** |
3209a807 | 50 | * Check create with no name specified. |
74e272af | 51 | * @dataProvider versionThreeAndFour |
21150aae | 52 | */ |
74e272af SL |
53 | public function testPaymentProcessorCreateWithoutName($version) { |
54 | $this->_apiversion = $version; | |
3209a807 | 55 | $this->callAPIFailure('payment_processor', 'create', ['is_active' => 1]); |
21150aae CW |
56 | } |
57 | ||
58 | /** | |
eceb18cc | 59 | * Create payment processor. |
74e272af | 60 | * @dataProvider versionThreeAndFour |
3209a807 | 61 | * |
62 | * @throws \CRM_Core_Exception | |
21150aae | 63 | */ |
74e272af SL |
64 | public function testPaymentProcessorCreate($version) { |
65 | $this->_apiversion = $version; | |
21150aae | 66 | $params = $this->_params; |
fa7b9efb | 67 | $result = $this->callAPIAndDocument('payment_processor', 'create', $params, __FUNCTION__, __FILE__); |
8563c1ce | 68 | $this->callAPISuccessGetSingle('EntityFinancialAccount', ['entity_table' => 'civicrm_payment_processor', 'entity_id' => $result['id']]); |
576c8a85 | 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 | ]); | |
8563c1ce | 78 | $this->getAndCheck($params, $result['id'], 'PaymentProcessor'); |
5de5c50e | 79 | $this->assertEquals(2, $result['values'][$result['id']]['payment_instrument_id']); |
21150aae CW |
80 | } |
81 | ||
29dd4ada JP |
82 | /** |
83 | * Update payment processor. | |
74e272af | 84 | * @dataProvider versionThreeAndFour |
3209a807 | 85 | * |
86 | * @throws \CRM_Core_Exception | |
29dd4ada | 87 | */ |
74e272af SL |
88 | public function testPaymentProcessorUpdate($version) { |
89 | $this->_apiversion = $version; | |
29dd4ada | 90 | $params = $this->_params; |
5de5c50e | 91 | $params['payment_instrument_id'] = 1; |
29dd4ada JP |
92 | $result = $this->callAPISuccess('payment_processor', 'create', $params); |
93 | $this->assertNotNull($result['id']); | |
94 | ||
9099cab3 | 95 | $updateParams = [ |
29dd4ada JP |
96 | 'id' => $result['id'], |
97 | 'name' => 'Update API Test', | |
9099cab3 | 98 | ]; |
29dd4ada JP |
99 | $this->assertDBState('CRM_Financial_DAO_PaymentProcessor', $result['id'], $params); |
100 | $this->callAPISuccess('payment_processor', 'create', $updateParams); | |
9099cab3 | 101 | $result = $this->callAPISuccess('payment_processor', 'get', ['id' => $result['id']]); |
29dd4ada | 102 | |
9099cab3 | 103 | $expectedResult = [ |
29dd4ada JP |
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, | |
29adc103 | 115 | 'is_active' => 1, |
9099cab3 | 116 | ]; |
74e272af SL |
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 | } | |
29dd4ada JP |
125 | $this->checkArrayEquals($expectedResult, $result['values'][$result['id']]); |
126 | } | |
127 | ||
21150aae | 128 | /** |
eceb18cc | 129 | * Test using example code. |
21150aae | 130 | */ |
00be9182 | 131 | public function testPaymentProcessorCreateExample() { |
be44cfcb | 132 | require_once 'api/v3/examples/PaymentProcessor/Create.ex.php'; |
21150aae CW |
133 | $result = payment_processor_create_example(); |
134 | $expectedResult = payment_processor_create_expectedresult(); | |
791c263c | 135 | $this->assertAPISuccess($result); |
21150aae CW |
136 | } |
137 | ||
21150aae | 138 | /** |
eceb18cc | 139 | * Check payment processor delete. |
74e272af | 140 | * @dataProvider versionThreeAndFour |
3209a807 | 141 | * |
142 | * @throws \CRM_Core_Exception | |
21150aae | 143 | */ |
74e272af SL |
144 | public function testPaymentProcessorDelete($version) { |
145 | $this->_apiversion = $version; | |
8563c1ce | 146 | $result = $this->callAPISuccess('payment_processor', 'create', $this->_params); |
9099cab3 | 147 | $params = [ |
8563c1ce | 148 | 'id' => $result['id'], |
9099cab3 | 149 | ]; |
21150aae | 150 | |
3c27d467 | 151 | $this->callAPIAndDocument('payment_processor', 'delete', $params, __FUNCTION__, __FILE__); |
21150aae CW |
152 | } |
153 | ||
21150aae | 154 | /** |
100fef9d | 155 | * Check with valid params array. |
74e272af | 156 | * @dataProvider versionThreeAndFour |
3209a807 | 157 | * |
158 | * @throws \CRM_Core_Exception | |
21150aae | 159 | */ |
74e272af SL |
160 | public function testPaymentProcessorsGet($version) { |
161 | $this->_apiversion = $version; | |
21150aae CW |
162 | $params = $this->_params; |
163 | $params['user_name'] = 'test@test.com'; | |
fa7b9efb | 164 | $this->callAPISuccess('payment_processor', 'create', $params); |
21150aae | 165 | |
9099cab3 | 166 | $params = [ |
21150aae | 167 | 'user_name' => 'test@test.com', |
9099cab3 | 168 | ]; |
fa7b9efb | 169 | $results = $this->callAPISuccess('payment_processor', 'get', $params); |
21150aae | 170 | |
3c27d467 | 171 | $this->assertEquals(1, $results['count']); |
172 | $this->assertEquals('test@test.com', $results['values'][$results['id']]['user_name']); | |
21150aae | 173 | } |
96025800 | 174 | |
21150aae | 175 | } |