Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAG2-204 - Added availability check to Creditcard method #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Model/Methods/Creditcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,79 @@ class Creditcard extends PayoneMethod

];

/**
* @var \Payone\Core\Helper\Payment
*/
protected $paymentHelper;

/**
* Constructor
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
* @param \Magento\Framework\Api\AttributeValueFactory $customAttrFactory
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Payment\Model\Method\Logger $logger
* @param \Payone\Core\Helper\Toolkit $toolkitHelper
* @param \Payone\Core\Helper\Shop $shopHelper
* @param \Magento\Framework\Url $url
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Payone\Core\Model\Api\Request\Debit $debitRequest
* @param \Payone\Core\Model\Api\Request\Capture $captureRequest
* @param \Payone\Core\Model\Api\Request\Authorization $authorizationRequest
* @param \Payone\Core\Model\ResourceModel\SavedPaymentData $savedPaymentData
* @param \Payone\Core\Helper\Payment $paymentHelper
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
\Magento\Framework\Api\AttributeValueFactory $customAttrFactory,
\Magento\Payment\Helper\Data $paymentData,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Payment\Model\Method\Logger $logger,
\Payone\Core\Helper\Toolkit $toolkitHelper,
\Payone\Core\Helper\Shop $shopHelper,
\Magento\Framework\Url $url,
\Magento\Checkout\Model\Session $checkoutSession,
\Payone\Core\Model\Api\Request\Debit $debitRequest,
\Payone\Core\Model\Api\Request\Capture $captureRequest,
\Payone\Core\Model\Api\Request\Authorization $authorizationRequest,
\Payone\Core\Model\ResourceModel\SavedPaymentData $savedPaymentData,
\Payone\Core\Helper\Payment $paymentHelper,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct($context, $registry, $extensionFactory, $customAttrFactory, $paymentData, $scopeConfig, $logger, $toolkitHelper, $shopHelper, $url, $checkoutSession, $debitRequest, $captureRequest, $authorizationRequest, $savedPaymentData, $resource, $resourceCollection, $data);
$this->paymentHelper = $paymentHelper;
}

/**
* Check whether payment method can be used
*
* @param \Magento\Quote\Api\Data\CartInterface|null $quote
* @return bool
*/
public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
{
$blParentReturn = parent::isAvailable($quote);
if ($blParentReturn === false) {
return false;
}

// dont show payment method when configuration is not complete
if (empty($this->paymentHelper->getAvailableCreditcardTypes())) {
return false;
}
return $blParentReturn;
}

/**
* Return parameters specific to this payment type
*
Expand Down
48 changes: 47 additions & 1 deletion Test/Unit/Model/Methods/CreditcardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

namespace Payone\Core\Test\Unit\Model\Methods;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\Store;
use Payone\Core\Helper\Payment;
use Payone\Core\Helper\Shop;
use Payone\Core\Helper\Toolkit;
use Payone\Core\Model\Methods\Creditcard as ClassToTest;
Expand Down Expand Up @@ -56,6 +58,16 @@ class CreditcardTest extends BaseTestCase
*/
private $info;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var Payment|\PHPUnit\Framework\MockObject\MockObject
*/
private $paymentHelper;

protected function setUp(): void
{
$this->objectManager = $this->getObjectManager();
Expand Down Expand Up @@ -84,10 +96,16 @@ protected function setUp(): void
->getMock();
$checkoutSession->method('getQuote')->willReturn($quote);

$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->disableOriginalConstructor()->getMock();

$this->paymentHelper = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock();

$this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
'toolkitHelper' => $toolkitHelper,
'scopeConfig' => $this->scopeConfig,
'shopHelper' => $shopHelper,
'checkoutSession' => $checkoutSession
'checkoutSession' => $checkoutSession,
'paymentHelper' => $this->paymentHelper,
]);
$this->classToTest->setInfoInstance($this->info);
}
Expand Down Expand Up @@ -130,6 +148,34 @@ public function testAssignData()
$this->assertInstanceOf(ClassToTest::class, $result);
}

public function testIsAvailableParent()
{
$this->scopeConfig->method('getValue')->willReturn(0);

$result = $this->classToTest->isAvailable();
$this->assertFalse($result);
}

public function testIsAvailableTrue()
{
$this->scopeConfig->method('getValue')->willReturn(1);

$this->paymentHelper->method("getAvailableCreditcardTypes")->willReturn(['visa']);

$result = $this->classToTest->isAvailable();
$this->assertTrue($result);
}

public function testIsAvailableFalse()
{
$this->scopeConfig->method('getValue')->willReturn(1);

$this->paymentHelper->method("getAvailableCreditcardTypes")->willReturn(false);

$result = $this->classToTest->isAvailable();
$this->assertFalse($result);
}

public function testAssignDataNoOrder()
{
$addData = [
Expand Down