123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\CheckoutAgreements\Test\Unit\Model\Checkout\Plugin;
- use Magento\CheckoutAgreements\Model\AgreementsProvider;
- use Magento\Store\Model\ScopeInterface;
- /**
- * Class ValidationTest
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ValidationTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation
- */
- protected $model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $agreementsValidatorMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $subjectMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $paymentMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $extensionAttributesMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $scopeConfigMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $checkoutAgreementsListMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- private $agreementsFilterMock;
- protected function setUp()
- {
- $this->agreementsValidatorMock = $this->createMock(\Magento\Checkout\Api\AgreementsValidatorInterface::class);
- $this->subjectMock = $this->createMock(\Magento\Checkout\Api\PaymentInformationManagementInterface::class);
- $this->paymentMock = $this->createMock(\Magento\Quote\Api\Data\PaymentInterface::class);
- $this->addressMock = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
- $this->extensionAttributesMock = $this->createPartialMock(
- \Magento\Quote\Api\Data\PaymentExtension::class,
- ['getAgreementIds']
- );
- $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
- $this->checkoutAgreementsListMock = $this->createMock(
- \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::class
- );
- $this->agreementsFilterMock = $this->createMock(
- \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter::class
- );
- $this->model = new \Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation(
- $this->agreementsValidatorMock,
- $this->scopeConfigMock,
- $this->checkoutAgreementsListMock,
- $this->agreementsFilterMock
- );
- }
- public function testBeforeSavePaymentInformationAndPlaceOrder()
- {
- $cartId = 100;
- $agreements = [1, 2, 3];
- $this->scopeConfigMock
- ->expects($this->once())
- ->method('isSetFlag')
- ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
- ->willReturn(true);
- $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
- $this->agreementsFilterMock->expects($this->once())
- ->method('buildSearchCriteria')
- ->willReturn($searchCriteriaMock);
- $this->checkoutAgreementsListMock->expects($this->once())
- ->method('getList')
- ->with($searchCriteriaMock)
- ->willReturn([1]);
- $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
- $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
- $this->paymentMock->expects(static::atLeastOnce())
- ->method('getExtensionAttributes')
- ->willReturn($this->extensionAttributesMock);
- $this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- */
- public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid()
- {
- $cartId = 100;
- $agreements = [1, 2, 3];
- $this->scopeConfigMock
- ->expects($this->once())
- ->method('isSetFlag')
- ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
- ->willReturn(true);
- $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
- $this->agreementsFilterMock->expects($this->once())
- ->method('buildSearchCriteria')
- ->willReturn($searchCriteriaMock);
- $this->checkoutAgreementsListMock->expects($this->once())
- ->method('getList')
- ->with($searchCriteriaMock)
- ->willReturn([1]);
- $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
- $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false);
- $this->paymentMock->expects(static::atLeastOnce())
- ->method('getExtensionAttributes')
- ->willReturn($this->extensionAttributesMock);
- $this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
- $this->expectExceptionMessage(
- "The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
- );
- }
- public function testBeforeSavePaymentInformation()
- {
- $cartId = 100;
- $agreements = [1, 2, 3];
- $this->scopeConfigMock
- ->expects($this->once())
- ->method('isSetFlag')
- ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
- ->willReturn(true);
- $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
- $this->agreementsFilterMock->expects($this->once())
- ->method('buildSearchCriteria')
- ->willReturn($searchCriteriaMock);
- $this->checkoutAgreementsListMock->expects($this->once())
- ->method('getList')
- ->with($searchCriteriaMock)
- ->willReturn([1]);
- $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
- $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
- $this->paymentMock->expects(static::atLeastOnce())
- ->method('getExtensionAttributes')
- ->willReturn($this->extensionAttributesMock);
- $this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
- }
- }
|