Skip to content

Commit 08d794b

Browse files
committed
Merge remote-tracking branch 'origin/MC-35707' into 2.4-develop-pr36
2 parents b8ef0fe + 28ec90f commit 08d794b

File tree

6 files changed

+467
-21
lines changed

6 files changed

+467
-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: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\MediaStorage\Helper\File\Storage\Database;
10+
use Magento\Sales\Model\Order;
11+
use Magento\Sales\Model\Order\Address;
12+
use Magento\Sales\Model\Order\Address\Renderer;
13+
use Magento\Sales\Model\Order\Creditmemo;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Class CreditmemoTest
19+
*
20+
* Tests Sales Order Creditmemo PDF model
21+
*
22+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23+
*/
24+
class CreditmemoTest extends TestCase
25+
{
26+
/**
27+
* @var \Magento\Sales\Model\Order\Pdf\Invoice
28+
*/
29+
protected $_model;
30+
31+
/**
32+
* @var \Magento\Sales\Model\Order\Pdf\Config|MockObject
33+
*/
34+
protected $_pdfConfigMock;
35+
36+
/**
37+
* @var Database|MockObject
38+
*/
39+
protected $databaseMock;
40+
41+
/**
42+
* @var ScopeConfigInterface|MockObject
43+
*/
44+
protected $scopeConfigMock;
45+
46+
/**
47+
* @var \Magento\Framework\Filesystem\Directory\Write|MockObject
48+
*/
49+
protected $directoryMock;
50+
51+
/**
52+
* @var Renderer|MockObject
53+
*/
54+
protected $addressRendererMock;
55+
56+
/**
57+
* @var \Magento\Payment\Helper\Data|MockObject
58+
*/
59+
protected $paymentDataMock;
60+
61+
/**
62+
* @var \Magento\Store\Model\App\Emulation
63+
*/
64+
private $appEmulation;
65+
66+
protected function setUp(): void
67+
{
68+
$this->_pdfConfigMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Pdf\Config::class)
69+
->disableOriginalConstructor()
70+
->getMock();
71+
$this->directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
72+
$this->directoryMock->expects($this->any())->method('getAbsolutePath')->will(
73+
$this->returnCallback(
74+
function ($argument) {
75+
return BP . '/' . $argument;
76+
}
77+
)
78+
);
79+
$filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
80+
$filesystemMock->expects($this->any())
81+
->method('getDirectoryRead')
82+
->will($this->returnValue($this->directoryMock));
83+
$filesystemMock->expects($this->any())
84+
->method('getDirectoryWrite')
85+
->will($this->returnValue($this->directoryMock));
86+
87+
$this->databaseMock = $this->createMock(Database::class);
88+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
89+
$this->addressRendererMock = $this->createMock(Renderer::class);
90+
$this->paymentDataMock = $this->createMock(\Magento\Payment\Helper\Data::class);
91+
$this->appEmulation = $this->createMock(\Magento\Store\Model\App\Emulation::class);
92+
93+
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
94+
$this->_model = $helper->getObject(
95+
\Magento\Sales\Model\Order\Pdf\Creditmemo::class,
96+
[
97+
'filesystem' => $filesystemMock,
98+
'pdfConfig' => $this->_pdfConfigMock,
99+
'fileStorageDatabase' => $this->databaseMock,
100+
'scopeConfig' => $this->scopeConfigMock,
101+
'addressRenderer' => $this->addressRendererMock,
102+
'string' => new \Magento\Framework\Stdlib\StringUtils(),
103+
'paymentData' => $this->paymentDataMock,
104+
'appEmulation' => $this->appEmulation
105+
]
106+
);
107+
}
108+
109+
public function testInsertLogoDatabaseMediaStorage()
110+
{
111+
$filename = 'image.jpg';
112+
$path = '/sales/store/logo/';
113+
$storeId = 1;
114+
115+
$this->appEmulation->expects($this->once())
116+
->method('startEnvironmentEmulation')
117+
->with(
118+
$storeId,
119+
\Magento\Framework\App\Area::AREA_FRONTEND,
120+
true
121+
)
122+
->willReturnSelf();
123+
$this->appEmulation->expects($this->once())
124+
->method('stopEnvironmentEmulation')
125+
->willReturnSelf();
126+
$this->_pdfConfigMock->expects($this->once())
127+
->method('getRenderersPerProduct')
128+
->with('creditmemo')
129+
->will($this->returnValue(['product_type_one' => 'Renderer_Type_One_Product_One']));
130+
$this->_pdfConfigMock->expects($this->any())
131+
->method('getTotals')
132+
->will($this->returnValue([]));
133+
134+
$block = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
135+
->disableOriginalConstructor()
136+
->setMethods(['setIsSecureMode','toPdf'])
137+
->getMock();
138+
$block->expects($this->any())
139+
->method('setIsSecureMode')
140+
->willReturn($block);
141+
$block->expects($this->any())
142+
->method('toPdf')
143+
->will($this->returnValue(''));
144+
$this->paymentDataMock->expects($this->any())
145+
->method('getInfoBlock')
146+
->willReturn($block);
147+
148+
$this->addressRendererMock->expects($this->any())
149+
->method('format')
150+
->will($this->returnValue(''));
151+
152+
$this->databaseMock->expects($this->any())
153+
->method('checkDbUsage')
154+
->will($this->returnValue(true));
155+
156+
$creditmemoMock = $this->createMock(Creditmemo::class);
157+
$orderMock = $this->createMock(Order::class);
158+
$addressMock = $this->createMock(Address::class);
159+
$orderMock->expects($this->any())
160+
->method('getBillingAddress')
161+
->willReturn($addressMock);
162+
$orderMock->expects($this->any())
163+
->method('getIsVirtual')
164+
->will($this->returnValue(true));
165+
$infoMock = $this->createMock(\Magento\Payment\Model\InfoInterface::class);
166+
$orderMock->expects($this->any())
167+
->method('getPayment')
168+
->willReturn($infoMock);
169+
$creditmemoMock->expects($this->any())
170+
->method('getStoreId')
171+
->willReturn($storeId);
172+
$creditmemoMock->expects($this->any())
173+
->method('getOrder')
174+
->willReturn($orderMock);
175+
$creditmemoMock->expects($this->any())
176+
->method('getAllItems')
177+
->willReturn([]);
178+
179+
$this->scopeConfigMock->expects($this->at(0))
180+
->method('getValue')
181+
->with('sales/identity/logo', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)
182+
->will($this->returnValue($filename));
183+
$this->scopeConfigMock->expects($this->at(1))
184+
->method('getValue')
185+
->with('sales/identity/address', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, null)
186+
->will($this->returnValue(''));
187+
188+
$this->directoryMock->expects($this->any())
189+
->method('isFile')
190+
->with($path . $filename)
191+
->willReturnOnConsecutiveCalls(
192+
$this->returnValue(false),
193+
$this->returnValue(false)
194+
);
195+
196+
$this->databaseMock->expects($this->once())
197+
->method('saveFileToFilesystem')
198+
->with($path . $filename);
199+
200+
$this->_model->getPdf([$creditmemoMock]);
201+
}
202+
}

0 commit comments

Comments
 (0)