Skip to content

Commit 8ee0f74

Browse files
author
Yaroslav Onischenko
committed
Merge remote-tracking branch 'origin/MAGETWO-51586' into develop
2 parents 5967155 + 8b92c68 commit 8ee0f74

File tree

2 files changed

+196
-2
lines changed

2 files changed

+196
-2
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
}
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)