Skip to content

Commit 58224cf

Browse files
committed
MC-19827: Escape translation strings.
Use object manager instead of mock object. Refactor localized exception message.
1 parent bc3e29a commit 58224cf

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

app/code/Magento/Bundle/Test/Unit/Block/Catalog/Product/View/Type/BundleTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Bundle\Block\Catalog\Product\View\Type\Bundle as BundleBlock;
99
use Magento\Catalog\Block\Product\Context;
10+
use \Magento\Framework\Escaper;
1011

1112
/**
1213
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -89,10 +90,8 @@ protected function setUp()
8990
->disableOriginalConstructor()
9091
->getMock();
9192

92-
$this->escaper = $this->createPartialMock(
93-
\Magento\Framework\Escaper::class,
94-
['escapeHtml']
95-
);
93+
$this->escaper = $objectHelper->getObject(Escaper::class);
94+
9695
$this->context->expects($this->once())->method('getRegistry')->willReturn($registry);
9796
$this->context->expects($this->once())->method('getEscaper')->willReturn($this->escaper);
9897
$this->context->expects($this->once())->method('getEventManager')->willReturn($this->eventManager);
@@ -127,10 +126,8 @@ public function testGetOptionHtmlNoRenderer()
127126
->disableOriginalConstructor()
128127
->getMock();
129128
$option->expects($this->any())->method('getType')->willReturn('checkbox');
129+
$expected='There is no defined renderer for "checkbox" option type.';
130130

131-
$data='There is no defined renderer for "checkbox" option type.';
132-
$expected='There is no defined renderer for "checkbox" option type.';
133-
$this->escaper->expects($this->once())->method('escapeHtml')->with($data)->willReturn($expected);
134131
$layout = $this->getMockBuilder(\Magento\Framework\View\Layout::class)
135132
->setMethods(['getChildName', 'getBlock'])
136133
->disableOriginalConstructor()

app/code/Magento/Checkout/Block/Cart.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Checkout\Block;
77

88
use Magento\Customer\Model\Context;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Phrase;
911

1012
/**
1113
* Shopping cart block
@@ -219,7 +221,7 @@ public function getMethodHtml($name)
219221
{
220222
$block = $this->getLayout()->getBlock($name);
221223
if (!$block) {
222-
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid method: %1', $this->escapeHtml($name)));
224+
throw new LocalizedException(new Phrase($this->escapeHtml(__('Invalid method: %1', $name))));
223225
}
224226
return $block->toHtml();
225227
}

app/code/Magento/Checkout/Test/Unit/Block/CartTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function setUp()
3838
$quoteMock = $this->createMock(Quote::class);
3939
$checkoutSession = $this->createMock(Session::class);
4040
$this->layoutMock = $this->createMock(LayoutInterface::class);
41-
$this->escaper = $this->createMock(Escaper::class);
41+
$this->escaper = $objectManager->getObject(Escaper::class);
4242
$quoteMock->expects($this->once())->method('getAllVisibleItems')->willReturn([]);
4343
$checkoutSession->expects($this->any())->method('getQuote')->willReturn($quoteMock);
4444
$this->context->expects($this->once())->method('getEscaper')->willReturn($this->escaper);
@@ -59,8 +59,6 @@ public function testGetMethodHtmlWithException()
5959
{
6060
$this->layoutMock->expects($this->any())->method('getBlock')->willReturn(false);
6161
$name='blockMethod';
62-
$expected='blockMethod';
63-
$this->escaper->expects($this->once())->method('escapeHtml')->with($name)->willReturn($expected);
6462
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
6563
$this->expectExceptionMessage(
6664
(string)__('Invalid method: %1', $name)

app/code/Magento/Directory/Test/Unit/Block/DataTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Store\Model\ScopeInterface;
1818
use Magento\Store\Model\Store;
1919
use Magento\Store\Model\StoreManagerInterface;
20+
use Magento\Framework\Escaper;
2021

2122
/**
2223
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -57,13 +58,14 @@ class DataTest extends \PHPUnit\Framework\TestCase
5758
private $serializerMock;
5859

5960
/**
60-
* @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
61+
* @var \Magento\Framework\Escaper
6162
*/
62-
private $escaperMock;
63+
private $escaper;
6364

6465
protected function setUp()
6566
{
6667
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
68+
$this->escaper= $objectManagerHelper->getObject(Escaper::class);
6769
$this->prepareContext();
6870

6971
$this->helperDataMock = $this->getMockBuilder(\Magento\Directory\Helper\Data::class)
@@ -129,12 +131,7 @@ protected function prepareContext()
129131
->method('getLayout')
130132
->willReturn($this->layoutMock);
131133

132-
$this->escaperMock = $this->createPartialMock(
133-
\Magento\Framework\Escaper::class,
134-
['escapeHtml']
135-
);
136-
137-
$this->contextMock->expects($this->once())->method('getEscaper')->willReturn($this->escaperMock);
134+
$this->contextMock->expects($this->once())->method('getEscaper')->willReturn($this->escaper);
138135
}
139136

140137
protected function prepareCountryCollection()
@@ -312,8 +309,6 @@ protected function mockElementHtmlSelect($defaultCountry, $options, $resultHtml)
312309
)
313310
->getMock();
314311

315-
$this->escaperMock->expects($this->once())->method('escapeHtml')->with(__($title))->willReturn($title);
316-
317312
$elementHtmlSelect->expects($this->once())
318313
->method('setName')
319314
->with($name)

0 commit comments

Comments
 (0)