Skip to content

Commit 8b92c68

Browse files
author
Leonid Poluyanov
committed
MAGETWO-51586: Category page cached for different users from one web client
1 parent 5a0c668 commit 8b92c68

File tree

2 files changed

+156
-6
lines changed

2 files changed

+156
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,17 @@ class PriceBoxTags
4747
* @param TimezoneInterface $dateTime
4848
* @param ScopeResolverInterface $scopeResolver
4949
* @param Session $customerSession
50-
* @param TaxCalculation $taxCalculation
5150
*/
5251
public function __construct(
5352
PriceCurrencyInterface $priceCurrency,
5453
TimezoneInterface $dateTime,
5554
ScopeResolverInterface $scopeResolver,
56-
Session $customerSession,
57-
TaxCalculation $taxCalculation
55+
Session $customerSession
5856
) {
5957
$this->dateTime = $dateTime;
6058
$this->customerSession = $customerSession;
6159
$this->priceCurrency = $priceCurrency;
6260
$this->scopeResolver = $scopeResolver;
63-
$this->taxCalculation = $taxCalculation;
6461
}
6562

6663
/**
@@ -104,7 +101,7 @@ private function getTaxRateIds(PriceBox $subject)
104101
}
105102

106103
if (!empty($billingAddress) || !empty($shippingAddress)) {
107-
$rateRequest = $this->taxCalculation->getRateRequest(
104+
$rateRequest = $this->getTaxCalculation()->getRateRequest(
108105
$billingAddress,
109106
$shippingAddress,
110107
$customerTaxClassId,
@@ -113,9 +110,24 @@ private function getTaxRateIds(PriceBox $subject)
113110
);
114111

115112
$rateRequest->setProductClassId($subject->getSaleableItem()->getTaxClassId());
116-
$rateIds = $this->taxCalculation->getResource()->getRateIds($rateRequest);
113+
$rateIds = $this->getTaxCalculation()->getResource()->getRateIds($rateRequest);
117114
}
118115

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

0 commit comments

Comments
 (0)