diff --git a/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php b/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php index 0d5f9de2e47c25c3261c4322fe94226c12058b70..9e9227adffd3487bf3f12b21966caee4fedd922d 100644 --- a/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php +++ b/app/code/Magento/Tax/Model/App/Action/ContextPlugin.php @@ -84,7 +84,8 @@ class ContextPlugin \Closure $proceed, \Magento\Framework\App\RequestInterface $request ) { - if (!$this->moduleManager->isEnabled('Magento_PageCache') || + if (!$this->customerSession->isLoggedIn() || + !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled() || !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) { return $proceed($request); diff --git a/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php b/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php index 66ad1afdbcad98f745caad2d25bc55af4ab53a91..a220173520494df90e106aa9c76734c667a83024 100644 --- a/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php +++ b/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php @@ -12,6 +12,16 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase */ protected $taxHelperMock; + /** + * @var \Magento\Weee\Helper\Data + */ + protected $weeeHelperMock; + + /** + * @var \Magento\Weee\Model\Tax + */ + protected $weeeTaxMock; + /** * @var \Magento\Framework\App\Http\Context */ @@ -49,6 +59,14 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); + $this->weeeHelperMock = $this->getMockBuilder('Magento\Weee\Helper\Data') + ->disableOriginalConstructor() + ->getMock(); + + $this->weeeTaxMock = $this->getMockBuilder('\Magento\Weee\Model\Tax') + ->disableOriginalConstructor() + ->getMock(); + $this->httpContextMock = $this->getMockBuilder('Magento\Framework\App\Http\Context') ->disableOriginalConstructor() ->getMock(); @@ -60,7 +78,8 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') ->disableOriginalConstructor() ->setMethods([ - 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId' + 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId', + 'getWebsiteId', 'isLoggedIn' ]) ->getMock(); @@ -78,57 +97,87 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase 'customerSession' => $this->customerSessionMock, 'httpContext' => $this->httpContextMock, 'calculation' => $this->taxCalculationMock, + 'weeeTax' => $this->weeeTaxMock, 'taxHelper' => $this->taxHelperMock, + 'weeeHelper' => $this->weeeHelperMock, 'moduleManager' => $this->moduleManagerMock, 'cacheConfig' => $this->cacheConfigMock ] ); } - public function testAroundDispatch() + /** + * @param bool $cache + * @param bool $taxEnabled + * @param bool $loggedIn + * @dataProvider dataProviderAroundDispatch + */ + public function testAroundDispatch($cache, $taxEnabled, $loggedIn) { + $this->customerSessionMock->expects($this->any()) + ->method('isLoggedIn') + ->willReturn($loggedIn); + $this->moduleManagerMock->expects($this->any()) ->method('isEnabled') ->with('Magento_PageCache') - ->willReturn(true); + ->willReturn($cache); $this->cacheConfigMock->expects($this->any()) ->method('isEnabled') - ->willReturn(true); - - $this->taxHelperMock->expects($this->any()) - ->method('isCatalogPriceDisplayAffectedByTax') - ->willReturn(true); - - $this->customerSessionMock->expects($this->once()) - ->method('getDefaultTaxBillingAddress') - ->willReturn(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); - $this->customerSessionMock->expects($this->once()) - ->method('getDefaultTaxShippingAddress') - ->willReturn(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); - $this->customerSessionMock->expects($this->once()) - ->method('getCustomerTaxClassId') - ->willReturn(1); - - $this->taxCalculationMock->expects($this->once()) - ->method('getTaxRates') - ->with( - ['country_id' => 1, 'region_id' => null, 'postcode' => 11111], - ['country_id' => 1, 'region_id' => null, 'postcode' => 11111], - 1 - ) - ->willReturn([]); - - $this->httpContextMock->expects($this->once()) - ->method('setValue') - ->with('tax_rates', [], 0); - - $action = $this->objectManager->getObject('Magento\Framework\App\Action\Action'); - $request = $this->getMock('\Magento\Framework\App\Request\Http', ['getActionName'], [], '', false); - $expectedResult = 'expectedResult'; - $proceed = function ($request) use ($expectedResult) { - return $expectedResult; - }; - $this->contextPlugin->aroundDispatch($action, $proceed, $request); + ->willReturn($cache); + + if ($cache && $loggedIn) { + $this->taxHelperMock->expects($this->any()) + ->method('isCatalogPriceDisplayAffectedByTax') + ->willReturn($taxEnabled); + + if ($taxEnabled) { + $this->customerSessionMock->expects($this->once()) + ->method('getDefaultTaxBillingAddress') + ->willReturn(['country_id' => 1, 'region_id' => 1, 'postcode' => 11111]); + $this->customerSessionMock->expects($this->once()) + ->method('getDefaultTaxShippingAddress') + ->willReturn(['country_id' => 1, 'region_id' => 1, 'postcode' => 11111]); + $this->customerSessionMock->expects($this->once()) + ->method('getCustomerTaxClassId') + ->willReturn(1); + + $this->taxCalculationMock->expects($this->once()) + ->method('getTaxRates') + ->with( + ['country_id' => 1, 'region_id' => 1, 'postcode' => 11111], + ['country_id' => 1, 'region_id' => 1, 'postcode' => 11111], + 1 + ) + ->willReturn([]); + + $this->httpContextMock->expects($this->any()) + ->method('setValue') + ->with('tax_rates', [], 0); + } + + $action = $this->objectManager->getObject('Magento\Framework\App\Action\Action'); + $request = $this->getMock('\Magento\Framework\App\Request\Http', ['getActionName'], [], '', false); + $expectedResult = 'expectedResult'; + $proceed = function ($request) use ($expectedResult) { + return $expectedResult; + }; + $this->contextPlugin->aroundDispatch($action, $proceed, $request); + } + } + + /** + * @return array + */ + public function dataProviderAroundDispatch() + { + return [ + [false, false, false], + [true, true, false], + [true, true, true], + [true, false, true], + [true, true, true] + ]; } } diff --git a/app/code/Magento/Tax/Test/Unit/Model/Observer/SessionTest.php b/app/code/Magento/Tax/Test/Unit/Model/Observer/SessionTest.php index 8ef95ead2e386d8e95a03ff03d02a76b8350c89b..1226fa6e9d35d62140f1abe8bae59273c496910d 100755 --- a/app/code/Magento/Tax/Test/Unit/Model/Observer/SessionTest.php +++ b/app/code/Magento/Tax/Test/Unit/Model/Observer/SessionTest.php @@ -64,7 +64,7 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') ->disableOriginalConstructor() ->setMethods([ - 'setCustomerTaxClassId', 'setDefaultTaxBillingAddress', 'setDefaultTaxShippingAddress' + 'setCustomerTaxClassId', 'setDefaultTaxBillingAddress', 'setDefaultTaxShippingAddress', 'setWebsiteId' ]) ->getMock(); diff --git a/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php b/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php new file mode 100644 index 0000000000000000000000000000000000000000..2fcb1aa0e363166b4b39e7cb4296a165b02ab123 --- /dev/null +++ b/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php @@ -0,0 +1,205 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Weee\Model\App\Action; + +/** + * Class ContextPlugin + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class ContextPlugin +{ + /** + * @var \Magento\Customer\Model\Session + */ + protected $customerSession; + + /** + * @var \Magento\Framework\App\Http\Context + */ + protected $httpContext; + + /** + * @var \Magento\Tax\Helper\Data + */ + protected $taxHelper; + + /** + * @var \Magento\Weee\Helper\Data + */ + protected $weeeHelper; + + /** + * @var \Magento\Framework\Module\Manager + */ + protected $moduleManager; + + /** + * @var \Magento\Weee\Model\Tax + */ + protected $weeeTax; + + /** + * @var \Magento\PageCache\Model\Config + */ + protected $cacheConfig; + + /** + * @var \Magento\Store\Model\StoreManagerInterface + */ + protected $storeManager; + + /** + * @var \Magento\Framework\App\Config\ScopeConfigInterface + */ + protected $scopeConfig; + + /** + * @param \Magento\Customer\Model\Session $customerSession + * @param \Magento\Framework\App\Http\Context $httpContext + * @param \Magento\Weee\Model\Tax $weeeTax + * @param \Magento\Tax\Helper\Data $taxHelper + * @param \Magento\Weee\Helper\Data $weeeHelper + * @param \Magento\Framework\Module\Manager $moduleManager + * @param \Magento\PageCache\Model\Config $cacheConfig + * @param \Magento\Store\Model\StoreManagerInterface $storeManager + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + */ + public function __construct( + \Magento\Customer\Model\Session $customerSession, + \Magento\Framework\App\Http\Context $httpContext, + \Magento\Weee\Model\Tax $weeeTax, + \Magento\Tax\Helper\Data $taxHelper, + \Magento\Weee\Helper\Data $weeeHelper, + \Magento\Framework\Module\Manager $moduleManager, + \Magento\PageCache\Model\Config $cacheConfig, + \Magento\Store\Model\StoreManagerInterface $storeManager, + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig + ) { + $this->customerSession = $customerSession; + $this->httpContext = $httpContext; + $this->weeeTax = $weeeTax; + $this->taxHelper = $taxHelper; + $this->weeeHelper = $weeeHelper; + $this->moduleManager = $moduleManager; + $this->cacheConfig = $cacheConfig; + $this->storeManager = $storeManager; + $this->scopeConfig = $scopeConfig; + } + + /** + * @param \Magento\Framework\App\Action\Action $subject + * @param callable $proceed + * @param \Magento\Framework\App\RequestInterface $request + * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function aroundDispatch( + \Magento\Framework\App\Action\Action $subject, + \Closure $proceed, + \Magento\Framework\App\RequestInterface $request + ) { + if (!$this->weeeHelper->isEnabled() || + !$this->customerSession->isLoggedIn() || + !$this->moduleManager->isEnabled('Magento_PageCache') || + !$this->cacheConfig->isEnabled()) { + return $proceed($request); + } + + $basedOn = $this->taxHelper->getTaxBasedOn(); + if ($basedOn != 'shipping' && $basedOn != 'billing') { + return $proceed($request); + } + + $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn); + $websiteId = $this->storeManager->getStore()->getWebsiteId(); + $countryId = $weeeTaxRegion['countryId']; + $regionId = $weeeTaxRegion['regionId']; + + if (!$countryId && !$regionId) { + // country and region does not exist + return $proceed($request); + } else if ($countryId && !$regionId) { + // country exist and region does not exist + $regionId = 0; + $exist = $this->weeeTax->isWeeeInLocation( + $countryId, + $regionId, + $websiteId + ); + } else { + // country and region exist + $exist = $this->weeeTax->isWeeeInLocation( + $countryId, + $regionId, + $websiteId + ); + if (!$exist) { + // just check the country for weee + $regionId = 0; + $exist = $this->weeeTax->isWeeeInLocation( + $countryId, + $regionId, + $websiteId + ); + } + } + + if ($exist) { + $this->httpContext->setValue( + 'weee_tax_region', + ['countryId' => $countryId, 'regionId' => $regionId], + 0 + ); + } + return $proceed($request); + } + + /** + * @param string $basedOn + * @return array + */ + protected function getWeeeTaxRegion($basedOn) + { + $countryId = null; + $regionId = null; + $defaultCountryId = $this->scopeConfig->getValue( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ); + $defaultRegionId = $this->scopeConfig->getValue( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ); + + if ($basedOn == 'shipping') { + $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress(); + if (empty($defaultShippingAddress)) { + $countryId = $defaultCountryId; + $regionId = $defaultRegionId; + } else { + $countryId = $defaultShippingAddress['country_id']; + $regionId = $defaultShippingAddress['region_id']; + } + + } else if ($basedOn == 'billing') { + $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress(); + if (empty($defaultBillingAddress)) { + $countryId = $defaultCountryId; + $regionId = $defaultRegionId; + } else { + $countryId = $defaultBillingAddress['country_id']; + $regionId = $defaultBillingAddress['region_id']; + } + } + return ['countryId' => $countryId, 'regionId' => $regionId]; + } +} diff --git a/app/code/Magento/Weee/Model/Observer/Session.php b/app/code/Magento/Weee/Model/Observer/Session.php new file mode 100644 index 0000000000000000000000000000000000000000..7f72b64c6bf792eff2d650191fa8016cb5ad2325 --- /dev/null +++ b/app/code/Magento/Weee/Model/Observer/Session.php @@ -0,0 +1,139 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ + +/** + * Customer Session Event Observer + */ +namespace Magento\Weee\Model\Observer; + +class Session +{ + /** + * @var \Magento\Customer\Model\Session + */ + protected $customerSession; + + /** + * @var \Magento\Weee\Helper\Data + */ + protected $weeeHelper; + + /** + * Module manager + * + * @var \Magento\Framework\Module\Manager + */ + private $moduleManager; + + /** + * Cache config + * + * @var \Magento\PageCache\Model\Config + */ + private $cacheConfig; + + /** + * @param \Magento\Customer\Model\Session $customerSession + * @param \Magento\Weee\Helper\Data $weeeHelper + * @param \Magento\Framework\Module\Manager $moduleManager + * @param \Magento\PageCache\Model\Config $cacheConfig + */ + public function __construct( + \Magento\Customer\Model\Session $customerSession, + \Magento\Weee\Helper\Data $weeeHelper, + \Magento\Framework\Module\Manager $moduleManager, + \Magento\PageCache\Model\Config $cacheConfig + ) { + $this->customerSession = $customerSession; + $this->weeeHelper = $weeeHelper; + $this->moduleManager = $moduleManager; + $this->cacheConfig = $cacheConfig; + } + + /** + * @param \Magento\Framework\Event\Observer $observer + * @return void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + */ + public function customerLoggedIn(\Magento\Framework\Event\Observer $observer) + { + if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && + $this->weeeHelper->isEnabled()) { + /** @var \Magento\Customer\Model\Data\Customer $customer */ + $customer = $observer->getData('customer'); + + /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ + $addresses = $customer->getAddresses(); + if (isset($addresses)) { + $defaultShippingFound = false; + $defaultBillingFound = false; + foreach ($addresses as $address) { + if ($address->isDefaultBilling()) { + $defaultBillingFound = true; + $this->customerSession->setDefaultTaxBillingAddress( + [ + 'country_id' => $address->getCountryId(), + 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, + 'postcode' => $address->getPostcode(), + ] + ); + } + if ($address->isDefaultShipping()) { + $defaultShippingFound = true; + $this->customerSession->setDefaultTaxShippingAddress( + [ + 'country_id' => $address->getCountryId(), + 'region_id' => $address->getRegion() ? $address->getRegion()->getRegionId() : null, + 'postcode' => $address->getPostcode(), + ] + ); + } + if ($defaultShippingFound && $defaultBillingFound) { + break; + } + } + } + } + } + + /** + * Address after save event handler + * + * @param \Magento\Framework\Event\Observer $observer + * @return void + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ + public function afterAddressSave($observer) + { + if ($this->moduleManager->isEnabled('Magento_PageCache') && $this->cacheConfig->isEnabled() && + $this->weeeHelper->isEnabled()) { + /** @var $customerAddress Address */ + $address = $observer->getCustomerAddress(); + + // Check if the address is either the default billing, shipping, or both + if ($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) { + $this->customerSession->setDefaultTaxBillingAddress( + [ + 'country_id' => $address->getCountryId(), + 'region_id' => $address->getRegion() ? $address->getRegionId() : null, + 'postcode' => $address->getPostcode(), + ] + ); + } + + if ($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) { + $this->customerSession->setDefaultTaxShippingAddress( + [ + 'country_id' => $address->getCountryId(), + 'region_id' => $address->getRegion() ? $address->getRegionId() : null, + 'postcode' => $address->getPostcode(), + ] + ); + } + } + } +} diff --git a/app/code/Magento/Weee/Model/Resource/Tax.php b/app/code/Magento/Weee/Model/Resource/Tax.php index e55e57340028adefa033fe7f02ba45e081ed9fcc..ee7e3783b9d62d0e305c28d03097f990be3d24c3 100644 --- a/app/code/Magento/Weee/Model/Resource/Tax.php +++ b/app/code/Magento/Weee/Model/Resource/Tax.php @@ -52,4 +52,38 @@ class Tax extends \Magento\Framework\Model\Resource\Db\AbstractDb { return $this->_getReadAdapter()->fetchOne($select); } + + /** + * @param int $countryId + * @param int $regionId + * @param int $websiteId + * @return boolean + */ + public function isWeeeInLocation($countryId, $regionId, $websiteId) + { + // Check if there is a weee_tax for the country and region + $attributeSelect = $this->getReadConnection()->select(); + $attributeSelect->from( + $this->getTable('weee_tax'), + 'value' + )->where( + 'website_id IN(?)', + [$websiteId, 0] + )->where( + 'country = ?', + $countryId + )->where( + 'state = ?', + $regionId + )->limit( + 1 + ); + + $value = $this->getReadConnection()->fetchOne($attributeSelect); + if ($value) { + return true; + } + + return false; + } } diff --git a/app/code/Magento/Weee/Model/Tax.php b/app/code/Magento/Weee/Model/Tax.php index 3b41cdeeb598382ab50aa542c442c0d672e0da65..91dd14ffa3f1f26d0607b862f2eae95544d20431 100644 --- a/app/code/Magento/Weee/Model/Tax.php +++ b/app/code/Magento/Weee/Model/Tax.php @@ -232,8 +232,18 @@ class Tax extends \Magento\Framework\Model\AbstractModel if ($customerId = $this->_customerSession->getCustomerId()) { $shipping = $this->accountManagement->getDefaultShippingAddress($customerId); $billing = $this->accountManagement->getDefaultBillingAddress($customerId); + $customerTaxClass = null; + } else { + $shippingAddressArray = $this->_customerSession->getDefaultTaxShippingAddress(); + $billingAddressArray = $this->_customerSession->getDefaultTaxBillingAddress(); + if (!empty($billingAddressArray)) { + $billing = new \Magento\Framework\Object($billingAddressArray); + } + if (!empty($shippingAddressArray)) { + $shipping = new \Magento\Framework\Object($shippingAddressArray); + } + $customerTaxClass = $this->_customerSession->getCustomerTaxClassId(); } - $customerTaxClass = null; } $rateRequest = $calculator->getRateRequest( @@ -322,4 +332,15 @@ class Tax extends \Magento\Framework\Model\AbstractModel } return $result; } + + /** + * @param int $countryId + * @param int $regionId + * @param int $websiteId + * @return boolean + */ + public function isWeeeInLocation($countryId, $regionId, $websiteId) + { + return $this->getResource()->isWeeeInLocation($countryId, $regionId, $websiteId); + } } diff --git a/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php b/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php new file mode 100644 index 0000000000000000000000000000000000000000..0f7a3ad75fb91b47bdcd3a62aea9ca32b7495586 --- /dev/null +++ b/app/code/Magento/Weee/Test/Unit/App/Action/ContextPluginTest.php @@ -0,0 +1,378 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Weee\Test\Unit\App\Action; + +/** + * Class ContextPluginTest + * + * @package Magento\Weee\Test\Unit\App\Action + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ +class ContextPluginTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Tax\Helper\Data + */ + protected $taxHelperMock; + + /** + * @var \Magento\Weee\Helper\Data + */ + protected $weeeHelperMock; + + /** + * @var \Magento\Weee\Model\Tax + */ + protected $weeeTaxMock; + + /** + * @var \Magento\Framework\App\Http\Context + */ + protected $httpContextMock; + + /** + * @var \Magento\Tax\Model\Calculation\Proxy + */ + protected $taxCalculationMock; + + /** + * @var \Magento\Framework\Module\Manager + */ + protected $moduleManagerMock; + + /** + * @var \Magento\PageCache\Model\Config + */ + protected $cacheConfigMock; + + /** + * @var \Magento\Store\Model\StoreManager + */ + protected $storeManageMock; + + /** + * @var \Magento\Framework\App\Config\ScopeConfig + */ + protected $scopeConfigMock; + + /** + * @var \Magento\Tax\Model\App\Action\ContextPlugin + */ + protected $contextPlugin; + + protected function setUp() + { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->taxHelperMock = $this->getMockBuilder('Magento\Tax\Helper\Data') + ->disableOriginalConstructor() + ->getMock(); + + $this->weeeHelperMock = $this->getMockBuilder('Magento\Weee\Helper\Data') + ->disableOriginalConstructor() + ->getMock(); + + $this->weeeTaxMock = $this->getMockBuilder('\Magento\Weee\Model\Tax') + ->disableOriginalConstructor() + ->getMock(); + + $this->httpContextMock = $this->getMockBuilder('Magento\Framework\App\Http\Context') + ->disableOriginalConstructor() + ->getMock(); + + $this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') + ->disableOriginalConstructor() + ->setMethods([ + 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId', + 'getWebsiteId', 'isLoggedIn' + ]) + ->getMock(); + + $this->moduleManagerMock = $this->getMockBuilder('Magento\Framework\Module\Manager') + ->disableOriginalConstructor() + ->getMock(); + + $this->cacheConfigMock = $this->getMockBuilder('Magento\PageCache\Model\Config') + ->disableOriginalConstructor() + ->getMock(); + + $this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config') + ->disableOriginalConstructor() + ->getMock(); + + $this->contextPlugin = $this->objectManager->getObject( + 'Magento\Weee\Model\App\Action\ContextPlugin', + [ + 'customerSession' => $this->customerSessionMock, + 'httpContext' => $this->httpContextMock, + 'weeeTax' => $this->weeeTaxMock, + 'taxHelper' => $this->taxHelperMock, + 'weeeHelper' => $this->weeeHelperMock, + 'moduleManager' => $this->moduleManagerMock, + 'cacheConfig' => $this->cacheConfigMock, + 'storeManager' => $this->storeManagerMock, + 'scopeConfig' => $this->scopeConfigMock + ] + ); + } + + public function testAroundDispatchBasedOnDefault() + { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + + $this->moduleManagerMock->expects($this->once()) + ->method('isEnabled') + ->with('Magento_PageCache') + ->willReturn(true); + + $this->cacheConfigMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->weeeHelperMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->taxHelperMock->expects($this->once()) + ->method('getTaxBasedOn') + ->willReturn('billing'); + + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') + ->disableOriginalConstructor() + ->getMock(); + + $storeMock->expects($this->once()) + ->method('getWebsiteId') + ->willReturn(1); + + $this->storeManagerMock->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + + $this->scopeConfigMock->expects($this->at(0)) + ->method('getValue') + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ) + ->willReturn('US'); + + $this->scopeConfigMock->expects($this->at(1)) + ->method('getValue') + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ) + ->willReturn(0); + + $this->weeeTaxMock->expects($this->once()) + ->method('isWeeeInLocation') + ->with('US', 0, 1) + ->willReturn(true); + + $this->httpContextMock->expects($this->once()) + ->method('setValue') + ->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 0], 0); + + $action = $this->objectManager->getObject('Magento\Framework\App\Action\Action'); + $request = $this->getMock('\Magento\Framework\App\Request\Http', ['getActionName'], [], '', false); + $expectedResult = 'expectedResult'; + $proceed = function ($request) use ($expectedResult) { + return $expectedResult; + }; + $this->contextPlugin->aroundDispatch($action, $proceed, $request); + } + + public function testAroundDispatchBasedOnOrigin() + { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + + $this->moduleManagerMock->expects($this->once()) + ->method('isEnabled') + ->with('Magento_PageCache') + ->willReturn(true); + + $this->cacheConfigMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->weeeHelperMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->taxHelperMock->expects($this->once()) + ->method('getTaxBasedOn') + ->willReturn('origin'); + + $action = $this->objectManager->getObject('Magento\Framework\App\Action\Action'); + $request = $this->getMock('\Magento\Framework\App\Request\Http', ['getActionName'], [], '', false); + $expectedResult = 'expectedResult'; + $proceed = function ($request) use ($expectedResult) { + return $expectedResult; + }; + $this->contextPlugin->aroundDispatch($action, $proceed, $request); + } + + public function testAroundDispatchBasedOnBilling() + { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + + $this->moduleManagerMock->expects($this->once()) + ->method('isEnabled') + ->with('Magento_PageCache') + ->willReturn(true); + + $this->cacheConfigMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->weeeHelperMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->taxHelperMock->expects($this->once()) + ->method('getTaxBasedOn') + ->willReturn('billing'); + + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') + ->disableOriginalConstructor() + ->getMock(); + + $storeMock->expects($this->once()) + ->method('getWebsiteId') + ->willReturn(1); + + $this->storeManagerMock->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + + $this->scopeConfigMock->expects($this->at(0)) + ->method('getValue') + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ) + ->willReturn('US'); + + $this->scopeConfigMock->expects($this->at(1)) + ->method('getValue') + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ) + ->willReturn(0); + + $this->customerSessionMock->expects($this->once()) + ->method('getDefaultTaxBillingAddress') + ->willReturn(['country_id' => 'US', 'region_id' => 1]); + + $this->weeeTaxMock->expects($this->once()) + ->method('isWeeeInLocation') + ->with('US', 1, 1) + ->willReturn(true); + + $this->httpContextMock->expects($this->once()) + ->method('setValue') + ->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 1], 0); + + $action = $this->objectManager->getObject('Magento\Framework\App\Action\Action'); + $request = $this->getMock('\Magento\Framework\App\Request\Http', ['getActionName'], [], '', false); + $expectedResult = 'expectedResult'; + $proceed = function ($request) use ($expectedResult) { + return $expectedResult; + }; + $this->contextPlugin->aroundDispatch($action, $proceed, $request); + } + + public function testAroundDispatchBasedOnShipping() + { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + + $this->moduleManagerMock->expects($this->once()) + ->method('isEnabled') + ->with('Magento_PageCache') + ->willReturn(true); + + $this->cacheConfigMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->weeeHelperMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->taxHelperMock->expects($this->once()) + ->method('getTaxBasedOn') + ->willReturn('shipping'); + + $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') + ->disableOriginalConstructor() + ->getMock(); + + $storeMock->expects($this->once()) + ->method('getWebsiteId') + ->willReturn(1); + + $this->storeManagerMock->expects($this->once()) + ->method('getStore') + ->willReturn($storeMock); + + $this->scopeConfigMock->expects($this->at(0)) + ->method('getValue') + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ) + ->willReturn('US'); + + $this->scopeConfigMock->expects($this->at(1)) + ->method('getValue') + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + null + ) + ->willReturn(0); + + $this->customerSessionMock->expects($this->once()) + ->method('getDefaultTaxShippingAddress') + ->willReturn(['country_id' => 'US', 'region_id' => 1]); + + $this->weeeTaxMock->expects($this->once()) + ->method('isWeeeInLocation') + ->with('US', 1, 1) + ->willReturn(true); + + $this->httpContextMock->expects($this->once()) + ->method('setValue') + ->with('weee_tax_region', ['countryId' => 'US', 'regionId' => 1], 0); + + $action = $this->objectManager->getObject('Magento\Framework\App\Action\Action'); + $request = $this->getMock('\Magento\Framework\App\Request\Http', ['getActionName'], [], '', false); + $expectedResult = 'expectedResult'; + $proceed = function ($request) use ($expectedResult) { + return $expectedResult; + }; + $this->contextPlugin->aroundDispatch($action, $proceed, $request); + } +} diff --git a/app/code/Magento/Weee/Test/Unit/Model/Observer/SessionTest.php b/app/code/Magento/Weee/Test/Unit/Model/Observer/SessionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..db31c7931bedab29ca21223702a8b8cd035a3070 --- /dev/null +++ b/app/code/Magento/Weee/Test/Unit/Model/Observer/SessionTest.php @@ -0,0 +1,166 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Weee\Test\Unit\Model\Observer; + +class SessionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Framework\Event\Observer + */ + protected $observerMock; + + /** + * @var \Magento\Customer\Model\Session + */ + protected $customerSessionMock; + + /** + * Module manager + * + * @var \Magento\Framework\Module\Manager + */ + private $moduleManagerMock; + + /** + * Cache config + * + * @var \Magento\PageCache\Model\Config + */ + private $cacheConfigMock; + + /** + * @var \Magento\Weee\Helper\Data + */ + protected $weeeHelperMock; + + /** + * @var \Magento\Tax\Model\Observer\Session + */ + protected $session; + + + protected function setUp() + { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->observerMock = $this->getMockBuilder('Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->setMethods([ + 'getCustomerAddress', 'getData' + ]) + ->getMock(); + + $this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') + ->disableOriginalConstructor() + ->setMethods([ + 'setDefaultTaxBillingAddress', 'setDefaultTaxShippingAddress', 'setWebsiteId' + ]) + ->getMock(); + + $this->moduleManagerMock = $this->getMockBuilder('Magento\Framework\Module\Manager') + ->disableOriginalConstructor() + ->getMock(); + + $this->cacheConfigMock = $this->getMockBuilder('Magento\PageCache\Model\Config') + ->disableOriginalConstructor() + ->getMock(); + + $this->weeeHelperMock = $this->getMockBuilder('Magento\Weee\Helper\Data') + ->disableOriginalConstructor() + ->getMock(); + + $this->session = $this->objectManager->getObject( + 'Magento\Weee\Model\Observer\Session', + [ + 'customerSession' => $this->customerSessionMock, + 'weeeHelper' => $this->weeeHelperMock, + 'moduleManager' => $this->moduleManagerMock, + 'cacheConfig' => $this->cacheConfigMock + ] + ); + } + + public function testCustomerLoggedIn() + { + $this->moduleManagerMock->expects($this->once()) + ->method('isEnabled') + ->with('Magento_PageCache') + ->willReturn(true); + + $this->cacheConfigMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->weeeHelperMock->expects($this->any()) + ->method('isEnabled') + ->willReturn(true); + + $customerMock = $this->getMockBuilder('Magento\Customer\Model\Data\Customer') + ->disableOriginalConstructor() + ->getMock(); + + $this->observerMock->expects($this->once()) + ->method('getData') + ->with('customer') + ->willReturn($customerMock); + + $address = $this->objectManager->getObject('Magento\Customer\Model\Data\Address'); + $address->setIsDefaultShipping(true); + $address->setIsDefaultBilling(true); + $address->setCountryId(1); + $address->setPostCode(11111); + + $addresses = [$address]; + $customerMock->expects($this->once()) + ->method('getAddresses') + ->willReturn($addresses); + + $this->customerSessionMock->expects($this->once()) + ->method('setDefaultTaxBillingAddress') + ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); + $this->customerSessionMock->expects($this->once()) + ->method('setDefaultTaxShippingAddress') + ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); + + $this->session->customerLoggedIn($this->observerMock); + } + + public function testAfterAddressSave() + { + $this->moduleManagerMock->expects($this->once()) + ->method('isEnabled') + ->with('Magento_PageCache') + ->willReturn(true); + + $this->cacheConfigMock->expects($this->once()) + ->method('isEnabled') + ->willReturn(true); + + $this->weeeHelperMock->expects($this->any()) + ->method('isEnabled') + ->willReturn(true); + + $address = $this->objectManager->getObject('Magento\Customer\Model\Address'); + $address->setIsDefaultShipping(true); + $address->setIsDefaultBilling(true); + $address->setIsPrimaryBilling(true); + $address->setIsPrimaryShipping(true); + $address->setCountryId(1); + $address->setData('postcode', 11111); + + $this->customerSessionMock->expects($this->once()) + ->method('setDefaultTaxBillingAddress') + ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); + $this->customerSessionMock->expects($this->once()) + ->method('setDefaultTaxShippingAddress') + ->with(['country_id' => 1, 'region_id' => null, 'postcode' => 11111]); + + $this->observerMock->expects($this->once()) + ->method('getCustomerAddress') + ->willReturn($address); + + $this->session->afterAddressSave($this->observerMock); + } +} diff --git a/app/code/Magento/Weee/Test/Unit/Model/Resource/TaxTest.php b/app/code/Magento/Weee/Test/Unit/Model/Resource/TaxTest.php new file mode 100644 index 0000000000000000000000000000000000000000..27a4dedc845dd2b530a082088417a4412686faa0 --- /dev/null +++ b/app/code/Magento/Weee/Test/Unit/Model/Resource/TaxTest.php @@ -0,0 +1,93 @@ +<?php +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Weee\Test\Unit\Model\Resource; + +class TaxTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Weee\Model\Resource\Tax + */ + protected $model; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $resourceMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $storeManagerMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $adapterMock; + + protected function setUp() + { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->storeManagerMock = $this->getMock('\Magento\Store\Model\StoreManagerInterface'); + + $this->selectMock = $this->getMock('\Magento\Framework\DB\Select', [], [], '', false); + + $this->adapterMock = $this->getMock('\Magento\Framework\DB\Adapter\AdapterInterface', [], [], '', false); + $this->adapterMock->expects($this->once()) + ->method('select') + ->willReturn($this->selectMock); + + $this->resourceMock = $this->getMock('\Magento\Framework\App\Resource', [], [], '', false); + $this->resourceMock->expects($this->at(0)) + ->method('getConnection') + ->with('core_write') + ->willReturn($this->adapterMock); + + $this->resourceMock->expects($this->at(1)) + ->method('getConnection') + ->with('core_read') + ->willReturn($this->adapterMock); + + $this->resourceMock->expects($this->once()) + ->method('getTableName') + ->willReturn('table_name'); + + $contextMock = $this->getMock('\Magento\Framework\Model\Resource\Db\Context', [], [], '', false); + $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock); + + $this->model = $this->objectManager->getObject( + 'Magento\Weee\Model\Resource\Tax', + [ + 'context' => $contextMock, + ] + ); + } + + public function testInWeeeLocation() + { + $this->selectMock->expects($this->at(1)) + ->method('where') + ->with('website_id IN(?)', [1, 0]) + ->willReturn($this->selectMock); + + $this->selectMock->expects($this->at(2)) + ->method('where') + ->with('country = ?', 'US') + ->willReturn($this->selectMock); + + $this->selectMock->expects($this->at(3)) + ->method('where') + ->with('state = ?', 0) + ->willReturn($this->selectMock); + + $this->selectMock->expects($this->any()) + ->method('from') + ->with('table_name', 'value') + ->willReturn($this->selectMock); + + $this->model->isWeeeInLocation('US', 0, 1); + } +} diff --git a/app/code/Magento/Weee/composer.json b/app/code/Magento/Weee/composer.json index a6d45896b5d202f3b0cc375979217c7576e0a331..6fe75a545c2e3d77c698cfc70bbd201305e99d51 100644 --- a/app/code/Magento/Weee/composer.json +++ b/app/code/Magento/Weee/composer.json @@ -11,6 +11,7 @@ "magento/module-directory": "0.74.0-beta15", "magento/module-eav": "0.74.0-beta15", "magento/module-customer": "0.74.0-beta15", + "magento/module-page-cache": "0.74.0-beta15", "magento/module-quote": "0.74.0-beta15", "magento/module-checkout": "0.74.0-beta15", "magento/framework": "0.74.0-beta15", diff --git a/app/code/Magento/Weee/etc/frontend/di.xml b/app/code/Magento/Weee/etc/frontend/di.xml index 274af56fa7134cb28eb908da6c486daa4c45b333..c9b706532877b79db60711a610859a92117c4fe7 100644 --- a/app/code/Magento/Weee/etc/frontend/di.xml +++ b/app/code/Magento/Weee/etc/frontend/di.xml @@ -13,4 +13,8 @@ </argument> </arguments> </type> + <type name="Magento\Framework\App\Action\Action"> + <plugin name="weee-app-action-dispatchController-context-plugin" + type="Magento\Weee\Model\App\Action\ContextPlugin"/> + </type> </config> diff --git a/app/code/Magento/Weee/etc/frontend/events.xml b/app/code/Magento/Weee/etc/frontend/events.xml new file mode 100644 index 0000000000000000000000000000000000000000..2bc96ce0399341f63d40f814ee72e061f54d205c --- /dev/null +++ b/app/code/Magento/Weee/etc/frontend/events.xml @@ -0,0 +1,16 @@ +<?xml version="1.0"?> +<!-- +/** + * Copyright © 2015 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +--> +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd"> + <event name="customer_data_object_login"> + <observer name="customer_weee_logged_in" instance="Magento\Weee\Model\Observer\Session" method="customerLoggedIn" /> + </event> + <event name="customer_address_save_after"> + <observer name="customer_weee_after_address_save" instance="Magento\Weee\Model\Observer\Session" method="afterAddressSave" /> + </event> +</config>