|
| 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 | + |
| 8 | +use Magento\CheckoutAgreements\Model\AgreementsProvider; |
| 9 | +use Magento\Store\Model\ScopeInterface; |
| 10 | + |
| 11 | +class GuestValidationTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var \Magento\CheckoutAgreements\Model\Checkout\Plugin\GuestValidation |
| 15 | + */ |
| 16 | + private $model; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 20 | + */ |
| 21 | + private $agreementsValidatorMock; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + private $subjectMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + private $paymentMock; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + private $addressMock; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + private $extensionAttributesMock; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + private $repositoryMock; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 50 | + */ |
| 51 | + private $scopeConfigMock; |
| 52 | + |
| 53 | + protected function setUp() |
| 54 | + { |
| 55 | + $this->agreementsValidatorMock = $this->getMock(\Magento\Checkout\Api\AgreementsValidatorInterface::class); |
| 56 | + $this->subjectMock = $this->getMock(\Magento\Checkout\Api\GuestPaymentInformationManagementInterface::class); |
| 57 | + $this->paymentMock = $this->getMock(\Magento\Quote\Api\Data\PaymentInterface::class); |
| 58 | + $this->addressMock = $this->getMock(\Magento\Quote\Api\Data\AddressInterface::class); |
| 59 | + $this->extensionAttributesMock = $this->getMock( |
| 60 | + \Magento\Quote\Api\Data\PaymentExtension::class, |
| 61 | + ['getAgreementIds'], |
| 62 | + [], |
| 63 | + '', |
| 64 | + false |
| 65 | + ); |
| 66 | + $this->scopeConfigMock = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class); |
| 67 | + $this->repositoryMock = $this->getMock( |
| 68 | + \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface::class |
| 69 | + ); |
| 70 | + |
| 71 | + $this->model = new \Magento\CheckoutAgreements\Model\Checkout\Plugin\GuestValidation( |
| 72 | + $this->agreementsValidatorMock, |
| 73 | + $this->scopeConfigMock, |
| 74 | + $this->repositoryMock |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function testBeforeSavePaymentInformationAndPlaceOrder() |
| 79 | + { |
| 80 | + $cartId = '100'; |
| 81 | + $email = 'email@example.com'; |
| 82 | + $agreements = [1, 2, 3]; |
| 83 | + $this->scopeConfigMock |
| 84 | + ->expects($this->once()) |
| 85 | + ->method('isSetFlag') |
| 86 | + ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE) |
| 87 | + ->willReturn(true); |
| 88 | + $this->repositoryMock->expects($this->once())->method('getList')->willReturn([1]); |
| 89 | + $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements); |
| 90 | + $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true); |
| 91 | + $this->paymentMock->expects(static::atLeastOnce()) |
| 92 | + ->method('getExtensionAttributes') |
| 93 | + ->willReturn($this->extensionAttributesMock); |
| 94 | + $this->model->beforeSavePaymentInformation( |
| 95 | + $this->subjectMock, |
| 96 | + $cartId, |
| 97 | + $email, |
| 98 | + $this->paymentMock, |
| 99 | + $this->addressMock |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @expectedException \Magento\Framework\Exception\CouldNotSaveException |
| 105 | + * @expectedExceptionMessage Please agree to all the terms and conditions before placing the order. |
| 106 | + */ |
| 107 | + public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotValid() |
| 108 | + { |
| 109 | + $cartId = 100; |
| 110 | + $email = 'email@example.com'; |
| 111 | + $agreements = [1, 2, 3]; |
| 112 | + $this->scopeConfigMock |
| 113 | + ->expects($this->once()) |
| 114 | + ->method('isSetFlag') |
| 115 | + ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE) |
| 116 | + ->willReturn(true); |
| 117 | + $this->repositoryMock->expects($this->once())->method('getList')->willReturn([1]); |
| 118 | + $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements); |
| 119 | + $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(false); |
| 120 | + $this->paymentMock->expects(static::atLeastOnce()) |
| 121 | + ->method('getExtensionAttributes') |
| 122 | + ->willReturn($this->extensionAttributesMock); |
| 123 | + $this->model->beforeSavePaymentInformation( |
| 124 | + $this->subjectMock, |
| 125 | + $cartId, |
| 126 | + $email, |
| 127 | + $this->paymentMock, |
| 128 | + $this->addressMock |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + public function testBeforeSavePaymentInformation() |
| 133 | + { |
| 134 | + $cartId = 100; |
| 135 | + $email = 'email@example.com'; |
| 136 | + $agreements = [1, 2, 3]; |
| 137 | + $this->scopeConfigMock |
| 138 | + ->expects($this->once()) |
| 139 | + ->method('isSetFlag') |
| 140 | + ->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE) |
| 141 | + ->willReturn(true); |
| 142 | + $this->repositoryMock->expects($this->once())->method('getList')->willReturn([1]); |
| 143 | + $this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements); |
| 144 | + $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true); |
| 145 | + $this->paymentMock->expects(static::atLeastOnce()) |
| 146 | + ->method('getExtensionAttributes') |
| 147 | + ->willReturn($this->extensionAttributesMock); |
| 148 | + $this->model->beforeSavePaymentInformation( |
| 149 | + $this->subjectMock, |
| 150 | + $cartId, |
| 151 | + $email, |
| 152 | + $this->paymentMock, |
| 153 | + $this->addressMock |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments