Skip to content

Commit 8a1f653

Browse files
author
Onischenko, Yaroslav(yonischenko)
committed
Merge pull request #622 from magento-nord/develop
[NORD] Bug fixes
2 parents 08eebe4 + 6702780 commit 8a1f653

File tree

6 files changed

+388
-36
lines changed

6 files changed

+388
-36
lines changed

app/code/Magento/Catalog/Block/Category/Plugin/PriceBoxTags.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Pricing\PriceCurrencyInterface;
1313
use Magento\Framework\Pricing\Render\PriceBox;
1414
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
15+
use Magento\Tax\Model\Calculation as TaxCalculation;
1516

1617
class PriceBoxTags
1718
{
@@ -35,6 +36,11 @@ class PriceBoxTags
3536
*/
3637
private $scopeResolver;
3738

39+
/**
40+
* @var TaxCalculation
41+
*/
42+
private $taxCalculation;
43+
3844
/**
3945
* PriceBoxTags constructor.
4046
* @param PriceCurrencyInterface $priceCurrency
@@ -58,8 +64,6 @@ public function __construct(
5864
* @param PriceBox $subject
5965
* @param string $result
6066
* @return string
61-
*
62-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6367
*/
6468
public function afterGetCacheKey(PriceBox $subject, $result)
6569
{
@@ -71,7 +75,59 @@ public function afterGetCacheKey(PriceBox $subject, $result)
7175
$this->dateTime->scopeDate($this->scopeResolver->getScope()->getId())->format('Ymd'),
7276
$this->scopeResolver->getScope()->getId(),
7377
$this->customerSession->getCustomerGroupId(),
78+
$this->getTaxRateIds($subject),
7479
]
7580
);
7681
}
82+
83+
/**
84+
* @param PriceBox $subject
85+
* @return string
86+
*/
87+
private function getTaxRateIds(PriceBox $subject)
88+
{
89+
$rateIds = [];
90+
91+
$customerSession = $this->customerSession;
92+
$billingAddress = $customerSession->getDefaultTaxBillingAddress();
93+
$shippingAddress = $customerSession->getDefaultTaxShippingAddress();
94+
$customerTaxClassId = $customerSession->getCustomerTaxClassId();
95+
96+
if (!empty($billingAddress)) {
97+
$billingAddress = new \Magento\Framework\DataObject($billingAddress);
98+
}
99+
if (!empty($shippingAddress)) {
100+
$shippingAddress = new \Magento\Framework\DataObject($shippingAddress);
101+
}
102+
103+
if (!empty($billingAddress) || !empty($shippingAddress)) {
104+
$rateRequest = $this->getTaxCalculation()->getRateRequest(
105+
$billingAddress,
106+
$shippingAddress,
107+
$customerTaxClassId,
108+
$this->scopeResolver->getScope()->getId(),
109+
$this->customerSession->getCustomerId()
110+
);
111+
112+
$rateRequest->setProductClassId($subject->getSaleableItem()->getTaxClassId());
113+
$rateIds = $this->getTaxCalculation()->getResource()->getRateIds($rateRequest);
114+
}
115+
116+
return implode('_', $rateIds);
117+
}
118+
119+
/**
120+
* Get the TaxCalculation model
121+
*
122+
* @return \Magento\Tax\Model\Calculation
123+
*
124+
* @deprecated
125+
*/
126+
private function getTaxCalculation()
127+
{
128+
if ($this->taxCalculation === null) {
129+
$this->taxCalculation = \Magento\Framework\App\ObjectManager::getInstance()->get(TaxCalculation::class);
130+
}
131+
return $this->taxCalculation;
132+
}
77133
}

app/code/Magento/Catalog/Model/Indexer/Product/Category/Action/Rows.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
namespace Magento\Catalog\Model\Indexer\Product\Category\Action;
77

