Skip to content

Commit 66d259e

Browse files
MC-35707: Invoice PDF is not generating in correct language
1 parent d76acc2 commit 66d259e

File tree

6 files changed

+463
-21
lines changed

6 files changed

+463
-21
lines changed

app/code/Magento/Sales/Model/Order/Pdf/Creditmemo.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Creditmemo extends AbstractPdf
2020
*/
2121
protected $_storeManager;
2222

23+
/**
24+
* @var \Magento\Store\Model\App\Emulation
25+
*/
26+
private $appEmulation;
27+
2328
/**
2429
* @param \Magento\Payment\Helper\Data $paymentData
2530
* @param \Magento\Framework\Stdlib\StringUtils $string
@@ -32,7 +37,7 @@ class Creditmemo extends AbstractPdf
3237
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
3338
* @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
3439
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
35-
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
40+
* @param \Magento\Store\Model\App\Emulation|null $appEmulation
3641
* @param array $data
3742
*
3843
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -50,11 +55,11 @@ public function __construct(
5055
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
5156
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
5257
\Magento\Store\Model\StoreManagerInterface $storeManager,
53-
\Magento\Framework\Locale\ResolverInterface $localeResolver,
58+
\Magento\Store\Model\App\Emulation $appEmulation,
5459
array $data = []
5560
) {
5661
$this->_storeManager = $storeManager;
57-
$this->_localeResolver = $localeResolver;
62+
$this->appEmulation = $appEmulation;
5863
parent::__construct(
5964
$paymentData,
6065
$string,
@@ -150,7 +155,11 @@ public function getPdf($creditmemos = [])
150155

151156
foreach ($creditmemos as $creditmemo) {
152157
if ($creditmemo->getStoreId()) {
153-
$this->_localeResolver->emulate($creditmemo->getStoreId());
158+
$this->appEmulation->startEnvironmentEmulation(
159+
$creditmemo->getStoreId(),
160+
\Magento\Framework\App\Area::AREA_FRONTEND,
161+
true
162+
);
154163
$this->_storeManager->setCurrentStore($creditmemo->getStoreId());
155164
}
156165
$page = $this->newPage();
@@ -185,7 +194,7 @@ public function getPdf($creditmemos = [])
185194
/* Add totals */
186195
$this->insertTotals($page, $creditmemo);
187196
if ($creditmemo->getStoreId()) {
188-
$this->_localeResolver->revert();
197+
$this->appEmulation->stopEnvironmentEmulation();
189198
}
190199
}
191200
$this->_afterGetPdf();

app/code/Magento/Sales/Model/Order/Pdf/Invoice.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Invoice extends AbstractPdf
1919
protected $_storeManager;
2020

2121
/**
22-
* @var \Magento\Framework\Locale\ResolverInterface
22+
* @var \Magento\Store\Model\App\Emulation
2323
*/
24-
protected $_localeResolver;
24+
private $appEmulation;
2525

2626
/**
2727
* @param \Magento\Payment\Helper\Data $paymentData
@@ -35,7 +35,7 @@ class Invoice extends AbstractPdf
3535
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
3636
* @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
3737
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
38-
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
38+
* @param \Magento\Store\Model\App\Emulation $appEmulation
3939
* @param array $data
4040
*
4141
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -52,11 +52,11 @@ public function __construct(
5252
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
5353
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
5454
\Magento\Store\Model\StoreManagerInterface $storeManager,
55-
\Magento\Framework\Locale\ResolverInterface $localeResolver,
55+
\Magento\Store\Model\App\Emulation $appEmulation,
5656
array $data = []
5757
) {
5858
$this->_storeManager = $storeManager;
59-
$this->_localeResolver = $localeResolver;
59+
$this->appEmulation = $appEmulation;
6060
parent::__construct(
6161
$paymentData,
6262
$string,
@@ -127,7 +127,11 @@ public function getPdf($invoices = [])
127127

128128
foreach ($invoices as $invoice) {
129129
if ($invoice->getStoreId()) {
130-
$this->_localeResolver->emulate($invoice->getStoreId());
130+
$this->appEmulation->startEnvironmentEmulation(
131+
$invoice->getStoreId(),
132+
\Magento\Framework\App\Area::AREA_FRONTEND,
133+
true
134+
);
131135
$this->_storeManager->setCurrentStore($invoice->getStoreId());
132136
}
133137
$page = $this->newPage();
@@ -162,7 +166,7 @@ public function getPdf($invoices = [])
162166
/* Add totals */
163167
$this->insertTotals($page, $invoice);
164168
if ($invoice->getStoreId()) {
165-
$this->_localeResolver->revert();
169+
$this->appEmulation->stopEnvironmentEmulation();
166170
}
167171
}
168172
$this->_afterGetPdf();

