Skip to content

Commit

Permalink
Merge pull request #622 from magento-nord/develop
Browse files Browse the repository at this point in the history
[NORD] Bug fixes
  • Loading branch information
Onischenko, Yaroslav(yonischenko) committed May 15, 2016
2 parents 08eebe4 + 6702780 commit 8a1f653
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 36 deletions.
60 changes: 58 additions & 2 deletions app/code/Magento/Catalog/Block/Category/Plugin/PriceBoxTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Pricing\Render\PriceBox;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Tax\Model\Calculation as TaxCalculation;

class PriceBoxTags
{
Expand All @@ -35,6 +36,11 @@ class PriceBoxTags
*/
private $scopeResolver;

/**
* @var TaxCalculation
*/
private $taxCalculation;

/**
* PriceBoxTags constructor.
* @param PriceCurrencyInterface $priceCurrency
Expand All @@ -58,8 +64,6 @@ public function __construct(
* @param PriceBox $subject
* @param string $result
* @return string
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetCacheKey(PriceBox $subject, $result)
{
Expand All @@ -71,7 +75,59 @@ public function afterGetCacheKey(PriceBox $subject, $result)
$this->dateTime->scopeDate($this->scopeResolver->getScope()->getId())->format('Ymd'),
$this->scopeResolver->getScope()->getId(),
$this->customerSession->getCustomerGroupId(),
$this->getTaxRateIds($subject),
]
);
}

/**
* @param PriceBox $subject
* @return string
*/
private function getTaxRateIds(PriceBox $subject)
{
$rateIds = [];

$customerSession = $this->customerSession;
$billingAddress = $customerSession->getDefaultTaxBillingAddress();
$shippingAddress = $customerSession->getDefaultTaxShippingAddress();
$customerTaxClassId = $customerSession->getCustomerTaxClassId();

if (!empty($billingAddress)) {
$billingAddress = new \Magento\Framework\DataObject($billingAddress);
}
if (!empty($shippingAddress)) {
$shippingAddress = new \Magento\Framework\DataObject($shippingAddress);
}

if (!empty($billingAddress) || !empty($shippingAddress)) {
$rateRequest = $this->getTaxCalculation()->getRateRequest(
$billingAddress,
$shippingAddress,
$customerTaxClassId,
$this->scopeResolver->getScope()->getId(),
$this->customerSession->getCustomerId()
);

$rateRequest->setProductClassId($subject->getSaleableItem()->getTaxClassId());
$rateIds = $this->getTaxCalculation()->getResource()->getRateIds($rateRequest);
}

return implode('_', $rateIds);
}

/**
* Get the TaxCalculation model
*
* @return \Magento\Tax\Model\Calculation
*
* @deprecated
*/
private function getTaxCalculation()
{
if ($this->taxCalculation === null) {
$this->taxCalculation = \Magento\Framework\App\ObjectManager::getInstance()->get(TaxCalculation::class);
}
return $this->taxCalculation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
namespace Magento\Catalog\Model\Indexer\Product\Category\Action;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\Framework\Indexer\CacheContext;

class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractAction
{
/**
Expand All @@ -14,6 +18,11 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
*/
protected $limitationByProducts;

/**
* @var \Magento\Framework\Indexer\CacheContext
*/
private $cacheContext;

/**
* Refresh entities index
*
Expand All @@ -30,9 +39,43 @@ public function execute(array $entityIds = [], $useTempTable = false)

$this->reindex();

$this->registerProducts($entityIds);
$this->registerCategories($entityIds);

return $this;
}

/**
* Register affected products
*
* @param array $entityIds
* @return void
*/
private function registerProducts($entityIds)
{
$this->getCacheContext()->registerEntities(Product::CACHE_TAG, $entityIds);
}

/**
* Register categories assigned to products
*
* @param array $entityIds
* @return void
*/
private function registerCategories($entityIds)
{
$categories = $this->connection->fetchCol(
$this->connection->select()
->from($this->getMainTable(), ['category_id'])
->where('product_id IN (?)', $entityIds)
->distinct()
);

if ($categories) {
$this->getCacheContext()->registerEntities(Category::CACHE_TAG, $categories);
}
}

/**
* Remove index entries before reindexation
*
Expand Down Expand Up @@ -91,4 +134,18 @@ protected function isRangingNeeded()
{
return false;
}

/**
* Get cache context
*
* @return \Magento\Framework\Indexer\CacheContext
* @deprecated
*/
private function getCacheContext()
{
if ($this->cacheContext === null) {
$this->cacheContext = \Magento\Framework\App\ObjectManager::getInstance()->get(CacheContext::class);
}
return $this->cacheContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
namespace Magento\Catalog\Plugin\Model\Product\Action;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Action;
use Magento\Framework\Indexer\CacheContext;
use Magento\Framework\Event\ManagerInterface as EventManager;
Expand Down Expand Up @@ -36,26 +35,28 @@ public function __construct(

/**
* @param Action $subject
* @param \Closure $proceed
* @param array $productIds
* @param array $attrData
* @param int $storeId
* @param Action $result
* @return Action
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundUpdateAttributes(
Action $subject,
\Closure $proceed,
$productIds,
$attrData,
$storeId
public function afterUpdateAttributes(
\Magento\Catalog\Model\Product\Action $subject,
\Magento\Catalog\Model\Product\Action $result
) {
$returnValue = $proceed($productIds, $attrData, $storeId);

$this->cacheContext->registerEntities(Product::CACHE_TAG, $productIds);
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
return $result;
}

return $returnValue;
/**
* @param Action $subject
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterUpdateWebsites(
\Magento\Catalog\Model\Product\Action $subject
) {
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Block\Category\Plugin;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class PriceBoxTagsTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface | \PHPUnit_Framework_MockObject_MockObject
*/
private $priceCurrencyInterface;

/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface | \PHPUnit_Framework_MockObject_MockObject
*/
private $timezoneInterface;

/**
* @var \Magento\Framework\App\ScopeResolverInterface | \PHPUnit_Framework_MockObject_MockObject
*/
private $scopeResolverInterface;

/**
* @var \Magento\Customer\Model\Session | \PHPUnit_Framework_MockObject_MockObject
*/
private $session;

/**
* @var \Magento\Tax\Model\Calculation | \PHPUnit_Framework_MockObject_MockObject
*/
private $taxCalculation;

/**
* @var \Magento\Catalog\Block\Category\Plugin\PriceBoxTags
*/
private $priceBoxTags;

protected function setUp()
{
$this->priceCurrencyInterface = $this->getMockBuilder(
\Magento\Framework\Pricing\PriceCurrencyInterface::class
)->getMock();
$this->timezoneInterface = $this->getMockBuilder(
\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class
)->getMock();
$this->scopeResolverInterface = $this->getMockBuilder(
\Magento\Framework\App\ScopeResolverInterface::class
)
->getMockForAbstractClass();
$this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)->disableOriginalConstructor()
->setMethods(
[
'getCustomerGroupId',
'getDefaultTaxBillingAddress',
'getDefaultTaxShippingAddress',
'getCustomerTaxClassId',
'getCustomerId'
]
)
->getMock();
$this->taxCalculation = $this->getMockBuilder(\Magento\Tax\Model\Calculation::class)
->disableOriginalConstructor()->getMock();
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->priceBoxTags = $objectManager->getObject(
\Magento\Catalog\Block\Category\Plugin\PriceBoxTags::class,
[
'priceCurrency' => $this->priceCurrencyInterface,
'dateTime' => $this->timezoneInterface,
'scopeResolver' => $this->scopeResolverInterface,
'customerSession' => $this->session,
'taxCalculation' => $this->taxCalculation
]
);
}

public function testAfterGetCacheKey()
{
$date = date('Ymd');
$currencySymbol = '$';
$result = 'result_string';
$billingAddress = ['billing_address'];
$shippingAddress = ['shipping_address'];
$scopeId = 1;
$customerGroupId = 2;
$customerTaxClassId = 3;
$customerId = 4;
$rateIds = [5,6];
$expected = implode(
'-',
[
$result,
$currencySymbol,
$date,
$scopeId,
$customerGroupId,
implode('_', $rateIds)
]
);
$priceBox = $this->getMockBuilder(\Magento\Framework\Pricing\Render\PriceBox::class)
->disableOriginalConstructor()->getMock();
$this->priceCurrencyInterface->expects($this->once())->method('getCurrencySymbol')->willReturn($currencySymbol);
$scope = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)->getMock();
$this->scopeResolverInterface->expects($this->any())->method('getScope')->willReturn($scope);
$scope->expects($this->any())->method('getId')->willReturn($scopeId);
$dateTime = $this->getMockBuilder(\DateTime::class)->getMock();
$this->timezoneInterface->expects($this->any())->method('scopeDate')->with($scopeId)->willReturn($dateTime);
$dateTime->expects($this->any())->method('format')->with('Ymd')->willReturn($date);
$this->session->expects($this->once())->method('getCustomerGroupId')->willReturn($customerGroupId);
$this->session->expects($this->once())->method('getDefaultTaxBillingAddress')->willReturn($billingAddress);
$this->session->expects($this->once())->method('getDefaultTaxShippingAddress')->willReturn($shippingAddress);
$this->session->expects($this->once())->method('getCustomerTaxClassId')
->willReturn($customerTaxClassId);
$this->session->expects($this->once())->method('getCustomerId')->willReturn($customerId);
$rateRequest = $this->getMockBuilder(\Magento\Framework\DataObject::class)->getMock();
$this->taxCalculation->expects($this->once())->method('getRateRequest')->with(
new \Magento\Framework\DataObject($billingAddress),
new \Magento\Framework\DataObject($shippingAddress),
$customerTaxClassId,
$scopeId,
$customerId
)->willReturn($rateRequest);
$salableInterface = $this->getMockBuilder(\Magento\Framework\Pricing\SaleableInterface::class)
->setMethods(['getTaxClassId'])
->getMockForAbstractClass();
$priceBox->expects($this->once())->method('getSaleableItem')->willReturn($salableInterface);
$salableInterface->expects($this->once())->method('getTaxClassId')->willReturn($customerTaxClassId);
$resource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
->setMethods(['getRateIds'])
->getMockForAbstractClass();
$this->taxCalculation->expects($this->once())->method('getResource')->willReturn($resource);
$resource->expects($this->once())->method('getRateIds')->with($rateRequest)->willReturn($rateIds);

$this->assertEquals($expected, $this->priceBoxTags->afterGetCacheKey($priceBox, $result));
}
}
Loading

0 comments on commit 8a1f653

Please sign in to comment.