8+
use Magento\Catalog\Model\Category;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Framework\Indexer\CacheContext;
11+
812
class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractAction
913
{
1014
/**
@@ -14,6 +18,11 @@ class Rows extends \Magento\Catalog\Model\Indexer\Category\Product\AbstractActio
1418
*/
1519
protected $limitationByProducts;
1620

21+
/**
22+
* @var \Magento\Framework\Indexer\CacheContext
23+
*/
24+
private $cacheContext;
25+
1726
/**
1827
* Refresh entities index
1928
*
@@ -30,9 +39,43 @@ public function execute(array $entityIds = [], $useTempTable = false)
3039

3140
$this->reindex();
3241

42+
$this->registerProducts($entityIds);
43+
$this->registerCategories($entityIds);
44+
3345
return $this;
3446
}
3547

48+
/**
49+
* Register affected products
50+
*
51+
* @param array $entityIds
52+
* @return void
53+
*/
54+
private function registerProducts($entityIds)
55+
{
56+
$this->getCacheContext()->registerEntities(Product::CACHE_TAG, $entityIds);
57+
}
58+
59+
/**
60+
* Register categories assigned to products
61+
*
62+
* @param array $entityIds
63+
* @return void
64+
*/
65+
private function registerCategories($entityIds)
66+
{
67+
$categories = $this->connection->fetchCol(
68+
$this->connection->select()
69+
->from($this->getMainTable(), ['category_id'])
70+
->where('product_id IN (?)', $entityIds)
71+
->distinct()
72+
);
73+
74+
if ($categories) {
75+
$this->getCacheContext()->registerEntities(Category::CACHE_TAG, $categories);
76+
}
77+
}
78+
3679
/**
3780
* Remove index entries before reindexation
3881
*
@@ -91,4 +134,18 @@ protected function isRangingNeeded()
91134
{
92135
return false;
93136
}
137+
138+
/**
139+
* Get cache context
140+
*
141+
* @return \Magento\Framework\Indexer\CacheContext
142+
* @deprecated
143+
*/
144+
private function getCacheContext()
145+
{
146+
if ($this->cacheContext === null) {
147+
$this->cacheContext = \Magento\Framework\App\ObjectManager::getInstance()->get(CacheContext::class);
148+
}
149+
return $this->cacheContext;
150+
}
94151
}

app/code/Magento/Catalog/Plugin/Model/Product/Action/UpdateAttributesFlushCache.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\Catalog\Plugin\Model\Product\Action;
77

