Skip to content

Commit 46b3f5c

Browse files
author
Alex Paliarush
committed
MAGETWO-61120: Tax and Order total not correct when discount is applied
- Added tests for RoundingErrors Notification
1 parent d9bb0d9 commit 46b3f5c

File tree

5 files changed

+160
-22
lines changed

5 files changed

+160
-22
lines changed

app/code/Magento/Tax/Model/System/Message/Notification/DiscountErrors.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function getSeverity()
116116
* @param null|int|bool|string|\Magento\Store\Model\Store $store $store
117117
* @return bool
118118
*/
119-
private function checkDiscountSettings($store = null)
119+
private function checkSettings($store = null)
120120
{
121121
return $this->taxConfig->applyTaxAfterDiscount($store);
122122
}
@@ -135,7 +135,7 @@ private function getStoresWithWrongSettings()
135135
$this->storesWithInvalidSettings = [];
136136
$storeCollection = $this->storeManager->getStores(true);
137137
foreach ($storeCollection as $store) {
138-
if (!$this->checkDiscountSettings($store)) {
138+
if (!$this->checkSettings($store)) {
139139
$website = $store->getWebsite();
140140
$this->storesWithInvalidSettings[] = $website->getName() . ' (' . $store->getName() . ')';
141141
}

app/code/Magento/Tax/Model/System/Message/Notification/RoundingErrors.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function __construct(
5151

5252
/**
5353
* {@inheritdoc}
54+
* @codeCoverageIgnore
5455
*/
5556
public function getIdentity()
5657
{
@@ -62,18 +63,9 @@ public function getIdentity()
6263
*/
6364
public function isDisplayed()
6465
{
65-
// Check if we are ignoring all notifications
66-
if ($this->taxConfig->isWrongDisplaySettingsIgnored()) {
67-
return false;
68-
}
69-
70-
$this->storesWithInvalidSettings = $this->getStoresWithWrongSettings();
71-
72-
// Check if we have valid tax notifications
73-
if ((!empty($this->storesWithInvalidSettings) && !$this->taxConfig->isWrongDisplaySettingsIgnored())) {
66+
if (!$this->taxConfig->isWrongDisplaySettingsIgnored() && $this->getStoresWithWrongSettings()) {
7467
return true;
7568
}
76-
7769
return false;
7870
}
7971

@@ -84,12 +76,12 @@ public function getText()
8476
{
8577
$messageDetails = '';
8678

87-
if (!empty($this->storesWithInvalidSettings) && !$this->taxConfig->isWrongDisplaySettingsIgnored()) {
79+
if (!empty($this->getStoresWithWrongSettings()) && !$this->taxConfig->isWrongDisplaySettingsIgnored()) {
8880
$messageDetails .= '<strong>';
8981
$messageDetails .= __('Warning tax configuration can result in rounding errors. ');
9082
$messageDetails .= '</strong><p>';
9183
$messageDetails .= __('Store(s) affected: ');
92-
$messageDetails .= implode(', ', $this->storesWithInvalidSettings);
84+
$messageDetails .= implode(', ', $this->getStoresWithWrongSettings());
9385
$messageDetails .= '</p><p>';
9486
$messageDetails .= __(
9587
'Click on the link to <a href="%1">ignore this notification</a>',
@@ -103,6 +95,7 @@ public function getText()
10395

10496
/**
10597
* {@inheritdoc}
98+
* @codeCoverageIgnore
10699
*/
107100
public function getSeverity()
108101
{
@@ -119,7 +112,7 @@ public function getSeverity()
119112
* @param null|int|bool|string|\Magento\Store\Model\Store $store $store
120113
* @return bool
121114
*/
122-
private function checkDisplaySettings($store = null)
115+
private function checkSettings($store = null)
123116
{
124117
if ($this->taxConfig->getAlgorithm($store) == \Magento\Tax\Model\Calculation::CALC_UNIT_BASE) {
125118
return true;
@@ -142,14 +135,17 @@ private function checkDisplaySettings($store = null)
142135
*/
143136
private function getStoresWithWrongSettings()
144137
{
145-
$storeNames = [];
138+
if (null !== $this->storesWithInvalidSettings) {
139+
return $this->storesWithInvalidSettings;
140+
}
141+
$this->storesWithInvalidSettings = [];
146142
$storeCollection = $this->storeManager->getStores(true);
147143
foreach ($storeCollection as $store) {
148-
if (!$this->checkDisplaySettings($store)) {
144+
if (!$this->checkSettings($store)) {
149145
$website = $store->getWebsite();
150-
$storeNames[] = $website->getName() . ' (' . $store->getName() . ')';
146+
$this->storesWithInvalidSettings[] = $website->getName() . ' (' . $store->getName() . ')';
151147
}
152148
}
153-
return $storeNames;
149+
return $this->storesWithInvalidSettings;
154150
}
155151
}

app/code/Magento/Tax/Model/System/Message/Notifications.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function getManageUrl()
156156
* @param null|int|bool|string|\Magento\Store\Model\Store $store $store
157157
* @return bool
158158
* @deprecated
159-
* @see \Magento\Tax\Model\System\Message\Notification\RoundingErrors::checkDisplaySettings
159+
* @see \Magento\Tax\Model\System\Message\Notification\RoundingErrors::checkSettings
160160
*/
161161
public function checkDisplaySettings($store = null)
162162
{
@@ -183,7 +183,7 @@ public function checkDisplaySettings($store = null)
183183
* @param null|int|bool|string|\Magento\Store\Model\Store $store $store
184184
* @return bool
185185
* @deprecated
186-
* @see \Magento\Tax\Model\System\Message\Notification\DiscountErrors::checkDiscountSettings
186+
* @see \Magento\Tax\Model\System\Message\Notification\DiscountErrors::checkSettings
187187
*/
188188
public function checkDiscountSettings($store = null)
189189
{

app/code/Magento/Tax/Test/Unit/Model/System/Message/Notification/DiscountErrorsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1212
use Magento\Framework\UrlInterface;
1313
use Magento\Store\Api\Data\StoreInterface;
14-
use \Magento\Store\Api\Data\WebsiteInterface;
14+
use Magento\Store\Api\Data\WebsiteInterface;
1515

1616
/**
1717
* Test class for @see \Magento\Tax\Model\System\Message\Notification\DiscountErrors
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Tax\Test\Unit\Model\System\Message\Notification;
7+
8+
use Magento\Tax\Model\Config as TaxConfig;
9+
use Magento\Tax\Model\System\Message\Notification\RoundingErrors as RoundingErrorsNotification;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Framework\UrlInterface;
13+
use Magento\Store\Api\Data\StoreInterface;
14+
use Magento\Store\Api\Data\WebsiteInterface;
15+
16+
/**
17+
* Test class for @see \Magento\Tax\Model\System\Message\Notification\RoundingErrors
18+
*/
19+
class RoundingErrorsTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var RoundingErrorsNotification
23+
*/
24+
private $roundingErrorsNotification;
25+
26+
/**
27+
* @var StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
private $storeManagerMock;
30+
31+
/**
32+
* @var UrlInterface | \PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
private $urlBuilderMock;
35+
36+
/**
37+
* @var TaxConfig | \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
private $taxConfigMock;
40+
41+
protected function setUp()
42+
{
43+
parent::setUp();
44+
45+
$websiteMock = $this->getMock(WebsiteInterface::class, [], [], '', false);
46+
$websiteMock->expects($this->any())->method('getName')->willReturn('testWebsiteName');
47+
$storeMock = $this->getMockForAbstractClass(
48+
StoreInterface::class,
49+
[],
50+
'',
51+
false,
52+
true,
53+
true,
54+
['getWebsite', 'getName']
55+
);
56+
$storeMock->expects($this->any())->method('getName')->willReturn('testStoreName');
57+
$storeMock->expects($this->any())->method('getWebsite')->willReturn($websiteMock);
58+
$this->storeManagerMock = $this->getMock(StoreManagerInterface::class, [], [], '', false);
59+
$this->storeManagerMock->expects($this->any())->method('getStores')->willReturn([$storeMock]);
60+
61+
$this->urlBuilderMock = $this->getMock(UrlInterface::class, [], [], '', false);
62+
$this->taxConfigMock = $this->getMock(TaxConfig::class, [], [], '', false);
63+
$this->roundingErrorsNotification = (new ObjectManager($this))->getObject(
64+
RoundingErrorsNotification::class,
65+
[
66+
'storeManager' => $this->storeManagerMock,
67+
'urlBuilder' => $this->urlBuilderMock,
68+
'taxConfig' => $this->taxConfigMock,
69+
]
70+
);
71+
}
72+
73+
public function testIsDisplayedNotDisplayedUnitBased()
74+
{
75+
$this->taxConfigMock->expects($this->any())->method('isWrongDisplaySettingsIgnored')->willReturn(false);
76+
77+
$this->taxConfigMock->expects($this->any())
78+
->method('getAlgorithm')->willReturn(\Magento\Tax\Model\Calculation::CALC_UNIT_BASE);
79+
80+
$this->taxConfigMock->expects($this->any())
81+
->method('getPriceDisplayType')->willReturn(\Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX);
82+
$this->taxConfigMock->expects($this->any())
83+
->method('getShippingPriceDisplayType')->willReturn(\Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX);
84+
85+
$this->taxConfigMock->expects($this->any())->method('displayCartPricesBoth')->willReturn(false);
86+
$this->taxConfigMock->expects($this->any())->method('displayCartSubtotalBoth')->willReturn(false);
87+
$this->taxConfigMock->expects($this->any())->method('displayCartShippingBoth')->willReturn(false);
88+
$this->taxConfigMock->expects($this->any())->method('displaySalesPricesBoth')->willReturn(false);
89+
$this->taxConfigMock->expects($this->any())->method('displaySalesSubtotalBoth')->willReturn(false);
90+
91+
$this->taxConfigMock->expects($this->any())->method('displaySalesShippingBoth')->willReturn(true);
92+
93+
$this->assertFalse($this->roundingErrorsNotification->isDisplayed());
94+
}
95+
96+
public function testIsDisplayedNotDisplayed()
97+
{
98+
$this->taxConfigMock->expects($this->any())->method('isWrongDisplaySettingsIgnored')->willReturn(false);
99+
100+
$this->taxConfigMock->expects($this->any())
101+
->method('getAlgorithm')->willReturn(\Magento\Tax\Model\Calculation::CALC_ROW_BASE);
102+
103+
$this->taxConfigMock->expects($this->any())
104+
->method('getPriceDisplayType')->willReturn(\Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX);
105+
$this->taxConfigMock->expects($this->any())
106+
->method('getShippingPriceDisplayType')->willReturn(\Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX);
107+
108+
$this->taxConfigMock->expects($this->any())->method('displayCartPricesBoth')->willReturn(false);
109+
$this->taxConfigMock->expects($this->any())->method('displayCartSubtotalBoth')->willReturn(false);
110+
$this->taxConfigMock->expects($this->any())->method('displayCartShippingBoth')->willReturn(false);
111+
$this->taxConfigMock->expects($this->any())->method('displaySalesPricesBoth')->willReturn(false);
112+
$this->taxConfigMock->expects($this->any())->method('displaySalesSubtotalBoth')->willReturn(false);
113+
$this->taxConfigMock->expects($this->any())->method('displaySalesShippingBoth')->willReturn(false);
114+
115+
$this->assertFalse($this->roundingErrorsNotification->isDisplayed());
116+
}
117+
118+
public function testIsDisplayedIgnoreWrongConfiguration()
119+
{
120+
$this->taxConfigMock->expects($this->any())->method('isWrongDisplaySettingsIgnored')->willReturn(true);
121+
$this->assertFalse($this->roundingErrorsNotification->isDisplayed());
122+
}
123+
124+
public function testGetText()
125+
{
126+
$this->taxConfigMock->expects($this->any())->method('isWrongDisplaySettingsIgnored')->willReturn(false);
127+
128+
$this->taxConfigMock->expects($this->any())->method('displaySalesShippingBoth')->willReturn(true);
129+
130+
$this->urlBuilderMock->expects($this->any())
131+
->method('getUrl')
132+
->with('tax/tax/ignoreTaxNotification', ['section' => 'price_display'])
133+
->willReturn('http://example.com');
134+
$this->roundingErrorsNotification->isDisplayed();
135+
$this->assertEquals(
136+
'<strong>Warning tax configuration can result in rounding errors. '
137+
. '</strong><p>Store(s) affected: testWebsiteName (testStoreName)</p><p>Click on the link to '
138+
. '<a href="http://example.com">ignore this notification</a></p>',
139+
$this->roundingErrorsNotification->getText()
140+
);
141+
}
142+
}

0 commit comments

Comments
 (0)