123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Vault\Test\Unit\Model\Ui\Adminhtml;
- use Magento\Backend\Model\Session\Quote;
- use Magento\Framework\Api\Filter;
- use Magento\Framework\Api\FilterBuilder;
- use Magento\Framework\Api\SearchCriteria;
- use Magento\Framework\Api\SearchCriteriaBuilder;
- use Magento\Framework\Exception\InputException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Intl\DateTimeFactory;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
- use Magento\Payment\Helper\Data;
- use Magento\Sales\Api\Data\OrderInterface;
- use Magento\Sales\Api\Data\OrderPaymentInterface;
- use Magento\Sales\Api\OrderRepositoryInterface;
- use Magento\Store\Api\Data\StoreInterface;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\Vault\Api\Data\PaymentTokenInterface;
- use Magento\Vault\Api\Data\PaymentTokenSearchResultsInterface;
- use Magento\Vault\Api\PaymentTokenManagementInterface;
- use Magento\Vault\Api\PaymentTokenRepositoryInterface;
- use Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider;
- use Magento\Vault\Model\Ui\TokenUiComponentInterface;
- use Magento\Vault\Model\Ui\TokenUiComponentProviderInterface;
- use Magento\Vault\Model\VaultPaymentInterface;
- use PHPUnit_Framework_MockObject_MockObject as MockObject;
- /**
- * Class TokensConfigProviderTest
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class TokensConfigProviderTest extends \PHPUnit\Framework\TestCase
- {
- /**#@+
- * Global values
- */
- const STORE_ID = 1;
- const ORDER_ID = 2;
- const ORDER_PAYMENT_ENTITY_ID = 3;
- const ENTITY_ID = 4;
- const VAULT_PAYMENT_CODE = 'vault_payment';
- const VAULT_PROVIDER_CODE = 'payment';
- /**#@-*/
- /**
- * @var PaymentTokenRepositoryInterface|MockObject
- */
- private $paymentTokenRepository;
- /**
- * @var FilterBuilder|MockObject
- */
- private $filterBuilder;
- /**
- * @var SearchCriteriaBuilder|MockObject
- */
- private $searchCriteriaBuilder;
- /**
- * @var Quote|MockObject
- */
- private $session;
- /**
- * @var StoreManagerInterface|MockObject
- */
- private $storeManager;
- /**
- * @var StoreInterface|MockObject
- */
- private $store;
- /**
- * @var DateTimeFactory|MockObject
- */
- private $dateTimeFactory;
- /**
- * @var Data|MockObject
- */
- private $paymentDataHelper;
- /**
- * @var VaultPaymentInterface|MockObject
- */
- private $vaultPayment;
- /**
- * @var PaymentTokenManagementInterface|MockObject
- */
- private $paymentTokenManagement;
- /**
- * @var OrderRepositoryInterface|MockObject
- */
- private $orderRepository;
- /**
- * @var TokenUiComponentProviderInterface|MockObject
- */
- private $tokenComponentProvider;
- /**
- * @var ObjectManager
- */
- private $objectManager;
- /**
- * @var TokensConfigProvider
- */
- private $configProvider;
- protected function setUp()
- {
- $this->paymentTokenRepository = $this->getMockBuilder(PaymentTokenRepositoryInterface::class)
- ->getMockForAbstractClass();
- $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->session = $this->getMockBuilder(Quote::class)
- ->disableOriginalConstructor()
- ->setMethods(['getCustomerId', 'getReordered'])
- ->getMock();
- $this->dateTimeFactory = $this->getMockBuilder(DateTimeFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->paymentDataHelper = $this->getMockBuilder(Data::class)
- ->disableOriginalConstructor()
- ->setMethods(['getMethodInstance'])
- ->getMock();
- $this->paymentTokenManagement = $this->getMockBuilder(PaymentTokenManagementInterface::class)
- ->getMockForAbstractClass();
- $this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
- ->getMockForAbstractClass();
- $this->vaultPayment = $this->getMockForAbstractClass(VaultPaymentInterface::class);
-
- $this->objectManager = new ObjectManager($this);
- $this->initStoreMock();
- $this->tokenComponentProvider = $this->createMock(TokenUiComponentProviderInterface::class);
- $this->configProvider = new TokensConfigProvider(
- $this->session,
- $this->paymentTokenRepository,
- $this->filterBuilder,
- $this->searchCriteriaBuilder,
- $this->storeManager,
- $this->dateTimeFactory,
- [
- self::VAULT_PROVIDER_CODE => $this->tokenComponentProvider
- ]
- );
- $this->objectManager->setBackwardCompatibleProperty(
- $this->configProvider,
- 'paymentDataHelper',
- $this->paymentDataHelper
- );
- $this->objectManager->setBackwardCompatibleProperty(
- $this->configProvider,
- 'paymentTokenManagement',
- $this->paymentTokenManagement
- );
- $this->objectManager->setBackwardCompatibleProperty(
- $this->configProvider,
- 'orderRepository',
- $this->orderRepository
- );
- }
- /**
- * @covers \Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider::getTokensComponents
- */
- public function testGetTokensComponentsRegisteredCustomer()
- {
- $customerId = 1;
- $this->session->expects(static::once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->paymentDataHelper->expects(static::once())
- ->method('getMethodInstance')
- ->with(self::VAULT_PAYMENT_CODE)
- ->willReturn($this->vaultPayment);
-
- $this->vaultPayment->expects(static::once())
- ->method('isActive')
- ->with(self::STORE_ID)
- ->willReturn(true);
- $this->vaultPayment->expects(static::once())
- ->method('getProviderCode')
- ->willReturn(self::VAULT_PROVIDER_CODE);
- /** @var PaymentTokenInterface|MockObject $token */
- $token = $this->getMockBuilder(PaymentTokenInterface::class)
- ->getMockForAbstractClass();
- $tokenUiComponent = $this->getTokenUiComponentProvider($token);
- $searchCriteria = $this->getSearchCriteria($customerId, self::ENTITY_ID, self::VAULT_PROVIDER_CODE);
- $date = $this->getMockBuilder(\DateTime::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->dateTimeFactory->expects(static::once())
- ->method('create')
- ->with("now", new \DateTimeZone('UTC'))
- ->willReturn($date);
- $date->expects(static::once())
- ->method('format')
- ->with('Y-m-d 00:00:00')
- ->willReturn('2015-01-01 00:00:00');
- $searchResult = $this->getMockBuilder(PaymentTokenSearchResultsInterface::class)
- ->getMockForAbstractClass();
- $this->paymentTokenRepository->expects(self::once())
- ->method('getList')
- ->with($searchCriteria)
- ->willReturn($searchResult);
- $searchResult->expects(self::once())
- ->method('getItems')
- ->willReturn([$token]);
- static::assertEquals([$tokenUiComponent], $this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE));
- }
- /**
- * @covers \Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider::getTokensComponents
- */
- public function testGetTokensComponentsGuestCustomer()
- {
- $customerId = null;
- $this->initStoreMock();
- $this->session->expects(static::once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->paymentDataHelper->expects(static::once())
- ->method('getMethodInstance')
- ->with(self::VAULT_PAYMENT_CODE)
- ->willReturn($this->vaultPayment);
- $this->vaultPayment->expects(static::once())
- ->method('isActive')
- ->with(self::STORE_ID)
- ->willReturn(true);
- $this->vaultPayment->expects(static::once())
- ->method('getProviderCode')
- ->willReturn(self::VAULT_PROVIDER_CODE);
- /** @var PaymentTokenInterface|MockObject $token */
- $token = $this->getMockBuilder(PaymentTokenInterface::class)
- ->getMockForAbstractClass();
- $this->session->expects(static::once())
- ->method('getReordered')
- ->willReturn(self::ORDER_ID);
- $this->orderRepository->expects(static::once())
- ->method('get')
- ->with(self::ORDER_ID)
- ->willReturn($this->getOrderMock());
- $this->paymentTokenManagement->expects(static::once())
- ->method('getByPaymentId')
- ->with(self::ORDER_PAYMENT_ENTITY_ID)
- ->willReturn($token);
- $token->expects(static::once())
- ->method('getEntityId')
- ->willReturn(self::ENTITY_ID);
- $tokenUiComponent = $this->getTokenUiComponentProvider($token);
- $searchCriteria = $this->getSearchCriteria($customerId, self::ENTITY_ID, self::VAULT_PROVIDER_CODE);
- $date = $this->getMockBuilder('DateTime')
- ->disableOriginalConstructor()
- ->getMock();
- $this->dateTimeFactory->expects(static::once())
- ->method('create')
- ->with("now", new \DateTimeZone('UTC'))
- ->willReturn($date);
- $date->expects(static::once())
- ->method('format')
- ->with('Y-m-d 00:00:00')
- ->willReturn('2015-01-01 00:00:00');
- $searchResult = $this->getMockBuilder(PaymentTokenSearchResultsInterface::class)
- ->getMockForAbstractClass();
- $this->paymentTokenRepository->expects(self::once())
- ->method('getList')
- ->with($searchCriteria)
- ->willReturn($searchResult);
- $searchResult->expects(self::once())
- ->method('getItems')
- ->willReturn([$token]);
- static::assertEquals([$tokenUiComponent], $this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE));
- }
- /**
- * @param \Exception $exception
- * @covers \Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider::getTokensComponents
- * @dataProvider getTokensComponentsGuestCustomerExceptionsProvider
- */
- public function testGetTokensComponentsGuestCustomerOrderNotFound($exception)
- {
- $customerId = null;
- $this->session->expects(static::once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->paymentDataHelper->expects(static::once())
- ->method('getMethodInstance')
- ->with(self::VAULT_PAYMENT_CODE)
- ->willReturn($this->vaultPayment);
- $this->vaultPayment->expects(static::once())
- ->method('isActive')
- ->with(self::STORE_ID)
- ->willReturn(true);
- $this->vaultPayment->expects(static::once())
- ->method('getProviderCode')
- ->willReturn(self::VAULT_PROVIDER_CODE);
- $this->session->expects(static::once())
- ->method('getReordered')
- ->willReturn(self::ORDER_ID);
- $this->orderRepository->expects(static::once())
- ->method('get')
- ->with(self::ORDER_ID)
- ->willThrowException($exception);
- $this->filterBuilder->expects(static::once())
- ->method('setField')
- ->with(PaymentTokenInterface::ENTITY_ID)
- ->willReturnSelf();
- $this->filterBuilder->expects(static::never())
- ->method('setValue');
- $this->searchCriteriaBuilder->expects(self::never())
- ->method('addFilters');
- static::assertEmpty($this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE));
- }
- /**
- * Set of catching exception types
- * @return array
- */
- public function getTokensComponentsGuestCustomerExceptionsProvider()
- {
- return [
- [new InputException()],
- [new NoSuchEntityException()],
- ];
- }
- /**
- * @covers \Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider::getTokensComponents
- */
- public function testGetTokensComponentsEmptyComponentProvider()
- {
- $customerId = 1;
- $this->session->expects(static::once())
- ->method('getCustomerId')
- ->willReturn($customerId);
- $this->initStoreMock();
- $this->paymentDataHelper->expects(static::once())
- ->method('getMethodInstance')
- ->with(self::VAULT_PAYMENT_CODE)
- ->willReturn($this->vaultPayment);
- $this->vaultPayment->expects(static::once())
- ->method('isActive')
- ->with(self::STORE_ID)
- ->willReturn(false);
- $this->paymentTokenRepository->expects(static::never())
- ->method('getList');
- $configProvider = new TokensConfigProvider(
- $this->session,
- $this->paymentTokenRepository,
- $this->filterBuilder,
- $this->searchCriteriaBuilder,
- $this->storeManager,
- $this->dateTimeFactory
- );
- $this->objectManager->setBackwardCompatibleProperty(
- $configProvider,
- 'paymentDataHelper',
- $this->paymentDataHelper
- );
- static::assertEmpty($configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE));
- }
- /**
- * @covers \Magento\Vault\Model\Ui\Adminhtml\TokensConfigProvider::getTokensComponents
- */
- public function testGetTokensComponentsForGuestCustomerWithoutStoredTokens()
- {
- $this->session->expects(static::once())
- ->method('getCustomerId')
- ->willReturn(null);
- $this->paymentDataHelper->expects(static::once())
- ->method('getMethodInstance')
- ->with(self::VAULT_PAYMENT_CODE)
- ->willReturn($this->vaultPayment);
- $this->vaultPayment->expects(static::once())
- ->method('isActive')
- ->with(self::STORE_ID)
- ->willReturn(true);
- $this->vaultPayment->expects(static::once())
- ->method('getProviderCode')
- ->willReturn(self::VAULT_PROVIDER_CODE);
- $this->session->expects(static::once())
- ->method('getReordered')
- ->willReturn(self::ORDER_ID);
- $this->orderRepository->expects(static::once())
- ->method('get')
- ->with(self::ORDER_ID)
- ->willReturn($this->getOrderMock());
- $this->paymentTokenManagement->expects(static::once())
- ->method('getByPaymentId')
- ->with(self::ORDER_PAYMENT_ENTITY_ID)
- ->willReturn(null);
- $this->filterBuilder->expects(static::once())
- ->method('setField')
- ->with(PaymentTokenInterface::ENTITY_ID)
- ->willReturnSelf();
- $this->filterBuilder->expects(static::never())
- ->method('setValue');
- $this->searchCriteriaBuilder->expects(static::never())
- ->method('addFilters');
- static::assertEmpty($this->configProvider->getTokensComponents(self::VAULT_PAYMENT_CODE));
- }
- /**
- * Create mock object for store
- */
- private function initStoreMock()
- {
- $this->store = $this->createMock(StoreInterface::class);
- $this->store->expects(static::any())
- ->method('getId')
- ->willReturn(self::STORE_ID);
- $this->storeManager = $this->createMock(StoreManagerInterface::class);
- $this->storeManager->expects(static::any())
- ->method('getStore')
- ->with(null)
- ->willReturn($this->store);
- }
- /**
- * Returns order mock with order payment mock
- * @return OrderInterface
- */
- private function getOrderMock()
- {
- /** @var OrderInterface|MockObject $order */
- $order = $this->getMockBuilder(OrderInterface::class)
- ->getMockForAbstractClass();
- /** @var OrderPaymentInterface|MockObject $orderPayment */
- $orderPayment = $this->getMockBuilder(OrderPaymentInterface::class)
- ->getMockForAbstractClass();
- $order->expects(static::once())
- ->method('getPayment')
- ->willReturn($orderPayment);
- $orderPayment->expects(static::once())
- ->method('getEntityId')
- ->willReturn(self::ORDER_PAYMENT_ENTITY_ID);
- return $order;
- }
- /**
- * Get mock for token ui component provider
- * @param PaymentTokenInterface $token
- * @return TokenUiComponentInterface|MockObject
- */
- private function getTokenUiComponentProvider($token)
- {
- $tokenUiComponent = $this->createMock(TokenUiComponentInterface::class);
- $this->tokenComponentProvider->expects(static::once())
- ->method('getComponentForToken')
- ->with($token)
- ->willReturn($tokenUiComponent);
- return $tokenUiComponent;
- }
- /**
- * @param string $field
- * @param mixed $value
- * @param int $atIndex
- *
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- private function createExpectedFilter($field, $value, $atIndex)
- {
- $filterObject = $this->getMockBuilder(Filter::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
- ->method('setField')
- ->with($field)
- ->willReturnSelf();
- $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
- ->method('setValue')
- ->with($value)
- ->willReturnSelf();
- $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
- ->method('create')
- ->willReturn($filterObject);
- return $filterObject;
- }
- /**
- * Build search criteria
- * @param int $customerId
- * @param int $entityId
- * @param string $vaultProviderCode
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- private function getSearchCriteria($customerId, $entityId, $vaultProviderCode)
- {
- $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
- ->disableOriginalConstructor()
- ->getMock();
- $customerFilter = $customerId ? $this->createExpectedFilter(PaymentTokenInterface::CUSTOMER_ID, $customerId, 0)
- : $this->createExpectedFilter(PaymentTokenInterface::ENTITY_ID, $entityId, 0);
- $codeFilter = $this->createExpectedFilter(
- PaymentTokenInterface::PAYMENT_METHOD_CODE,
- $vaultProviderCode,
- 1
- );
- $isActiveFilter = $this->createExpectedFilter(PaymentTokenInterface::IS_ACTIVE, 1, 2);
- // express at expectations
- $expiresAtFilter = $this->createExpectedFilter(
- PaymentTokenInterface::EXPIRES_AT,
- '2015-01-01 00:00:00',
- 3
- );
- $this->filterBuilder->expects(static::once())
- ->method('setConditionType')
- ->with('gt')
- ->willReturnSelf();
- $this->searchCriteriaBuilder->expects(self::exactly(4))
- ->method('addFilters')
- ->willReturnMap(
- [
- [$customerFilter, $this->searchCriteriaBuilder],
- [$codeFilter, $this->searchCriteriaBuilder],
- [$expiresAtFilter, $this->searchCriteriaBuilder],
- [$isActiveFilter, $this->searchCriteriaBuilder],
- ]
- );
- $this->searchCriteriaBuilder->expects(self::once())
- ->method('create')
- ->willReturn($searchCriteria);
- return $searchCriteria;
- }
- }
|