8-
use Magento\Catalog\Model\Product;
98
use Magento\Catalog\Model\Product\Action;
109
use Magento\Framework\Indexer\CacheContext;
1110
use Magento\Framework\Event\ManagerInterface as EventManager;
@@ -36,26 +35,28 @@ public function __construct(
3635

3736
/**
3837
* @param Action $subject
39-
* @param \Closure $proceed
40-
* @param array $productIds
41-
* @param array $attrData
42-
* @param int $storeId
38+
* @param Action $result
4339
* @return Action
4440
*
4541
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4642
*/
47-
public function aroundUpdateAttributes(
48-
Action $subject,
49-
\Closure $proceed,
50-
$productIds,
51-
$attrData,
52-
$storeId
43+
public function afterUpdateAttributes(
44+
\Magento\Catalog\Model\Product\Action $subject,
45+
\Magento\Catalog\Model\Product\Action $result
5346
) {
54-
$returnValue = $proceed($productIds, $attrData, $storeId);
55-
56-
$this->cacheContext->registerEntities(Product::CACHE_TAG, $productIds);
5747
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
48+
return $result;
49+
}
5850

59-
return $returnValue;
51+
/**
52+
* @param Action $subject
53+
* @return void
54+
*
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function afterUpdateWebsites(
58+
\Magento\Catalog\Model\Product\Action $subject
59+
) {
60+
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
6061
}
6162
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Unit\Block\Category\Plugin;
8+
9+
/**
10+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11+
*/
12+
class PriceBoxTagsTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var \Magento\Framework\Pricing\PriceCurrencyInterface | \PHPUnit_Framework_MockObject_MockObject
16+
*/
17+
private $priceCurrencyInterface;
18+
19+
/**
20+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface | \PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $timezoneInterface;
23+
24+
/**
25+
* @var \Magento\Framework\App\ScopeResolverInterface | \PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
private $scopeResolverInterface;
28+
29+
/**
30+
* @var \Magento\Customer\Model\Session | \PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $session;
33+
34+
/**
35+
* @var \Magento\Tax\Model\Calculation | \PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $taxCalculation;
38+
39+
/**
40+
* @var \Magento\Catalog\Block\Category\Plugin\PriceBoxTags
41+
*/
42+
private $priceBoxTags;
43+
44+
protected function setUp()
45+
{
46+
$this->priceCurrencyInterface = $this->getMockBuilder(
47+
\Magento\Framework\Pricing\PriceCurrencyInterface::class
48+
)->getMock();
49+
$this->timezoneInterface = $this->getMockBuilder(
50+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class
51+
)->getMock();
52+
$this->scopeResolverInterface = $this->getMockBuilder(
53+
\Magento\Framework\App\ScopeResolverInterface::class
54+
)
55+
->getMockForAbstractClass();
56+
$this->session = $this->getMockBuilder(\Magento\Customer\Model\Session::class)->disableOriginalConstructor()
57+
->setMethods(
58+
[
59+
'getCustomerGroupId',
60+
'getDefaultTaxBillingAddress',
61+
'getDefaultTaxShippingAddress',
62+
'getCustomerTaxClassId',
63+
'getCustomerId'
64+
]
65+
)
66+
->getMock();
67+
$this->taxCalculation = $this->getMockBuilder(\Magento\Tax\Model\Calculation::class)
68+
->disableOriginalConstructor()->getMock();
69+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
70+
$this->priceBoxTags = $objectManager->getObject(
71+
\Magento\Catalog\Block\Category\Plugin\PriceBoxTags::class,
72+
[
73+
'priceCurrency' => $this->priceCurrencyInterface,
74+
'dateTime' => $this->timezoneInterface,
75+
'scopeResolver' => $this->scopeResolverInterface,
76+
'customerSession' => $this->session,
77+
'taxCalculation' => $this->taxCalculation
78+
]
79+
);
80+
}
81+
82+
public function testAfterGetCacheKey()
83+
{
84+
$date = date('Ymd');
85+
$currencySymbol = '$';
86+
$result = 'result_string';
87+
$billingAddress = ['billing_address'];
88+
$shippingAddress = ['shipping_address'];
89+
$scopeId = 1;
90+
$customerGroupId = 2;
91+
$customerTaxClassId = 3;
92+
$customerId = 4;
93+
$rateIds = [5,6];
94+
$expected = implode(
95+
'-',
96+
[
97+
$result,
98+
$currencySymbol,
99+
$date,
100+
$scopeId,
101+
$customerGroupId,
102+
implode('_', $rateIds)
103+
]
104+
);
105+
$priceBox = $this->getMockBuilder(\Magento\Framework\Pricing\Render\PriceBox::class)
106+
->disableOriginalConstructor()->getMock();
107+
$this->priceCurrencyInterface->expects($this->once())->method('getCurrencySymbol')->willReturn($currencySymbol);
108+
$scope = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)->getMock();
109+
$this->scopeResolverInterface->expects($this->any())->method('getScope')->willReturn($scope);
110+
$scope->expects($this->any())->method('getId')->willReturn($scopeId);
111+
$dateTime = $this->getMockBuilder(\DateTime::class)->getMock();
112+
$this->timezoneInterface->expects($this->any())->method('scopeDate')->with($scopeId)->willReturn($dateTime);
113+
$dateTime->expects($this->any())->method('format')->with('Ymd')->willReturn($date);
114+
$this->session->expects($this->once())->method('getCustomerGroupId')->willReturn($customerGroupId);
115+
$this->session->expects($this->once())->method('getDefaultTaxBillingAddress')->willReturn($billingAddress);
116+
$this->session->expects($this->once())->method('getDefaultTaxShippingAddress')->willReturn($shippingAddress);
117+
$this->session->expects($this->once())->method('getCustomerTaxClassId')
118+
->willReturn($customerTaxClassId);
119+
$this->session->expects($this->once())->method('getCustomerId')->willReturn($customerId);
120+
$rateRequest = $this->getMockBuilder(\Magento\Framework\DataObject::class)->getMock();
121+
$this->taxCalculation->expects($this->once())->method('getRateRequest')->with(
122+
new \Magento\Framework\DataObject($billingAddress),
123+
new \Magento\Framework\DataObject($shippingAddress),
124+
$customerTaxClassId,
125+
$scopeId,
126+
$customerId
127+
)->willReturn($rateRequest);
128+
$salableInterface = $this->getMockBuilder(\Magento\Framework\Pricing\SaleableInterface::class)
129+
->setMethods(['getTaxClassId'])
130+
->getMockForAbstractClass();
131+
$priceBox->expects($this->once())->method('getSaleableItem')->willReturn($salableInterface);
132+
$salableInterface->expects($this->once())->method('getTaxClassId')->willReturn($customerTaxClassId);
133+
$resource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
134+
->setMethods(['getRateIds'])
135+
->getMockForAbstractClass();
136+
$this->taxCalculation->expects($this->once())->method('getResource')->willReturn($resource);
137+
$resource->expects($this->once())->method('getRateIds')->with($rateRequest)->willReturn($rateIds);
138+
139+
$this->assertEquals($expected, $this->priceBoxTags->afterGetCacheKey($priceBox, $result));
140+
}
141+
}

0 commit comments

Comments
 (0)