Merge pull request #21945 from masetto/profile-data-attribute
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / PaymentProcessorTest.php
CommitLineData
c294dbbc
SL
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
c294dbbc 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 |
c294dbbc
SL
9 +--------------------------------------------------------------------+
10 */
11
67557afe 12use Civi\Api4\PaymentProcessor;
c3408d93 13
c294dbbc
SL
14/**
15 * Class CRM_Financial_BAO_PaymentProcessorTypeTest
16 * @group headless
17 */
8d79abbe 18class CRM_Financial_BAO_PaymentProcessorTest extends CiviUnitTestCase {
39b959db 19
c294dbbc
SL
20 /**
21 * Check method create()
22 */
23 public function testGetCreditCards() {
9099cab3 24 $params = [
c294dbbc
SL
25 'name' => 'API_Test_PP_Type',
26 'title' => 'API Test Payment Processor Type',
27 'class_name' => 'CRM_Core_Payment_APITest',
28 'billing_mode' => 'form',
29 'payment_processor_type_id' => 1,
30 'is_recur' => 0,
31 'domain_id' => 1,
9099cab3 32 'accepted_credit_cards' => json_encode([
c294dbbc
SL
33 'Visa' => 'Visa',
34 'Mastercard' => 'Mastercard',
35 'Amex' => 'Amex',
9099cab3
CW
36 ]),
37 ];
c294dbbc 38 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($params);
9099cab3 39 $expectedCards = [
c294dbbc
SL
40 'Visa' => 'Visa',
41 'Mastercard' => 'Mastercard',
42 'Amex' => 'Amex',
9099cab3 43 ];
c294dbbc
SL
44 $cards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($paymentProcessor->id);
45 $this->assertEquals($cards, $expectedCards, 'Verify correct credit card types are returned');
46 }
47
31eddd0d 48 /**
49 * Test the processor retrieval function.
50 *
67557afe 51 * @throws \API_Exception
31eddd0d 52 * @throws \CiviCRM_API3_Exception
67557afe 53 * @throws \Civi\API\Exception\UnauthorizedException
31eddd0d 54 */
55 public function testGetProcessors() {
56 $testProcessor = $this->dummyProcessorCreate();
57 $testProcessorID = $testProcessor->getID();
58 $liveProcessorID = $testProcessorID + 1;
59
60 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode']);
61 $this->assertEquals([$testProcessorID, 0], array_keys($processors), 'Only the test processor and the manual processor should be returned');
62
63 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode'], [$liveProcessorID]);
64 $this->assertEquals([$testProcessorID], array_keys($processors), 'Only the test processor should be returned');
65
66 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode'], [$testProcessorID]);
67 $this->assertEquals([$testProcessorID], array_keys($processors), 'Only the test processor should be returned');
68
69 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'LiveMode']);
70 $this->assertEquals([$liveProcessorID, 0], array_keys($processors), 'Only the Live processor and the manual processor should be returned');
71
72 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'LiveMode'], [$liveProcessorID]);
73 $this->assertEquals([$liveProcessorID], array_keys($processors), 'Only the Live processor should be returned');
67557afe 74
75 PaymentProcessor::update()->addWhere('id', 'IS NOT NULL')->setValues(['domain_id' => 2])->execute();
76 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'LiveMode'], [$liveProcessorID]);
77 $this->assertEquals([$liveProcessorID], array_keys($processors), 'Live processor should still be returned even though it is on a different domain');
78
79 // The api won't permit disabling only live mode due to lack of integrity so use direct SQL
80 CRM_Core_DAO::executeQuery('UPDATE civicrm_payment_processor SET is_active = 0 WHERE is_test = 0');
81 Civi\Payment\System::singleton()->flushProcessors();
82 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'LiveMode'], [$liveProcessorID]);
83 $this->assertEquals([], array_keys($processors), 'Live processor should not be returned as it is inactive');
84
85 CRM_Core_DAO::executeQuery('UPDATE civicrm_payment_processor SET is_active = 0 WHERE is_test = 0');
86 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['BackOffice', 'TestMode'], [$testProcessorID]);
87 $this->assertEquals([$testProcessorID], array_keys($processors), 'The test processor should still be returned');
88
31eddd0d 89 }
90
6d7d7089
AS
91 /**
92 * Test the Manual processor supports 'NoEmailProvided'
93 */
94 public function testManualProcessorSupportsNoEmailProvided() {
95 $processors = CRM_Financial_BAO_PaymentProcessor::getPaymentProcessors(['NoEmailProvided']);
96 $found = FALSE;
97 foreach ($processors as $processor) {
67557afe 98 if ($processor['class_name'] === 'Payment_Manual') {
6d7d7089
AS
99 $found = TRUE;
100 continue;
101 }
102 }
103 $this->assertTrue($found, 'The Manual payment processor should support "NoEmailProvided"');
104 }
105
c294dbbc 106}