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 7b1c1293a68dd9a3c327866952ec690b8ba161d7..5ffa575a4ed5ce93226735c93cabd5418f5b585c 100644 --- a/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php +++ b/app/code/Magento/Tax/Test/Unit/App/Action/ContextPluginTest.php @@ -78,7 +78,8 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') ->disableOriginalConstructor() ->setMethods([ - 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId', 'getWebsiteId' + 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId', + 'getWebsiteId', 'isLoggedIn' ]) ->getMock(); @@ -108,11 +109,15 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase /** * @param bool $cache * @param bool $taxEnabled - * @param bool $weeeEnabled + * @param bool $loggedIn * @dataProvider dataProviderAroundDispatch */ - public function testAroundDispatch($cache, $taxEnabled, $weeeEnabled) + 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') @@ -122,7 +127,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->method('isEnabled') ->willReturn($cache); - if ($cache) { + if ($cache && $loggedIn) { $this->taxHelperMock->expects($this->any()) ->method('isCatalogPriceDisplayAffectedByTax') ->willReturn($taxEnabled); @@ -170,6 +175,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase return [ [false, false, false], [true, true, false], + [true, true, true], [true, false, true], [true, true, true] ]; diff --git a/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php b/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php index 54ccefbaeecdc173a71061def63cf4e1ca017e02..5e6e1179a03bf139ee7c479ecb12b21f927e574a 100644 --- a/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php +++ b/app/code/Magento/Weee/Model/App/Action/ContextPlugin.php @@ -8,6 +8,7 @@ namespace Magento\Weee\Model\App\Action; /** * Class ContextPlugin + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class ContextPlugin { @@ -37,7 +38,7 @@ class ContextPlugin protected $moduleManager; /** - * @var \Magento\Weee\Model\Resource\Tax + * @var \Magento\Weee\Model\Tax */ protected $weeeTax; @@ -59,7 +60,7 @@ class ContextPlugin /** * @param \Magento\Customer\Model\Session $customerSession * @param \Magento\Framework\App\Http\Context $httpContext - * @param \Magento\Weee\Model\Resource\WeeeTax $weeeTax + * @param \Magento\Weee\Model\Tax $weeeTax * @param \Magento\Tax\Helper\Data $taxHelper * @param \Magento\Weee\Helper\Data $weeeHelper * @param \Magento\Framework\Module\Manager $moduleManager @@ -70,7 +71,7 @@ class ContextPlugin public function __construct( \Magento\Customer\Model\Session $customerSession, \Magento\Framework\App\Http\Context $httpContext, - \Magento\Weee\Model\Resource\Tax $weeeTax, + \Magento\Weee\Model\Tax $weeeTax, \Magento\Tax\Helper\Data $taxHelper, \Magento\Weee\Helper\Data $weeeHelper, \Magento\Framework\Module\Manager $moduleManager, @@ -95,21 +96,77 @@ class ContextPlugin * @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->moduleManager->isEnabled('Magento_PageCache') || - !$this->cacheConfig->isEnabled() || - !$this->weeeHelper->isEnabled()) { + 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 + $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); + } + + /* + * @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, @@ -121,21 +178,7 @@ class ContextPlugin null ); - if ($basedOn == 'default') { - $countryId = $defaultCountryId; - $regionId = $defaultRegionId; - } else if ($basedOn == 'origin') { - $countryId = $this->scopeConfig->getValue( - \Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - null - ); - $regionId = $this->scopeConfig->getValue( - \Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - null - ); - } else if ($basedOn == 'shipping') { + if ($basedOn == 'shipping') { $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress(); if (empty($defaultShippingAddress)) { $countryId = $defaultCountryId; @@ -155,55 +198,6 @@ class ContextPlugin $regionId = $defaultBillingAddress['region_id']; } } - - if (!$countryId && !$regionId) { - // country and region does not exist - return $proceed($request); - } else if ($countryId && !$regionId) { - // country exist and region does not exist - $exist = $this->weeeTax->isWeeeInLocation( - $countryId, - $regionId, - $websiteId - ); - if ($exist) { - $this->httpContext->setValue( - 'weee_taxes', - ['countryId' => $countryId, 'regionId' => $regionId], - 0 - ); - } - } else { - // country and region exist - $exist = $this->weeeTax->isWeeeInLocation( - $countryId, - $regionId, - $websiteId - ); - if ($exist) { - $this->httpContext->setValue( - 'weee_taxes', - ['countryId' => $countryId, 'regionId' => $regionId], - 0 - ); - } else { - // just check the country for weee - $regionId = 0; - $exist = $this->weeeTax->isWeeeInLocation( - $countryId, - $regionId, - $websiteId - ); - if ($exist) { - $this->httpContext->setValue( - 'weee_taxes', - ['countryId' => $countryId, 'regionId' => $regionId], - 0 - ); - } - } - } - - return $proceed($request); + return ['countryId' => $countryId, 'regionId' => $regionId]; } } diff --git a/app/code/Magento/Weee/Model/Tax.php b/app/code/Magento/Weee/Model/Tax.php index baeee2703d3d8410914213c8e32494c9e46f42d8..91dd14ffa3f1f26d0607b862f2eae95544d20431 100644 --- a/app/code/Magento/Weee/Model/Tax.php +++ b/app/code/Magento/Weee/Model/Tax.php @@ -332,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/Model/App/Action/ContextPluginTest.php b/app/code/Magento/Weee/Test/Unit/Model/App/Action/ContextPluginTest.php index 127e5435d7e6c0a40122a34b653a542327509c6f..0f7a3ad75fb91b47bdcd3a62aea9ca32b7495586 100644 --- a/app/code/Magento/Weee/Test/Unit/Model/App/Action/ContextPluginTest.php +++ b/app/code/Magento/Weee/Test/Unit/Model/App/Action/ContextPluginTest.php @@ -5,6 +5,12 @@ */ 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 { /** @@ -69,7 +75,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->disableOriginalConstructor() ->getMock(); - $this->weeeTaxMock = $this->getMockBuilder('\Magento\Weee\Model\Resource\Tax') + $this->weeeTaxMock = $this->getMockBuilder('\Magento\Weee\Model\Tax') ->disableOriginalConstructor() ->getMock(); @@ -80,7 +86,8 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $this->customerSessionMock = $this->getMockBuilder('Magento\Customer\Model\Session') ->disableOriginalConstructor() ->setMethods([ - 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId', 'getWebsiteId' + 'getDefaultTaxBillingAddress', 'getDefaultTaxShippingAddress', 'getCustomerTaxClassId', + 'getWebsiteId', 'isLoggedIn' ]) ->getMock(); @@ -118,6 +125,10 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase public function testAroundDispatchBasedOnDefault() { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->moduleManagerMock->expects($this->once()) ->method('isEnabled') ->with('Magento_PageCache') @@ -133,7 +144,7 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $this->taxHelperMock->expects($this->once()) ->method('getTaxBasedOn') - ->willReturn('default'); + ->willReturn('billing'); $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') ->disableOriginalConstructor() @@ -149,16 +160,20 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase $this->scopeConfigMock->expects($this->at(0)) ->method('getValue') - ->with(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - null) + null + ) ->willReturn('US'); $this->scopeConfigMock->expects($this->at(1)) ->method('getValue') - ->with(\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, + ->with( + \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - null) + null + ) ->willReturn(0); $this->weeeTaxMock->expects($this->once()) @@ -166,9 +181,9 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->with('US', 0, 1) ->willReturn(true); - $this->httpContextMock->expects($this->any()) + $this->httpContextMock->expects($this->once()) ->method('setValue') - ->with('weee_taxes', ['countryId' => 'US', 'regionId' => 0], 0); + ->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); @@ -181,6 +196,10 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase public function testAroundDispatchBasedOnOrigin() { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->moduleManagerMock->expects($this->once()) ->method('isEnabled') ->with('Magento_PageCache') @@ -198,41 +217,6 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->method('getTaxBasedOn') ->willReturn('origin'); - $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(2)) - ->method('getValue') - ->with(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_COUNTRY_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - null) - ->willReturn('US'); - - $this->scopeConfigMock->expects($this->at(3)) - ->method('getValue') - ->with(\Magento\Shipping\Model\Config::XML_PATH_ORIGIN_REGION_ID, - \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->any()) - ->method('setValue') - ->with('weee_taxes', ['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'; @@ -244,6 +228,10 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase public function testAroundDispatchBasedOnBilling() { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->moduleManagerMock->expects($this->once()) ->method('isEnabled') ->with('Magento_PageCache') @@ -273,18 +261,36 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->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, 'postcode' => 11111]); + ->willReturn(['country_id' => 'US', 'region_id' => 1]); $this->weeeTaxMock->expects($this->once()) ->method('isWeeeInLocation') ->with('US', 1, 1) ->willReturn(true); - $this->httpContextMock->expects($this->any()) + $this->httpContextMock->expects($this->once()) ->method('setValue') - ->with('weee_taxes', ['countryId' => 'US', 'regionId' => 1], 0); + ->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); @@ -297,6 +303,10 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase public function testAroundDispatchBasedOnShipping() { + $this->customerSessionMock->expects($this->once()) + ->method('isLoggedIn') + ->willReturn(true); + $this->moduleManagerMock->expects($this->once()) ->method('isEnabled') ->with('Magento_PageCache') @@ -326,18 +336,36 @@ class ContextPluginTest extends \PHPUnit_Framework_TestCase ->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, 'postcode' => 11111]); + ->willReturn(['country_id' => 'US', 'region_id' => 1]); $this->weeeTaxMock->expects($this->once()) ->method('isWeeeInLocation') ->with('US', 1, 1) ->willReturn(true); - $this->httpContextMock->expects($this->any()) + $this->httpContextMock->expects($this->once()) ->method('setValue') - ->with('weee_taxes', ['countryId' => 'US', 'regionId' => 1], 0); + ->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); 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..b7f979691761b514b013b140d806895446e54ce0 --- /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); + } +}