ValidationTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CheckoutAgreements\Test\Unit\Model\Checkout\Plugin;
  7. use Magento\CheckoutAgreements\Model\AgreementsProvider;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * Class ValidationTest
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class ValidationTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var \Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation
  17. */
  18. protected $model;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $agreementsValidatorMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $subjectMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $paymentMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $addressMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $extensionAttributesMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $scopeConfigMock;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $checkoutAgreementsListMock;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $agreementsFilterMock;
  51. protected function setUp()
  52. {
  53. $this->agreementsValidatorMock = $this->createMock(\Magento\Checkout\Api\AgreementsValidatorInterface::class);
  54. $this->subjectMock = $this->createMock(\Magento\Checkout\Api\PaymentInformationManagementInterface::class);
  55. $this->paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
  56. $this->addressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
  57. $this->extensionAttributesMock = $this->createPartialMock(
  58. \Magento\Quote\Api\Data\PaymentExtension::class,
  59. ['getAgreementIds']
  60. );
  61. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  62. $this->checkoutAgreementsListMock = $this->createMock(
  63. \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::class
  64. );
  65. $this->agreementsFilterMock = $this->createMock(
  66. \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter::class
  67. );
  68. $this->model = new \Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation(
  69. $this->agreementsValidatorMock,
  70. $this->scopeConfigMock,
  71. $this->checkoutAgreementsListMock,
  72. $this->agreementsFilterMock
  73. );
  74. }
  75. public function testBeforeSavePaymentInformationAndPlaceOrder()
  76. {
  77. $cartId = 100;
  78. $agreements = [1, 2, 3];
  79. $this->scopeConfigMock
  80. ->expects($this->once())
  81. ->method('isSetFlag')
  82. ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
  83. ->willReturn(true);
  84. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  85. $this->agreementsFilterMock->expects($this->once())
  86. ->method('buildSearchCriteria')
  87. ->willReturn($searchCriteriaMock);
  88. $this->checkoutAgreementsListMock->expects($this->once())
  89. ->method('getList')
  90. ->with($searchCriteriaMock)
  91. ->willReturn([1]);
  92. $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
  93. $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
  94. $this->paymentMock->expects(static::atLeastOnce())
  95. ->method('getExtensionAttributes')
  96. ->willReturn($this->extensionAttributesMock);
  97. $this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
  98. }
  99. /**
  100. * @expectedException \Magento\Framework\Exception\CouldNotSaveException
  101. */
  102. public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid()
  103. {
  104. $cartId = 100;
  105. $agreements = [1, 2, 3];
  106. $this->scopeConfigMock
  107. ->expects($this->once())
  108. ->method('isSetFlag')
  109. ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
  110. ->willReturn(true);
  111. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  112. $this->agreementsFilterMock->expects($this->once())
  113. ->method('buildSearchCriteria')
  114. ->willReturn($searchCriteriaMock);
  115. $this->checkoutAgreementsListMock->expects($this->once())
  116. ->method('getList')
  117. ->with($searchCriteriaMock)
  118. ->willReturn([1]);
  119. $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
  120. $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
  121. $this->paymentMock->expects(static::atLeastOnce())
  122. ->method('getExtensionAttributes')
  123. ->willReturn($this->extensionAttributesMock);
  124. $this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
  125. $this->expectExceptionMessage(
  126. "The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
  127. );
  128. }
  129. public function testBeforeSavePaymentInformation()
  130. {
  131. $cartId = 100;
  132. $agreements = [1, 2, 3];
  133. $this->scopeConfigMock
  134. ->expects($this->once())
  135. ->method('isSetFlag')
  136. ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
  137. ->willReturn(true);
  138. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  139. $this->agreementsFilterMock->expects($this->once())
  140. ->method('buildSearchCriteria')
  141. ->willReturn($searchCriteriaMock);
  142. $this->checkoutAgreementsListMock->expects($this->once())
  143. ->method('getList')
  144. ->with($searchCriteriaMock)
  145. ->willReturn([1]);
  146. $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
  147. $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
  148. $this->paymentMock->expects(static::atLeastOnce())
  149. ->method('getExtensionAttributes')
  150. ->willReturn($this->extensionAttributesMock);
  151. $this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
  152. }
  153. }