PaymentTokenManagementTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Test\Unit\Model;
  7. use Magento\Framework\Api\Filter;
  8. use Magento\Framework\Api\FilterBuilder;
  9. use Magento\Framework\Api\SearchCriteria;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\Framework\Encryption\EncryptorInterface;
  12. use Magento\Framework\Intl\DateTimeFactory;
  13. use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
  14. use Magento\Sales\Api\Data\OrderPaymentInterface;
  15. use Magento\Vault\Api\Data\PaymentTokenInterface;
  16. use Magento\Vault\Api\Data\PaymentTokenSearchResultsInterface;
  17. use Magento\Vault\Api\Data\PaymentTokenSearchResultsInterfaceFactory;
  18. use Magento\Vault\Api\PaymentTokenRepositoryInterface;
  19. use Magento\Vault\Model\PaymentTokenFactory;
  20. use Magento\Vault\Model\PaymentTokenManagement;
  21. use Magento\Vault\Model\ResourceModel\PaymentToken as PaymentTokenResourceModel;
  22. /**
  23. * Class PaymentTokenManagementTest
  24. *
  25. * @see \Magento\Vault\Model\PaymentTokenManagement
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class PaymentTokenManagementTest extends \PHPUnit\Framework\TestCase
  29. {
  30. /**
  31. * @var PaymentTokenManagement
  32. */
  33. private $paymentTokenManagement;
  34. /**
  35. * @var PaymentTokenRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $paymentTokenRepository;
  38. /**
  39. * @var PaymentTokenResourceModel|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $paymentTokenResourceModel;
  42. /**
  43. * @var PaymentTokenResourceModel|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $resourceModel;
  46. /**
  47. * @var PaymentTokenFactory|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $paymentTokenFactory;
  50. /**
  51. * @var PaymentTokenSearchResultsInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. private $searchResultsFactory;
  54. /**
  55. * @var FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. private $filterBuilder;
  58. /**
  59. * @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. private $searchCriteriaBuilder;
  62. /**
  63. * @var EncryptorInterface|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. private $encryptor;
  66. /**
  67. * @var DateTimeFactory|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. private $dateTimeFactory;
  70. /**
  71. * Set up
  72. */
  73. protected function setUp()
  74. {
  75. $this->paymentTokenRepository = $this->getMockBuilder(PaymentTokenRepositoryInterface::class)
  76. ->getMockForAbstractClass();
  77. $this->paymentTokenResourceModel = $this->getMockBuilder(PaymentTokenResourceModel::class)
  78. ->disableOriginalConstructor()
  79. ->getMock();
  80. $this->resourceModel = $this->getMockBuilder(PaymentTokenResourceModel::class)
  81. ->disableOriginalConstructor()
  82. ->getMock();
  83. $this->paymentTokenFactory = $this->getMockBuilder(PaymentTokenFactory::class)
  84. ->setMethods(['create'])
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $this->searchResultsFactory = $this->getMockBuilder(PaymentTokenSearchResultsInterfaceFactory::class)
  88. ->setMethods(['create'])
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $this->searchCriteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class)
  95. ->disableOriginalConstructor()
  96. ->getMock();
  97. $this->encryptor = $this->createMock(EncryptorInterface::class);
  98. $this->dateTimeFactory = $this->getMockBuilder(DateTimeFactory::class)
  99. ->disableOriginalConstructor()
  100. ->getMock();
  101. $this->paymentTokenManagement = new PaymentTokenManagement(
  102. $this->paymentTokenRepository,
  103. $this->paymentTokenResourceModel,
  104. $this->paymentTokenFactory,
  105. $this->filterBuilder,
  106. $this->searchCriteriaBuilder,
  107. $this->searchResultsFactory,
  108. $this->encryptor,
  109. $this->dateTimeFactory
  110. );
  111. }
  112. /**
  113. * Run test for getListByCustomerId method
  114. */
  115. public function testGetListByCustomerId()
  116. {
  117. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $tokenMock */
  118. $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
  119. ->getMockForAbstractClass();
  120. /** @var Filter| $filterMock */
  121. $filterMock = $this->getMockBuilder(Filter::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $searchResult = $this->getMockBuilder(PaymentTokenSearchResultsInterface::class)
  128. ->getMockForAbstractClass();
  129. $this->filterBuilder->expects(self::once())
  130. ->method('setField')
  131. ->with(PaymentTokenInterface::CUSTOMER_ID)
  132. ->willReturnSelf();
  133. $this->filterBuilder->expects(self::once())
  134. ->method('setValue')
  135. ->with(1)
  136. ->willReturnSelf();
  137. $this->filterBuilder->expects(self::once())
  138. ->method('create')
  139. ->willReturn($filterMock);
  140. $this->searchCriteriaBuilder->expects(self::once())
  141. ->method('addFilters')
  142. ->with([$filterMock])
  143. ->willReturnSelf();
  144. $this->searchCriteriaBuilder->expects(self::once())
  145. ->method('create')
  146. ->willReturn($searchCriteria);
  147. $this->paymentTokenRepository->expects(self::once())
  148. ->method('getList')
  149. ->with($searchCriteria)
  150. ->willReturn($searchResult);
  151. $searchResult->expects(self::once())
  152. ->method('getItems')
  153. ->willReturn([$tokenMock]);
  154. self::assertEquals([$tokenMock], $this->paymentTokenManagement->getListByCustomerId(1));
  155. }
  156. /**
  157. * Run test for getByPaymentId method
  158. */
  159. public function testGetByPaymentId()
  160. {
  161. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $tokenMock */
  162. $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
  163. ->getMockForAbstractClass();
  164. $this->paymentTokenResourceModel->expects(self::once())
  165. ->method('getByOrderPaymentId')
  166. ->with(1)
  167. ->willReturn(['token-data']);
  168. $this->paymentTokenFactory->expects(self::once())
  169. ->method('create')
  170. ->with(['data' => ['token-data']])
  171. ->willReturn($tokenMock);
  172. self::assertEquals($tokenMock, $this->paymentTokenManagement->getByPaymentId(1));
  173. }
  174. /**
  175. * Run test for getByPaymentId method (return NULL)
  176. */
  177. public function testGetByPaymentIdNull()
  178. {
  179. $this->paymentTokenResourceModel->expects(self::once())
  180. ->method('getByOrderPaymentId')
  181. ->with(1)
  182. ->willReturn([]);
  183. $this->paymentTokenFactory->expects(self::never())
  184. ->method('create');
  185. self::assertEquals(null, $this->paymentTokenManagement->getByPaymentId(1));
  186. }
  187. /**
  188. * Run test for getByGatewayToken method
  189. */
  190. public function testGetByGatewayToken()
  191. {
  192. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $tokenMock */
  193. $tokenMock = $this->getMockBuilder(PaymentTokenInterface::class)
  194. ->getMockForAbstractClass();
  195. $this->paymentTokenResourceModel->expects(self::once())
  196. ->method('getByGatewayToken')
  197. ->with('token', 1, 1)
  198. ->willReturn(['token-data']);
  199. $this->paymentTokenFactory->expects(self::once())
  200. ->method('create')
  201. ->with(['data' => ['token-data']])
  202. ->willReturn($tokenMock);
  203. self::assertEquals($tokenMock, $this->paymentTokenManagement->getByGatewayToken('token', 1, 1));
  204. }
  205. /**
  206. * Run test for getByGatewayToken method (return NULL)
  207. */
  208. public function testGetByGatewayTokenNull()
  209. {
  210. $this->paymentTokenResourceModel->expects(self::once())
  211. ->method('getByGatewayToken')
  212. ->with('some-not-exists-token', 1, 1)
  213. ->willReturn([]);
  214. $this->paymentTokenFactory->expects(self::never())
  215. ->method('create');
  216. self::assertEquals(null, $this->paymentTokenManagement->getByGatewayToken('some-not-exists-token', 1, 1));
  217. }
  218. /**
  219. * Run test for getByGatewayToken method (return NULL)
  220. */
  221. public function testGetByPublicHash()
  222. {
  223. $this->paymentTokenResourceModel->expects(self::once())
  224. ->method('getByPublicHash')
  225. ->with('some-not-exists-token', 1)
  226. ->willReturn([]);
  227. $this->paymentTokenFactory->expects(self::never())
  228. ->method('create');
  229. self::assertEquals(null, $this->paymentTokenManagement->getByPublicHash('some-not-exists-token', 1));
  230. }
  231. /**
  232. * Run test for saveTokenWithPaymentLink method
  233. */
  234. public function testSaveTokenWithPaymentLinkNoDuplicate()
  235. {
  236. /** @var OrderPaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentMock */
  237. $paymentMock = $this->createMock(OrderPaymentInterface::class);
  238. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $tokenMock */
  239. $tokenMock = $this->createMock(PaymentTokenInterface::class);
  240. $customerId = 1;
  241. $entityId = 1;
  242. $publicHash = 'some-not-existing-token';
  243. $paymentId = 1;
  244. $tokenMock->expects(static::atLeastOnce())
  245. ->method('getPublicHash')
  246. ->willReturn($publicHash);
  247. $tokenMock->expects(static::atLeastOnce())
  248. ->method('getCustomerId')
  249. ->willReturn($customerId);
  250. $this->paymentTokenResourceModel->expects(self::once())
  251. ->method('getByPublicHash')
  252. ->with($publicHash, 1)
  253. ->willReturn([]);
  254. $this->paymentTokenFactory->expects(self::never())
  255. ->method('create');
  256. $tokenMock->expects(self::once())
  257. ->method('getEntityId')
  258. ->willReturn($entityId);
  259. $this->paymentTokenRepository->expects(self::once())
  260. ->method('save')
  261. ->with($tokenMock);
  262. $paymentMock->expects(self::once())
  263. ->method('getEntityId')
  264. ->willReturn($paymentId);
  265. $this->paymentTokenResourceModel->expects(static::once())
  266. ->method('addLinkToOrderPayment')
  267. ->with($entityId, $paymentId);
  268. $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
  269. }
  270. /**
  271. * Run test for saveTokenWithPaymentLink method
  272. */
  273. public function testSaveTokenWithPaymentLinkWithDuplicateTokenVisible()
  274. {
  275. /** @var OrderPaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentMock */
  276. $paymentMock = $this->createMock(OrderPaymentInterface::class);
  277. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $tokenMock */
  278. $tokenMock = $this->createMock(PaymentTokenInterface::class);
  279. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $duplicateToken */
  280. $duplicateToken = $this->createMock(PaymentTokenInterface::class);
  281. $entityId = 1;
  282. $customerId = 1;
  283. $paymentId = 1;
  284. $publicHash = 'existing-token';
  285. $duplicateTokenData = [
  286. 'entity_id' => $entityId
  287. ];
  288. $tokenMock->expects(static::atLeastOnce())
  289. ->method('getPublicHash')
  290. ->willReturn($publicHash);
  291. $tokenMock->expects(static::atLeastOnce())
  292. ->method('getCustomerId')
  293. ->willReturn($customerId);
  294. $this->paymentTokenResourceModel->expects(self::once())
  295. ->method('getByPublicHash')
  296. ->with($publicHash, $customerId)
  297. ->willReturn($duplicateTokenData);
  298. $this->paymentTokenFactory->expects(self::once())
  299. ->method('create')
  300. ->with(['data' => $duplicateTokenData])
  301. ->willReturn($duplicateToken);
  302. $tokenMock->expects(static::once())
  303. ->method('getIsVisible')
  304. ->willReturn(true);
  305. $duplicateToken->expects(static::once())
  306. ->method('getEntityId')
  307. ->willReturn($entityId);
  308. $tokenMock->expects(self::once())
  309. ->method('getEntityId')
  310. ->willReturn($entityId);
  311. $this->paymentTokenRepository->expects(self::once())
  312. ->method('save')
  313. ->with($tokenMock);
  314. $paymentMock->expects(self::once())
  315. ->method('getEntityId')
  316. ->willReturn($paymentId);
  317. $this->paymentTokenResourceModel->expects(static::once())
  318. ->method('addLinkToOrderPayment')
  319. ->with($entityId, $paymentId);
  320. $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
  321. }
  322. /**
  323. * Run test for saveTokenWithPaymentLink method
  324. */
  325. public function testSaveTokenWithPaymentLinkWithDuplicateTokenNotVisible()
  326. {
  327. /** @var OrderPaymentInterface|\PHPUnit_Framework_MockObject_MockObject $paymentMock */
  328. $paymentMock = $this->createMock(OrderPaymentInterface::class);
  329. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $tokenMock */
  330. $tokenMock = $this->createMock(PaymentTokenInterface::class);
  331. /** @var PaymentTokenInterface|\PHPUnit_Framework_MockObject_MockObject $duplicateToken */
  332. $duplicateToken = $this->createMock(PaymentTokenInterface::class);
  333. $entityId = 1;
  334. $newEntityId = 1;
  335. $paymentId = 1;
  336. $customerId = 1;
  337. $gatewayToken = 'xs4vf3';
  338. $publicHash = 'existing-token';
  339. $duplicateTokenData = [
  340. 'entity_id' => $entityId
  341. ];
  342. $newHash = 'new-token2';
  343. $tokenMock->expects(static::atLeastOnce())
  344. ->method('getPublicHash')
  345. ->willReturn($publicHash);
  346. $tokenMock->expects(static::atLeastOnce())
  347. ->method('getCustomerId')
  348. ->willReturn($customerId);
  349. $this->paymentTokenResourceModel->expects(self::once())
  350. ->method('getByPublicHash')
  351. ->with($publicHash, $customerId)
  352. ->willReturn($duplicateTokenData);
  353. $this->paymentTokenFactory->expects(self::once())
  354. ->method('create')
  355. ->with(['data' => $duplicateTokenData])
  356. ->willReturn($duplicateToken);
  357. $tokenMock->expects(static::atLeastOnce())
  358. ->method('getIsVisible')
  359. ->willReturn(false);
  360. $tokenMock->expects(static::atLeastOnce())
  361. ->method('getCustomerId')
  362. ->willReturn($customerId);
  363. $tokenMock->expects(static::atLeastOnce())
  364. ->method('getGatewayToken')
  365. ->willReturn($gatewayToken);
  366. $this->encryptor->expects(static::once())
  367. ->method('getHash')
  368. ->with($publicHash . $gatewayToken)
  369. ->willReturn($newHash);
  370. $tokenMock->expects(static::once())
  371. ->method('setPublicHash')
  372. ->with($newHash);
  373. $this->paymentTokenRepository->expects(self::once())
  374. ->method('save')
  375. ->with($tokenMock);
  376. $tokenMock->expects(static::atLeastOnce())
  377. ->method('getEntityId')
  378. ->willReturn($newEntityId);
  379. $paymentMock->expects(self::atLeastOnce())
  380. ->method('getEntityId')
  381. ->willReturn($paymentId);
  382. $this->paymentTokenResourceModel->expects(static::once())
  383. ->method('addLinkToOrderPayment')
  384. ->with($newEntityId, $paymentId);
  385. $this->paymentTokenManagement->saveTokenWithPaymentLink($tokenMock, $paymentMock);
  386. }
  387. public function testGetVisibleAvailableTokens()
  388. {
  389. $customerId = 1;
  390. $searchCriteria = $this->getMockBuilder(SearchCriteria::class)
  391. ->disableOriginalConstructor()
  392. ->getMock();
  393. $searchResult = $this->getMockForAbstractClass(PaymentTokenSearchResultsInterface::class);
  394. $token = $this->getMockForAbstractClass(PaymentTokenInterface::class);
  395. $customerFilter = $this->createExpectedFilter(PaymentTokenInterface::CUSTOMER_ID, $customerId, 0);
  396. $visibilityFilter = $this->createExpectedFilter(PaymentTokenInterface::IS_VISIBLE, true, 1);
  397. $isActiveFilter = $this->createExpectedFilter(PaymentTokenInterface::IS_ACTIVE, true, 2);
  398. // express at expectations
  399. $expiresAtFilter = $this->createExpectedFilter(
  400. PaymentTokenInterface::EXPIRES_AT,
  401. '2015-01-01 00:00:00',
  402. 3
  403. );
  404. $this->filterBuilder->expects(static::once())
  405. ->method('setConditionType')
  406. ->with('gt')
  407. ->willReturnSelf();
  408. $date = $this->getMockBuilder(\DateTime::class)
  409. ->disableOriginalConstructor()
  410. ->getMock();
  411. $this->dateTimeFactory->expects(static::once())
  412. ->method('create')
  413. ->with("now", new \DateTimeZone('UTC'))
  414. ->willReturn($date);
  415. $date->expects(static::once())
  416. ->method('format')
  417. ->with('Y-m-d 00:00:00')
  418. ->willReturn('2015-01-01 00:00:00');
  419. $this->searchCriteriaBuilder->expects(self::exactly(4))
  420. ->method('addFilters')
  421. ->withConsecutive($customerFilter, $visibilityFilter, $isActiveFilter, $expiresAtFilter)
  422. ->willReturnSelf();
  423. $this->searchCriteriaBuilder->expects(self::once())
  424. ->method('create')
  425. ->willReturn($searchCriteria);
  426. $this->paymentTokenRepository->expects(self::once())
  427. ->method('getList')
  428. ->with($searchCriteria)
  429. ->willReturn($searchResult);
  430. $searchResult->expects(self::once())
  431. ->method('getItems')
  432. ->willReturn([$token]);
  433. static::assertEquals(
  434. [$token],
  435. $this->paymentTokenManagement->getVisibleAvailableTokens($customerId)
  436. );
  437. }
  438. /**
  439. * @param string $field
  440. * @param mixed $value
  441. * @param int $atIndex
  442. *
  443. * @return \PHPUnit_Framework_MockObject_MockObject
  444. */
  445. private function createExpectedFilter($field, $value, $atIndex)
  446. {
  447. $filterObject = $this->getMockBuilder(Filter::class)
  448. ->disableOriginalConstructor()
  449. ->getMock();
  450. $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
  451. ->method('setField')
  452. ->with($field)
  453. ->willReturnSelf();
  454. $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
  455. ->method('setValue')
  456. ->with($value)
  457. ->willReturnSelf();
  458. $this->filterBuilder->expects(new MethodInvokedAtIndex($atIndex))
  459. ->method('create')
  460. ->willReturn($filterObject);
  461. return $filterObject;
  462. }
  463. }