app/code/Magento/Sales/Model/Order/Pdf/Shipment.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Shipment extends AbstractPdf
1717
protected $_storeManager;
1818

1919
/**
20-
* @var \Magento\Framework\Locale\ResolverInterface
20+
* @var \Magento\Store\Model\App\Emulation
2121
*/
22-
protected $_localeResolver;
22+
private $appEmulation;
2323

2424
/**
2525
* @param \Magento\Payment\Helper\Data $paymentData
@@ -33,7 +33,7 @@ class Shipment extends AbstractPdf
3333
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
3434
* @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer
3535
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
36-
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
36+
* @param \Magento\Store\Model\App\Emulation $appEmulation
3737
* @param array $data
3838
*
3939
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -50,11 +50,11 @@ public function __construct(
5050
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
5151
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
5252
\Magento\Store\Model\StoreManagerInterface $storeManager,
53-
\Magento\Framework\Locale\ResolverInterface $localeResolver,
53+
\Magento\Store\Model\App\Emulation $appEmulation,
5454
array $data = []
5555
) {
5656
$this->_storeManager = $storeManager;
57-
$this->_localeResolver = $localeResolver;
57+
$this->appEmulation = $appEmulation;
5858
parent::__construct(
5959
$paymentData,
6060
$string,
@@ -118,7 +118,11 @@ public function getPdf($shipments = [])
118118
$this->_setFontBold($style, 10);
119119
foreach ($shipments as $shipment) {
120120
if ($shipment->getStoreId()) {
121-
$this->_localeResolver->emulate($shipment->getStoreId());
121+
$this->appEmulation->startEnvironmentEmulation(
122+
$shipment->getStoreId(),
123+
\Magento\Framework\App\Area::AREA_FRONTEND,
124+
true
125+
);
122126
$this->_storeManager->setCurrentStore($shipment->getStoreId());
123127
}
124128
$page = $this->newPage();
@@ -151,7 +155,7 @@ public function getPdf($shipments = [])
151155
$page = end($pdf->pages);
152156
}
153157
if ($shipment->getStoreId()) {
154-
$this->_localeResolver->revert();
158+
$this->appEmulation->stopEnvironmentEmulation();
155159
}
156160
}
157161
$this->_afterGetPdf();
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Test\Unit\Model\Order\Pdf;
7+
8+
use Magento\MediaStorage\Helper\File\Storage\Database;
9+
use Magento\Sales\Model\Order\Creditmemo;
10+
use Magento\Sales\Model\Order;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Sales\Model\Order\Address;
13+
use Magento\Sales\Model\Order\Address\Renderer;
14+
15+
/**
16+
* Class CreditmemoTest
17+
*
18+
* Tests Sales Order Creditmemo PDF model
19+
*
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
*/
22+
class CreditmemoTest extends \PHPUnit\Framework\TestCase
23+
{
24+
/**
25+
* @var \Magento\Sales\Model\Order\Pdf\Invoice
26+
*/
27+
protected $_model;
28+
29+
/**
30+
* @var \Magento\Sales\Model\Order\Pdf\Config|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
protected $_pdfConfigMock;
33+
34+
/**
35+
* @var Database|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
protected $databaseMock;
38+
39+
/**
40+
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
41+
*/
42+
protected $scopeConfigMock;
43+
44+
/**
45+
* @var \Magento\Framework\Filesystem\Directory\Write|\PHPUnit_Framework_MockObject_MockObject
46+
*/
47+
protected $directoryMock;
48+
49+
/**
50+
* @var Renderer|\PHPUnit_Framework_MockObject_MockObject
51+
*/
52+
protected $addressRendererMock;
53+
54+
/**
55+
* @var \Magento\Payment\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
56+
*/
57+
protected $paymentDataMock;
58+
59+
/**
60+
* @var \Magento\Store\Model\App\Emulation
61+
*/
62+
private $appEmulation;
63+
64+
protected function setUp()
65+
{
66+
$this->_pdfConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Pdf\Config::class)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
$this->directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
70+
$this->directoryMock->expects($this->any())->method('getAbsolutePath')->will(
71+
$this->returnCallback(
72+
function ($argument) {
73+
return BP . '/' . $argument;
74+
}
75+
)
76+
);
77+
$filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
78+
$filesystemMock->expects($this->any())
79+
->method('getDirectoryRead')
80+
->will($this->returnValue($this->directoryMock));
81+
$filesystemMock->expects($this->any())
82+
->method('getDirectoryWrite')
83+
->will($this->returnValue($this->directoryMock));
84+
85+
$this->databaseMock = $this->createMock(Database::class);
86+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
87+
$this->addressRendererMock = $this->createMock(Renderer::class);
88+
$this->paymentDataMock = $this->createMock(\Magento\Payment\Helper\Data::class);
89+
$this->appEmulation = $this->createMock(\Magento\Store\Model\App\Emulation::class);
90+
91+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
92+
$this->_model = $helper->getObject(
93+
\Magento\Sales\Model\Order\Pdf\Creditmemo::class,
94+
[
95+
'filesystem' => $filesystemMock,
96+
'pdfConfig' => $this->_pdfConfigMock,
97+
'fileStorageDatabase' => $this->databaseMock,
98+
'scopeConfig' => $this->scopeConfigMock,
99+
'addressRenderer' => $this->addressRendererMock,
100+
'string' => new \Magento\Framework\Stdlib\StringUtils(),
101+
'paymentData' => $this->paymentDataMock,
102+
'appEmulation' => $this->appEmulation
103+
]
104+
);
105+
}
106+
107+
public function testInsertLogoDatabaseMediaStorage()
108+
{
109+
$filename = 'image.jpg';
110+
$path = '/sales/store/logo/';
111+
$storeId = 1;
112+
113+
$this->appEmulation->expects($this->once())
114+
->method('startEnvironmentEmulation')
115+
->with(
116+
$storeId,
117+
\Magento\Framework\App\Area::AREA_FRONTEND,
118+
true
119+
)
120+
->willReturnSelf();
121+
$this->appEmulation->expects($this->once())
122+
->method('stopEnvironmentEmulation')
123+
->willReturnSelf();
124+
$this->_pdfConfigMock->expects($this->once())
125+
->method('getRenderersPerProduct')
126+
->with('creditmemo')
127+
->will($this->returnValue(['product_type_one' => 'Renderer_Type_One_Product_One']));
128+
$this->_pdfConfigMock->expects($this->any())
129+
->method('getTotals')
130+
->will($this->returnValue([]));
131+
132+
$block = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
133+
->disableOriginalConstructor()
134+
->setMethods(['setIsSecureMode','toPdf'])
135+
->getMock();
136+
$block->expects($this->any())
137+
->method('setIsSecureMode')
138+
->willReturn($block);
139+
$block->expects($this->any())
140+
->method('toPdf')
141+
->will($this->returnValue(''));
142+
$this->paymentDataMock->expects($this->any())
143+
->method('getInfoBlock')
144+
->willReturn($block);
145+
146+
$this->addressRendererMock->expects($this->any())
147+
->method('format')
148+
->will($this->returnValue(''));
149+
150+
$this->databaseMock->expects($this->any())
151+
->method('checkDbUsage')
152+
->will($this->returnValue(true));
153+
154+
$creditmemoMock = $this->createMock(Creditmemo::class);
155+
$orderMock = $this->createMock(Order::class);
156+
$addressMock = $this->createMock(Address::class);
157+
$orderMock->expects($this->any())
158+
->method('getBillingAddress')
159+
->willReturn($addressMock);
160+
$orderMock->expects($this->any())
161+
->method('getIsVirtual')
162+
->will($this->returnValue(true));
163+
$infoMock = $this->createMock(\Magento\Payment\Model\InfoInterface::class);
164+
$orderMock->expects($this->any())
165+
->method('getPayment')
166+
->willReturn($infoMock);
167+
$creditmemoMock->expects($this->any())
168+
->method('getStoreId')
169+
->willReturn($storeId);
170+
$creditmemoMock->expects($this->any())
171+
->method('getOrder')
172+
->willReturn($orderMock);
173+
$creditmemoMock->expects($this->any())
174+
->method('getAllItems')
175+
->willReturn([]);
176+
177+
$this->scopeConfigMock->expects($this->at(0))
178+
->method('getValue')
179+
->with('sales/identity/logo', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)
180+
->will($this->returnValue($filename));
181+
$this->scopeConfigMock->expects($this->at(1))
182+
->method('getValue')
183+
->with('sales/identity/address', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)
184+
->will($this->returnValue(''));
185+
186+
$this->directoryMock->expects($this->any())
187+
->method('isFile')
188+
->with($path . $filename)
189+
->willReturnOnConsecutiveCalls(
190+
$this->returnValue(false),
191+
$this->returnValue(false)
192+
);
193+
194+
$this->databaseMock->expects($this->once())
195+
->method('saveFileToFilesystem')
196+
->with($path . $filename);
197+
198+
$this->_model->getPdf([$creditmemoMock]);
199+
}
200+
}

0 commit comments

Comments
 (0)